1 /*
2 * Copyright (c) 2021 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 #include <ctype.h>
16
17 #include "init_param.h"
18 #include "init_service_manager.h"
19 #include "init_utils.h"
20 #include "param_manager.h"
21 #include "param_message.h"
22 #include "param_utils.h"
23 #include "trigger_checker.h"
24 #include "trigger_manager.h"
25 #include "securec.h"
26 #include "hookmgr.h"
27 #include "bootstage.h"
28
29 #define MAX_TRIGGER_COUNT_RUN_ONCE 20
30 static TriggerWorkSpace g_triggerWorkSpace = {};
31
DoTriggerExecute_(const TriggerNode * trigger,const char * content,uint32_t size)32 static int DoTriggerExecute_(const TriggerNode *trigger, const char *content, uint32_t size)
33 {
34 PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
35 PARAM_LOGV("Do execute trigger %s type: %d", GetTriggerName(trigger), trigger->type);
36 PARAM_CHECK(trigger->type <= TRIGGER_UNKNOW, return -1, "Invalid trigger type %d", trigger->type);
37 CommandNode *cmd = GetNextCmdNode((JobNode *)trigger, NULL);
38 while (cmd != NULL) {
39 #ifndef STARTUP_INIT_TEST
40 DoCmdByIndex(cmd->cmdKeyIndex, cmd->content, &cmd->cfgContext);
41 #endif
42 cmd = GetNextCmdNode((JobNode *)trigger, cmd);
43 }
44 return 0;
45 }
46
DoTriggerCheckResult(TriggerNode * trigger,const char * content,uint32_t size)47 static int DoTriggerCheckResult(TriggerNode *trigger, const char *content, uint32_t size)
48 {
49 UNUSED(content);
50 UNUSED(size);
51 if (TRIGGER_IN_QUEUE(trigger)) {
52 PARAM_LOGI("DoTiggerExecute trigger %s has been waiting execute", GetTriggerName(trigger));
53 return 0;
54 }
55 TRIGGER_SET_FLAG(trigger, TRIGGER_FLAGS_QUEUE);
56 PARAM_LOGV("Add trigger %s to execute queue", GetTriggerName(trigger));
57 ExecuteQueuePush(&g_triggerWorkSpace, trigger);
58 return 0;
59 }
60
ExecuteTriggerImmediately(TriggerNode * trigger,const char * content,uint32_t size)61 static int ExecuteTriggerImmediately(TriggerNode *trigger, const char *content, uint32_t size)
62 {
63 PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
64 PARAM_LOGV("ExecuteTriggerImmediately trigger %s", GetTriggerName(trigger));
65 TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, trigger->type);
66 if (triggerHead != NULL) {
67 triggerHead->executeTrigger(trigger, content, size);
68 TRIGGER_CLEAR_FLAG(trigger, TRIGGER_FLAGS_QUEUE);
69
70 if (TRIGGER_TEST_FLAG(trigger, TRIGGER_FLAGS_ONCE)) {
71 FreeTrigger(&g_triggerWorkSpace, trigger);
72 }
73 }
74 return 0;
75 }
76
StartTriggerExecute_(TriggerNode * trigger,const char * content,uint32_t size)77 static void StartTriggerExecute_(TriggerNode *trigger, const char *content, uint32_t size)
78 {
79 TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, trigger->type);
80 if (triggerHead != NULL) {
81 PARAM_LOGV("StartTriggerExecute_ trigger %s flags:0x%04x",
82 GetTriggerName(trigger), trigger->flags);
83 triggerHead->executeTrigger(trigger, content, size);
84 TRIGGER_CLEAR_FLAG(trigger, TRIGGER_FLAGS_QUEUE);
85 if (TRIGGER_TEST_FLAG(trigger, TRIGGER_FLAGS_SUBTRIGGER)) { // boot && xxx=xxx trigger
86 const char *condition = triggerHead->getCondition(trigger);
87 CheckTrigger(&g_triggerWorkSpace, TRIGGER_UNKNOW, condition, strlen(condition), ExecuteTriggerImmediately);
88 }
89 if (TRIGGER_TEST_FLAG(trigger, TRIGGER_FLAGS_ONCE)) {
90 FreeTrigger(&g_triggerWorkSpace, trigger);
91 }
92 }
93 }
94
ExecuteQueueWork(uint32_t maxCount,void (* bootStateChange)(int start,const char *))95 static void ExecuteQueueWork(uint32_t maxCount, void (*bootStateChange)(int start, const char *))
96 {
97 uint32_t executeCount = 0;
98 TriggerNode *trigger = ExecuteQueuePop(&g_triggerWorkSpace);
99 while (trigger != NULL) {
100 if (bootStateChange != NULL) {
101 bootStateChange(0, (const char *)GetTriggerName(trigger));
102 }
103 StartTriggerExecute_(trigger, NULL, 0);
104 if (bootStateChange != NULL) {
105 bootStateChange(1, (const char *)GetTriggerName(trigger));
106 }
107 executeCount++;
108 if (executeCount > maxCount) {
109 break;
110 }
111 trigger = ExecuteQueuePop(&g_triggerWorkSpace);
112 }
113 }
114
ProcessBeforeEvent(const ParamTaskPtr stream,uint64_t eventId,const uint8_t * content,uint32_t size)115 PARAM_STATIC void ProcessBeforeEvent(const ParamTaskPtr stream,
116 uint64_t eventId, const uint8_t *content, uint32_t size)
117 {
118 PARAM_LOGV("ProcessBeforeEvent %s ", (char *)content);
119 switch (eventId) {
120 case EVENT_TRIGGER_PARAM: {
121 CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM,
122 (const char *)content, size, DoTriggerCheckResult);
123 ExecuteQueueWork(MAX_TRIGGER_COUNT_RUN_ONCE, NULL);
124 break;
125 }
126 case EVENT_TRIGGER_BOOT: {
127 if (g_triggerWorkSpace.bootStateChange != NULL) {
128 g_triggerWorkSpace.bootStateChange(0, (const char *)content);
129 }
130 CheckTrigger(&g_triggerWorkSpace, TRIGGER_BOOT,
131 (const char *)content, size, DoTriggerCheckResult);
132 ExecuteQueueWork(1, NULL);
133 if (g_triggerWorkSpace.bootStateChange != NULL) {
134 g_triggerWorkSpace.bootStateChange(1, (const char *)content);
135 }
136 ExecuteQueueWork(MAX_TRIGGER_COUNT_RUN_ONCE, g_triggerWorkSpace.bootStateChange);
137 break;
138 }
139 case EVENT_TRIGGER_PARAM_WAIT: {
140 CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM_WAIT,
141 (const char *)content, size, ExecuteTriggerImmediately);
142 break;
143 }
144 case EVENT_TRIGGER_PARAM_WATCH: {
145 CheckTrigger(&g_triggerWorkSpace, TRIGGER_PARAM_WATCH,
146 (const char *)content, size, ExecuteTriggerImmediately);
147 break;
148 }
149 default:
150 break;
151 }
152 }
153
SendTriggerEvent(int type,const char * content,uint32_t contentLen)154 static void SendTriggerEvent(int type, const char *content, uint32_t contentLen)
155 {
156 PARAM_CHECK(content != NULL, return, "Invalid param");
157 PARAM_LOGV("SendTriggerEvent type %d content %s", type, content);
158 ParamEventSend(g_triggerWorkSpace.eventHandle, (uint64_t)type, content, contentLen);
159 }
160
PostParamTrigger(int type,const char * name,const char * value)161 void PostParamTrigger(int type, const char *name, const char *value)
162 {
163 PARAM_CHECK(name != NULL && value != NULL, return, "Invalid param");
164 uint32_t bufferSize = strlen(name) + strlen(value) + 1 + 1 + 1;
165 PARAM_CHECK(bufferSize < (PARAM_CONST_VALUE_LEN_MAX + PARAM_NAME_LEN_MAX + 1 + 1 + 1),
166 return, "bufferSize is longest %d", bufferSize);
167 char *buffer = (char *)malloc(bufferSize);
168 PARAM_CHECK(buffer != NULL, return, "Failed to alloc memory for param %s", name);
169 int ret = sprintf_s(buffer, bufferSize - 1, "%s=%s", name, value);
170 PARAM_CHECK(ret > EOK, free(buffer);
171 return, "Failed to copy param");
172 SendTriggerEvent(type, buffer, strlen(buffer));
173 free(buffer);
174 }
175
PostTrigger(EventType type,const char * content,uint32_t contentLen)176 void PostTrigger(EventType type, const char *content, uint32_t contentLen)
177 {
178 PARAM_CHECK(content != NULL && contentLen > 0, return, "Invalid param");
179 SendTriggerEvent(type, content, contentLen);
180 }
181
GetTriggerType(const char * type)182 static int GetTriggerType(const char *type)
183 {
184 if (strncmp("param:", type, strlen("param:")) == 0) {
185 return TRIGGER_PARAM;
186 }
187 if (strncmp("boot-service:", type, strlen("boot-service:")) == 0) {
188 return TRIGGER_BOOT;
189 }
190 const char *triggerTypeStr[] = {
191 "pre-init", "boot", "early-init", "init", "early-init", "late-init", "post-init",
192 "fs", "early-fs", "post-fs", "late-fs", "early-boot", "post-fs-data", "reboot", "suspend"
193 };
194 for (size_t i = 0; i < ARRAY_LENGTH(triggerTypeStr); i++) {
195 if (strcmp(triggerTypeStr[i], type) == 0) {
196 return TRIGGER_BOOT;
197 }
198 }
199 return TRIGGER_UNKNOW;
200 }
201
GetCommandInfo(const char * cmdLine,int * cmdKeyIndex,char ** content)202 static int GetCommandInfo(const char *cmdLine, int *cmdKeyIndex, char **content)
203 {
204 const char *matchCmd = GetMatchCmd(cmdLine, cmdKeyIndex);
205 PARAM_CHECK(matchCmd != NULL, return -1, "Command not support %s", cmdLine);
206 char *str = strstr(cmdLine, matchCmd);
207 if (str != NULL) {
208 str += strlen(matchCmd);
209 }
210 while (str != NULL && isspace(*str)) {
211 str++;
212 }
213 *content = str;
214 return 0;
215 }
216
ParseJobHookExecute(const char * name,const cJSON * jobNode)217 static void ParseJobHookExecute(const char *name, const cJSON *jobNode)
218 {
219 JOB_PARSE_CTX context;
220
221 context.jobName = name;
222 context.jobNode = jobNode;
223
224 (void)HookMgrExecute(GetBootStageHookMgr(), INIT_JOB_PARSE, (void *)(&context), NULL);
225 }
226
ParseTrigger_(const TriggerWorkSpace * workSpace,const cJSON * triggerItem,int (* checkJobValid)(const char * jobName),const ConfigContext * cfgContext)227 static int ParseTrigger_(const TriggerWorkSpace *workSpace,
228 const cJSON *triggerItem, int (*checkJobValid)(const char *jobName), const ConfigContext *cfgContext)
229 {
230 PARAM_CHECK(triggerItem != NULL, return -1, "Invalid file");
231 PARAM_CHECK(workSpace != NULL, return -1, "Failed to create trigger list");
232 char *name = cJSON_GetStringValue(cJSON_GetObjectItem(triggerItem, "name"));
233 PARAM_CHECK(name != NULL, return -1, "Can not get name from cfg");
234 char *condition = cJSON_GetStringValue(cJSON_GetObjectItem(triggerItem, "condition"));
235 int type = GetTriggerType(name);
236 PARAM_CHECK(type <= TRIGGER_UNKNOW, return -1, "Failed to get trigger index");
237 if (type != TRIGGER_BOOT && checkJobValid != NULL && checkJobValid(name) != 0) {
238 PARAM_LOGI("Trigger %s not exist in group", name);
239 return 0;
240 }
241
242 TriggerHeader *header = GetTriggerHeader(workSpace, type);
243 PARAM_CHECK(header != NULL, return -1, "Failed to get header %d", type);
244 JobNode *trigger = UpdateJobTrigger(workSpace, type, condition, name);
245 PARAM_CHECK(trigger != NULL, return -1, "Failed to create trigger %s", name);
246 PARAM_LOGV("ParseTrigger %s type %d count %d", name, type, header->triggerCount);
247 cJSON *cmdItems = cJSON_GetObjectItem(triggerItem, CMDS_ARR_NAME_IN_JSON);
248 if (cmdItems == NULL || !cJSON_IsArray(cmdItems)) {
249 return 0;
250 }
251 int cmdLinesCnt = cJSON_GetArraySize(cmdItems);
252 PARAM_CHECK(cmdLinesCnt > 0, return -1, "Command array size must positive %s", name);
253
254 int ret;
255 int cmdKeyIndex = 0;
256 for (int i = 0; (i < cmdLinesCnt) && (i < TRIGGER_MAX_CMD); ++i) {
257 char *cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdItems, i));
258 PARAM_CHECK(cmdLineStr != NULL, continue, "Command is null");
259
260 char *content = NULL;
261 ret = GetCommandInfo(cmdLineStr, &cmdKeyIndex, &content);
262 PARAM_CHECK(ret == 0, continue, "Command not support %s", cmdLineStr);
263 ret = AddCommand(trigger, (uint32_t)cmdKeyIndex, content, cfgContext);
264 PARAM_CHECK(ret == 0, continue, "Failed to add command %s", cmdLineStr);
265 header->cmdNodeCount++;
266 }
267 return 0;
268 }
269
ParseTriggerConfig(const cJSON * fileRoot,int (* checkJobValid)(const char * jobName),void * context)270 int ParseTriggerConfig(const cJSON *fileRoot, int (*checkJobValid)(const char *jobName), void *context)
271 {
272 PARAM_CHECK(fileRoot != NULL, return -1, "Invalid file");
273 ConfigContext *cfgContext = (ConfigContext *)context;
274 cJSON *triggers = cJSON_GetObjectItemCaseSensitive(fileRoot, TRIGGER_ARR_NAME_IN_JSON);
275 if (triggers == NULL || !cJSON_IsArray(triggers)) {
276 return 0;
277 }
278 int size = cJSON_GetArraySize(triggers);
279 PARAM_CHECK(size > 0, return -1, "Trigger array size must positive");
280
281 for (int i = 0; i < size && i < TRIGGER_MAX_CMD; ++i) {
282 cJSON *item = cJSON_GetArrayItem(triggers, i);
283 ParseTrigger_(&g_triggerWorkSpace, item, checkJobValid, cfgContext);
284 /*
285 * execute job parsing hooks
286 */
287 ParseJobHookExecute(cJSON_GetStringValue(cJSON_GetObjectItem(item, "name")), item);
288 }
289 return 0;
290 }
291
CheckAndMarkTrigger(int type,const char * name)292 int CheckAndMarkTrigger(int type, const char *name)
293 {
294 TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, type);
295 if (triggerHead) {
296 return triggerHead->checkAndMarkTrigger(&g_triggerWorkSpace, type, name);
297 }
298 return 0;
299 }
300
InitTriggerWorkSpace(void)301 int InitTriggerWorkSpace(void)
302 {
303 if (g_triggerWorkSpace.eventHandle != NULL) {
304 return 0;
305 }
306 g_triggerWorkSpace.bootStateChange = NULL;
307 ParamEventTaskCreate(&g_triggerWorkSpace.eventHandle, ProcessBeforeEvent);
308 PARAM_CHECK(g_triggerWorkSpace.eventHandle != NULL, return -1, "Failed to event handle");
309
310 // executeQueue
311 g_triggerWorkSpace.executeQueue.executeQueue = calloc(1, TRIGGER_EXECUTE_QUEUE * sizeof(TriggerNode *));
312 PARAM_CHECK(g_triggerWorkSpace.executeQueue.executeQueue != NULL,
313 return -1, "Failed to alloc memory for executeQueue");
314 g_triggerWorkSpace.executeQueue.queueCount = TRIGGER_EXECUTE_QUEUE;
315 g_triggerWorkSpace.executeQueue.startIndex = 0;
316 g_triggerWorkSpace.executeQueue.endIndex = 0;
317 InitTriggerHead(&g_triggerWorkSpace);
318 RegisterTriggerExec(TRIGGER_BOOT, DoTriggerExecute_);
319 RegisterTriggerExec(TRIGGER_PARAM, DoTriggerExecute_);
320 RegisterTriggerExec(TRIGGER_UNKNOW, DoTriggerExecute_);
321 PARAM_LOGV("InitTriggerWorkSpace success");
322 return 0;
323 }
324
CloseTriggerWorkSpace(void)325 void CloseTriggerWorkSpace(void)
326 {
327 for (size_t i = 0; i < sizeof(g_triggerWorkSpace.triggerHead) / sizeof(g_triggerWorkSpace.triggerHead[0]); i++) {
328 ClearTrigger(&g_triggerWorkSpace, i);
329 }
330 OH_HashMapDestory(g_triggerWorkSpace.hashMap, NULL);
331 g_triggerWorkSpace.hashMap = NULL;
332 free(g_triggerWorkSpace.executeQueue.executeQueue);
333 g_triggerWorkSpace.executeQueue.executeQueue = NULL;
334 ParamTaskClose(g_triggerWorkSpace.eventHandle);
335 g_triggerWorkSpace.eventHandle = NULL;
336 }
337
GetTriggerWorkSpace(void)338 TriggerWorkSpace *GetTriggerWorkSpace(void)
339 {
340 return &g_triggerWorkSpace;
341 }
342
RegisterTriggerExec(int type,int32_t (* executeTrigger)(const struct tagTriggerNode_ *,const char *,uint32_t))343 void RegisterTriggerExec(int type,
344 int32_t (*executeTrigger)(const struct tagTriggerNode_ *, const char *, uint32_t))
345 {
346 TriggerHeader *triggerHead = GetTriggerHeader(&g_triggerWorkSpace, type);
347 if (triggerHead != NULL) {
348 triggerHead->executeTrigger = executeTrigger;
349 }
350 }
351
DoTriggerExec(const char * triggerName)352 void DoTriggerExec(const char *triggerName)
353 {
354 PARAM_CHECK(triggerName != NULL, return, "Invalid param");
355 JobNode *trigger = GetTriggerByName(&g_triggerWorkSpace, triggerName);
356 if (trigger != NULL && !TRIGGER_IN_QUEUE((TriggerNode *)trigger)) {
357 PARAM_LOGV("Trigger job %s", trigger->name);
358 TRIGGER_SET_FLAG((TriggerNode *)trigger, TRIGGER_FLAGS_QUEUE);
359 ExecuteQueuePush(&g_triggerWorkSpace, (TriggerNode *)trigger);
360 } else {
361 PARAM_LOGW("Can not find trigger %s", triggerName);
362 }
363 }
364
DoJobExecNow(const char * triggerName)365 void DoJobExecNow(const char *triggerName)
366 {
367 PARAM_CHECK(triggerName != NULL, return, "Invalid param");
368 JobNode *trigger = GetTriggerByName(&g_triggerWorkSpace, triggerName);
369 if (trigger != NULL) {
370 StartTriggerExecute_((TriggerNode *)trigger, NULL, 0);
371 }
372 }
373
AddCompleteJob(const char * name,const char * condition,const char * cmdContent)374 int AddCompleteJob(const char *name, const char *condition, const char *cmdContent)
375 {
376 PARAM_CHECK(name != NULL, return -1, "Invalid name");
377 PARAM_CHECK(cmdContent != NULL, return -1, "Invalid cmdContent");
378 int type = GetTriggerType(name);
379 PARAM_CHECK(type <= TRIGGER_UNKNOW, return -1, "Failed to get trigger index");
380 TriggerHeader *header = GetTriggerHeader(&g_triggerWorkSpace, type);
381 PARAM_CHECK(header != NULL, return -1, "Failed to get header %d", type);
382
383 JobNode *trigger = UpdateJobTrigger(&g_triggerWorkSpace, type, condition, name);
384 PARAM_CHECK(trigger != NULL, return -1, "Failed to create trigger");
385 char *content = NULL;
386 int cmdKeyIndex = 0;
387 int ret = GetCommandInfo(cmdContent, &cmdKeyIndex, &content);
388 PARAM_CHECK(ret == 0, return -1, "Command not support %s", cmdContent);
389 ret = AddCommand(trigger, (uint32_t)cmdKeyIndex, content, NULL); // use default context
390 PARAM_CHECK(ret == 0, return -1, "Failed to add command %s", cmdContent);
391 header->cmdNodeCount++;
392 PARAM_LOGV("AddCompleteJob %s type %d count %d", name, type, header->triggerCount);
393 return 0;
394 }
395
RegisterBootStateChange(void (* bootStateChange)(int,const char *))396 void RegisterBootStateChange(void (*bootStateChange)(int, const char *))
397 {
398 if (bootStateChange != NULL) {
399 g_triggerWorkSpace.bootStateChange = bootStateChange;
400 }
401 }
402