init_config.c 4.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "init.h"
Z
zhong_ning 已提交
16
#include "init_jobs_internal.h"
17 18 19
#include "init_log.h"
#include "init_service_manager.h"
#include "init_utils.h"
M
Mupceet 已提交
20
#include "init_param.h"
21 22 23

static void ParseAllImports(const cJSON *root);

C
cheng_jinsong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
InitContextType GetConfigContextType(const char *cfgName)
{
    static const char *vendorDir[] = {
        "/vendor/etc/init/", "/chipset/etc/init/", "/chip_prod/etc/init/"
    };

    for (size_t j = 0; j < ARRAY_LENGTH(vendorDir); j++) {
        if (strncmp(vendorDir[j], cfgName, strlen(vendorDir[j])) == 0) {
            return INIT_CONTEXT_CHIPSET;
        }
    }
    return INIT_CONTEXT_MAIN;
}

38 39 40
static void ParseInitCfgContents(const char *cfgName, const cJSON *root)
{
    INIT_ERROR_CHECK(root != NULL, return, "Root is null");
C
cheng_jinsong 已提交
41 42 43 44
    ConfigContext context = { INIT_CONTEXT_MAIN };
    context.type = GetConfigContextType(cfgName);
    INIT_LOGV("Parse %s configs in context %d", cfgName, context.type);
    ParseAllServices(root, &context);
45
    // parse jobs
C
cheng_jinsong 已提交
46
    ParseAllJobs(root, &context);
47 48 49 50
    // parse imports
    ParseAllImports(root);
}

X
xionglei6 已提交
51
int ParseInitCfg(const char *configFile, void *context)
52 53
{
    UNUSED(context);
C
fix log  
cheng_jinsong 已提交
54
    INIT_LOGV("Parse init configs from %s", configFile);
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    char *fileBuf = ReadFileToBuf(configFile);
    INIT_ERROR_CHECK(fileBuf != NULL, return -1, "Failed to read file content %s", configFile);

    cJSON *fileRoot = cJSON_Parse(fileBuf);
    INIT_ERROR_CHECK(fileRoot != NULL, free(fileBuf);
        return -1, "Failed to parse json file %s", configFile);

    ParseInitCfgContents(configFile, fileRoot);
    cJSON_Delete(fileRoot);
    free(fileBuf);
    return 0;
}

static void ParseAllImports(const cJSON *root)
{
C
cheng_jinsong 已提交
70 71
    char *tmpParamValue = calloc(PARAM_VALUE_LEN_MAX + 1, sizeof(char));
    INIT_ERROR_CHECK(tmpParamValue != NULL, return, "Failed to alloc memory for param");
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

    cJSON *importAttr = cJSON_GetObjectItemCaseSensitive(root, "import");
    if (!cJSON_IsArray(importAttr)) {
        free(tmpParamValue);
        return;
    }
    int importAttrSize = cJSON_GetArraySize(importAttr);
    for (int i = 0; i < importAttrSize; i++) {
        cJSON *importItem = cJSON_GetArrayItem(importAttr, i);
        if (!cJSON_IsString(importItem)) {
            INIT_LOGE("Invalid type of import item. should be string");
            break;
        }
        char *importContent = cJSON_GetStringValue(importItem);
        if (importContent == NULL) {
            INIT_LOGE("cannot get import config file");
            break;
        }
        int ret = GetParamValue(importContent, strlen(importContent), tmpParamValue, PARAM_VALUE_LEN_MAX);
        if (ret != 0) {
            INIT_LOGE("cannot get value for %s", importContent);
            continue;
        }
        INIT_LOGI("Import %s  ...", tmpParamValue);
        ParseInitCfg(tmpParamValue, NULL);
    }
    free(tmpParamValue);
    return;
}

void ReadConfig(void)
{
    // parse cfg
M
Mupceet 已提交
105 106 107
    char buffer[32] = {0}; // 32 reason max leb
    uint32_t len = sizeof(buffer);
    SystemReadParam("ohos.boot.reboot_reason", buffer, &len);
W
wangxutao 已提交
108
    INIT_LOGI("ohos.boot.reboot_reason %s", buffer);
M
Mupceet 已提交
109
    if (strcmp(buffer, "poweroff_charge") == 0) {
T
toutes 已提交
110 111 112
        ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
        ReadFileInDir(OTHER_CHARGE_PATH, ".cfg", ParseInitCfg, NULL);
    } else if (InUpdaterMode() == 0) {
113
        ParseInitCfg(INIT_CONFIGURATION_FILE, NULL);
C
cheng_jinsong 已提交
114
        ParseInitCfgByPriority();
115 116 117 118
    } else {
        ReadFileInDir("/etc", ".cfg", ParseInitCfg, NULL);
    }
}