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