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