1 /*
2 * Copyright (c) 2020-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 "init.h"
16 #include "init_jobs_internal.h"
17 #include "init_log.h"
18 #include "init_service_manager.h"
19 #include "init_utils.h"
20
21 static void ParseAllImports(const cJSON *root);
22
ParseInitCfgContents(const char * cfgName,const cJSON * root)23 static void ParseInitCfgContents(const char *cfgName, const cJSON *root)
24 {
25 INIT_ERROR_CHECK(root != NULL, return, "Root is null");
26 ParseAllServices(root);
27 // parse jobs
28 ParseAllJobs(root);
29 // parse imports
30 ParseAllImports(root);
31 }
32
ParseInitCfg(const char * configFile,void * context)33 int ParseInitCfg(const char *configFile, void *context)
34 {
35 UNUSED(context);
36 INIT_LOGI("ParseInitCfg %s", configFile);
37 static const char *excludeCfg[] = {
38 "/system/etc/init/weston.cfg"
39 };
40 for (int i = 0; i < (int)ARRAY_LENGTH(excludeCfg); i++) {
41 if (strcmp(configFile, excludeCfg[i]) == 0) {
42 INIT_LOGE("ParseInitCfg %s not support", configFile);
43 return 0;
44 }
45 }
46 char *fileBuf = ReadFileToBuf(configFile);
47 INIT_ERROR_CHECK(fileBuf != NULL, return -1, "Failed to read file content %s", configFile);
48
49 cJSON *fileRoot = cJSON_Parse(fileBuf);
50 INIT_ERROR_CHECK(fileRoot != NULL, free(fileBuf);
51 return -1, "Failed to parse json file %s", configFile);
52
53 ParseInitCfgContents(configFile, fileRoot);
54 cJSON_Delete(fileRoot);
55 free(fileBuf);
56 return 0;
57 }
58
ParseAllImports(const cJSON * root)59 static void ParseAllImports(const cJSON *root)
60 {
61 char *tmpParamValue = calloc(sizeof(char), PARAM_VALUE_LEN_MAX + 1);
62 INIT_ERROR_CHECK(tmpParamValue != 0, return, "Failed to alloc memory for param");
63
64 cJSON *importAttr = cJSON_GetObjectItemCaseSensitive(root, "import");
65 if (!cJSON_IsArray(importAttr)) {
66 free(tmpParamValue);
67 return;
68 }
69 int importAttrSize = cJSON_GetArraySize(importAttr);
70 for (int i = 0; i < importAttrSize; i++) {
71 cJSON *importItem = cJSON_GetArrayItem(importAttr, i);
72 if (!cJSON_IsString(importItem)) {
73 INIT_LOGE("Invalid type of import item. should be string");
74 break;
75 }
76 char *importContent = cJSON_GetStringValue(importItem);
77 if (importContent == NULL) {
78 INIT_LOGE("cannot get import config file");
79 break;
80 }
81 int ret = GetParamValue(importContent, strlen(importContent), tmpParamValue, PARAM_VALUE_LEN_MAX);
82 if (ret != 0) {
83 INIT_LOGE("cannot get value for %s", importContent);
84 continue;
85 }
86 INIT_LOGI("Import %s ...", tmpParamValue);
87 ParseInitCfg(tmpParamValue, NULL);
88 }
89 free(tmpParamValue);
90 return;
91 }
92
ReadConfig(void)93 void ReadConfig(void)
94 {
95 // parse cfg
96 if (InUpdaterMode() == 0) {
97 ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
98 ReadFileInDir(OTHER_CFG_PATH, ".cfg", ParseInitCfg, NULL);
99 ReadFileInDir("/vendor/etc/init", ".cfg", ParseInitCfg, NULL);
100 } else {
101 ReadFileInDir("/etc", ".cfg", ParseInitCfg, NULL);
102 }
103 }
104