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 #ifdef ENABLE_ENTER_APPSPAWN_SANDBOX
20 #include "appspawn.h"
21 #endif
22 #include "begetctl.h"
23 #include "beget_ext.h"
24 #include "control_fd.h"
25 #include "securec.h"
26 #include "init_param.h"
27 
28 #define DUMP_APPSPAWN_CMD_ARGS 1
29 #define DUMP_SERVICE_INFO_CMD_ARGS 2
30 #define DUMP_SERVICE_BOOTEVENT_CMD_ARGS 3
31 
SendAppspawnCmdMessage(const CmdAgent * agent,uint16_t type,const char * cmd,const char * ptyName)32 static int SendAppspawnCmdMessage(const CmdAgent *agent, uint16_t type, const char *cmd, const char *ptyName)
33 {
34 #ifdef ENABLE_ENTER_APPSPAWN_SANDBOX
35     if ((agent == NULL) || (cmd == NULL) || (ptyName == NULL)) {
36         BEGET_LOGE("Invalid parameter");
37         return -1;
38     }
39 
40     int ret = -1;
41     AppSpawnClientHandle clientHandle;
42     if (strcmp(cmd, "dump_appspawn") == 0) {
43         ret = AppSpawnClientInit("AppSpawn", &clientHandle);
44     } else if (strcmp(cmd, "dump_nwebspawn") == 0) {
45         ret = AppSpawnClientInit("NWebSpawn", &clientHandle);
46     } else {
47         BEGET_LOGE("Invalid parameter to dump appspawn");
48     }
49     BEGET_ERROR_CHECK(ret == 0, return -1, "AppSpawnClientInit error, errno = %d", errno);
50     AppSpawnReqMsgHandle reqHandle;
51     ret = AppSpawnReqMsgCreate(MSG_DUMP, ptyName, &reqHandle);
52     BEGET_ERROR_CHECK(ret == 0, AppSpawnClientDestroy(clientHandle);
53         return -1, "AppSpawnReqMsgCreate error");
54     ret = AppSpawnReqMsgAddStringInfo(reqHandle, "pty-name", ptyName);
55     BEGET_ERROR_CHECK(ret == 0, AppSpawnClientDestroy(clientHandle);
56         return -1, "add %s request message error", ptyName);
57     AppSpawnResult result = {};
58     ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
59     if (ret != 0 || result.result != 0) {
60         AppSpawnClientDestroy(clientHandle);
61         return -1;
62     }
63     AppSpawnClientDestroy(clientHandle);
64     return 0;
65 #endif
66     return -1;
67 }
68 
DumpAppspawnClientInit(const char * cmd,CallbackSendMsgProcess sendMsg)69 static void DumpAppspawnClientInit(const char *cmd, CallbackSendMsgProcess sendMsg)
70 {
71     if (cmd == NULL) {
72         BEGET_LOGE("[control_fd] Invalid parameter");
73         return;
74     }
75 
76     CmdAgent agent;
77     int ret = InitPtyInterface(&agent, ACTION_DUMP, cmd, sendMsg);
78     if (ret != 0) {
79         BEGET_LOGE("App with pid=%s does not support entering sandbox environment", cmd);
80         return;
81     }
82     LE_RunLoop(LE_GetDefaultLoop());
83     LE_CloseLoop(LE_GetDefaultLoop());
84     BEGET_LOGI("Cmd Client exit ");
85 }
86 
main_cmd(BShellHandle shell,int argc,char ** argv)87 static int main_cmd(BShellHandle shell, int argc, char **argv)
88 {
89     if (argc == DUMP_APPSPAWN_CMD_ARGS) {
90         DumpAppspawnClientInit(argv[0], SendAppspawnCmdMessage);
91     } else if (argc == DUMP_SERVICE_INFO_CMD_ARGS) {
92         printf("dump service info \n");
93         CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, argv[1], NULL);
94     } else if (argc == DUMP_SERVICE_BOOTEVENT_CMD_ARGS) {
95         if (strcmp(argv[1], "parameter_service") == 0) {
96             printf("dump parameter service info \n");
97         }
98         size_t serviceNameLen = strlen(argv[1]) + strlen(argv[2]) + 2; // 2 is \0 and #
99         char *cmd = (char *)calloc(1, serviceNameLen);
100         BEGET_ERROR_CHECK(cmd != NULL, return 0, "failed to allocate cmd memory");
101         BEGET_ERROR_CHECK(sprintf_s(cmd, serviceNameLen, "%s#%s", argv[1], argv[2]) >= 0, free(cmd);
102             return 0, "dump service arg create failed");
103         CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, cmd, NULL);
104         free(cmd);
105     } else {
106         BShellCmdHelp(shell, argc, argv);
107     }
108     return 0;
109 }
110 
BootEventEnable(BShellHandle shell,int argc,char ** argv)111 static int BootEventEnable(BShellHandle shell, int argc, char **argv)
112 {
113     if (SystemSetParameter("persist.init.bootevent.enable", "true") == 0) {
114         printf("bootevent enabled\n");
115     }
116     return 0;
117 }
BootEventDisable(BShellHandle shell,int argc,char ** argv)118 static int BootEventDisable(BShellHandle shell, int argc, char **argv)
119 {
120     if (SystemSetParameter("persist.init.bootevent.enable", "false") == 0) {
121         printf("bootevent disabled\n");
122     }
123     return 0;
124 }
125 
MODULE_CONSTRUCTOR(void)126 MODULE_CONSTRUCTOR(void)
127 {
128     const CmdInfo infos[] = {
129         {"dump_service", main_cmd, "dump all loop info", "dump_service loop", NULL},
130         {"dump_service", main_cmd, "dump one service info by serviceName", "dump_service serviceName", NULL},
131         {"dump_service", main_cmd, "dump all services info", "dump_service all", NULL},
132         {"dump_service", main_cmd, "dump parameter-service trigger",
133             "dump_service parameter_service trigger", NULL},
134         {"bootevent", BootEventEnable, "bootevent enable", "bootevent enable",
135             "bootevent enable"},
136         {"bootevent", BootEventDisable, "bootevent disable", "bootevent disable",
137             "bootevent disable"},
138         {"dump_appspawn", main_cmd, "dump appspawn info", "dump_appspawn", NULL },
139         {"dump_nwebspawn", main_cmd, "dump nwebspawn info", "dump_nwebspawn", NULL}
140     };
141     for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
142         BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
143     }
144 }
145