提交 f18df88e 编写于 作者: H handyohos

feat: optimize hookmgr api definition

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: Nhandyohos <zhangxiaotian@huawei.com>
Change-Id: I675dd74e9c913bd538333fbeda605f40bfdee03f
上级 fa73124e
...@@ -30,8 +30,7 @@ typedef struct tagHOOK_STAGE HOOK_STAGE; ...@@ -30,8 +30,7 @@ typedef struct tagHOOK_STAGE HOOK_STAGE;
*/ */
typedef struct tagHOOK_ITEM { typedef struct tagHOOK_ITEM {
ListNode node; ListNode node;
int prio; HOOK_INFO info;
OhosHook hook;
HOOK_STAGE *stage; HOOK_STAGE *stage;
} HOOK_ITEM; } HOOK_ITEM;
...@@ -116,7 +115,7 @@ static int hookItemCompare(ListNode *node, ListNode *newNode) ...@@ -116,7 +115,7 @@ static int hookItemCompare(ListNode *node, ListNode *newNode)
hookItem = (const HOOK_ITEM *)node; hookItem = (const HOOK_ITEM *)node;
newItem = (const HOOK_ITEM *)newNode; newItem = (const HOOK_ITEM *)newNode;
return (hookItem->prio - newItem->prio); return (hookItem->info.prio - newItem->info.prio);
} }
struct HOOKITEM_COMPARE_VAL { struct HOOKITEM_COMPARE_VAL {
...@@ -130,13 +129,13 @@ static int hookItemCompareValue(ListNode *node, void *data) ...@@ -130,13 +129,13 @@ static int hookItemCompareValue(ListNode *node, void *data)
struct HOOKITEM_COMPARE_VAL *compareVal = (struct HOOKITEM_COMPARE_VAL *)data; struct HOOKITEM_COMPARE_VAL *compareVal = (struct HOOKITEM_COMPARE_VAL *)data;
hookItem = (const HOOK_ITEM *)node; hookItem = (const HOOK_ITEM *)node;
BEGET_CHECK(hookItem->prio == compareVal->prio, return (hookItem->prio - compareVal->prio)); BEGET_CHECK(hookItem->info.prio == compareVal->prio, return (hookItem->info.prio - compareVal->prio));
BEGET_CHECK(hookItem->hook != compareVal->hook, return 0); BEGET_CHECK(hookItem->info.hook != compareVal->hook, return 0);
return -1; return -1;
} }
// Add hook to stage list with prio ordered // 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; HOOK_ITEM *hookItem;
struct HOOKITEM_COMPARE_VAL compareVal; struct HOOKITEM_COMPARE_VAL compareVal;
...@@ -150,8 +149,10 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook) ...@@ -150,8 +149,10 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook)
// Create new item // Create new item
hookItem = (HOOK_ITEM *)malloc(sizeof(HOOK_ITEM)); hookItem = (HOOK_ITEM *)malloc(sizeof(HOOK_ITEM));
BEGET_CHECK(hookItem != NULL, return -1); BEGET_CHECK(hookItem != NULL, return -1);
hookItem->prio = prio; hookItem->info.stage = hookStage->stage;
hookItem->hook = hook; hookItem->info.prio = prio;
hookItem->info.hook = hook;
hookItem->info.hookCookie = hookCookie;
hookItem->stage = hookStage; hookItem->stage = hookStage;
// Insert with order // Insert with order
...@@ -159,21 +160,32 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook) ...@@ -159,21 +160,32 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook)
return 0; 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; 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 // Get HOOK_MGR
hookMgr = getHookMgr(hookMgr, true); hookMgr = getHookMgr(hookMgr, true);
BEGET_CHECK(hookMgr != NULL, return -1); BEGET_CHECK(hookMgr != NULL, return -1);
// Get HOOK_STAGE list // Get HOOK_STAGE list
stageItem = getHookStage(hookMgr, stage, true); stageItem = getHookStage(hookMgr, hookInfo->stage, true);
BEGET_CHECK(stageItem != NULL, return -1); BEGET_CHECK(stageItem != NULL, return -1);
// Add hook to stage // 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) static int hookTraversalDelProc(ListNode *node, void *cookie)
...@@ -181,7 +193,7 @@ static int hookTraversalDelProc(ListNode *node, void *cookie) ...@@ -181,7 +193,7 @@ static int hookTraversalDelProc(ListNode *node, void *cookie)
HOOK_ITEM *hookItem = (HOOK_ITEM *)node; HOOK_ITEM *hookItem = (HOOK_ITEM *)node;
// Not equal, just return // 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 // Remove from the list
ListRemove(node); ListRemove(node);
...@@ -219,40 +231,36 @@ void HookMgrDel(HOOK_MGR *hookMgr, int stage, OhosHook hook) ...@@ -219,40 +231,36 @@ void HookMgrDel(HOOK_MGR *hookMgr, int stage, OhosHook hook)
hookStageDestroy((ListNode *)stageItem); 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_ITEM *hookItem = (HOOK_ITEM *)node;
HOOK_INFO hookInfo; HOOK_EXECUTION_ARGS *args = (HOOK_EXECUTION_ARGS *)cookie;
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;
}
if ((args != NULL) && (args->preHook != NULL)) { if ((args->options != NULL) && (args->options->preHook != NULL)) {
args->preHook(&hookInfo); args->options->preHook(&hookItem->info, args->executionContext);
} }
hookInfo.retVal = hookItem->hook(hookInfo.stage, hookItem->prio, hookInfo.cookie); ret = hookItem->info.hook(&hookItem->info, args->executionContext);
if ((args != NULL) && (args->postHook != NULL)) { if ((args->options != NULL) && (args->options->postHook != NULL)) {
args->postHook(&hookInfo); 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; int flags;
HOOK_STAGE *stageItem; HOOK_STAGE *stageItem;
HOOK_EXECUTION_ARGS args;
// Get HOOK_MGR // Get HOOK_MGR
hookMgr = getHookMgr(hookMgr, 0); hookMgr = getHookMgr(hookMgr, 0);
...@@ -263,13 +271,16 @@ int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *args) ...@@ -263,13 +271,16 @@ int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *args)
BEGET_CHECK(stageItem != NULL, return -1); BEGET_CHECK(stageItem != NULL, return -1);
flags = 0; flags = 0;
if (args != NULL) { if (options != NULL) {
flags = args->flags; flags = options->flags;
} }
args.executionContext = executionContext;
args.options = options;
// Traversal all hooks in the specified stage // Traversal all hooks in the specified stage
return ListTraversal(&(stageItem->hooks), (void *)args, return ListTraversal(&(stageItem->hooks), (void *)(&args),
hookTraversalProc, flags); hookExecutionProc, flags);
} }
HOOK_MGR *HookMgrCreate(const char *name) HOOK_MGR *HookMgrCreate(const char *name)
...@@ -306,7 +317,7 @@ void HookMgrDestroy(HOOK_MGR *hookMgr) ...@@ -306,7 +317,7 @@ void HookMgrDestroy(HOOK_MGR *hookMgr)
} }
typedef struct tagHOOK_TRAVERSAL_ARGS { typedef struct tagHOOK_TRAVERSAL_ARGS {
HOOK_INFO hookInfo; void *traversalCookie;
OhosHookTraversal traversal; OhosHookTraversal traversal;
} HOOK_TRAVERSAL_ARGS; } HOOK_TRAVERSAL_ARGS;
...@@ -318,32 +329,21 @@ static int hookItemTraversal(ListNode *node, void *data) ...@@ -318,32 +329,21 @@ static int hookItemTraversal(ListNode *node, void *data)
hookItem = (HOOK_ITEM *)node; hookItem = (HOOK_ITEM *)node;
stageArgs = (HOOK_TRAVERSAL_ARGS *)data; stageArgs = (HOOK_TRAVERSAL_ARGS *)data;
stageArgs->hookInfo.prio = hookItem->prio; stageArgs->traversal(&(hookItem->info), stageArgs->traversalCookie);
stageArgs->hookInfo.hook = hookItem->hook;
stageArgs->traversal(&(stageArgs->hookInfo));
return 0; return 0;
} }
static int hookStageTraversal(ListNode *node, void *data) static int hookStageTraversal(ListNode *node, void *data)
{ {
HOOK_STAGE *stageItem; HOOK_STAGE *stageItem = (HOOK_STAGE *)node;
HOOK_TRAVERSAL_ARGS *stageArgs;
stageItem = (HOOK_STAGE *)node;
stageArgs = (HOOK_TRAVERSAL_ARGS *)data;
stageArgs->hookInfo.stage = stageItem->stage;
ListTraversal(&(stageItem->hooks), data, hookItemTraversal, 0); ListTraversal(&(stageItem->hooks), data, hookItemTraversal, 0);
return 0; return 0;
} }
/* /*
* 遍历所有的hooks * 遍历所有的hooks
*/ */
void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal traversal) void HookMgrTraversal(HOOK_MGR *hookMgr, void *traversalCookie, OhosHookTraversal traversal)
{ {
HOOK_TRAVERSAL_ARGS stageArgs; HOOK_TRAVERSAL_ARGS stageArgs;
...@@ -353,8 +353,7 @@ void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal travers ...@@ -353,8 +353,7 @@ void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal travers
BEGET_CHECK(hookMgr != NULL, return); BEGET_CHECK(hookMgr != NULL, return);
// Prepare common args // Prepare common args
stageArgs.hookInfo.cookie = cookie; stageArgs.traversalCookie = traversalCookie;
stageArgs.hookInfo.retVal = 0;
stageArgs.traversal = traversal; stageArgs.traversal = traversal;
ListTraversal(&(hookMgr->stages), (void *)(&stageArgs), hookStageTraversal, 0); ListTraversal(&(hookMgr->stages), (void *)(&stageArgs), hookStageTraversal, 0);
} }
......
...@@ -66,15 +66,24 @@ extern "C" { ...@@ -66,15 +66,24 @@ extern "C" {
/* Forward declaration for HookManager */ /* Forward declaration for HookManager */
typedef struct tagHOOK_MGR HOOK_MGR; typedef struct tagHOOK_MGR HOOK_MGR;
/* Forward declaration for HOOK_INFO */
typedef struct tagHOOK_INFO HOOK_INFO;
/** /**
* @brief Hook function prototype * @brief Hook function prototype
* *
* @param stage hook stage * @param hookInfo hook information
* @param prio hook priority * @param executionContext input arguments for running the hook execution context
* @param cookie input arguments for running the hook function
* @return return 0 if succeed; other values if failed. * @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 * @brief Add a hook function
...@@ -88,6 +97,16 @@ typedef int (*OhosHook)(int stage, int priority, void *cookie); ...@@ -88,6 +97,16 @@ typedef int (*OhosHook)(int stage, int priority, void *cookie);
*/ */
int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook); 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 * @brief Delete hook function
* *
...@@ -101,23 +120,23 @@ int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook); ...@@ -101,23 +120,23 @@ int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook);
void HookMgrDel(HOOK_MGR *hookMgr, int stage, 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 { typedef void (*OhosHookPreExecution)(const HOOK_INFO *hookInfo, void *executionContext);
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;
/** /**
* @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 * @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 */ /* Executing hooks in descending priority order */
#define HOOK_EXEC_REVERSE_ORDER 0x01 #define HOOK_EXEC_REVERSE_ORDER 0x01
...@@ -127,16 +146,14 @@ typedef void (*OhosHookExecutionHook)(const HOOK_INFO *hookInfo); ...@@ -127,16 +146,14 @@ typedef void (*OhosHookExecutionHook)(const HOOK_INFO *hookInfo);
/** /**
* @brief Extra execution arguments for HookMgrExecute * @brief Extra execution arguments for HookMgrExecute
*/ */
typedef struct tagHOOK_EXEC_ARGS { typedef struct tagHOOK_EXEC_OPTIONS {
/* Executing flags */ /* Executing flags */
int flags; int flags;
/* Executing cookie */
void *cookie;
/* preHook for before executing each hook */ /* preHook for before executing each hook */
OhosHookExecutionHook preHook; OhosHookPreExecution preHook;
/* postHook for before executing each hook */ /* postHook for before executing each hook */
OhosHookExecutionHook postHook; OhosHookPostExecution postHook;
} HOOK_EXEC_ARGS; } HOOK_EXEC_OPTIONS;
/** /**
* @brief Executing each hooks in specified stages * @brief Executing each hooks in specified stages
...@@ -147,7 +164,7 @@ typedef struct tagHOOK_EXEC_ARGS { ...@@ -147,7 +164,7 @@ typedef struct tagHOOK_EXEC_ARGS {
* @param extraArgs HOOK_EXEC_ARGS for executing each hook. * @param extraArgs HOOK_EXEC_ARGS for executing each hook.
* @return return 0 if succeed; other values if failed. * @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 * @brief Create a HookManager handle
...@@ -172,18 +189,18 @@ void HookMgrDestroy(HOOK_MGR *hookMgr); ...@@ -172,18 +189,18 @@ void HookMgrDestroy(HOOK_MGR *hookMgr);
* @param hookInfo HOOK_INFO for traversing each hook. * @param hookInfo HOOK_INFO for traversing each hook.
* @return None * @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 * @brief Traversing all hooks in the HookManager
* *
* @param hookMgr HookManager handle. * @param hookMgr HookManager handle.
* If hookMgr is NULL, it will use default HookManager * If hookMgr is NULL, it will use default HookManager
* @param cookie traversal cookie. * @param traversalCookie traversal cookie.
* @param traversal traversal function. * @param traversal traversal function.
* @return None. * @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 * @brief Get number of hooks in specified stage
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#ifndef BASE_STARTUP_INIT_PLUGIN_H #ifndef BASE_STARTUP_INIT_PLUGIN_H
#define BASE_STARTUP_INIT_PLUGIN_H #define BASE_STARTUP_INIT_PLUGIN_H
#include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include "modulemgr.h" #include "modulemgr.h"
...@@ -32,6 +33,8 @@ int SystemWriteParam(const char *name, const char *value); ...@@ -32,6 +33,8 @@ int SystemWriteParam(const char *name, const char *value);
int SystemReadParam(const char *name, char *value, unsigned int *len); 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); typedef int (*CmdExecutor)(int id, const char *name, int argc, const char **argv);
int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd); int AddCmdExecutor(const char *cmdName, CmdExecutor execCmd);
......
...@@ -72,7 +72,7 @@ static int ModuleMgrCmdUninstall(int id, const char *name, int argc, const char ...@@ -72,7 +72,7 @@ static int ModuleMgrCmdUninstall(int id, const char *name, int argc, const char
return 0; return 0;
} }
static int moduleMgrCommandsInit(int stage, int prio, void *cookie) static int moduleMgrCommandsInit(const HOOK_INFO *info, void *cookie)
{ {
// "ohos.servicectrl.install" // "ohos.servicectrl.install"
(void)AddCmdExecutor("install", ModuleMgrCmdInstall); (void)AddCmdExecutor("install", ModuleMgrCmdInstall);
...@@ -81,7 +81,7 @@ static int moduleMgrCommandsInit(int stage, int prio, void *cookie) ...@@ -81,7 +81,7 @@ static int moduleMgrCommandsInit(int stage, int prio, void *cookie)
return 0; 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"); MODULE_MGR *autorun = ModuleMgrScan("init/autorun");
INIT_LOGV("Load autorun modules return %p", autorun); INIT_LOGV("Load autorun modules return %p", autorun);
......
[ [
{ "name": "SystemWriteParam" }, { "name": "SystemWriteParam" },
{ "name": "SystemReadParam" }, { "name": "SystemReadParam" },
{ "name": "LoadParamsFile" },
{ "name": "SplitString" },
{ "name": "AddCmdExecutor" }, { "name": "AddCmdExecutor" },
{ "name": "RemoveCmdExecutor" }, { "name": "RemoveCmdExecutor" },
{ "name": "HookMgrAdd" }, { "name": "HookMgrAdd" },
......
...@@ -317,17 +317,17 @@ typedef struct HOOK_TIMING_STAT { ...@@ -317,17 +317,17 @@ typedef struct HOOK_TIMING_STAT {
struct timespec endTime; struct timespec endTime;
} HOOK_TIMING_STAT; } 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)); 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; long long diff;
const long long baseTime = 1000; 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)); clock_gettime(CLOCK_MONOTONIC, &(stat->endTime));
diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) / baseTime); diff = (long long)((stat->endTime.tv_sec - stat->startTime.tv_sec) / baseTime);
...@@ -338,23 +338,22 @@ static void InitPostHook(const HOOK_INFO *hookInfo) ...@@ -338,23 +338,22 @@ static void InitPostHook(const HOOK_INFO *hookInfo)
} }
INIT_LOGV("Executing hook [%d:%d:%p] cost [%lld]ms, return %d.", 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) void SystemConfig(void)
{ {
HOOK_TIMING_STAT timingStat; HOOK_TIMING_STAT timingStat;
HOOK_EXEC_ARGS args; HOOK_EXEC_OPTIONS options;
args.flags = 0; options.flags = 0;
args.cookie = (void *)&timingStat; options.preHook = InitPreHook;
args.preHook = InitPreHook; options.postHook = InitPostHook;
args.postHook = InitPostHook;
HookMgrExecute(NULL, INIT_GLOBAL_INIT, (void *)&args); HookMgrExecute(NULL, INIT_GLOBAL_INIT, (void *)&timingStat, (void *)&options);
InitServiceSpace(); InitServiceSpace();
HookMgrExecute(NULL, INIT_PRE_PARAM_SERVICE, (void *)&args); HookMgrExecute(NULL, INIT_PRE_PARAM_SERVICE, (void *)&timingStat, (void *)&options);
InitParamService(); InitParamService();
InitParseGroupCfg(); InitParseGroupCfg();
RegisterBootStateChange(BootStateChange); RegisterBootStateChange(BootStateChange);
...@@ -363,13 +362,13 @@ void SystemConfig(void) ...@@ -363,13 +362,13 @@ void SystemConfig(void)
// Do not move position! // Do not move position!
SystemLoadSelinux(); SystemLoadSelinux();
// parse parameters // parse parameters
HookMgrExecute(NULL, INIT_PRE_PARAM_LOAD, (void *)&args); HookMgrExecute(NULL, INIT_PRE_PARAM_LOAD, (void *)&timingStat, (void *)&options);
InitLoadParamFiles(); InitLoadParamFiles();
// read config // read config
HookMgrExecute(NULL, INIT_PRE_CFG_LOAD, (void *)&args); HookMgrExecute(NULL, INIT_PRE_CFG_LOAD, (void *)&timingStat, (void *)&options);
ReadConfig(); ReadConfig();
INIT_LOGI("Parse init config file done."); 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 // dump config
#if defined(OHOS_SERVICE_DUMP) #if defined(OHOS_SERVICE_DUMP)
......
...@@ -175,7 +175,7 @@ static void DoLoadPersistParams(const struct CmdArgs *ctx) ...@@ -175,7 +175,7 @@ static void DoLoadPersistParams(const struct CmdArgs *ctx)
{ {
INIT_LOGV("LoadPersistParams"); INIT_LOGV("LoadPersistParams");
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) static void DoTriggerCmd(const struct CmdArgs *ctx)
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
#include "init_module_engine.h" #include "init_module_engine.h"
#include "plugin_adapter.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 char enable[4] = {}; // 4 enable size
uint32_t size = sizeof(enable); uint32_t size = sizeof(enable);
......
...@@ -188,6 +188,11 @@ static int ProcessParamFile(const char *fileName, void *context) ...@@ -188,6 +188,11 @@ static int ProcessParamFile(const char *fileName, void *context)
return LoadDefaultParam_(fileName, mode, exclude, ARRAY_LENGTH(exclude)); 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) int LoadDefaultParams(const char *fileName, uint32_t mode)
{ {
PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load"); PARAM_CHECK(fileName != NULL, return -1, "Invalid filename for load");
......
...@@ -33,15 +33,15 @@ struct HookExecCtx { ...@@ -33,15 +33,15 @@ struct HookExecCtx {
int retErr; int retErr;
}; };
static int OhosHookTestCommon(void *cookie, int result) static int OhosHookTestCommon(void *executionContext, int result)
{ {
struct HookExecCtx *ctx; struct HookExecCtx *ctx;
if (cookie == NULL) { if (executionContext == NULL) {
return 0; return 0;
} }
ctx = (struct HookExecCtx *)cookie; ctx = (struct HookExecCtx *)executionContext;
ctx->result = result; ctx->result = result;
if (ctx->retErr) { if (ctx->retErr) {
return -1; return -1;
...@@ -49,22 +49,22 @@ static int OhosHookTestCommon(void *cookie, int result) ...@@ -49,22 +49,22 @@ static int OhosHookTestCommon(void *cookie, int result)
return 0; 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); 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) ...@@ -251,52 +251,51 @@ HWTEST_F(HookMgrUnitTest, HookMgrExecute_unitest, TestSize.Level1)
{ {
int ret; int ret;
struct HookExecCtx ctx; struct HookExecCtx ctx;
HOOK_EXEC_ARGS args; HOOK_EXEC_OPTIONS options;
ctx.result = 0; ctx.result = 0;
ctx.retErr = 0; ctx.retErr = 0;
args.flags = 0; options.flags = 0;
args.cookie = (void *)&ctx; options.preHook = NULL;
args.preHook = NULL; options.postHook = NULL;
args.postHook = NULL;
ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOK); ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOK);
EXPECT_EQ(ret, 0); 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(ret, 0);
EXPECT_EQ(ctx.result, 1); EXPECT_EQ(ctx.result, 1);
// Check ignore error // Check ignore error
ctx.retErr = 1; 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(ret, 0);
EXPECT_EQ(ctx.result, 1); EXPECT_EQ(ctx.result, 1);
// Do not ignore return errors // Do not ignore return errors
ctx.retErr = 1; ctx.retErr = 1;
args.flags = HOOK_EXEC_EXIT_WHEN_ERROR; options.flags = HOOK_EXEC_EXIT_WHEN_ERROR;
ret = HookMgrExecute(NULL, STAGE_TEST_ONE, &args); ret = HookMgrExecute(NULL, STAGE_TEST_ONE, (void *)&ctx, &options);
ASSERT_NE(ret, 0); ASSERT_NE(ret, 0);
EXPECT_EQ(ctx.result, 1); EXPECT_EQ(ctx.result, 1);
args.flags = 0; options.flags = 0;
// Add another hook with same priority // Add another hook with same priority
ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOKEx); ret = HookMgrAdd(NULL, STAGE_TEST_ONE, 0, OhosTestHookRetOKEx);
EXPECT_EQ(ret, 0); 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(ret, 0);
EXPECT_EQ(ctx.result, 2); EXPECT_EQ(ctx.result, 2);
// Add another hook with higher priority // Add another hook with higher priority
ret = HookMgrAdd(NULL, STAGE_TEST_ONE, -1, OhosTestHookRetOKEx); ret = HookMgrAdd(NULL, STAGE_TEST_ONE, -1, OhosTestHookRetOKEx);
EXPECT_EQ(ret, 0); 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(ret, 0);
EXPECT_EQ(ctx.result, 2); EXPECT_EQ(ctx.result, 2);
HookMgrDel(NULL, STAGE_TEST_ONE, OhosTestHookRetOKEx); 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(ret, 0);
EXPECT_EQ(ctx.result, 1); EXPECT_EQ(ctx.result, 1);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册