1 /*
2 * Copyright (c) 2022 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 <errno.h>
16 #include <stdio.h>
17 #include <string.h>
18
19 #include "begetctl.h"
20 #include "beget_ext.h"
21 #include "control_fd.h"
22 #include "securec.h"
23 #include "init_param.h"
24
25 #define DUMP_SERVICE_INFO_CMD_ARGS 2
26 #define DUMP_SERVICE_BOOTEVENT_CMD_ARGS 3
main_cmd(BShellHandle shell,int argc,char ** argv)27 static int main_cmd(BShellHandle shell, int argc, char **argv)
28 {
29 if (argc == DUMP_SERVICE_INFO_CMD_ARGS) {
30 printf("dump service info \n");
31 CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, argv[1]);
32 } else if (argc == DUMP_SERVICE_BOOTEVENT_CMD_ARGS) {
33 if (strcmp(argv[1], "parameter_service") == 0) {
34 printf("dump parameter service info \n");
35 }
36 size_t serviceNameLen = strlen(argv[1]) + strlen(argv[2]) + 2; // 2 is \0 and #
37 char *cmd = (char *)calloc(1, serviceNameLen);
38 BEGET_ERROR_CHECK(cmd != NULL, return 0, "failed to allocate cmd memory");
39 BEGET_ERROR_CHECK(sprintf_s(cmd, serviceNameLen, "%s#%s", argv[1], argv[2]) >= 0, free(cmd);
40 return 0, "dump service arg create failed");
41 CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, cmd);
42 free(cmd);
43 } else {
44 BShellCmdHelp(shell, argc, argv);
45 }
46 return 0;
47 }
48
BootEventEnable(BShellHandle shell,int argc,char ** argv)49 static int BootEventEnable(BShellHandle shell, int argc, char **argv)
50 {
51 if (SystemSetParameter("persist.init.bootevent.enable", "true") == 0) {
52 printf("bootevent enabled\n");
53 }
54 return 0;
55 }
BootEventDisable(BShellHandle shell,int argc,char ** argv)56 static int BootEventDisable(BShellHandle shell, int argc, char **argv)
57 {
58 if (SystemSetParameter("persist.init.bootevent.enable", "false") == 0) {
59 printf("bootevent disabled\n");
60 }
61 return 0;
62 }
63
MODULE_CONSTRUCTOR(void)64 MODULE_CONSTRUCTOR(void)
65 {
66 const CmdInfo infos[] = {
67 {"dump_service", main_cmd, "dump one service info by serviceName", "dump_service serviceName", NULL},
68 {"dump_service", main_cmd, "dump all services info", "dump_service all", NULL},
69 {"dump_service", main_cmd, "dump parameter-service trigger",
70 "dump_service parameter_service trigger", NULL},
71 {"bootevent", BootEventEnable, "bootevent enable", "bootevent enable",
72 "bootevent enable"},
73 {"bootevent", BootEventDisable, "bootevent disable", "bootevent disable",
74 "bootevent disable"},
75 };
76 for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
77 BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
78 }
79 }
80