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_SNAPSHOT_ACTIVE = 6,
35 INIT_DISABLE_CHECKPOINT = 7,
36 INIT_HEALTH_CHECK_ACTIVE = 8,
37 INIT_PRE_PARAM_SERVICE = 10,
38 INIT_VAB_HVBCHECK = 11,
39 INIT_PRE_PARAM_LOAD = 20,
40 INIT_PARAM_LOAD_FILTER = 25,
41 INIT_PRE_CFG_LOAD = 30,
42 INIT_SERVICE_PARSE = 35,
43 INIT_POST_PERSIST_PARAM_LOAD = 40,
44 INIT_POST_CFG_LOAD = 50,
45 INIT_CMD_RECORD = 51,
46 INIT_REBOOT = 55,
47 INIT_SERVICE_CLEAR = 56,
48 INIT_SERVICE_DUMP = 57,
49 INIT_SERVICE_FORK_BEFORE = 58,
50 INIT_SERVICE_SET_PERMS_BEFORE = 59,
51 INIT_SERVICE_SET_PERMS = 60,
52 INIT_SERVICE_FORK_AFTER = 61,
53 INIT_SERVICE_BOOTEVENT = 62,
54 INIT_SERVICE_REAP = 65,
55 INIT_SHUT_DETECTOR = 66,
56 INIT_ALL_SERVICES_REAP = 67,
57 INIT_SERVICE_RESTART = 71,
58 INIT_JOB_PARSE = 70,
59 INIT_BOOT_COMPLETE = 100,
60 };
61
62 HOOK_MGR *GetBootStageHookMgr(void);
63
InitAddGlobalInitHook(int prio,OhosHook hook)64 __attribute__((always_inline)) inline int InitAddGlobalInitHook(int prio, OhosHook hook)
65 {
66 return HookMgrAdd(GetBootStageHookMgr(), INIT_GLOBAL_INIT, prio, hook);
67 }
68
InitAddPreParamServiceHook(int prio,OhosHook hook)69 __attribute__((always_inline)) inline int InitAddPreParamServiceHook(int prio, OhosHook hook)
70 {
71 return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_SERVICE, prio, hook);
72 }
73
InitAddPreParamLoadHook(int prio,OhosHook hook)74 __attribute__((always_inline)) inline int InitAddPreParamLoadHook(int prio, OhosHook hook)
75 {
76 return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_PARAM_LOAD, prio, hook);
77 }
78
79 /**
80 * @brief Parameter load filter context
81 */
82 typedef struct tagPARAM_LOAD_FILTER_CTX {
83 const char *name; /* Parameter name */
84 const char *value; /* Parameter value */
85 int ignored; /* Ignore this parameter or not */
86 } PARAM_LOAD_FILTER_CTX;
87
88 /**
89 * @brief Parameter Load Hook function prototype
90 *
91 * @param hookInfo hook information
92 * @param filter filter information context
93 * @return return 0 if succeed; other values if failed.
94 */
95 typedef int (*ParamLoadFilter)(const HOOK_INFO *hookInfo, PARAM_LOAD_FILTER_CTX *filter);
96
InitAddParamLoadFilterHook(int prio,ParamLoadFilter filter)97 __attribute__((always_inline)) inline int InitAddParamLoadFilterHook(int prio, ParamLoadFilter filter)
98 {
99 return HookMgrAdd(GetBootStageHookMgr(), INIT_PARAM_LOAD_FILTER, prio, (OhosHook)filter);
100 }
101
InitAddPreCfgLoadHook(int prio,OhosHook hook)102 __attribute__((always_inline)) inline int InitAddPreCfgLoadHook(int prio, OhosHook hook)
103 {
104 return HookMgrAdd(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, prio, hook);
105 }
106
InitAddPostCfgLoadHook(int prio,OhosHook hook)107 __attribute__((always_inline)) inline int InitAddPostCfgLoadHook(int prio, OhosHook hook)
108 {
109 return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_CFG_LOAD, prio, hook);
110 }
111
InitAddPostPersistParamLoadHook(int prio,OhosHook hook)112 __attribute__((always_inline)) inline int InitAddPostPersistParamLoadHook(int prio, OhosHook hook)
113 {
114 return HookMgrAdd(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, prio, hook);
115 }
116
InitAddBootCompleteHook(int prio,OhosHook hook)117 __attribute__((always_inline)) inline int InitAddBootCompleteHook(int prio, OhosHook hook)
118 {
119 return HookMgrAdd(GetBootStageHookMgr(), INIT_BOOT_COMPLETE, prio, hook);
120 }
121
122 /**
123 * @brief service config parsing context information
124 */
125 typedef struct tagSERVICE_PARSE_CTX {
126 const char *serviceName; /* Service name */
127 const cJSON *serviceNode; /* Service JSON node */
128 } SERVICE_PARSE_CTX;
129
130 /**
131 * @brief job config parsing context information
132 */
133 typedef struct tagJOB_PARSE_CTX {
134 const char *jobName; /* job name */
135 const cJSON *jobNode; /* job JSON node */
136 } JOB_PARSE_CTX;
137
138 /**
139 * @brief service info
140 */
141 typedef struct tagSERVICE_INFO_CTX {
142 const char *serviceName; /* Service name */
143 const char *reserved; /* reserved info */
144 } SERVICE_INFO_CTX;
145
146 /**
147 * @brief service info
148 */
149 typedef struct tagSERVICE_BOOTEVENT_CTX {
150 const char *serviceName; /* Service name */
151 const char *reserved; /* reserved info */
152 int state; /* bootevent state */
153 } SERVICE_BOOTEVENT_CTX;
154
155 /**
156 * @brief service restart info
157 */
158 typedef struct tagSERVICE_RESTART_CTX {
159 const char *serviceName; /* Service name */
160 const char *serviceNode; /* Service node */
161 } SERVICE_RESTART_CTX;
162
163 /**
164 * @brief init cmd info
165 */
166 typedef struct InitCmdInfo {
167 const char *cmdName; /* cmd name */
168 const char *cmdContent; /* cmd content */
169 const char *reserved; /* reserved info */
170 } INIT_CMD_INFO;
171
172 /**
173 * @brief service config parse hook function prototype
174 *
175 * @param serviceParseCtx service config parsing context information
176 * @return None
177 */
178 typedef void (*ServiceParseHook)(SERVICE_PARSE_CTX *serviceParseCtx);
179
180 /**
181 * @brief job config parse hook function prototype
182 *
183 * @param JobParseHook job config parsing context information
184 * @return None
185 */
186 typedef void (*JobParseHook)(JOB_PARSE_CTX *jobParseCtx);
187
188 /**
189 * @brief service hook function prototype
190 *
191 * @param ServiceHook service info
192 * @return None
193 */
194 typedef void (*ServiceHook)(SERVICE_INFO_CTX *serviceCtx);
195
196 /**
197 * @brief service restart hook function prototype
198 *
199 * @param ServiceRestartHook service info
200 * @return None
201 */
202 typedef void (*ServiceRestartHook) (SERVICE_RESTART_CTX *serviceRestartCtx);
203
204 /**
205 * @brief Register a hook for service config parsing
206 *
207 * @param hook service config parsing hook
208 * in the hook, we can parse extra fields in the config file.
209 * @return return 0 if succeed; other values if failed.
210 */
211 int InitAddServiceParseHook(ServiceParseHook hook);
212
213 /**
214 * @brief service config parsing context information
215 */
216 typedef struct tagReboot {
217 char *reason;
218 } RebootHookCtx;
219
220 /**
221 * @brief service config parse hook function prototype
222 *
223 * @param serviceParseCtx service config parsing context information
224 * @return None
225 */
226 typedef void (*InitRebootHook)(RebootHookCtx *ctx);
227 /**
228 * @brief Register a hook for reboot
229 *
230 * @param hook
231 *
232 * @return return 0 if succeed; other values if failed.
233 */
234 int InitAddRebootHook(InitRebootHook hook);
235
236 /**
237 * @brief Register a hook for job config parsing
238 *
239 * @param hook job config parsing hook
240 * in the hook, we can parse extra fields in the config file.
241 * @return return 0 if succeed; other values if failed.
242 */
243 int InitAddJobParseHook(JobParseHook hook);
244
245 /**
246 * @brief Register a hook for service
247 *
248 * @param hook service hook
249 * in the hook, we can get service.
250 * @param hookState init boot state
251 * @return return 0 if succeed; other values if failed.
252 */
253 int InitAddServiceHook(ServiceHook hook, int hookState);
254
255 /**
256 * @brief Register a hook for service restart
257 *
258 * @param hook service restart hook
259 * in the hook, we can get service.
260 * @return return 0 if succeed; other values if failed.
261 */
262 int InitServiceRestartHook(ServiceRestartHook hook, int hookState);
263
264 #ifdef __cplusplus
265 #if __cplusplus
266 }
267 #endif
268 #endif
269 #endif
270