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