1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "service_control.h"
16
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20
21 #include "begetctl.h"
22 #include "sys_param.h"
23 #include "init_utils.h"
24
25 #define SERVICE_START_NUMBER 2
26 #define SERVICE_CONTROL_NUMBER 3
27 #define CONTROL_SERVICE_POS 2
28 #define SERVICE_CONTROL_MAX_SIZE 50
29
ServiceControlUsage(BShellHandle shell,int argc,char ** argv)30 static void ServiceControlUsage(BShellHandle shell, int argc, char **argv)
31 {
32 BShellCmdHelp(shell, argc, argv);
33 return;
34 }
35
main_cmd(BShellHandle shell,int argc,char ** argv)36 static int main_cmd(BShellHandle shell, int argc, char **argv)
37 {
38 if (argc < SERVICE_START_NUMBER) {
39 ServiceControlUsage(shell, argc, argv);
40 return 0;
41 }
42 if (strcmp(argv[0], "start_service") == 0) {
43 ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
44 } else if (strcmp(argv[0], "stop_service") == 0) {
45 ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
46 } else if (strcmp(argv[0], "start") == 0) {
47 ServiceControlWithExtra(argv[1], 0, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
48 } else if (strcmp(argv[0], "stop") == 0) {
49 ServiceControlWithExtra(argv[1], 1, (const char **)argv + SERVICE_START_NUMBER, argc - SERVICE_START_NUMBER);
50 } else if (strcmp(argv[0], "timer_start") == 0) {
51 if (argc < SERVICE_START_NUMBER) {
52 return -1;
53 }
54 char *timeBuffer = argv[SERVICE_START_NUMBER];
55 errno = 0;
56 uint64_t timeout = strtoull(timeBuffer, NULL, DECIMAL_BASE);
57 if (errno != 0) {
58 return -1;
59 }
60 StartServiceByTimer(argv[1], timeout);
61 } else if (strcmp(argv[0], "timer_stop") == 0) {
62 StopServiceTimer(argv[1]);
63 } else {
64 ServiceControlUsage(shell, argc, argv);
65 }
66 return 0;
67 }
68
MODULE_CONSTRUCTOR(void)69 MODULE_CONSTRUCTOR(void)
70 {
71 CmdInfo infos[] = {
72 {"service_control", main_cmd, "stop service", "service_control stop servicename", "service_control stop"},
73 {"service_control", main_cmd, "start service", "service_control start servicename", "service_control start"},
74 {"stop_service", main_cmd, "stop service", "stop_service servicename", ""},
75 {"start_service", main_cmd, "start service", "start_service servicename", ""},
76 {"timer_start", main_cmd, "start service by timer", "timer_start servicename timeout", ""},
77 {"timer_stop", main_cmd, "stop service timer", "timer_stop servicename", ""},
78 };
79 for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
80 BShellEnvRegitsterCmd(GetShellHandle(), &infos[i]);
81 }
82 }
83