diff --git a/interfaces/innerkits/fs_manager/fstab_mount.c b/interfaces/innerkits/fs_manager/fstab_mount.c index 6b240e965c2aab256d4ff5dbacd6077a915318b8..540be0662c38e1105fc82a32d115403ca6d1ead6 100644 --- a/interfaces/innerkits/fs_manager/fstab_mount.c +++ b/interfaces/innerkits/fs_manager/fstab_mount.c @@ -107,11 +107,11 @@ int DoFormat(const char *devPath, const char *fsType) } else if (strcmp(fsType, "f2fs") == 0) { #ifdef __MUSL__ char *formatCmds[] = { - "/bin/mkfs.f2fs", (char *)devPath, NULL + "/bin/mkfs.f2fs", "-d1", "-O", "encrypt", "-O", "quota", "-O", "verity", (char *)devPath, NULL }; #else char *formatCmds[] = { - "/bin/make_f2fs", (char *)devPath, NULL + "/bin/make_f2fs", "-d1", "-O", "encrypt", "-O", "quota", "-O", "verity", (char *)devPath, NULL }; #endif int argc = ARRAY_LENGTH(formatCmds); diff --git a/interfaces/innerkits/init_module_engine/include/init_cmdexecutor.h b/interfaces/innerkits/init_module_engine/include/init_cmdexecutor.h index d878f7edb83a363b8b92c8f13b5e578e020d2310..0060a83a70fb13b93d9875c1194a444e0721bb12 100644 --- a/interfaces/innerkits/init_module_engine/include/init_cmdexecutor.h +++ b/interfaces/innerkits/init_module_engine/include/init_cmdexecutor.h @@ -46,6 +46,8 @@ const char *GetPluginCmdNameByIndex(int index); int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd); +int AddRebootCmdExecutor(const char *cmd, CmdExecutor executor); + #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json index 013c5d8c7229a309e5cbae029b755ff28a2ad7a2..bf210c7a4c5e0a8fbdcdf41f58a3e4b5c1cb631b 100755 --- a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json +++ b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json @@ -17,5 +17,6 @@ { "name": "StartupLog" }, { "name": "DoJobNow" }, { "name": "GetServiceExtData" }, - { "name": "UpdateMiscMessage" } + { "name": "UpdateMiscMessage" }, + { "name": "AddRebootCmdExecutor" } ] diff --git a/interfaces/innerkits/modulemgr/modulemgr.c b/interfaces/innerkits/modulemgr/modulemgr.c index 1751f68fc86f96e5ec0b0fab77cfcfb494f78abe..dcc077c4b8a77a0eb4f63edf9ffcad39ff0e508e 100644 --- a/interfaces/innerkits/modulemgr/modulemgr.c +++ b/interfaces/innerkits/modulemgr/modulemgr.c @@ -33,6 +33,7 @@ #else #define MODULE_LIB_NAME "lib" #endif +#define LIB_NAME_LEN 3 struct tagMODULE_MGR { ListNode modules; @@ -103,9 +104,9 @@ static void *ModuleInstall(MODULE_ITEM *module, int argc, const char *argv[]) module->moduleMgr->installArgs.argc = argc; module->moduleMgr->installArgs.argv = argv; - if (module->moduleMgr->name[0] == '/') { - if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s/%s" MODULE_SUFFIX_D, - module->moduleMgr->name, module->name) < 0) { + BEGET_LOGV("Module install name %s", module->name); + if (module->name[0] == '/') { + if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s" MODULE_SUFFIX_D, module->name) < 0) { return NULL; } } else { @@ -126,8 +127,14 @@ static void *ModuleInstall(MODULE_ITEM *module, int argc, const char *argv[]) static int ModuleCompare(ListNode *node, void *data) { MODULE_ITEM *module = (MODULE_ITEM *)node; - - return strcmp(module->name, (char *)data); + const char *name = module->name; + if (module->name[0] == '/') { + name = strrchr((name), '/') + 1; + } + if (strncmp(name, "lib", LIB_NAME_LEN) == 0) { + name = name + LIB_NAME_LEN; + } + return strcmp(name, (char *)data); } /* @@ -160,7 +167,7 @@ int ModuleMgrInstall(MODULE_MGR *moduleMgr, const char *moduleName, // Install module->handle = ModuleInstall(module, argc, argv); if (module->handle == NULL) { - BEGET_LOGE("Failed to install module %s", moduleName); + BEGET_LOGE("Failed to install module %s", module->name); ModuleDestroy((ListNode *)module); return -1; } @@ -190,15 +197,12 @@ static int StringEndsWith(const char *srcStr, const char *endStr) static void ScanModules(MODULE_MGR *moduleMgr, const char *path) { - int end; - DIR *dir; - struct dirent *file; - - dir = opendir(path); + BEGET_LOGV("Scan module with name '%s'", path); + DIR *dir = opendir(path); BEGET_CHECK(dir != NULL, return); - - while (1) { - file = readdir(dir); + char *moduleName = malloc(PATH_MAX); + while (moduleName != NULL) { + struct dirent *file = readdir(dir); if (file == NULL) { break; } @@ -207,20 +211,22 @@ static void ScanModules(MODULE_MGR *moduleMgr, const char *path) } // Must be ended with MODULE_SUFFIX_D - end = StringEndsWith(file->d_name, MODULE_SUFFIX_D); + int end = StringEndsWith(file->d_name, MODULE_SUFFIX_D); if (end <= 0) { continue; } file->d_name[end] = '\0'; - BEGET_LOGV("Scan module with name %s", file->d_name); - if (strncmp(file->d_name, "lib", strlen("lib")) == 0) { - ModuleMgrInstall(moduleMgr, file->d_name + strlen("lib"), 0, NULL); - } else { - ModuleMgrInstall(moduleMgr, file->d_name, 0, NULL); + int len = sprintf_s(moduleName, PATH_MAX - 1, "%s/%s", path, file->d_name); + if (len > 0) { + moduleName[len] = '\0'; + BEGET_LOGI("Scan module with name '%s'", moduleName); + ModuleMgrInstall(moduleMgr, moduleName, 0, NULL); } } - + if (moduleName != NULL) { + free(moduleName); + } closedir(dir); } diff --git a/services/init/standard/init_reboot.c b/services/init/standard/init_reboot.c index 3699ce3e672b32d9829854266ab5e06e341eaf5f..4839e8f19aa4d14ff148e49c217ccfa706902ba8 100644 --- a/services/init/standard/init_reboot.c +++ b/services/init/standard/init_reboot.c @@ -17,11 +17,16 @@ #include "init_cmdexecutor.h" #include "init_log.h" #include "init_group_manager.h" +#include "init_modulemgr.h" void ExecReboot(const char *value) { INIT_LOGI("ExecReboot %s", value); +#ifndef STARTUP_INIT_TEST + // install module + InitModuleMgrInstall("rebootmodule"); PluginExecCmdByName("reboot", value); +#endif return; } diff --git a/services/modules/init_hook/BUILD.gn b/services/modules/init_hook/BUILD.gn index 36ea68af8fe5f51f5ebb2a37e64a0e6d1b2e781e..98171616e80505f39d9227fe9bb21e0283d5d676 100755 --- a/services/modules/init_hook/BUILD.gn +++ b/services/modules/init_hook/BUILD.gn @@ -30,7 +30,10 @@ comm_include = [ ] if (defined(ohos_lite)) { static_library("inithook") { - defines = [ "_GNU_SOURCE" ] + defines = [ + "_GNU_SOURCE", + "OHOS_LITE", + ] include_dirs = comm_include sources = [ "param_hook.c" ] public_configs = [ ":inithook_config" ] diff --git a/services/modules/init_hook/init_hook.h b/services/modules/init_hook/init_hook.h index 1b8c85f38b045b567d41265816e16b2330e307f7..87441514fe8d14ddf999927fd712d36e9d3aa9b4 100755 --- a/services/modules/init_hook/init_hook.h +++ b/services/modules/init_hook/init_hook.h @@ -28,9 +28,9 @@ extern "C" { #define SERVICE_CTL_CMD_INDEX 2 typedef struct { - const char *name; // system parameter partial name - const char *replace; // replace content if filed name match system parameter - const char *cmd; // command name + char *name; // system parameter partial name + char *replace; // replace content if filed name match system parameter + char *cmd; // command name } ParamCmdInfo; const ParamCmdInfo *GetServiceStartCtrl(size_t *size); diff --git a/services/modules/init_hook/param_hook.c b/services/modules/init_hook/param_hook.c index e1b8152cbcd0dcd8fa3282347ae34c380f8ba640..64a6c92353bb868c2e246ed54829537edaa0271b 100755 --- a/services/modules/init_hook/param_hook.c +++ b/services/modules/init_hook/param_hook.c @@ -43,19 +43,20 @@ const ParamCmdInfo *GetServiceCtl(size_t *size) return installParam; } +#ifdef OHOS_LITE const ParamCmdInfo *GetStartupPowerCtl(size_t *size) { static const ParamCmdInfo powerCtrlArg[] = { {"reboot,shutdown", "reboot.shutdown", "reboot.shutdown"}, {"reboot,updater", "reboot.updater", "reboot.updater"}, {"reboot,flashd", "reboot.flashd", "reboot.flashd"}, - {"reboot,loader", "reboot.loader", "reboot.loader"}, {"reboot,charge", "reboot.charge", "reboot.charge"}, {"reboot", "reboot", "reboot"}, }; *size = ARRAY_LENGTH(powerCtrlArg); return powerCtrlArg; } +#endif const ParamCmdInfo *GetOtherSpecial(size_t *size) { diff --git a/services/modules/reboot/reboot.c b/services/modules/reboot/reboot.c index 6bc9b163eaff17662504c646c257b4cbac740569..61e567e5fb191449762fe9c6ae24b32ba92fe9a5 100644 --- a/services/modules/reboot/reboot.c +++ b/services/modules/reboot/reboot.c @@ -20,12 +20,6 @@ #include "plugin_adapter.h" #include "securec.h" -typedef struct { - const char *cmd; - CmdExecutor executor; - uint32_t cmdId; -} ModuleCmdInfo; - static int DoRoot_(const char *jobName, int type) { // by job to stop service and unmount @@ -34,11 +28,17 @@ static int DoRoot_(const char *jobName, int type) } #ifndef STARTUP_INIT_TEST return reboot(type); +#else + return 0; #endif } static int DoReboot(int id, const char *name, int argc, const char **argv) { + UNUSED(id); + UNUSED(name); + UNUSED(argc); + UNUSED(argv); // clear misc (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL); return DoRoot_("reboot", RB_AUTOBOOT); @@ -46,6 +46,10 @@ static int DoReboot(int id, const char *name, int argc, const char **argv) static int DoRebootShutdown(int id, const char *name, int argc, const char **argv) { + UNUSED(id); + UNUSED(name); + UNUSED(argc); + UNUSED(argv); // clear misc (void)UpdateMiscMessage(NULL, "reboot", NULL, NULL); return DoRoot_("reboot", RB_POWER_OFF); @@ -53,6 +57,7 @@ static int DoRebootShutdown(int id, const char *name, int argc, const char **arg static int DoRebootUpdater(int id, const char *name, int argc, const char **argv) { + UNUSED(id); PLUGIN_LOGI("DoRebootUpdater argc %d %s", argc, name); PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter"); PLUGIN_LOGI("DoRebootUpdater argv %s", argv[0]); @@ -65,6 +70,7 @@ static int DoRebootUpdater(int id, const char *name, int argc, const char **argv static int DoRebootFlashed(int id, const char *name, int argc, const char **argv) { + UNUSED(id); PLUGIN_LOGI("DoRebootFlashed argc %d %s", argc, name); PLUGIN_CHECK(argc >= 1, return -1, "Invalid parameter"); PLUGIN_LOGI("DoRebootFlashd argv %s", argv[0]); @@ -77,6 +83,10 @@ static int DoRebootFlashed(int id, const char *name, int argc, const char **argv static int DoRebootCharge(int id, const char *name, int argc, const char **argv) { + UNUSED(id); + UNUSED(name); + UNUSED(argc); + UNUSED(argv); int ret = UpdateMiscMessage(NULL, "charge", "charge:", "boot_charge"); if (ret == 0) { return DoRoot_("reboot", RB_AUTOBOOT); @@ -84,46 +94,25 @@ static int DoRebootCharge(int id, const char *name, int argc, const char **argv) return ret; } -#ifdef PRODUCT_RK -#include -#define REBOOT_MAGIC1 0xfee1dead -#define REBOOT_MAGIC2 672274793 -#define REBOOT_CMD_RESTART2 0xA1B2C3D4 -static int DoRebootLoader(int id, const char *name, int argc, const char **argv) +static int DoRebootSuspend(int id, const char *name, int argc, const char **argv) { - // by job to stop service and unmount - DoJobNow("reboot"); - syscall(__NR_reboot, REBOOT_MAGIC1, REBOOT_MAGIC2, REBOOT_CMD_RESTART2, "loader"); - return 0; + UNUSED(id); + UNUSED(name); + UNUSED(argc); + UNUSED(argv); + return DoRoot_("suspend", RB_AUTOBOOT); } -#endif - -static ModuleCmdInfo g_rebootCmdIds[] = { - {"reboot.shutdown", DoRebootShutdown, 0}, - {"reboot.flashd", DoRebootFlashed, 0}, - {"reboot.updater", DoRebootUpdater, 0}, - {"reboot.charge", DoRebootCharge, 0}, -#ifdef PRODUCT_RK - {"reboot.loader", DoRebootLoader, 0}, -#endif - {"reboot", DoReboot, 0}, -}; static void RebootAdpInit(void) { - for (size_t i = 0; i < sizeof(g_rebootCmdIds)/sizeof(g_rebootCmdIds[0]); i++) { - g_rebootCmdIds[i].cmdId = (uint32_t)AddCmdExecutor(g_rebootCmdIds[i].cmd, g_rebootCmdIds[i].executor); - } -} - -static void RebootAdpExit(void) -{ - for (size_t i = 0; i < sizeof(g_rebootCmdIds)/sizeof(g_rebootCmdIds[0]); i++) { - if (g_rebootCmdIds[i].cmdId == 0) { - continue; - } - RemoveCmdExecutor(g_rebootCmdIds[i].cmd, g_rebootCmdIds[i].cmdId); - } + // sample {"reboot,shutdown", "reboot.shutdown", "reboot.shutdown"}, + // add default reboot cmd + (void)AddCmdExecutor("reboot", DoReboot); + AddRebootCmdExecutor("shutdown", DoRebootShutdown); + AddRebootCmdExecutor("flashd", DoRebootFlashed); + AddRebootCmdExecutor("updater", DoRebootUpdater); + AddRebootCmdExecutor("charge", DoRebootCharge); + AddRebootCmdExecutor("suspend", DoRebootSuspend); } MODULE_CONSTRUCTOR(void) @@ -135,5 +124,4 @@ MODULE_CONSTRUCTOR(void) MODULE_DESTRUCTOR(void) { PLUGIN_LOGI("Reboot adapter plug-in exit now ..."); - RebootAdpExit(); } diff --git a/services/modules/reboot/reboot_static.c b/services/modules/reboot/reboot_static.c index 25d6e99948003967dae1e94aad3a45dff2bc603a..7263ffbeb432c6211eacd231178dffc1d5f4049f 100644 --- a/services/modules/reboot/reboot_static.c +++ b/services/modules/reboot/reboot_static.c @@ -12,11 +12,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + +#include "bootstage.h" +#include "hookmgr.h" #include "init_hook.h" #include "init_module_engine.h" #include "plugin_adapter.h" -#include "hookmgr.h" -#include "bootstage.h" +#include "securec.h" + +#define REBOOT_NAME_PREFIX "reboot," +#define REBOOT_CMD_PREFIX "reboot." +#define REBOOT_REPLACE_PREFIX "reboot." static int RebootHookWrapper(const HOOK_INFO *hookInfo, void *executionContext) { @@ -36,13 +43,113 @@ int InitAddRebootHook(InitRebootHook hook) return HookMgrAddEx(GetBootStageHookMgr(), &info); } -static int RebookGlobalHook(const HOOK_INFO *hookInfo, void *cookie) +static ParamCmdInfo *g_rebootParamCmdInfos = NULL; +static int g_rebootParamCmdMaxNumber = 0; +static int g_rebootParamCmdValidNumber = 0; +static char *Dup2String(const char *prefix, const char *str) { - InitModuleMgrInstall("rebootmodule"); - PLUGIN_LOGI("Install rebootmodule."); + if (str == NULL) { + return strdup("reboot"); + } + size_t len = strlen(prefix) + strlen(str) + 1; + char *tmp = calloc(1, len); + PLUGIN_CHECK(tmp != NULL, return NULL, "Failed to alloc %s %s", prefix, str); + int ret = sprintf_s(tmp, len, "%s%s", prefix, str); + PLUGIN_CHECK(ret > 0, free(tmp); + return NULL, "Failed to sprintf %s %s", prefix, str); + return tmp; +} + +static int CheckParamCmdExist(const char *cmd) +{ + if (g_rebootParamCmdInfos == NULL) { + return 0; + } + char *cmdName = Dup2String(REBOOT_CMD_PREFIX, cmd); + PLUGIN_CHECK(cmdName != NULL, return 0, "Failed to copy %s", cmd); + for (int i = 0; i < g_rebootParamCmdValidNumber; i++) { + if (strcmp(g_rebootParamCmdInfos[i].cmd, cmdName) == 0) { + free(cmdName); + return 1; + } + } + free(cmdName); return 0; } +static int SetParamCmdInfo(ParamCmdInfo *currInfo, CmdExecutor executor, const char *cmd) +{ + do { + currInfo->name = Dup2String(REBOOT_NAME_PREFIX, cmd); + PLUGIN_CHECK(currInfo->name != NULL, break, "Failed to copy %s", cmd); + currInfo->replace = Dup2String(REBOOT_REPLACE_PREFIX, cmd); + PLUGIN_CHECK(currInfo->replace != NULL, break, "Failed to copy %s", cmd); + currInfo->cmd = Dup2String(REBOOT_CMD_PREFIX, cmd); + PLUGIN_CHECK(currInfo->cmd != NULL, break, "Failed to copy %s", cmd); + if (executor != NULL) { + int cmdId = AddCmdExecutor(currInfo->cmd, executor); + PLUGIN_CHECK(cmdId > 0, break, "Failed to add cmd %s", cmd); + } + PLUGIN_LOGV("SetParamCmdInfo '%s' '%s' '%s' ", currInfo->name, currInfo->cmd, currInfo->replace); + currInfo = NULL; + g_rebootParamCmdValidNumber++; + return 0; + } while (0); + if (currInfo != NULL) { + if (currInfo->name != NULL) { + free(currInfo->name); + } + if (currInfo->cmd != NULL) { + free(currInfo->cmd); + } + if (currInfo->replace != NULL) { + free(currInfo->replace); + } + } + return -1; +} + +static int AddRebootCmdExecutor_(const char *cmd, CmdExecutor executor) +{ + if (g_rebootParamCmdMaxNumber == 0 || g_rebootParamCmdMaxNumber <= g_rebootParamCmdValidNumber) { + g_rebootParamCmdMaxNumber += 5; // inc 5 once time + ParamCmdInfo *cmdInfos = calloc(1, sizeof(ParamCmdInfo) * g_rebootParamCmdMaxNumber); + PLUGIN_CHECK(cmdInfos != NULL, return -1, "Failed to add reboot cmd %s", cmd); + if (g_rebootParamCmdInfos != NULL) { // delete old + // copy from old + for (int i = 0; i < g_rebootParamCmdValidNumber; i++) { + cmdInfos[i].name = g_rebootParamCmdInfos[i].name; + cmdInfos[i].replace = g_rebootParamCmdInfos[i].replace; + cmdInfos[i].cmd = g_rebootParamCmdInfos[i].cmd; + } + free(g_rebootParamCmdInfos); + } + g_rebootParamCmdInfos = cmdInfos; + } + return SetParamCmdInfo(&g_rebootParamCmdInfos[g_rebootParamCmdValidNumber], executor, cmd); +} + +int AddRebootCmdExecutor(const char *cmd, CmdExecutor executor) +{ + PLUGIN_CHECK(cmd != NULL && executor != NULL, return EINVAL, "Invalid input parameter"); + int ret = CheckParamCmdExist(cmd); + if (ret != 0) { + PLUGIN_LOGI("Cmd %s exist", cmd); + return EEXIST; + } + return AddRebootCmdExecutor_(cmd, executor); +} + +const ParamCmdInfo *GetStartupPowerCtl(size_t *size) +{ + RebootHookCtx context; + context.reason = ""; + (void)HookMgrExecute(GetBootStageHookMgr(), INIT_REBOOT, (void *)(&context), NULL); + PLUGIN_LOGI("After install reboot module"); + *size = g_rebootParamCmdValidNumber; + return g_rebootParamCmdInfos; +} + static void InitRebootHook_(RebootHookCtx *ctx) { InitModuleMgrInstall("rebootmodule"); @@ -53,5 +160,4 @@ MODULE_CONSTRUCTOR(void) { // 执行reboot时调用,安装reboot模块 InitAddRebootHook(InitRebootHook_); - InitAddGlobalInitHook(0, RebookGlobalHook); -} \ No newline at end of file +} diff --git a/services/param/linux/param_service.c b/services/param/linux/param_service.c index 489ea4c89299abbbbb6fb22760d5011bedf90d37..51121fceeac85d423ed2f4522f93065494d37d82 100755 --- a/services/param/linux/param_service.c +++ b/services/param/linux/param_service.c @@ -95,7 +95,7 @@ static int SendResponseMsg(ParamTaskPtr worker, const ParamMessage *msg, int res response->result = result; response->msg.msgSize = sizeof(ParamResponseMessage); ParamTaskSendMsg(worker, (ParamMessage *)response); - PARAM_LOGI("Send response msg msgId %d result %d", msg->id.msgId, result); + PARAM_LOGV("Send response msg msgId %d result %d", msg->id.msgId, result); return 0; } diff --git a/services/param/manager/param_manager.c b/services/param/manager/param_manager.c index ea0e7b15d0f088cdce6904a5b0229bed4c95c633..8ebdbf76eaa04f76f7bc4380788088f1e7db5a6c 100644 --- a/services/param/manager/param_manager.c +++ b/services/param/manager/param_manager.c @@ -279,24 +279,40 @@ static int CreateCtrlInfo(ServiceCtrlInfo **ctrlInfo, const char *cmd, uint32_t return 0; } +static int GetServiceCtrlInfoForPowerCtrl(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo) +{ + size_t size = 0; + const ParamCmdInfo *powerCtrlArg = GetStartupPowerCtl(&size); + PARAM_CHECK(powerCtrlArg != NULL, return -1, "Invalid ctrlInfo for %s", name); + uint32_t valueOffset = strlen(OHOS_SERVICE_CTRL_PREFIX) + strlen("reboot") + 1; + if (strcmp(value, "reboot") == 0) { + return CreateCtrlInfo(ctrlInfo, "reboot", valueOffset, 1, + "%s%s.%s", OHOS_SERVICE_CTRL_PREFIX, "reboot", value); + } + for (size_t i = 0; i < size; i++) { + PARAM_LOGV("Get power ctrl %s name %s value %s", powerCtrlArg[i].name, name, value); + if (strncmp(value, powerCtrlArg[i].name, strlen(powerCtrlArg[i].name)) == 0) { + valueOffset = strlen(OHOS_SERVICE_CTRL_PREFIX) + strlen(powerCtrlArg[i].replace) + 1; + return CreateCtrlInfo(ctrlInfo, powerCtrlArg[i].cmd, valueOffset, 1, + "%s%s.%s", OHOS_SERVICE_CTRL_PREFIX, powerCtrlArg[i].replace, value); + } + } + // not found reboot, so reboot by normal + valueOffset = strlen(OHOS_SERVICE_CTRL_PREFIX) + strlen("reboot") + 1; + return CreateCtrlInfo(ctrlInfo, "reboot", valueOffset, 1, "%s%s.%s", OHOS_SERVICE_CTRL_PREFIX, "reboot", value); +} + INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo) { PARAM_CHECK(ctrlInfo != NULL, return -1, "Invalid ctrlInfo %s", name); *ctrlInfo = NULL; size_t size = 0; if (strcmp("ohos.startup.powerctrl", name) == 0) { - const ParamCmdInfo *powerCtrlArg = GetStartupPowerCtl(&size); - for (size_t i = 0; i < size; i++) { - if (strncmp(value, powerCtrlArg[i].name, strlen(powerCtrlArg[i].name)) == 0) { - uint32_t valueOffset = strlen(OHOS_SERVICE_CTRL_PREFIX) + strlen(powerCtrlArg[i].replace) + 1; - return CreateCtrlInfo(ctrlInfo, powerCtrlArg[i].cmd, valueOffset, 1, - "%s%s.%s", OHOS_SERVICE_CTRL_PREFIX, powerCtrlArg[i].replace, value); - } - } - return 0; + return GetServiceCtrlInfoForPowerCtrl(name, value, ctrlInfo); } if (strncmp("ohos.ctl.", name, strlen("ohos.ctl.")) == 0) { const ParamCmdInfo *ctrlParam = GetServiceStartCtrl(&size); + PARAM_CHECK(ctrlParam != NULL, return -1, "Invalid ctrlInfo for %s", name); for (size_t i = 0; i < size; i++) { if (strcmp(name, ctrlParam[i].name) == 0) { uint32_t valueOffset = strlen(OHOS_SERVICE_CTRL_PREFIX) + strlen(ctrlParam[i].replace) + 1; @@ -307,6 +323,7 @@ INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, Servi } if (strncmp("ohos.servicectrl.", name, strlen("ohos.servicectrl.")) == 0) { const ParamCmdInfo *installParam = GetServiceCtl(&size); + PARAM_CHECK(installParam != NULL, return -1, "Invalid ctrlInfo for %s", name); for (size_t i = 0; i < size; i++) { if (strncmp(name, installParam[i].name, strlen(installParam[i].name)) == 0) { return CreateCtrlInfo(ctrlInfo, installParam[i].cmd, strlen(name) + 1, 1, "%s.%s", name, value); diff --git a/services/param/trigger/trigger_checker.c b/services/param/trigger/trigger_checker.c index 6d61552e056b5e32981c52c9d68a4eaf03dd1d08..972ce925ec763885bc1dd62bdc6987b92c244515 100644 --- a/services/param/trigger/trigger_checker.c +++ b/services/param/trigger/trigger_checker.c @@ -328,8 +328,20 @@ int CheckMatchSubCondition(const char *condition, const char *input, int length) { PARAM_CHECK(condition != NULL, return 0, "Invalid condition"); PARAM_CHECK(input != NULL, return 0, "Invalid input"); - char *tmp = strstr(condition, input); - if ((tmp != NULL) && ((int)strlen(tmp) > length) && (tmp[length] == '=')) { + const char *tmp = strstr(condition, input); + if (tmp == NULL) { + return 0; + } + PARAM_LOGV("CheckMatchSubCondition Condition: '%s' content: '%s' length %d", condition, input, length); + if (((int)strlen(tmp) <= length) || (tmp[length] != '=')) { + return 0; + } + // for condition: parameter = 1 + if (tmp == condition) { + return 1; + } + // for condition: parameter1 = 1 && parameter2 = 1 + if (*(tmp - 1) == ' ') { return 1; } return 0; diff --git a/services/param/trigger/trigger_manager.c b/services/param/trigger/trigger_manager.c index 3f7d61fe5c5b280ea74726be07a679f7968846bf..982a983dfc020dedcf5cb6d5c809d73d73208039 100644 --- a/services/param/trigger/trigger_manager.c +++ b/services/param/trigger/trigger_manager.c @@ -448,8 +448,7 @@ int32_t CheckAndMarkTrigger_(const TriggerWorkSpace *workSpace, int type, const trigger = head->nextTrigger(head, trigger); continue; } - const char *tmp = strstr(head->getCondition(trigger), name); - if (tmp != NULL && strncmp(tmp + strlen(name), "=", 1) == 0) { + if (CheckMatchSubCondition(head->getCondition(trigger), name, strlen(name)) == 1) { TRIGGER_SET_FLAG(trigger, TRIGGER_FLAGS_RELATED); ret = 1; } diff --git a/test/moduletest/param_test_cmds.c b/test/moduletest/param_test_cmds.c index 9a73e6980f1a83772fa2a567bae12e3c8d9fcea5..bf7641c0a677b8cc9d0e3e01fad4ef019e0b9b28 100644 --- a/test/moduletest/param_test_cmds.c +++ b/test/moduletest/param_test_cmds.c @@ -100,7 +100,7 @@ static void HandleParamChange2(const char *key, const char *value, void *context index = 5; // 5 add context ret = SystemWatchParameter(key, HandleParamChange2, (void *)index); if (ret != 0) { - printf("Add watcher %s fail %d \n", key, index); + printf("Add watcher %s fail %zu \n", key, index); } addWatcher = 1; return; @@ -109,21 +109,21 @@ static void HandleParamChange2(const char *key, const char *value, void *context index = 3; // 3 delete context RemoveParameterWatcher(key, HandleParamChange2, (void *)index); if (ret != 0) { - printf("Remove watcher fail %d \n", index); + printf("Remove watcher fail %zu \n", index); } return; } if (index == 1) { // 1 when context == 1 delete 1 RemoveParameterWatcher(key, HandleParamChange2, (void *)index); if (ret != 0) { - printf("Remove watcher fail %d \n", index); + printf("Remove watcher fail %zu \n", index); } return; } if ((index == 5) && (addWatcher == 1)) { // 5 when context == 5 delete 5 RemoveParameterWatcher(key, HandleParamChange2, (void *)index); if (ret != 0) { - printf("Remove watcher fail %d \n", index); + printf("Remove watcher fail %zu \n", index); } addWatcher = 0; } @@ -143,18 +143,18 @@ static void *CmdThreadWatcher(void *args) for (size_t i = 1; i <= MAX_NUMBER; i++) { int ret = SystemWatchParameter(context->name, HandleParamChange2, (void *)i); if (ret != 0) { - printf("Add watcher %s fail %d \n", context->name, i); + printf("Add watcher %s fail %zu \n", context->name, i); } ret = SetParameter(context->name, context->name); if (ret != 0) { - printf("Set parameter %s fail %d \n", context->name, i); + printf("Set parameter %s fail %zu \n", context->name, i); } } sleep(1); for (size_t i = 1; i <= MAX_NUMBER; i++) { int ret = RemoveParameterWatcher(context->name, HandleParamChange2, (void *)i); if (ret != 0) { - printf("Remove watcher %s fail %d \n", context->name, i); + printf("Remove watcher %s fail %zu \n", context->name, i); } } free(context); diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index cd459769031f2ac692cf13f8bdea66dc5c51c155..39b5b7c0260fb4a9718476f9d03ed991c3a14a45 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -81,7 +81,9 @@ ohos_unittest("init_unittest") { "//base/startup/init/services/modules/bootevent/bootevent.c", "//base/startup/init/services/modules/init_hook/init_hook.c", "//base/startup/init/services/modules/init_hook/param_hook.c", + "//base/startup/init/services/modules/reboot/reboot.c", "//base/startup/init/services/modules/reboot/reboot_misc.c", + "//base/startup/init/services/modules/reboot/reboot_static.c", "//base/startup/init/services/param/adapter/param_dac.c", "//base/startup/init/services/param/adapter/param_persistadp.c", "//base/startup/init/services/param/base/param_base.c", diff --git a/test/unittest/init/init_reboot_unittest.cpp b/test/unittest/init/init_reboot_unittest.cpp index 5e39bbcfc5680d092e28af95b90ee1c8382d9218..2db2bccb8e2d2b571819d823b7ee80c8e289fdfb 100644 --- a/test/unittest/init/init_reboot_unittest.cpp +++ b/test/unittest/init/init_reboot_unittest.cpp @@ -21,6 +21,7 @@ #include "init_utils.h" #include "trigger_manager.h" #include "init_group_manager.h" +#include "init_cmdexecutor.h" using namespace testing::ext; using namespace std; @@ -34,6 +35,59 @@ public: void TearDown() {}; }; +#ifndef OHOS_LITE +static int g_result = 0; +HWTEST_F(InitRebootUnitTest, TestAddRebootCmd, TestSize.Level1) +{ + auto rebootCallback = [](int id, const char *name, int argc, const char **argv) -> int { + return 0; + }; + int ret = AddRebootCmdExecutor("reboot_cmd1", rebootCallback); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd2", rebootCallback); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd3", rebootCallback); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd4", [](int id, const char *name, int argc, const char **argv)-> int { + g_result = 4; // 4 test index + return 0; + }); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd5", [](int id, const char *name, int argc, const char **argv)-> int { + g_result = 5; // 5 test index + return 0; + }); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd6", [](int id, const char *name, int argc, const char **argv)-> int { + g_result = 6; // 6 test index + return 0; + }); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback); + EXPECT_EQ(ret, 0); + ret = AddRebootCmdExecutor("reboot_cmd7", rebootCallback); + EXPECT_NE(ret, 0); + + TestSetParamCheckResult("ohos.servicectrl.reboot", 0777, 0); + // exec + SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd4"); + EXPECT_EQ(g_result, 4); // 4 test index + SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd5"); + EXPECT_EQ(g_result, 5); // 5 test index + SystemWriteParam("ohos.startup.powerctrl", "reboot,reboot_cmd6"); + EXPECT_EQ(g_result, 6); // 6 test index + + // invalid test + ret = AddRebootCmdExecutor(nullptr, [](int id, const char *name, int argc, const char **argv)-> int { + printf("reboot_cmd7 %s", name); + return 0; + }); + EXPECT_NE(ret, 0); + ret = AddRebootCmdExecutor(nullptr, nullptr); + EXPECT_NE(ret, 0); +} +#endif + HWTEST_F(InitRebootUnitTest, TestInitReboot, TestSize.Level1) { ExecReboot("reboot");