• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BASE_STARTUP_BOOTSTAGE_H
17 #define BASE_STARTUP_BOOTSTAGE_H
18 
19 #include "hookmgr.h"
20 #include "cJSON.h"
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 enum INIT_BOOTSTAGE {
29     INIT_GLOBAL_INIT       = 0,
30     INIT_FIRST_STAGE       = 1,
31     INIT_MOUNT_STAGE       = 3,
32     INIT_RESTORECON       = 4,
33     INIT_POST_DATA_UNENCRYPT       = 5,
34     INIT_PRE_PARAM_SERVICE = 10,
35     INIT_PRE_PARAM_LOAD    = 20,
36     INIT_PARAM_LOAD_FILTER = 25,
37     INIT_PRE_CFG_LOAD      = 30,
38     INIT_SERVICE_PARSE     = 35,
39     INIT_POST_PERSIST_PARAM_LOAD   = 40,
40     INIT_POST_CFG_LOAD     = 50,
41     INIT_CMD_RECORD     = 51,
42     INIT_REBOOT            = 55,
43     INIT_SERVICE_CLEAR     = 56,
44     INIT_SERVICE_DUMP      = 57,
45     INIT_SERVICE_FORK_BEFORE       = 58,
46     INIT_SERVICE_SET_PERMS = 59,
47     INIT_SERVICE_FORK_AFTER = 60,
48     INIT_SERVICE_BOOTEVENT = 61,
49     INIT_SERVICE_REAP      = 65,
50     INIT_SHUT_DETECTOR     = 66,
51     INIT_SERVICE_RESTART   = 71,
52     INIT_JOB_PARSE         = 70,
53     INIT_BOOT_COMPLETE     = 100,
54 };
55 
56 HOOK_MGR *GetBootStageHookMgr();
57 
InitAddGlobalInitHook(int prio,OhosHook hook)58 __attribute__((always_inline)) inline int InitAddGlobalInitHook(int prio, OhosHook hook)
59 {
60     return HookMgrAdd(GetBootStageHookMgr(), INIT_GLOBAL_INIT, prio, hook);
61 }
62 
InitAddPreParamServiceHook(int prio,OhosHook hook)63 __attribute__((always_inline)) inline int InitAddPreParamServiceHook(int prio, OhosHook hook)
64 {
65     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, prio, hook);
66 }
67 
InitAddPreParamLoadHook(int prio,OhosHook hook)68 __attribute__((always_inline)) inline int InitAddPreParamLoadHook(int prio, OhosHook hook)
69 {
70     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_LOAD, prio, hook);
71 }
72 
73 /**
74  * @brief Parameter load filter context
75  */
76 typedef struct tagPARAM_LOAD_FILTER_CTX {
77     const char *name;    /* Parameter name */
78     const char *value;   /* Parameter value */
79     int ignored;         /* Ignore this parameter or not */
80 } PARAM_LOAD_FILTER_CTX;
81 
82 /**
83  * @brief Parameter Load Hook function prototype
84  *
85  * @param hookInfo hook information
86  * @param filter filter information context
87  * @return return 0 if succeed; other values if failed.
88  */
89 typedef int (*ParamLoadFilter)(const HOOK_INFO *hookInfo, PARAM_LOAD_FILTER_CTX *filter);
90 
InitAddParamLoadFilterHook(int prio,ParamLoadFilter filter)91 __attribute__((always_inline)) inline int InitAddParamLoadFilterHook(int prio, ParamLoadFilter filter)
92 {
93     return HookMgrAdd(GetBootStageHookMgr(), INIT_PARAM_LOAD_FILTER, prio, (OhosHook)filter);
94 }
95 
InitAddPreCfgLoadHook(int prio,OhosHook hook)96 __attribute__((always_inline)) inline int InitAddPreCfgLoadHook(int prio, OhosHook hook)
97 {
98     return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, prio, hook);
99 }
100 
InitAddPostCfgLoadHook(int prio,OhosHook hook)101 __attribute__((always_inline)) inline int InitAddPostCfgLoadHook(int prio, OhosHook hook)
102 {
103     return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, prio, hook);
104 }
105 
InitAddPostPersistParamLoadHook(int prio,OhosHook hook)106 __attribute__((always_inline)) inline int InitAddPostPersistParamLoadHook(int prio, OhosHook hook)
107 {
108     return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, prio, hook);
109 }
110 
111 /**
112  * @brief service config parsing context information
113  */
114 typedef struct tagSERVICE_PARSE_CTX {
115     const char *serviceName;    /* Service name */
116     const cJSON *serviceNode;   /* Service JSON node */
117 } SERVICE_PARSE_CTX;
118 
119 /**
120  * @brief job config parsing context information
121  */
122 typedef struct tagJOB_PARSE_CTX {
123     const char *jobName;    /* job name */
124     const cJSON *jobNode;   /* job JSON node */
125 } JOB_PARSE_CTX;
126 
127 /**
128  * @brief service info
129  */
130 typedef struct tagSERVICE_INFO_CTX {
131     const char *serviceName;    /* Service name */
132     const char *reserved;       /* reserved info */
133 } SERVICE_INFO_CTX;
134 
135 /**
136  * @brief service info
137  */
138 typedef struct tagSERVICE_BOOTEVENT_CTX {
139     const char *serviceName;    /* Service name */
140     const char *reserved;       /* reserved info */
141     int state;                  /* bootevent state */
142 } SERVICE_BOOTEVENT_CTX;
143 
144 /**
145  * @brief service restart info
146  */
147 typedef struct tagSERVICE_RESTART_CTX {
148     const char *serviceName;    /* Service name */
149     const char *serviceNode;    /* Service node */
150 } SERVICE_RESTART_CTX;
151 
152 /**
153  * @brief init cmd info
154  */
155 typedef struct InitCmdInfo {
156     const char *cmdName;    /* cmd name */
157     const char *cmdContent;    /* cmd content */
158     const char *reserved;       /* reserved info */
159 } INIT_CMD_INFO;
160 
161 /**
162  * @brief service config parse hook function prototype
163  *
164  * @param serviceParseCtx service config parsing context information
165  * @return None
166  */
167 typedef void (*ServiceParseHook)(SERVICE_PARSE_CTX *serviceParseCtx);
168 
169 /**
170  * @brief job config parse hook function prototype
171  *
172  * @param JobParseHook job config parsing context information
173  * @return None
174  */
175 typedef void (*JobParseHook)(JOB_PARSE_CTX *jobParseCtx);
176 
177 /**
178  * @brief service hook function prototype
179  *
180  * @param ServiceHook service info
181  * @return None
182  */
183 typedef void (*ServiceHook)(SERVICE_INFO_CTX *serviceCtx);
184 
185 /**
186  * @brief service restart hook function prototype
187  *
188  * @param ServiceRestartHook service info
189  * @return None
190  */
191 typedef void (*ServiceRestartHook) (SERVICE_RESTART_CTX *serviceRestartCtx);
192 
193 /**
194  * @brief Register a hook for service config parsing
195  *
196  * @param hook service config parsing hook
197  *   in the hook, we can parse extra fields in the config file.
198  * @return return 0 if succeed; other values if failed.
199  */
200 int InitAddServiceParseHook(ServiceParseHook hook);
201 
202 /**
203  * @brief service config parsing context information
204  */
205 typedef struct tagReboot {
206     char *reason;
207 } RebootHookCtx;
208 
209 /**
210  * @brief service config parse hook function prototype
211  *
212  * @param serviceParseCtx service config parsing context information
213  * @return None
214  */
215 typedef void (*InitRebootHook)(RebootHookCtx *ctx);
216 /**
217  * @brief Register a hook for reboot
218  *
219  * @param hook
220  *
221  * @return return 0 if succeed; other values if failed.
222  */
223 int InitAddRebootHook(InitRebootHook hook);
224 
225 /**
226  * @brief Register a hook for job config parsing
227  *
228  * @param hook job config parsing hook
229  *   in the hook, we can parse extra fields in the config file.
230  * @return return 0 if succeed; other values if failed.
231  */
232 int InitAddJobParseHook(JobParseHook hook);
233 
234 /**
235  * @brief Register a hook for service
236  *
237  * @param hook service hook
238  *   in the hook, we can get service.
239  * @param hookState init boot state
240  * @return return 0 if succeed; other values if failed.
241  */
242 int InitAddServiceHook(ServiceHook hook, int hookState);
243 
244 /**
245  * @brief Register a hook for service restart
246  *
247  * @param hook service restart hook
248  *  in the hook, we can get service.
249  * @return return 0 if succeed; other values if failed.
250  */
251 int InitServiceRestartHook(ServiceRestartHook hook, int hookState);
252 
253 #ifdef __cplusplus
254 #if __cplusplus
255 }
256 #endif
257 #endif
258 #endif
259