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