1d9f0492fSopenharmony_ci/* 2d9f0492fSopenharmony_ci* Copyright (c) 2022 Huawei Device Co., Ltd. 3d9f0492fSopenharmony_ci* Licensed under the Apache License, Version 2.0 (the "License"); 4d9f0492fSopenharmony_ci* you may not use this file except in compliance with the License. 5d9f0492fSopenharmony_ci* You may obtain a copy of the License at 6d9f0492fSopenharmony_ci* 7d9f0492fSopenharmony_ci* http://www.apache.org/licenses/LICENSE-2.0 8d9f0492fSopenharmony_ci* 9d9f0492fSopenharmony_ci* Unless required by applicable law or agreed to in writing, software 10d9f0492fSopenharmony_ci* distributed under the License is distributed on an "AS IS" BASIS, 11d9f0492fSopenharmony_ci* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12d9f0492fSopenharmony_ci* See the License for the specific language governing permissions and 13d9f0492fSopenharmony_ci* limitations under the License. 14d9f0492fSopenharmony_ci*/ 15d9f0492fSopenharmony_ci#include <errno.h> 16d9f0492fSopenharmony_ci#include <stdio.h> 17d9f0492fSopenharmony_ci#include <string.h> 18d9f0492fSopenharmony_ci 19d9f0492fSopenharmony_ci#ifdef ENABLE_ENTER_APPSPAWN_SANDBOX 20d9f0492fSopenharmony_ci#include "appspawn.h" 21d9f0492fSopenharmony_ci#endif 22d9f0492fSopenharmony_ci#include "begetctl.h" 23d9f0492fSopenharmony_ci#include "beget_ext.h" 24d9f0492fSopenharmony_ci#include "control_fd.h" 25d9f0492fSopenharmony_ci#include "securec.h" 26d9f0492fSopenharmony_ci#include "init_param.h" 27d9f0492fSopenharmony_ci 28d9f0492fSopenharmony_ci#define DUMP_APPSPAWN_CMD_ARGS 1 29d9f0492fSopenharmony_ci#define DUMP_SERVICE_INFO_CMD_ARGS 2 30d9f0492fSopenharmony_ci#define DUMP_SERVICE_BOOTEVENT_CMD_ARGS 3 31d9f0492fSopenharmony_ci 32d9f0492fSopenharmony_cistatic int SendAppspawnCmdMessage(const CmdAgent *agent, uint16_t type, const char *cmd, const char *ptyName) 33d9f0492fSopenharmony_ci{ 34d9f0492fSopenharmony_ci#ifdef ENABLE_ENTER_APPSPAWN_SANDBOX 35d9f0492fSopenharmony_ci if ((agent == NULL) || (cmd == NULL) || (ptyName == NULL)) { 36d9f0492fSopenharmony_ci BEGET_LOGE("Invalid parameter"); 37d9f0492fSopenharmony_ci return -1; 38d9f0492fSopenharmony_ci } 39d9f0492fSopenharmony_ci 40d9f0492fSopenharmony_ci int ret = -1; 41d9f0492fSopenharmony_ci AppSpawnClientHandle clientHandle; 42d9f0492fSopenharmony_ci if (strcmp(cmd, "dump_appspawn") == 0) { 43d9f0492fSopenharmony_ci ret = AppSpawnClientInit("AppSpawn", &clientHandle); 44d9f0492fSopenharmony_ci } else if (strcmp(cmd, "dump_nwebspawn") == 0) { 45d9f0492fSopenharmony_ci ret = AppSpawnClientInit("NWebSpawn", &clientHandle); 46d9f0492fSopenharmony_ci } else { 47d9f0492fSopenharmony_ci BEGET_LOGE("Invalid parameter to dump appspawn"); 48d9f0492fSopenharmony_ci } 49d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, return -1, "AppSpawnClientInit error, errno = %d", errno); 50d9f0492fSopenharmony_ci AppSpawnReqMsgHandle reqHandle; 51d9f0492fSopenharmony_ci ret = AppSpawnReqMsgCreate(MSG_DUMP, ptyName, &reqHandle); 52d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, AppSpawnClientDestroy(clientHandle); 53d9f0492fSopenharmony_ci return -1, "AppSpawnReqMsgCreate error"); 54d9f0492fSopenharmony_ci ret = AppSpawnReqMsgAddStringInfo(reqHandle, "pty-name", ptyName); 55d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(ret == 0, AppSpawnClientDestroy(clientHandle); 56d9f0492fSopenharmony_ci return -1, "add %s request message error", ptyName); 57d9f0492fSopenharmony_ci AppSpawnResult result = {}; 58d9f0492fSopenharmony_ci ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result); 59d9f0492fSopenharmony_ci if (ret != 0 || result.result != 0) { 60d9f0492fSopenharmony_ci AppSpawnClientDestroy(clientHandle); 61d9f0492fSopenharmony_ci return -1; 62d9f0492fSopenharmony_ci } 63d9f0492fSopenharmony_ci AppSpawnClientDestroy(clientHandle); 64d9f0492fSopenharmony_ci return 0; 65d9f0492fSopenharmony_ci#endif 66d9f0492fSopenharmony_ci return -1; 67d9f0492fSopenharmony_ci} 68d9f0492fSopenharmony_ci 69d9f0492fSopenharmony_cistatic void DumpAppspawnClientInit(const char *cmd, CallbackSendMsgProcess sendMsg) 70d9f0492fSopenharmony_ci{ 71d9f0492fSopenharmony_ci if (cmd == NULL) { 72d9f0492fSopenharmony_ci BEGET_LOGE("[control_fd] Invalid parameter"); 73d9f0492fSopenharmony_ci return; 74d9f0492fSopenharmony_ci } 75d9f0492fSopenharmony_ci 76d9f0492fSopenharmony_ci CmdAgent agent; 77d9f0492fSopenharmony_ci int ret = InitPtyInterface(&agent, ACTION_DUMP, cmd, sendMsg); 78d9f0492fSopenharmony_ci if (ret != 0) { 79d9f0492fSopenharmony_ci BEGET_LOGE("App with pid=%s does not support entering sandbox environment", cmd); 80d9f0492fSopenharmony_ci return; 81d9f0492fSopenharmony_ci } 82d9f0492fSopenharmony_ci LE_RunLoop(LE_GetDefaultLoop()); 83d9f0492fSopenharmony_ci LE_CloseLoop(LE_GetDefaultLoop()); 84d9f0492fSopenharmony_ci BEGET_LOGI("Cmd Client exit "); 85d9f0492fSopenharmony_ci} 86d9f0492fSopenharmony_ci 87d9f0492fSopenharmony_cistatic int main_cmd(BShellHandle shell, int argc, char **argv) 88d9f0492fSopenharmony_ci{ 89d9f0492fSopenharmony_ci if (argc == DUMP_APPSPAWN_CMD_ARGS) { 90d9f0492fSopenharmony_ci DumpAppspawnClientInit(argv[0], SendAppspawnCmdMessage); 91d9f0492fSopenharmony_ci } else if (argc == DUMP_SERVICE_INFO_CMD_ARGS) { 92d9f0492fSopenharmony_ci printf("dump service info \n"); 93d9f0492fSopenharmony_ci CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, argv[1], NULL); 94d9f0492fSopenharmony_ci } else if (argc == DUMP_SERVICE_BOOTEVENT_CMD_ARGS) { 95d9f0492fSopenharmony_ci if (strcmp(argv[1], "parameter_service") == 0) { 96d9f0492fSopenharmony_ci printf("dump parameter service info \n"); 97d9f0492fSopenharmony_ci } 98d9f0492fSopenharmony_ci size_t serviceNameLen = strlen(argv[1]) + strlen(argv[2]) + 2; // 2 is \0 and # 99d9f0492fSopenharmony_ci char *cmd = (char *)calloc(1, serviceNameLen); 100d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(cmd != NULL, return 0, "failed to allocate cmd memory"); 101d9f0492fSopenharmony_ci BEGET_ERROR_CHECK(sprintf_s(cmd, serviceNameLen, "%s#%s", argv[1], argv[2]) >= 0, free(cmd); 102d9f0492fSopenharmony_ci return 0, "dump service arg create failed"); 103d9f0492fSopenharmony_ci CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, cmd, NULL); 104d9f0492fSopenharmony_ci free(cmd); 105d9f0492fSopenharmony_ci } else { 106d9f0492fSopenharmony_ci BShellCmdHelp(shell, argc, argv); 107d9f0492fSopenharmony_ci } 108d9f0492fSopenharmony_ci return 0; 109d9f0492fSopenharmony_ci} 110d9f0492fSopenharmony_ci 111d9f0492fSopenharmony_cistatic int BootEventEnable(BShellHandle shell, int argc, char **argv) 112d9f0492fSopenharmony_ci{ 113d9f0492fSopenharmony_ci if (SystemSetParameter("persist.init.bootevent.enable", "true") == 0) { 114d9f0492fSopenharmony_ci printf("bootevent enabled\n"); 115d9f0492fSopenharmony_ci } 116d9f0492fSopenharmony_ci return 0; 117d9f0492fSopenharmony_ci} 118d9f0492fSopenharmony_cistatic int BootEventDisable(BShellHandle shell, int argc, char **argv) 119d9f0492fSopenharmony_ci{ 120d9f0492fSopenharmony_ci if (SystemSetParameter("persist.init.bootevent.enable", "false") == 0) { 121d9f0492fSopenharmony_ci printf("bootevent disabled\n"); 122d9f0492fSopenharmony_ci } 123d9f0492fSopenharmony_ci return 0; 124d9f0492fSopenharmony_ci} 125d9f0492fSopenharmony_ci 126d9f0492fSopenharmony_ciMODULE_CONSTRUCTOR(void) 127d9f0492fSopenharmony_ci{ 128d9f0492fSopenharmony_ci const CmdInfo infos[] = { 129d9f0492fSopenharmony_ci {"dump_service", main_cmd, "dump all loop info", "dump_service loop", NULL}, 130d9f0492fSopenharmony_ci {"dump_service", main_cmd, "dump one service info by serviceName", "dump_service serviceName", NULL}, 131d9f0492fSopenharmony_ci {"dump_service", main_cmd, "dump all services info", "dump_service all", NULL}, 132d9f0492fSopenharmony_ci {"dump_service", main_cmd, "dump parameter-service trigger", 133d9f0492fSopenharmony_ci "dump_service parameter_service trigger", NULL}, 134d9f0492fSopenharmony_ci {"bootevent", BootEventEnable, "bootevent enable", "bootevent enable", 135d9f0492fSopenharmony_ci "bootevent enable"}, 136d9f0492fSopenharmony_ci {"bootevent", BootEventDisable, "bootevent disable", "bootevent disable", 137d9f0492fSopenharmony_ci "bootevent disable"}, 138d9f0492fSopenharmony_ci {"dump_appspawn", main_cmd, "dump appspawn info", "dump_appspawn", NULL }, 139d9f0492fSopenharmony_ci {"dump_nwebspawn", main_cmd, "dump nwebspawn info", "dump_nwebspawn", NULL} 140d9f0492fSopenharmony_ci }; 141d9f0492fSopenharmony_ci for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) { 142d9f0492fSopenharmony_ci BShellEnvRegisterCmd(GetShellHandle(), &infos[i]); 143d9f0492fSopenharmony_ci } 144d9f0492fSopenharmony_ci} 145