提交 cde18501 编写于 作者: C cheng_jinsong

Merge branch 'master' of https://gitee.com/chneg-jinsong/startup_init_lite_sf1017 into init1017

......@@ -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);
......
......@@ -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
}
......
......@@ -17,5 +17,6 @@
{ "name": "StartupLog" },
{ "name": "DoJobNow" },
{ "name": "GetServiceExtData" },
{ "name": "UpdateMiscMessage" }
{ "name": "UpdateMiscMessage" },
{ "name": "AddRebootCmdExecutor" }
]
......@@ -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);
}
......
......@@ -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;
}
......
......@@ -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" ]
......
......@@ -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);
......
......@@ -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)
{
......
......@@ -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 <sys/syscall.h>
#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();
}
......@@ -12,11 +12,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <errno.h>
#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
}
......@@ -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;
}
......
......@@ -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);
......
......@@ -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;
......
......@@ -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;
}
......
......@@ -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);
......
......@@ -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",
......
......@@ -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");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册