init_jobs.c 5.7 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_jobs.h"
17

W
wenjun 已提交
18 19
#include <stdio.h>
#include <string.h>
Z
zhong_ning 已提交
20
#include <unistd.h>
21

W
wenjun 已提交
22
#include "init_cmds.h"
Z
zhong_ning 已提交
23
#include "init_log.h"
W
wenjun 已提交
24 25
#include "securec.h"

M
mamingshuai 已提交
26

W
wenjun 已提交
27 28
#define JOBS_ARR_NAME_IN_JSON "jobs"
#define CMDS_ARR_NAME_IN_JSON "cmds"
Z
zhong_ning 已提交
29
#define MAX_JOBS_COUNT        100
W
wenjun 已提交
30

Z
zhong_ning 已提交
31 32 33 34 35
// static const char* g_supportedJobs[] = {
//     "pre-init",
//     "init",
//     "post-init",
// };
W
wenjun 已提交
36 37 38 39

static Job* g_jobs = NULL;
static int g_jobCnt = 0;

Z
zhong_ning 已提交
40 41
void DumpAllJobs()
{
Z
zhong_ning 已提交
42
    INIT_LOGD("Ready to dump all jobs:\n");
Z
zhong_ning 已提交
43
    for (int i = 0; i < g_jobCnt; i++) {
Z
zhong_ning 已提交
44 45
        INIT_LOGD("\tjob name: %s\n", g_jobs[i].name);
        INIT_LOGD("\tlist all commands:\n");
Z
zhong_ning 已提交
46
        for (int j = 0; j < g_jobs[i].cmdLinesCnt; j++) {
Z
zhong_ning 已提交
47
            INIT_LOGD("\t\tcommand name : %s, command options: %s\n",
Z
zhong_ning 已提交
48 49 50
                g_jobs[i].cmdLines[j].name, g_jobs[i].cmdLines[j].cmdContent);
        }
    }
Z
zhong_ning 已提交
51
    INIT_LOGD("To dump all jobs finished\n");
Z
zhong_ning 已提交
52 53
}

W
wenjun 已提交
54 55 56 57 58 59 60
static int GetJobName(const cJSON* jobItem, Job* resJob)
{
    char* jobNameStr = cJSON_GetStringValue(cJSON_GetObjectItem(jobItem, "name"));
    if (jobNameStr == NULL) {
        return 0;
    }

Z
zhong_ning 已提交
61
    if (memcpy_s(resJob->name, MAX_JOB_NAME_LEN, jobNameStr, strlen(jobNameStr)) != EOK) {
Z
zhong_ning 已提交
62
        INIT_LOGE("Get job name \"%s\" failed\n", jobNameStr);
Z
zhong_ning 已提交
63
        return 0;
W
wenjun 已提交
64
    }
Z
zhong_ning 已提交
65 66
    resJob->name[strlen(jobNameStr)] = '\0';
    return 1;
W
wenjun 已提交
67 68 69 70 71
}

static void ParseJob(const cJSON* jobItem, Job* resJob)
{
    if (!GetJobName(jobItem, resJob)) {
Z
zhong_ning 已提交
72
        INIT_LOGE("get JobName failed\n");
W
wenjun 已提交
73 74 75 76 77 78
        (void)memset_s(resJob, sizeof(*resJob), 0, sizeof(*resJob));
        return;
    }

    cJSON* cmdsItem = cJSON_GetObjectItem(jobItem, CMDS_ARR_NAME_IN_JSON);
    if (!cJSON_IsArray(cmdsItem)) {
Z
zhong_ning 已提交
79
        INIT_LOGE("job %s is not an arrary\n", resJob->name);
W
wenjun 已提交
80 81 82 83 84
        return;
    }

    int cmdLinesCnt = cJSON_GetArraySize(cmdsItem);
    if (cmdLinesCnt <= 0) {  // empty job, no cmd
Z
zhong_ning 已提交
85
        INIT_LOGE("empty job \"%s\"\n", resJob->name);
W
wenjun 已提交
86 87 88
        return;
    }

Z
zhong_ning 已提交
89
    INIT_LOGD("job = %s, cmdLineCnt = %d\n", resJob->name, cmdLinesCnt);
W
wenjun 已提交
90
    if (cmdLinesCnt > MAX_CMD_CNT_IN_ONE_JOB) {
Z
zhong_ning 已提交
91
        INIT_LOGE("ParseAllJobs, too many cmds[cnt %d] in one job, it should not exceed %d.\n",
W
wenjun 已提交
92 93 94 95 96 97
            cmdLinesCnt, MAX_CMD_CNT_IN_ONE_JOB);
        return;
    }

    resJob->cmdLines = (CmdLine*)malloc(cmdLinesCnt * sizeof(CmdLine));
    if (resJob->cmdLines == NULL) {
Z
zhong_ning 已提交
98
        INIT_LOGE("allocate memory for command line failed\n");
W
wenjun 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        return;
    }

    if (memset_s(resJob->cmdLines, cmdLinesCnt * sizeof(CmdLine), 0, cmdLinesCnt * sizeof(CmdLine)) != EOK) {
        free(resJob->cmdLines);
        resJob->cmdLines = NULL;
        return;
    }
    resJob->cmdLinesCnt = cmdLinesCnt;

    for (int i = 0; i < cmdLinesCnt; ++i) {
        char* cmdLineStr = cJSON_GetStringValue(cJSON_GetArrayItem(cmdsItem, i));
        ParseCmdLine(cmdLineStr, &(resJob->cmdLines[i]));
    }
}

void ParseAllJobs(const cJSON* fileRoot)
{
    if (fileRoot == NULL) {
Z
zhong_ning 已提交
118
        INIT_LOGE("ParseAllJobs, input fileRoot is NULL!\n");
W
wenjun 已提交
119 120 121 122
        return;
    }

    cJSON* jobArr = cJSON_GetObjectItemCaseSensitive(fileRoot, JOBS_ARR_NAME_IN_JSON);
M
mamingshuai 已提交
123
    if (!cJSON_IsArray(jobArr)) {
Z
zhong_ning 已提交
124
        INIT_LOGE("ParseAllJobs, job item is not array!\n");
M
mamingshuai 已提交
125
        return;
W
wenjun 已提交
126 127
    }

M
mamingshuai 已提交
128
    int jobArrSize = cJSON_GetArraySize(jobArr);
W
wenjun 已提交
129
    if (jobArrSize <= 0 || jobArrSize > MAX_JOBS_COUNT) {
Z
zhong_ning 已提交
130
        INIT_LOGE("ParseAllJobs, jobs count %d is invalid, should be positive and not exceeding %d.\n",
W
wenjun 已提交
131 132 133 134
            jobArrSize, MAX_JOBS_COUNT);
        return;
    }

Z
zhong_ning 已提交
135
    Job* retJobs = (Job*)realloc(g_jobs, sizeof(Job) * (g_jobCnt + jobArrSize));
W
wenjun 已提交
136
    if (retJobs == NULL) {
Z
zhong_ning 已提交
137
        INIT_LOGE("ParseAllJobs, malloc failed! job arrSize %d.\n", jobArrSize);
W
wenjun 已提交
138 139 140
        return;
    }

Z
zhong_ning 已提交
141 142
    Job* tmp = retJobs + g_jobCnt;
    if (memset_s(tmp, sizeof(Job) * jobArrSize, 0, sizeof(Job) * jobArrSize) != EOK) {
Z
zhong_ning 已提交
143
        INIT_LOGE("ParseAllJobs, memset_s failed.\n");
W
wenjun 已提交
144 145 146 147 148 149 150
        free(retJobs);
        retJobs = NULL;
        return;
    }

    for (int i = 0; i < jobArrSize; ++i) {
        cJSON* jobItem = cJSON_GetArrayItem(jobArr, i);
Z
zhong_ning 已提交
151
        ParseJob(jobItem, &(tmp[i]));
W
wenjun 已提交
152 153
    }
    g_jobs = retJobs;
Z
zhong_ning 已提交
154
    g_jobCnt += jobArrSize;
W
wenjun 已提交
155 156 157 158 159
}

void DoJob(const char* jobName)
{
    if (jobName == NULL) {
Z
zhong_ning 已提交
160
        INIT_LOGE("DoJob, input jobName NULL!\n");
W
wenjun 已提交
161 162 163
        return;
    }

Z
zhong_ning 已提交
164
    INIT_LOGD("Call job with name %s\n", jobName);
W
wenjun 已提交
165 166 167 168 169 170
    for (int i = 0; i < g_jobCnt; ++i) {
        if (strncmp(jobName, g_jobs[i].name, strlen(g_jobs[i].name)) == 0) {
            CmdLine* cmdLines = g_jobs[i].cmdLines;
            for (int j = 0; j < g_jobs[i].cmdLinesCnt; ++j) {
                DoCmd(&(cmdLines[j]));
            }
Z
zhong_ning 已提交
171 172
            // Walk through all jobs
            // break;
W
wenjun 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
        }
    }
}

void ReleaseAllJobs()
{
    if (g_jobs == NULL) {
        return;
    }

    for (int i = 0; i < g_jobCnt; ++i) {
        if (g_jobs[i].cmdLines != NULL) {
            free(g_jobs[i].cmdLines);
            g_jobs[i].cmdLines = NULL;
            g_jobs[i].cmdLinesCnt = 0;
        }
    }

    free(g_jobs);
    g_jobs = NULL;
    g_jobCnt = 0;
}
M
mamingshuai 已提交
195