未验证 提交 647e000c 编写于 作者: O openharmony_ci 提交者: Gitee

!842 add begetctl command for init module engine

Merge pull request !842 from handy/0624
......@@ -48,6 +48,7 @@ typedef enum {
ACTION_SANDBOX = 0,
ACTION_DUMP,
ACTION_PARAM_SHELL,
ACTION_MODULEMGR,
ACTION_MAX
} ActionType;
......
......@@ -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")
......
......@@ -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
}
......
......@@ -28,6 +28,8 @@ int InitModuleMgrInstall(const char *moduleName);
void InitModuleMgrUnInstall(const char *moduleName);
void InitModuleMgrDump(void);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -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");
......
......@@ -3,6 +3,7 @@
{ "name": "SystemReadParam" },
{ "name": "AddCmdExecutor" },
{ "name": "RemoveCmdExecutor" },
{ "name": "HookMgrAdd" },
{ "name": "InitAddGlobalInitHook" },
{ "name": "InitAddPreParamServiceHook" },
{ "name": "InitAddPreParamLoadHook" },
......
......@@ -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);
}
......@@ -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",
......
/*
* 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 <errno.h>
#include <stdio.h>
#include <string.h>
#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]);
}
}
......@@ -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;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册