diff --git a/interfaces/innerkits/control_fd/control_fd.h b/interfaces/innerkits/control_fd/control_fd.h old mode 100644 new mode 100755 index c45cb4ec4ce17343cf0982e861f93a37adbd501f..253c6c90b55ff1c15fb5bbad311fc03fd4780856 --- a/interfaces/innerkits/control_fd/control_fd.h +++ b/interfaces/innerkits/control_fd/control_fd.h @@ -48,6 +48,7 @@ typedef enum { ACTION_SANDBOX = 0, ACTION_DUMP, ACTION_PARAM_SHELL, + ACTION_MODULEMGR, ACTION_MAX } ActionType; diff --git a/interfaces/innerkits/control_fd/control_fd_service.c b/interfaces/innerkits/control_fd/control_fd_service.c old mode 100644 new mode 100755 index 3531e0d864416385395c7fea3f0602d7c3da00bb..8f3f234942cddc30b78a5f73fc13f06dabb9180d --- a/interfaces/innerkits/control_fd/control_fd_service.c +++ b/interfaces/innerkits/control_fd/control_fd_service.c @@ -27,21 +27,6 @@ static CmdService g_cmdService; CallbackControlFdProcess g_controlFdFunc = NULL; -static void CmdOnClose(const TaskHandle task) -{ - BEGET_LOGI("[control_fd] CmdOnClose"); -} - -static void CmdDisConnectComplete(const TaskHandle client) -{ - BEGET_LOGI("[control_fd] CmdDisConnectComplete"); -} - -static void CmdOnSendMessageComplete(const TaskHandle task, const BufferHandle handle) -{ - BEGET_LOGI("[control_fd] CmdOnSendMessageComplete "); -} - static int ReadFifoPath(const char *name, bool read, pid_t pid, char *resolvedPath) { if ((name == NULL) || (pid < 0) || resolvedPath == NULL) { @@ -60,7 +45,6 @@ static int ReadFifoPath(const char *name, bool read, pid_t pid, char *resolvedPa static void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint32_t buffLen) { - BEGET_LOGI("[control_fd] CmdOnRecvMessage"); if (buffer == NULL) { return; } @@ -115,14 +99,13 @@ static int SendMessage(LoopHandle loop, TaskHandle task, const char *message) static int CmdOnIncommingConntect(const LoopHandle loop, const TaskHandle server) { - BEGET_LOGI("[control_fd] CmdOnIncommingConntect"); TaskHandle client = NULL; LE_StreamInfo info = {}; info.baseInfo.flags = TASK_STREAM | TASK_PIPE | TASK_CONNECT; - info.baseInfo.close = CmdOnClose; + info.baseInfo.close = NULL; info.baseInfo.userDataSize = sizeof(CmdAgent); - info.disConntectComplete = CmdDisConnectComplete; - info.sendMessageComplete = CmdOnSendMessageComplete; + info.disConntectComplete = NULL; + info.sendMessageComplete = NULL; info.recvMessage = CmdOnRecvMessage; int ret = LE_AcceptStreamClient(LE_GetDefaultLoop(), server, &client, &info); BEGET_ERROR_CHECK(ret == 0, return -1, "[control_fd] Failed accept stream") diff --git a/interfaces/innerkits/include/modulemgr.h b/interfaces/innerkits/include/modulemgr.h index 88894f75f6229ba4cad10fd074724a5da624a6fe..5f788e4da20a28bc358175ab94b93ade4e807ca5 100755 --- a/interfaces/innerkits/include/modulemgr.h +++ b/interfaces/innerkits/include/modulemgr.h @@ -128,6 +128,33 @@ void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name); */ int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr); +/** + * @brief Module information for traversing modules + */ +typedef struct tagMODULE_INFO { + const char *name; /* module name */ + void *handle; /* module handler */ + void *cookie; /* hook execution cookie */ +} MODULE_INFO; + +/** + * @brief Module traversal function prototype + * + * @param moduleInfo MODULE_INFO for traversing each module. + * @return None + */ +typedef void (*OhosModuleTraversal)(const MODULE_INFO *moduleInfo); + +/** + * @brief Traversing all modules in the ModuleManager + * + * @param moduleMgr module manager handle + * @param cookie traversal cookie. + * @param traversal traversal function. + * @return None. + */ +void ModuleMgrTraversal(const MODULE_MGR *moduleMgr, void *cookie, OhosModuleTraversal traversal); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/init_module_engine/include/init_modulemgr.h b/interfaces/innerkits/init_module_engine/include/init_modulemgr.h index e4ecbb926c6e644e9adaef183be8065bd54c39f8..099c7ae5fdb861677cf4821f28a6eafd4be2e85d 100755 --- a/interfaces/innerkits/init_module_engine/include/init_modulemgr.h +++ b/interfaces/innerkits/init_module_engine/include/init_modulemgr.h @@ -28,6 +28,8 @@ int InitModuleMgrInstall(const char *moduleName); void InitModuleMgrUnInstall(const char *moduleName); +void InitModuleMgrDump(void); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/init_module_engine/init_modulemgr.c b/interfaces/innerkits/init_module_engine/init_modulemgr.c index 4a4f741dcf0c9e8678c62c305bd4b8519551f53d..e3f8f637e5572a7fbc8978c074c9a149845a257f 100644 --- a/interfaces/innerkits/init_module_engine/init_modulemgr.c +++ b/interfaces/innerkits/init_module_engine/init_modulemgr.c @@ -39,6 +39,20 @@ void InitModuleMgrUnInstall(const char *moduleName) ModuleMgrUninstall(defaultModuleMgr, moduleName); } +static void InitModuleDump(const MODULE_INFO *moduleInfo) +{ + printf("%s\n", moduleInfo->name); +} + +void InitModuleMgrDump(void) +{ + if (defaultModuleMgr == NULL) { + return; + } + + ModuleMgrTraversal(defaultModuleMgr, NULL, InitModuleDump); +} + static int ModuleMgrCmdInstall(int id, const char *name, int argc, const char **argv) { INIT_ERROR_CHECK(argv != NULL && argc >= 1, return -1, "Invalid install parameter"); diff --git a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json index 4c373c6781f03367c0322c718c760812e7a2dd91..5da2164d162b239e54c813a174a6f448f1cda9fb 100755 --- a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json +++ b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json @@ -3,6 +3,7 @@ { "name": "SystemReadParam" }, { "name": "AddCmdExecutor" }, { "name": "RemoveCmdExecutor" }, + { "name": "HookMgrAdd" }, { "name": "InitAddGlobalInitHook" }, { "name": "InitAddPreParamServiceHook" }, { "name": "InitAddPreParamLoadHook" }, diff --git a/interfaces/innerkits/modulemgr/modulemgr.c b/interfaces/innerkits/modulemgr/modulemgr.c index 7fb3e6a431fcdb66393b6617360caa81038bb806..1d3c8f2e11b81dbfe4cb8b7c349f55e14e0049b7 100644 --- a/interfaces/innerkits/modulemgr/modulemgr.c +++ b/interfaces/innerkits/modulemgr/modulemgr.c @@ -265,3 +265,46 @@ int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr) BEGET_CHECK(moduleMgr != NULL, return 0); return ListGetCnt(&(moduleMgr->modules)); } + +typedef struct tagMODULE_TRAVERSAL_ARGS { + void *cookie; + OhosModuleTraversal traversal; +} MODULE_TRAVERSAL_ARGS; + +static int moduleTraversalProc(ListNode *node, void *cookie) +{ + MODULE_ITEM *module; + MODULE_TRAVERSAL_ARGS *args; + MODULE_INFO info; + + module = (MODULE_ITEM *)node; + args = (MODULE_TRAVERSAL_ARGS *)cookie; + + info.cookie = args->cookie; + info.handle = module->handle; + info.name = module->name; + args->traversal(&info); + + return 0; +} + +/** + * @brief Traversing all hooks in the HookManager + * + * @param moduleMgr HookManager handle. + * If hookMgr is NULL, it will use default HookManager + * @param cookie traversal cookie. + * @param traversal traversal function. + * @return None. + */ +void ModuleMgrTraversal(const MODULE_MGR *moduleMgr, void *cookie, OhosModuleTraversal traversal) +{ + MODULE_TRAVERSAL_ARGS args; + if (moduleMgr == NULL) { + return; + } + + args.cookie = cookie; + args.traversal = traversal; + ListTraversal((ListNode *)(&(moduleMgr->modules)), (void *)(&args), moduleTraversalProc, 0); +} diff --git a/services/begetctl/BUILD.gn b/services/begetctl/BUILD.gn index fa8ffda8edbdd417f4a74e743e771f1cb0c2bc96..5a4c390e919f0d10a1001b4479c64c740f0b4858 100755 --- a/services/begetctl/BUILD.gn +++ b/services/begetctl/BUILD.gn @@ -70,6 +70,7 @@ if (defined(ohos_lite)) { "init_cmd_reboot.c", "main.c", "misc_daemon.cpp", + "modulectl.c", "param_cmd.c", "sandbox.cpp", "service_control.c", diff --git a/services/begetctl/modulectl.c b/services/begetctl/modulectl.c new file mode 100755 index 0000000000000000000000000000000000000000..302edf82a8641aa760a9001b23c32560c8ad4dbc --- /dev/null +++ b/services/begetctl/modulectl.c @@ -0,0 +1,53 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include +#include +#include + +#include "begetctl.h" +#include "control_fd.h" +#include "init_utils.h" +#include "securec.h" + +#define MODULE_CTL_CMD_ARGS 2 +static int main_cmd(BShellHandle shell, int argc, char **argv) +{ + char combinedArgs[MAX_BUFFER_LEN]; + + if (argc < MODULE_CTL_CMD_ARGS) { + BShellCmdHelp(shell, argc, argv); + return 0; + } + if (argc > MODULE_CTL_CMD_ARGS) { + (void)snprintf_s(combinedArgs, sizeof(combinedArgs), + sizeof(combinedArgs) - 1, "%s:%s", argv[1], argv[2]); + CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_MODULEMGR, combinedArgs, "FIFO"); + } else { + CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_MODULEMGR, argv[1], "FIFO"); + } + return 0; +} + +MODULE_CONSTRUCTOR(void) +{ + const CmdInfo infos[] = { + {"modulectl", main_cmd, "dump all modules installed", "modulectl list", NULL}, + {"modulectl", main_cmd, "install or uninstall specified module", + "modulectl install|uninstall moduleName", NULL}, + }; + for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) { + BShellEnvRegitsterCmd(GetShellHandle(), &infos[i]); + } +} diff --git a/services/init/standard/init_control_fd_service.c b/services/init/standard/init_control_fd_service.c old mode 100644 new mode 100755 index 25270a4b83fd61cac21f174626a8cc6af1bf4834..73c44ea0a736958b32bf46f98ee36ea1b0a7cca6 --- a/services/init/standard/init_control_fd_service.c +++ b/services/init/standard/init_control_fd_service.c @@ -21,6 +21,7 @@ #include "control_fd.h" #include "init_service.h" #include "init_service_manager.h" +#include "init_modulemgr.h" #include "init_utils.h" #include "init_log.h" @@ -54,6 +55,33 @@ static void ProcessDumpServiceControlFd(uint16_t type, const char *serviceCmd) return; } +static void ProcessModuleMgrControlFd(uint16_t type, const char *serviceCmd) +{ +#define MODULE_INSTALL_PREFIX "install:" +#define MODULE_UNINSTALL_PREFIX "uninstall:" + int cmdLen; + + if ((type != ACTION_MODULEMGR) || (serviceCmd == NULL)) { + return; + } + if (strcmp(serviceCmd, "list") == 0) { + InitModuleMgrDump(); + return; + } + cmdLen = strlen(MODULE_INSTALL_PREFIX); + if (strncmp(serviceCmd, MODULE_INSTALL_PREFIX, cmdLen) == 0) { + INIT_LOGI("Install %s now ...\n", serviceCmd + cmdLen); + InitModuleMgrInstall(serviceCmd + cmdLen); + return; + } + cmdLen = strlen(MODULE_UNINSTALL_PREFIX); + if (strncmp(serviceCmd, MODULE_UNINSTALL_PREFIX, cmdLen) == 0) { + INIT_LOGI("Uninstall %s now ...\n", serviceCmd + cmdLen); + InitModuleMgrUnInstall(serviceCmd + cmdLen); + return; + } +} + static void ProcessParamShellControlFd(uint16_t type, const char *serviceCmd) { if ((type != ACTION_PARAM_SHELL) || (serviceCmd == NULL)) { @@ -85,6 +113,9 @@ void ProcessControlFd(uint16_t type, const char *serviceCmd, const void *context case ACTION_PARAM_SHELL : ProcessParamShellControlFd(type, serviceCmd); break; + case ACTION_MODULEMGR : + ProcessModuleMgrControlFd(type, serviceCmd); + break; default : INIT_LOGW("Unknown control fd type."); break;