init_read_cfg.c 4.0 KB
Newer Older
W
wenjun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * 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.
 */
15

W
wenjun 已提交
16
#include "init_read_cfg.h"
17

W
wenjun 已提交
18
#include <errno.h>
Z
zhong_ning 已提交
19
#include <dirent.h>
W
wenjun 已提交
20 21 22 23 24 25
#include <linux/capability.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
26

Z
zhong_ning 已提交
27
#include "init_import.h"
W
wenjun 已提交
28
#include "init_jobs.h"
Z
zhong_ning 已提交
29
#include "init_log.h"
W
wenjun 已提交
30 31
#include "init_perms.h"
#include "init_service_manager.h"
Z
zhong_ning 已提交
32 33 34
#include "init_utils.h"

#ifndef OHOS_LITE
Z
zhong_ning 已提交
35
#include "init_param.h"
Z
zhong_ning 已提交
36
#endif
W
wenjun 已提交
37
#include "securec.h"
38
#ifndef __LINUX__
L
lanxueyuan 已提交
39
#ifdef OHOS_LITE
40 41
#include "init_stage.h"
#endif
L
lanxueyuan 已提交
42
#endif
M
mamingshuai 已提交
43

Z
zhong_ning 已提交
44 45
#define FILE_NAME_MAX_SIZE 100
static void ParseInitCfgContents(cJSON *root)
W
wenjun 已提交
46
{
S
sun_fan 已提交
47 48 49 50
    if (root == NULL) {
        INIT_LOGE("ParseInitCfgContents root is NULL");
        return;
    }
Z
zhong_ning 已提交
51 52
     // parse services
    ParseAllServices(root);
S
sun_fan 已提交
53
#ifdef OHOS_LITE
Z
zhong_ning 已提交
54 55
    // parse jobs
    ParseAllJobs(root);
S
sun_fan 已提交
56
#else
S
sun_fan 已提交
57
    ParseTriggerConfig(root);
Z
zhong_ning 已提交
58
#endif
M
mamingshuai 已提交
59

Z
zhong_ning 已提交
60 61
    // parse imports
    ParseAllImports(root);
M
mamingshuai 已提交
62 63
}

Z
zhong_ning 已提交
64
void ParseInitCfg(const char *configFile)
M
mamingshuai 已提交
65
{
Z
zhong_ning 已提交
66
    if (configFile == NULL || *configFile == '\0') {
Z
zhong_ning 已提交
67
        INIT_LOGE("Invalid config file");
Z
zhong_ning 已提交
68
        return;
W
wenjun 已提交
69 70
    }

Z
zhong_ning 已提交
71
    char *fileBuf = ReadFileToBuf(configFile);
S
sun_fan 已提交
72
    if (fileBuf == NULL) {
Z
zhong_ning 已提交
73
        INIT_LOGE("Read %s failed", configFile);
S
sun_fan 已提交
74 75
        return;
    }
Z
zhong_ning 已提交
76 77 78
    cJSON* fileRoot = cJSON_Parse(fileBuf);
    free(fileBuf);
    fileBuf = NULL;
W
wenjun 已提交
79

Z
zhong_ning 已提交
80
    if (fileRoot == NULL) {
Z
zhong_ning 已提交
81
        INIT_LOGE("InitReadCfg, parse failed! please check file %s format.", configFile);
Z
zhong_ning 已提交
82
        return;
W
wenjun 已提交
83
    }
Z
zhong_ning 已提交
84 85 86 87
    ParseInitCfgContents(fileRoot);
    // Release JSON object
    cJSON_Delete(fileRoot);
    return;
W
wenjun 已提交
88 89
}

Z
zhong_ning 已提交
90
static void ReadCfgs(const char *dirPath)
W
wenjun 已提交
91
{
Z
zhong_ning 已提交
92 93
    DIR *pDir = opendir(dirPath);
    if (pDir == NULL) {
Z
zhong_ning 已提交
94
        INIT_LOGE("ParseCfgs open cfg dir :%s failed.%d", dirPath, errno);
Z
zhong_ning 已提交
95
        return;
W
wenjun 已提交
96
    }
Z
zhong_ning 已提交
97 98 99 100
    struct dirent *dp;
    while ((dp = readdir(pDir)) != NULL) {
        char fileName[FILE_NAME_MAX_SIZE];
        if (snprintf_s(fileName, FILE_NAME_MAX_SIZE, FILE_NAME_MAX_SIZE - 1, "%s/%s", dirPath, dp->d_name) == -1) {
Z
zhong_ning 已提交
101
            INIT_LOGE("ParseCfgs snprintf_s failed.");
Z
zhong_ning 已提交
102 103 104 105 106 107 108 109
            closedir(pDir);
            return;
        }
        struct stat st;
        if (stat(fileName, &st) == 0) {
            if (strstr(dp->d_name, ".cfg") == NULL) {
                continue;
            }
Z
zhong_ning 已提交
110
            INIT_LOGI("ReadCfgs :%s from %s success.", fileName, dirPath);
Z
zhong_ning 已提交
111
            ParseInitCfg(fileName);
W
wenjun 已提交
112 113
        }
    }
Z
zhong_ning 已提交
114 115
    closedir(pDir);
    return;
W
wenjun 已提交
116 117
}

Z
zhong_ning 已提交
118
static void ParseOtherCfgs()
X
xionglei6 已提交
119
{
Z
zhong_ning 已提交
120 121
    ReadCfgs("/system/etc/init");
    return;
W
wenjun 已提交
122 123 124 125
}

void InitReadCfg()
{
Z
zhong_ning 已提交
126 127
#ifndef OHOS_LITE
    InitParamService();
Z
zhong_ning 已提交
128
    LoadDefaultParams("/system/etc/ohos.para");
Z
zhong_ning 已提交
129
#endif
Z
zhong_ning 已提交
130 131
    ParseInitCfg(INIT_CONFIGURATION_FILE);
    ParseOtherCfgs();
Z
zhong_ning 已提交
132
    INIT_LOGI("Parse init config file done.");
S
sun_fan 已提交
133
#ifdef OHOS_SERVICE_DUMP
Z
zhong_ning 已提交
134
    DumpAllServices();
S
sun_fan 已提交
135 136
#endif

S
sun_fan 已提交
137
#ifdef OHOS_LITE
W
wenjun 已提交
138 139
    // do jobs
    DoJob("pre-init");
140 141 142 143
#ifndef __LINUX__
    TriggerStage(EVENT1, EVENT1_WAITTIME, QS_STAGE1);
#endif

W
wenjun 已提交
144
    DoJob("init");
145 146 147 148
#ifndef __LINUX__
    TriggerStage(EVENT2, EVENT2_WAITTIME, QS_STAGE2);
#endif

W
wenjun 已提交
149
    DoJob("post-init");
150 151 152 153 154
#ifndef __LINUX__
    TriggerStage(EVENT3, EVENT3_WAITTIME, QS_STAGE3);

    InitStageFinished();
#endif
W
wenjun 已提交
155
    ReleaseAllJobs();
S
sun_fan 已提交
156 157 158 159 160 161 162
#else
    PostTrigger(EVENT_BOOT, "pre-init", strlen("pre-init"));

    PostTrigger(EVENT_BOOT, "init", strlen("init"));

    PostTrigger(EVENT_BOOT, "post-init", strlen("post-init"));
#endif
W
wenjun 已提交
163
}
M
mamingshuai 已提交
164