From f18df88eb15b939b8de5737c3c30f28a30b34af2 Mon Sep 17 00:00:00 2001 From: handyohos Date: Mon, 27 Jun 2022 12:04:47 +0800 Subject: [PATCH] feat: optimize hookmgr api definition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) 新增HookMgrAddEx接口,通过HOOK_INFO描述Hook的详细信息,包括hookCookie。 2) 更新了HookMgrExecute的preHook和postHook原型定义,HOOK_INFO和executionContext分离。 3)更新HookMgrTraversal的OhosHookTraversal原型定义,HOOK_INFO和traversalCookie分离。 4)增加了init_module_engine中的LoadParamsFile和SplitString接口。 5)同步更新了接口变更影响的代码。 Signed-off-by: handyohos Change-Id: I675dd74e9c913bd538333fbeda605f40bfdee03f --- interfaces/innerkits/hookmgr/hookmgr.c | 107 +++++++++--------- interfaces/innerkits/include/hookmgr.h | 67 +++++++---- .../include/init_module_engine.h | 3 + .../init_module_engine/init_modulemgr.c | 4 +- .../init_module_engine/stub/libinit.stub.json | 2 + services/init/standard/init.c | 29 +++-- services/init/standard/init_cmds.c | 2 +- services/modules/bootchart/bootchart_static.c | 2 +- services/param/manager/param_server.c | 5 + test/unittest/innerkits/hookmgr_unittest.cpp | 45 ++++---- 10 files changed, 145 insertions(+), 121 deletions(-) mode change 100644 => 100755 services/init/standard/init.c mode change 100644 => 100755 services/init/standard/init_cmds.c mode change 100644 => 100755 services/param/manager/param_server.c mode change 100644 => 100755 test/unittest/innerkits/hookmgr_unittest.cpp diff --git a/interfaces/innerkits/hookmgr/hookmgr.c b/interfaces/innerkits/hookmgr/hookmgr.c index f3baab9f..9233ab4e 100644 --- a/interfaces/innerkits/hookmgr/hookmgr.c +++ b/interfaces/innerkits/hookmgr/hookmgr.c @@ -30,8 +30,7 @@ typedef struct tagHOOK_STAGE HOOK_STAGE; */ typedef struct tagHOOK_ITEM { ListNode node; - int prio; - OhosHook hook; + HOOK_INFO info; HOOK_STAGE *stage; } HOOK_ITEM; @@ -116,7 +115,7 @@ static int hookItemCompare(ListNode *node, ListNode *newNode) hookItem = (const HOOK_ITEM *)node; newItem = (const HOOK_ITEM *)newNode; - return (hookItem->prio - newItem->prio); + return (hookItem->info.prio - newItem->info.prio); } struct HOOKITEM_COMPARE_VAL { @@ -130,13 +129,13 @@ static int hookItemCompareValue(ListNode *node, void *data) struct HOOKITEM_COMPARE_VAL *compareVal = (struct HOOKITEM_COMPARE_VAL *)data; hookItem = (const HOOK_ITEM *)node; - BEGET_CHECK(hookItem->prio == compareVal->prio, return (hookItem->prio - compareVal->prio)); - BEGET_CHECK(hookItem->hook != compareVal->hook, return 0); + BEGET_CHECK(hookItem->info.prio == compareVal->prio, return (hookItem->info.prio - compareVal->prio)); + BEGET_CHECK(hookItem->info.hook != compareVal->hook, return 0); return -1; } // Add hook to stage list with prio ordered -static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook) +static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook, void *hookCookie) { HOOK_ITEM *hookItem; struct HOOKITEM_COMPARE_VAL compareVal; @@ -150,8 +149,10 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook) // Create new item hookItem = (HOOK_ITEM *)malloc(sizeof(HOOK_ITEM)); BEGET_CHECK(hookItem != NULL, return -1); - hookItem->prio = prio; - hookItem->hook = hook; + hookItem->info.stage = hookStage->stage; + hookItem->info.prio = prio; + hookItem->info.hook = hook; + hookItem->info.hookCookie = hookCookie; hookItem->stage = hookStage; // Insert with order @@ -159,21 +160,32 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook) return 0; } -int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook) +int HookMgrAddEx(HOOK_MGR *hookMgr, const HOOK_INFO *hookInfo) { HOOK_STAGE *stageItem; - BEGET_CHECK(hook != NULL, return -1); + BEGET_CHECK(hookInfo != NULL, return -1); + BEGET_CHECK(hookInfo->hook != NULL, return -1); // Get HOOK_MGR hookMgr = getHookMgr(hookMgr, true); BEGET_CHECK(hookMgr != NULL, return -1); // Get HOOK_STAGE list - stageItem = getHookStage(hookMgr, stage, true); + stageItem = getHookStage(hookMgr, hookInfo->stage, true); BEGET_CHECK(stageItem != NULL, return -1); // Add hook to stage - return addHookToStage(stageItem, prio, hook); + return addHookToStage(stageItem, hookInfo->prio, hookInfo->hook, hookInfo->hookCookie); +} + +int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook) +{ + HOOK_INFO info; + info.stage = stage; + info.prio = prio; + info.hook = hook; + info.hookCookie = NULL; + return HookMgrAddEx(hookMgr, &info); } static int hookTraversalDelProc(ListNode *node, void *cookie) @@ -181,7 +193,7 @@ static int hookTraversalDelProc(ListNode *node, void *cookie) HOOK_ITEM *hookItem = (HOOK_ITEM *)node; // Not equal, just return - BEGET_CHECK((void *)hookItem->hook == cookie, return 0); + BEGET_CHECK((void *)hookItem->info.hook == cookie, return 0); // Remove from the list ListRemove(node); @@ -219,40 +231,36 @@ void HookMgrDel(HOOK_MGR *hookMgr, int stage, OhosHook hook) hookStageDestroy((ListNode *)stageItem); } -static int hookTraversalProc(ListNode *node, void *cookie) +typedef struct tagHOOK_EXECUTION_ARGS { + void *executionContext; + const HOOK_EXEC_OPTIONS *options; +} HOOK_EXECUTION_ARGS; + +static int hookExecutionProc(ListNode *node, void *cookie) { + int ret; HOOK_ITEM *hookItem = (HOOK_ITEM *)node; - HOOK_INFO hookInfo; - const HOOK_EXEC_ARGS *args = (const HOOK_EXEC_ARGS *)cookie; - - hookInfo.stage = hookItem->stage->stage; - hookInfo.prio = hookItem->prio; - hookInfo.hook = hookItem->hook; - hookInfo.cookie = NULL; - hookInfo.retVal = 0; - - if (args != NULL) { - hookInfo.cookie = args->cookie; - } + HOOK_EXECUTION_ARGS *args = (HOOK_EXECUTION_ARGS *)cookie; - if ((args != NULL) && (args->preHook != NULL)) { - args->preHook(&hookInfo); + if ((args->options != NULL) && (args->options->preHook != NULL)) { + args->options->preHook(&hookItem->info, args->executionContext); } - hookInfo.retVal = hookItem->hook(hookInfo.stage, hookItem->prio, hookInfo.cookie); - if ((args != NULL) && (args->postHook != NULL)) { - args->postHook(&hookInfo); + ret = hookItem->info.hook(&hookItem->info, args->executionContext); + if ((args->options != NULL) && (args->options->postHook != NULL)) { + args->options->postHook(&hookItem->info, args->executionContext, ret); } - return hookInfo.retVal; + return ret; } /* * 执行钩子函数 */ -int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *args) +int HookMgrExecute(HOOK_MGR *hookMgr, int stage, void *executionContext, const HOOK_EXEC_OPTIONS *options) { int flags; HOOK_STAGE *stageItem; + HOOK_EXECUTION_ARGS args; // Get HOOK_MGR hookMgr = getHookMgr(hookMgr, 0); @@ -263,13 +271,16 @@ int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *args) BEGET_CHECK(stageItem != NULL, return -1); flags = 0; - if (args != NULL) { - flags = args->flags; + if (options != NULL) { + flags = options->flags; } + args.executionContext = executionContext; + args.options = options; + // Traversal all hooks in the specified stage - return ListTraversal(&(stageItem->hooks), (void *)args, - hookTraversalProc, flags); + return ListTraversal(&(stageItem->hooks), (void *)(&args), + hookExecutionProc, flags); } HOOK_MGR *HookMgrCreate(const char *name) @@ -306,7 +317,7 @@ void HookMgrDestroy(HOOK_MGR *hookMgr) } typedef struct tagHOOK_TRAVERSAL_ARGS { - HOOK_INFO hookInfo; + void *traversalCookie; OhosHookTraversal traversal; } HOOK_TRAVERSAL_ARGS; @@ -318,32 +329,21 @@ static int hookItemTraversal(ListNode *node, void *data) hookItem = (HOOK_ITEM *)node; stageArgs = (HOOK_TRAVERSAL_ARGS *)data; - stageArgs->hookInfo.prio = hookItem->prio; - stageArgs->hookInfo.hook = hookItem->hook; - - stageArgs->traversal(&(stageArgs->hookInfo)); + stageArgs->traversal(&(hookItem->info), stageArgs->traversalCookie); return 0; } static int hookStageTraversal(ListNode *node, void *data) { - HOOK_STAGE *stageItem; - HOOK_TRAVERSAL_ARGS *stageArgs; - - stageItem = (HOOK_STAGE *)node; - stageArgs = (HOOK_TRAVERSAL_ARGS *)data; - - stageArgs->hookInfo.stage = stageItem->stage; - + HOOK_STAGE *stageItem = (HOOK_STAGE *)node; ListTraversal(&(stageItem->hooks), data, hookItemTraversal, 0); - return 0; } /* * 遍历所有的hooks */ -void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal traversal) +void HookMgrTraversal(HOOK_MGR *hookMgr, void *traversalCookie, OhosHookTraversal traversal) { HOOK_TRAVERSAL_ARGS stageArgs; @@ -353,8 +353,7 @@ void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal travers BEGET_CHECK(hookMgr != NULL, return); // Prepare common args - stageArgs.hookInfo.cookie = cookie; - stageArgs.hookInfo.retVal = 0; + stageArgs.traversalCookie = traversalCookie; stageArgs.traversal = traversal; ListTraversal(&(hookMgr->stages), (void *)(&stageArgs), hookStageTraversal, 0); } diff --git a/interfaces/innerkits/include/hookmgr.h b/interfaces/innerkits/include/hookmgr.h index 15e06a04..43fec7b4 100755 --- a/interfaces/innerkits/include/hookmgr.h +++ b/interfaces/innerkits/include/hookmgr.h @@ -66,15 +66,24 @@ extern "C" { /* Forward declaration for HookManager */ typedef struct tagHOOK_MGR HOOK_MGR; +/* Forward declaration for HOOK_INFO */ +typedef struct tagHOOK_INFO HOOK_INFO; + /** * @brief Hook function prototype * - * @param stage hook stage - * @param prio hook priority - * @param cookie input arguments for running the hook function + * @param hookInfo hook information + * @param executionContext input arguments for running the hook execution context * @return return 0 if succeed; other values if failed. */ -typedef int (*OhosHook)(int stage, int priority, void *cookie); +typedef int (*OhosHook)(const HOOK_INFO *hookInfo, void *executionContext); + +struct tagHOOK_INFO { + int stage; /* hook stage */ + int prio; /* hook priority */ + OhosHook hook; /* hook function */ + void *hookCookie; /* hook function cookie, for current hook only */ +}; /** * @brief Add a hook function @@ -88,6 +97,16 @@ typedef int (*OhosHook)(int stage, int priority, void *cookie); */ int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook); +/** + * @brief Add a hook function with full hook information + * + * @param hookMgr HookManager handle. + * If hookMgr is NULL, it will use default HookManager + * @param hookInfo full hook information + * @return return 0 if succeed; other values if failed. + */ +int HookMgrAddEx(HOOK_MGR *hookMgr, const HOOK_INFO *hookInfo); + /** * @brief Delete hook function * @@ -101,23 +120,23 @@ int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook); void HookMgrDel(HOOK_MGR *hookMgr, int stage, OhosHook hook); /** - * @brief Hook information for executing or traversing hooks + * @brief preHook function prototype for HookMgrExecute each hook + * + * @param hookInfo HOOK_INFO for the each hook. + * @param executionContext input arguments for running the hook execution context. + * @return None */ -typedef struct tagHOOK_INFO { - int stage; /* hook stage */ - int prio; /* hook priority */ - OhosHook hook; /* hook function */ - void *cookie; /* hook execution cookie */ - int retVal; /* hook execution return value */ -} HOOK_INFO; +typedef void (*OhosHookPreExecution)(const HOOK_INFO *hookInfo, void *executionContext); /** - * @brief preHook and postHook function prototype for HookMgrExecute + * @brief postHook function prototype for HookMgrExecute each hook * - * @param context HOOK_INFO for executing each hook. + * @param hookInfo HOOK_INFO for the each hook. + * @param executionContext input arguments for running the hook execution context. + * @param executionRetVal return value for running the hook. * @return None */ -typedef void (*OhosHookExecutionHook)(const HOOK_INFO *hookInfo); +typedef void (*OhosHookPostExecution)(const HOOK_INFO *hookInfo, void *executionContext, int executionRetVal); /* Executing hooks in descending priority order */ #define HOOK_EXEC_REVERSE_ORDER 0x01 @@ -127,16 +146,14 @@ typedef void (*OhosHookExecutionHook)(const HOOK_INFO *hookInfo); /** * @brief Extra execution arguments for HookMgrExecute */ -typedef struct tagHOOK_EXEC_ARGS { +typedef struct tagHOOK_EXEC_OPTIONS { /* Executing flags */ int flags; - /* Executing cookie */ - void *cookie; /* preHook for before executing each hook */ - OhosHookExecutionHook preHook; + OhosHookPreExecution preHook; /* postHook for before executing each hook */ - OhosHookExecutionHook postHook; -} HOOK_EXEC_ARGS; + OhosHookPostExecution postHook; +} HOOK_EXEC_OPTIONS; /** * @brief Executing each hooks in specified stages @@ -147,7 +164,7 @@ typedef struct tagHOOK_EXEC_ARGS { * @param extraArgs HOOK_EXEC_ARGS for executing each hook. * @return return 0 if succeed; other values if failed. */ -int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *extraArgs); +int HookMgrExecute(HOOK_MGR *hookMgr, int stage, void *executionContext, const HOOK_EXEC_OPTIONS *extraArgs); /** * @brief Create a HookManager handle @@ -172,18 +189,18 @@ void HookMgrDestroy(HOOK_MGR *hookMgr); * @param hookInfo HOOK_INFO for traversing each hook. * @return None */ -typedef void (*OhosHookTraversal)(const HOOK_INFO *hookInfo); +typedef void (*OhosHookTraversal)(const HOOK_INFO *hookInfo, void *traversalCookie); /** * @brief Traversing all hooks in the HookManager * * @param hookMgr HookManager handle. * If hookMgr is NULL, it will use default HookManager - * @param cookie traversal cookie. + * @param traversalCookie traversal cookie. * @param traversal traversal function. * @return None. */ -void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal traversal); +void HookMgrTraversal(HOOK_MGR *hookMgr, void *traversalCookie, OhosHookTraversal traversal); /** * @brief Get number of hooks in specified stage diff --git a/interfaces/innerkits/init_module_engine/include/init_module_engine.h b/interfaces/innerkits/init_module_engine/include/init_module_engine.h index 8ca35e7b..dd9c8d32 100755 --- a/interfaces/innerkits/init_module_engine/include/init_module_engine.h +++ b/interfaces/innerkits/init_module_engine/include/init_module_engine.h @@ -15,6 +15,7 @@ #ifndef BASE_STARTUP_INIT_PLUGIN_H #define BASE_STARTUP_INIT_PLUGIN_H +#include #include #include #include "modulemgr.h" @@ -32,6 +33,8 @@ int SystemWriteParam(const char *name, const char *value); int SystemReadParam(const char *name, char *value, unsigned int *len); +int LoadParamsFile(const char *fileName, bool onlyAdd); + typedef int (*CmdExecutor)(int id, const char *name, int argc, const char **argv); int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd); diff --git a/interfaces/innerkits/init_module_engine/init_modulemgr.c b/interfaces/innerkits/init_module_engine/init_modulemgr.c index e3f8f637..015f0306 100644 --- a/interfaces/innerkits/init_module_engine/init_modulemgr.c +++ b/interfaces/innerkits/init_module_engine/init_modulemgr.c @@ -72,7 +72,7 @@ static int ModuleMgrCmdUninstall(int id, const char *name, int argc, const char return 0; } -static int moduleMgrCommandsInit(int stage, int prio, void *cookie) +static int moduleMgrCommandsInit(const HOOK_INFO *info, void *cookie) { // "ohos.servicectrl.install" (void)AddCmdExecutor("install", ModuleMgrCmdInstall); @@ -81,7 +81,7 @@ static int moduleMgrCommandsInit(int stage, int prio, void *cookie) return 0; } -static int loadAutorunModules(int stage, int prio, void *cookie) +static int loadAutorunModules(const HOOK_INFO *info, void *cookie) { MODULE_MGR *autorun = ModuleMgrScan("init/autorun"); INIT_LOGV("Load autorun modules return %p", autorun); diff --git a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json index 5da2164d..624200b8 100755 --- a/interfaces/innerkits/init_module_engine/stub/libinit.stub.json +++ b/interfaces/innerkits/init_module_engine/stub/libinit.stub.json @@ -1,6 +1,8 @@ [ { "name": "SystemWriteParam" }, { "name": "SystemReadParam" }, + { "name": "LoadParamsFile" }, + { "name": "SplitString" }, { "name": "AddCmdExecutor" }, { "name": "RemoveCmdExecutor" }, { "name": "HookMgrAdd" }, diff --git a/services/init/standard/init.c b/services/init/standard/init.c old mode 100644 new mode 100755 index 4ea6c541..11aeeaff --- a/services/init/standard/init.c +++ b/services/init/standard/init.c @@ -317,17 +317,17 @@ typedef struct HOOK_TIMING_STAT { struct timespec endTime; } HOOK_TIMING_STAT; -static void InitPreHook(const HOOK_INFO *hookInfo) +static void InitPreHook(const HOOK_INFO *hookInfo, void *executionContext) { - HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)hookInfo->cookie; + HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)executionContext; clock_gettime(CLOCK_MONOTONIC, &(stat->startTime)); } -static void InitPostHook(const HOOK_INFO *hookInfo) +static void InitPostHook(const HOOK_INFO *hookInfo, void *executionContext, int executionRetVal) { long long diff; const long long baseTime = 1000; - HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)hookInfo->cookie; + HOOK_TIMING_STAT *stat = (HOOK_TIMING_STAT *)executionContext; clock_gettime(CLOCK_MONOTONIC, &(stat->endTime)); diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) / baseTime); @@ -338,23 +338,22 @@ static void InitPostHook(const HOOK_INFO *hookInfo) } INIT_LOGV("Executing hook [%d:%d:%p] cost [%lld]ms, return %d.", - hookInfo->stage, hookInfo->prio, hookInfo->hook, diff, hookInfo->retVal); + hookInfo->stage, hookInfo->prio, hookInfo->hook, diff, executionRetVal); } void SystemConfig(void) { HOOK_TIMING_STAT timingStat; - HOOK_EXEC_ARGS args; + HOOK_EXEC_OPTIONS options; - args.flags = 0; - args.cookie = (void *)&timingStat; - args.preHook = InitPreHook; - args.postHook = InitPostHook; + options.flags = 0; + options.preHook = InitPreHook; + options.postHook = InitPostHook; - HookMgrExecute(NULL, INIT_GLOBAL_INIT, (void *)&args); + HookMgrExecute(NULL, INIT_GLOBAL_INIT, (void *)&timingStat, (void *)&options); InitServiceSpace(); - HookMgrExecute(NULL, INIT_PRE_PARAM_SERVICE, (void *)&args); + HookMgrExecute(NULL, INIT_PRE_PARAM_SERVICE, (void *)&timingStat, (void *)&options); InitParamService(); InitParseGroupCfg(); RegisterBootStateChange(BootStateChange); @@ -363,13 +362,13 @@ void SystemConfig(void) // Do not move position! SystemLoadSelinux(); // parse parameters - HookMgrExecute(NULL, INIT_PRE_PARAM_LOAD, (void *)&args); + HookMgrExecute(NULL, INIT_PRE_PARAM_LOAD, (void *)&timingStat, (void *)&options); InitLoadParamFiles(); // read config - HookMgrExecute(NULL, INIT_PRE_CFG_LOAD, (void *)&args); + HookMgrExecute(NULL, INIT_PRE_CFG_LOAD, (void *)&timingStat, (void *)&options); ReadConfig(); INIT_LOGI("Parse init config file done."); - HookMgrExecute(NULL, INIT_POST_CFG_LOAD, (void *)&args); + HookMgrExecute(NULL, INIT_POST_CFG_LOAD, (void *)&timingStat, (void *)&options); // dump config #if defined(OHOS_SERVICE_DUMP) diff --git a/services/init/standard/init_cmds.c b/services/init/standard/init_cmds.c old mode 100644 new mode 100755 index 93341b98..623a477a --- a/services/init/standard/init_cmds.c +++ b/services/init/standard/init_cmds.c @@ -175,7 +175,7 @@ static void DoLoadPersistParams(const struct CmdArgs *ctx) { INIT_LOGV("LoadPersistParams"); LoadPersistParams(); - HookMgrExecute(NULL, INIT_POST_PERSIST_PARAM_LOAD, NULL); + HookMgrExecute(NULL, INIT_POST_PERSIST_PARAM_LOAD, NULL, NULL); } static void DoTriggerCmd(const struct CmdArgs *ctx) diff --git a/services/modules/bootchart/bootchart_static.c b/services/modules/bootchart/bootchart_static.c index 23bb35fc..bc1e9414 100644 --- a/services/modules/bootchart/bootchart_static.c +++ b/services/modules/bootchart/bootchart_static.c @@ -16,7 +16,7 @@ #include "init_module_engine.h" #include "plugin_adapter.h" -static int bootchartEarlyHook(int stage, int prio, void *cookie) +static int bootchartEarlyHook(const HOOK_INFO *info, void *cookie) { char enable[4] = {}; // 4 enable size uint32_t size = sizeof(enable); diff --git a/services/param/manager/param_server.c b/services/param/manager/param_server.c old mode 100644 new mode 100755 index 606053df..e75a0c82 --- a/services/param/manager/param_server.c +++ b/services/param/manager/param_server.c @@ -188,6 +188,11 @@ static int ProcessParamFile(const char *fileName, void *context) return LoadDefaultParam_(fileName, mode, exclude, ARRAY_LENGTH(exclude)); } +int LoadParamsFile(const char *fileName, bool onlyAdd) +{ + return LoadDefaultParams(fileName, onlyAdd ? LOAD_PARAM_ONLY_ADD : LOAD_PARAM_NORMAL); +} + int LoadDefaultParams(const char *fileName, uint32_t mode) { PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load"); diff --git a/test/unittest/innerkits/hookmgr_unittest.cpp b/test/unittest/innerkits/hookmgr_unittest.cpp old mode 100644 new mode 100755 index 3a107897..7df0eea1 --- a/test/unittest/innerkits/hookmgr_unittest.cpp +++ b/test/unittest/innerkits/hookmgr_unittest.cpp @@ -33,15 +33,15 @@ struct HookExecCtx { int retErr; }; -static int OhosHookTestCommon(void *cookie, int result) +static int OhosHookTestCommon(void *executionContext, int result) { struct HookExecCtx *ctx; - if (cookie == NULL) { + if (executionContext == NULL) { return 0; } - ctx = (struct HookExecCtx *)cookie; + ctx = (struct HookExecCtx *)executionContext; ctx->result = result; if (ctx->retErr) { return -1; @@ -49,22 +49,22 @@ static int OhosHookTestCommon(void *cookie, int result) return 0; } -static int OhosTestHookRetOK(int stage, int priority, void *cookie) +static int OhosTestHookRetOK(const HOOK_INFO *hookInfo, void *executionContext) { - return OhosHookTestCommon(cookie, 1); + return OhosHookTestCommon(executionContext, 1); } -static int OhosTestHookRetOKEx(int stage, int priority, void *cookie) +static int OhosTestHookRetOKEx(const HOOK_INFO *hookInfo, void *executionContext) { - return OhosHookTestCommon(cookie, 2); + return OhosHookTestCommon(executionContext, 2); } -static int OhosTestHookRetOKEx2(int stage, int priority, void *cookie) +static int OhosTestHookRetOKEx2(const HOOK_INFO *hookInfo, void *executionContext) { - return OhosHookTestCommon(cookie, 3); + return OhosHookTestCommon(executionContext, 3); } -static void OhosHookPrint(const HOOK_INFO *hookInfo) +static void OhosHookPrint(const HOOK_INFO *hookInfo, void *traversalCookie) { printf("\tstage[%02d] prio[%02d] hook[%p].\n", hookInfo->stage, hookInfo->prio, hookInfo->hook); } @@ -251,52 +251,51 @@ HWTEST_F(HookMgrUnitTest, HookMgrExecute_unitest, TestSize.Level1) { int ret; struct HookExecCtx ctx; - HOOK_EXEC_ARGS args; + HOOK_EXEC_OPTIONS options; ctx.result = 0; ctx.retErr = 0; - args.flags = 0; - args.cookie = (void *)&ctx; - args.preHook = NULL; - args.postHook = NULL; + options.flags = 0; + options.preHook = NULL; + options.postHook = NULL; ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOK); EXPECT_EQ(ret, 0); - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, NULL); EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 1); // Check ignore error ctx.retErr = 1; - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, NULL); EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 1); // Do not ignore return errors ctx.retErr = 1; - args.flags = HOOK_EXEC_EXIT_WHEN_ERROR; - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + options.flags = HOOK_EXEC_EXIT_WHEN_ERROR; + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, &options); ASSERT_NE(ret, 0); EXPECT_EQ(ctx.result, 1); - args.flags = 0; + options.flags = 0; // Add another hook with same priority ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOKEx); EXPECT_EQ(ret, 0); - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, NULL); EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 2); // Add another hook with higher priority ret = HookMgrAdd(NULL, STAGE_TEST_ONE, -1, OhosTestHookRetOKEx); EXPECT_EQ(ret, 0); - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, NULL); EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 2); HookMgrDel(NULL, STAGE_TEST_ONE, OhosTestHookRetOKEx); - ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); + ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, NULL); EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 1); } -- GitLab