init_cmds.c 32.4 KB
Newer Older
W
wenjun 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
W
wenjun 已提交
3 4 5 6 7 8 9 10 11 12 13 14
 * 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_cmds.h"
Z
zhong_ning 已提交
17

W
wenjun 已提交
18 19
#include <ctype.h>
#include <errno.h>
20
#include <stdbool.h>
W
wenjun 已提交
21 22 23 24
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
Z
zhong_ning 已提交
25
#include <sys/resource.h>
W
wenjun 已提交
26
#include <sys/stat.h>
Z
zhong_ning 已提交
27 28
#include <sys/sysmacros.h>
#include <sys/wait.h>
W
wenjun 已提交
29
#include <unistd.h>
Z
zhong_ning 已提交
30
#include <fcntl.h>
Z
zhong_ning 已提交
31
#ifndef OHOS_LITE
Z
zhong_ning 已提交
32 33
#include <linux/module.h>
#endif
Z
zhong_ning 已提交
34 35 36
#include <sys/syscall.h>
#include "init_jobs.h"
#include "init_log.h"
Z
zhong_ning 已提交
37 38 39
#ifndef OHOS_LITE
#include "init_param.h"
#endif
Z
zhong_ning 已提交
40
#include "init_reboot.h"
W
wenjun 已提交
41
#include "init_service_manager.h"
Z
zhong_ning 已提交
42
#include "init_utils.h"
W
wenjun 已提交
43 44 45 46 47 48
#include "securec.h"

#define DEFAULT_DIR_MODE 0755  // mkdir, default mode
#define SPACES_CNT_IN_CMD_MAX 10   // mount, max number of spaces in cmdline
#define SPACES_CNT_IN_CMD_MIN 2    // mount, min number of spaces in cmdline

M
mamingshuai 已提交
49 50 51
#define LOADCFG_BUF_SIZE  128  // loadcfg, max buffer for one cmdline
#define LOADCFG_MAX_FILE_LEN 51200  // loadcfg, max file size is 50K
#define LOADCFG_MAX_LOOP 20  // loadcfg, to prevent to be trapped in infite loop
L
leon 已提交
52
#define OCTAL_TYPE 8  // 8 means octal to decimal
Z
zhong_ning 已提交
53
#define MAX_BUFFER 256
Z
zhong_ning 已提交
54
#define AUTHORITY_MAX_SIZE 128
Z
zhong_ning 已提交
55
#define WAIT_MAX_COUNT 10
Z
zhong_ning 已提交
56
#define MAX_EACH_CMD_LENGTH 30
Z
zhong_ning 已提交
57

M
mamingshuai 已提交
58
static const char *g_supportCfg[] = {
J
jason_gitee 已提交
59
    "/etc/patch.cfg",
M
mamingshuai 已提交
60 61 62
    "/patch/fstab.cfg",
};

W
wenjun 已提交
63 64 65 66 67 68
static const char* g_supportedCmds[] = {
    "start ",
    "mkdir ",
    "chmod ",
    "chown ",
    "mount ",
Z
zhong_ning 已提交
69
    "export ",
M
mamingshuai 已提交
70
    "loadcfg ",
Z
zhong_ning 已提交
71
    "insmod ",
Z
zhong_ning 已提交
72 73 74 75 76 77 78 79 80 81 82
    "rm ",
    "rmdir ",
    "write ",
    "exec ",
    "mknode ",
    "makedev ",
    "symlink ",
    "stop ",
    "trigger ",
    "reset ",
    "copy ",
Z
zhong_ning 已提交
83
    "setparam ",
Z
zhong_ning 已提交
84 85 86
    "load_persist_params ",
    "load_param ",
    "reboot ",
Z
zhong_ning 已提交
87
    "setrlimit ",
W
wenjun 已提交
88 89
};

Z
zhong_ning 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#ifndef OHOS_LITE
int GetParamValue(char *symValue, char *paramValue, unsigned int paramLen)
{
    if ((symValue == NULL) || (paramValue == NULL) || (paramLen == 0)) {
        return -1;
    }
    char tmpName[MAX_PARAM_NAME_LEN] = {0};
    char tmpValue[MAX_PARAM_VALUE_LEN] = {0};
    unsigned int tmpLen = 0;
    char *p = NULL;
    char *tmpptr = NULL;
    p = strchr(symValue, '$');
    if (p == NULL) { // not has '$' copy the original string
        INIT_CHECK_ONLY_RETURN(strncpy_s(paramValue, paramLen, symValue,
        paramLen - 1) == EOK, return -1);
        return 0;
    }
    tmpLen = p - symValue;
    if (tmpLen > 0) { // copy '$' front string
        INIT_CHECK_ONLY_RETURN(strncpy_s(paramValue, paramLen, symValue, tmpLen) == EOK, return -1);
    }
    p++;
    if (*p == '{') {
        p++;
        char *right = strchr(p, '}');
        if (right == NULL) {
Z
zhong_ning 已提交
116
            INIT_LOGE("Invalid cfg file name, miss '}'.");
Z
zhong_ning 已提交
117 118 119 120
            return -1;
        }
        tmpLen = right - p;
        if (tmpLen > MAX_PARAM_NAME_LEN) {
Z
zhong_ning 已提交
121
            INIT_LOGE("Parameter name longer than %d", MAX_PARAM_NAME_LEN);
Z
zhong_ning 已提交
122 123 124 125 126
            return -1;
        }
        INIT_CHECK_ONLY_RETURN(strncpy_s(tmpName, MAX_PARAM_NAME_LEN, p, tmpLen) == EOK, return -1);
        int ret = SystemReadParam(tmpName, tmpValue, &tmpLen); // get param
        if (ret != 0) {
Z
zhong_ning 已提交
127
            INIT_LOGE("Failed to read parameter \" %s \"", tmpName);
Z
zhong_ning 已提交
128 129 130 131 132 133 134 135 136
            return -1;
        }
        // change param to new string
        INIT_CHECK_ONLY_RETURN(strncat_s(paramValue, paramLen, tmpValue, MAX_PARAM_VALUE_LEN) == EOK, return -1);
        tmpptr = right + 1;
        tmpLen = paramLen - (tmpptr - symValue);
        if (*tmpptr != '\0') { // copy last string
            INIT_CHECK_ONLY_RETURN(strncat_s(paramValue, paramLen, tmpptr, tmpLen) == EOK, return -1);
        }
Z
zhong_ning 已提交
137
        INIT_LOGI("paramValue is %s ", paramValue);
Z
zhong_ning 已提交
138 139
        return 0;
    } else {
Z
zhong_ning 已提交
140
        INIT_LOGE("Invalid cfg file name, miss '{'.");
Z
zhong_ning 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        return -1;
    }
}
#endif

struct CmdArgs* GetCmd(const char *cmdContent, const char *delim)
{
    struct CmdArgs *ctx = (struct CmdArgs *)malloc(sizeof(struct CmdArgs));
    INIT_CHECK_ONLY_RETURN(ctx != NULL, return NULL);

    ctx->argv = (char**)malloc(sizeof(char*) * MAX_CMD_NAME_LEN);
    INIT_CHECK_ONLY_RETURN(ctx->argv != NULL, FreeCmd(&ctx); return NULL);

    char tmpCmd[MAX_BUFFER];
    INIT_CHECK_ONLY_RETURN(strncpy_s(tmpCmd, strlen(cmdContent) + 1, cmdContent, strlen(cmdContent)) == EOK,
        FreeCmd(&ctx);
        return NULL);
    tmpCmd[strlen(cmdContent)] = '\0';

    char *buffer = NULL;
    char *token = strtok_r(tmpCmd, delim, &buffer);
    ctx->argc = 0;
    while (token != NULL) {
#ifndef OHOS_LITE
        ctx->argv[ctx->argc] = calloc(sizeof(char *), MAX_EACH_CMD_LENGTH + MAX_PARAM_VALUE_LEN);
        INIT_CHECK_ONLY_RETURN(ctx->argv[ctx->argc] != NULL, FreeCmd(&ctx); return NULL);
        INIT_CHECK_ONLY_RETURN(GetParamValue(token, ctx->argv[ctx->argc], MAX_EACH_CMD_LENGTH + MAX_PARAM_VALUE_LEN) == 0,
            FreeCmd(&ctx);
            return NULL);
#else
        ctx->argv[ctx->argc] = calloc(sizeof(char *), MAX_EACH_CMD_LENGTH);
        INIT_CHECK_ONLY_RETURN(ctx->argv[ctx->argc] != NULL, FreeCmd(&ctx); return NULL);
        INIT_CHECK_ONLY_RETURN(strncpy_s(ctx->argv[ctx->argc], strlen(cmdContent) + 1, token, strlen(token)) == EOK,
            FreeCmd(&ctx);
            return NULL);
#endif
        if (ctx->argc > MAX_CMD_NAME_LEN - 1) {
Z
zhong_ning 已提交
178
            INIT_LOGE("GetCmd failed, max cmd number is 10.");
Z
zhong_ning 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
            FreeCmd(&ctx);
            return NULL;
        }
        token = strtok_r(NULL, delim, &buffer);
        ctx->argc += 1;
    }
    ctx->argv[ctx->argc] = NULL;
    return ctx;
}

void FreeCmd(struct CmdArgs **cmd)
{
    struct CmdArgs *tmpCmd = *cmd;
    INIT_CHECK_ONLY_RETURN(tmpCmd != NULL, return);
    for (int i = 0; i < tmpCmd->argc; ++i) {
        INIT_CHECK_ONLY_RETURN(tmpCmd->argv[i] == NULL, free(tmpCmd->argv[i]));
    }
    INIT_CHECK_ONLY_RETURN(tmpCmd->argv == NULL, free(tmpCmd->argv));
    free(tmpCmd);
    return;
}

W
wenjun 已提交
201 202
void ParseCmdLine(const char* cmdStr, CmdLine* resCmd)
{
203 204
    size_t cmdLineLen = 0;
    if (cmdStr == NULL || resCmd == NULL || (cmdLineLen = strlen(cmdStr)) == 0) {
W
wenjun 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        return;
    }

    size_t supportCmdCnt = sizeof(g_supportedCmds) / sizeof(g_supportedCmds[0]);
    int foundAndSucceed = 0;
    for (size_t i = 0; i < supportCmdCnt; ++i) {
        size_t curCmdNameLen = strlen(g_supportedCmds[i]);
        if (cmdLineLen > curCmdNameLen && cmdLineLen <= (curCmdNameLen + MAX_CMD_CONTENT_LEN) &&
            strncmp(g_supportedCmds[i], cmdStr, curCmdNameLen) == 0) {
            if (memcpy_s(resCmd->name, MAX_CMD_NAME_LEN, cmdStr, curCmdNameLen) != EOK) {
                break;
            }
            resCmd->name[curCmdNameLen] = '\0';

            const char* cmdContent = cmdStr + curCmdNameLen;
            size_t cmdContentLen = cmdLineLen - curCmdNameLen;
            if (memcpy_s(resCmd->cmdContent, MAX_CMD_CONTENT_LEN, cmdContent, cmdContentLen) != EOK) {
                break;
            }
            resCmd->cmdContent[cmdContentLen] = '\0';
            foundAndSucceed = 1;
            break;
        }
    }

    if (!foundAndSucceed) {
Z
zhong_ning 已提交
231
        INIT_LOGE("Cannot parse command: %s", cmdStr);
W
wenjun 已提交
232 233 234 235 236 237
        (void)memset_s(resCmd, sizeof(*resCmd), 0, sizeof(*resCmd));
    }
}

static void DoStart(const char* cmdContent)
{
Z
zhong_ning 已提交
238
    INIT_LOGD("DoStart %s ", cmdContent);
W
wenjun 已提交
239 240 241
    StartServiceByName(cmdContent);
}

Z
zhong_ning 已提交
242
static void DoStop(const char* cmdContent)
W
wenjun 已提交
243
{
Z
zhong_ning 已提交
244
    INIT_LOGD("DoStop %s ", cmdContent);
Z
zhong_ning 已提交
245
    StopServiceByName(cmdContent);
W
wenjun 已提交
246 247
}

Z
zhong_ning 已提交
248
static void DoReset(const char* cmdContent)
W
wenjun 已提交
249
{
Z
zhong_ning 已提交
250
    INIT_LOGD("DoReset %s ", cmdContent);
Z
zhong_ning 已提交
251 252 253
    DoStop(cmdContent);
    DoStart(cmdContent);
}
W
wenjun 已提交
254

Z
zhong_ning 已提交
255 256 257 258 259 260 261 262 263 264 265
static void DoCopy(const char* cmdContent)
{
    int srcFd = -1;
    int dstFd = -1;
    int rdLen = 0;
    int rtLen = 0;
    char buf[MAX_COPY_BUF_SIZE] = {0};
    mode_t mode = 0;
    struct stat fileStat = {0};
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != DEFAULT_COPY_ARGS_CNT) {
Z
zhong_ning 已提交
266
        INIT_LOGE("DoCopy failed.");
Z
zhong_ning 已提交
267 268 269
        goto out;
    }
    srcFd = open(ctx->argv[0], O_RDONLY);
Z
zhong_ning 已提交
270 271
    INIT_ERROR_CHECK(srcFd >= 0, goto out, "copy open %s fail %d! ", ctx->argv[0], errno);
    INIT_ERROR_CHECK(stat(ctx->argv[0], &fileStat) == 0, goto out, "stat fail ");
Z
zhong_ning 已提交
272 273
    mode = fileStat.st_mode;
    dstFd = open(ctx->argv[1], O_WRONLY | O_TRUNC | O_CREAT, mode);
Z
zhong_ning 已提交
274
    INIT_ERROR_CHECK(dstFd >= 0, goto out, "copy open %s fail %d! ", ctx->argv[1], errno);
Z
zhong_ning 已提交
275 276
    while ((rdLen = read(srcFd, buf, sizeof(buf) - 1)) > 0) {
        rtLen = write(dstFd, buf, rdLen);
Z
zhong_ning 已提交
277
        INIT_ERROR_CHECK(rtLen == rdLen, goto out, "write %s file fail %d! ", ctx->argv[1], errno);
Z
zhong_ning 已提交
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    }
    fsync(dstFd);
out:
    FreeCmd(&ctx);
    ctx = NULL;
    close(srcFd);
    srcFd = -1;
    close(dstFd);
    dstFd = -1;
    return;
}

static void DoChown(const char* cmdContent)
{
    // format: chown owner group /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 3) {
Z
zhong_ning 已提交
295
        INIT_LOGE("DoChown failed.");
Z
zhong_ning 已提交
296 297 298 299 300 301 302
        goto out;
    }

    uid_t owner = (uid_t)-1;
    gid_t group = (gid_t)-1;
    if (isalpha(ctx->argv[0][0])) {
        owner = DecodeUid(ctx->argv[0]);
Z
zhong_ning 已提交
303
        INIT_ERROR_CHECK(owner != (uid_t)-1, goto out, "DoChown decode owner failed.");
Z
zhong_ning 已提交
304 305
    } else {
        owner = strtoul(ctx->argv[0], NULL, 0);
W
wenjun 已提交
306 307
    }

Z
zhong_ning 已提交
308 309
    if (isalpha(ctx->argv[1][0])) {
        group = DecodeUid(ctx->argv[1]);
Z
zhong_ning 已提交
310
        INIT_ERROR_CHECK(group != (gid_t)-1, goto out, "DoChown decode group failed.");
Z
zhong_ning 已提交
311 312
    } else {
        group = strtoul(ctx->argv[1], NULL, 0);
W
wenjun 已提交
313 314
    }

Z
zhong_ning 已提交
315 316
    int pathPos = 2;
    if (chown(ctx->argv[pathPos], owner, group) != 0) {
Z
zhong_ning 已提交
317
        INIT_LOGE("DoChown, failed for %s, err %d.", cmdContent, errno);
W
wenjun 已提交
318
    }
Z
zhong_ning 已提交
319 320 321
out:
    FreeCmd(&ctx);
    return;
W
wenjun 已提交
322 323
}

Z
zhong_ning 已提交
324
static void DoMkDir(const char* cmdContent)
W
wenjun 已提交
325
{
Z
zhong_ning 已提交
326 327 328
    // format: mkdir /xxx/xxx/xxx or mkdir /xxx/xxx/xxx mode owner group
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc < 1) {
Z
zhong_ning 已提交
329
        INIT_LOGE("DoMkDir failed.");
Z
zhong_ning 已提交
330 331 332 333
        goto out;
    }

    mode_t mode = DEFAULT_DIR_MODE;
Z
zhong_ning 已提交
334
    if (mkdir(ctx->argv[0], mode) != 0 && errno != EEXIST) {
Z
zhong_ning 已提交
335
        INIT_LOGE("DoMkDir, failed for %s, err %d.", cmdContent, errno);
Z
zhong_ning 已提交
336
        goto out;
W
wenjun 已提交
337 338
    }

Z
zhong_ning 已提交
339 340 341
    if (ctx->argc > 1) {
        mode = strtoul(ctx->argv[1], NULL, OCTAL_TYPE);
        if (chmod(ctx->argv[0], mode) != 0) {
Z
zhong_ning 已提交
342
            INIT_LOGE("DoMkDir failed for %s, err %d.", cmdContent, errno);
Z
zhong_ning 已提交
343 344 345 346 347 348
        }
        int ownerPos = 2;
        int groupPos = 3;
        char chownCmdContent[AUTHORITY_MAX_SIZE] = { 0 };
        if (snprintf_s(chownCmdContent, AUTHORITY_MAX_SIZE, AUTHORITY_MAX_SIZE - 1, "%s %s %s",
            ctx->argv[ownerPos], ctx->argv[groupPos], ctx->argv[0]) == -1) {
Z
zhong_ning 已提交
349
            INIT_LOGE("DoMkDir snprintf failed.");
Z
zhong_ning 已提交
350
            goto out;
W
wenjun 已提交
351
        }
Z
zhong_ning 已提交
352 353 354 355 356 357 358 359 360 361 362 363
        DoChown(chownCmdContent);
    }
out:
    FreeCmd(&ctx);
    return;
}

static void DoChmod(const char* cmdContent)
{
    // format: chmod xxxx /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 2) {
Z
zhong_ning 已提交
364
        INIT_LOGE("DoChmod failed.");
Z
zhong_ning 已提交
365
        goto out;
W
wenjun 已提交
366 367
    }

Z
zhong_ning 已提交
368 369
    mode_t mode = strtoul(ctx->argv[0], NULL, OCTAL_TYPE);
    if (mode == 0) {
Z
zhong_ning 已提交
370
        INIT_LOGE("DoChmod, strtoul failed for %s, er %d.", cmdContent, errno);
Z
zhong_ning 已提交
371 372 373 374
        goto out;
    }

    if (chmod(ctx->argv[1], mode) != 0) {
Z
zhong_ning 已提交
375
        INIT_LOGE("DoChmod, failed for %s, err %d.", cmdContent, errno);
W
wenjun 已提交
376
    }
Z
zhong_ning 已提交
377 378 379
out:
    FreeCmd(&ctx);
    return;
W
wenjun 已提交
380 381 382 383 384
}

static char* CopySubStr(const char* srcStr, size_t startPos, size_t endPos)
{
    if (endPos <= startPos) {
Z
zhong_ning 已提交
385
        INIT_LOGE("DoMount, invalid params<%zu, %zu> for %s.", endPos, startPos, srcStr);
W
wenjun 已提交
386 387 388 389 390 391
        return NULL;
    }

    size_t mallocLen = endPos - startPos + 1;
    char* retStr = (char*)malloc(mallocLen);
    if (retStr == NULL) {
Z
zhong_ning 已提交
392
        INIT_LOGE("DoMount, malloc failed! malloc size %zu, for %s.", mallocLen, srcStr);
W
wenjun 已提交
393 394 395 396 397
        return NULL;
    }

    const char* copyStart = srcStr + startPos;
    if (memcpy_s(retStr, mallocLen, copyStart, endPos - startPos) != EOK) {
Z
zhong_ning 已提交
398
        INIT_LOGE("DoMount, memcpy_s failed for %s.", srcStr);
W
wenjun 已提交
399 400 401 402 403 404 405 406 407 408 409 410
        free(retStr);
        return NULL;
    }
    retStr[mallocLen - 1] = '\0';

    // for example, source may be none
    if (strncmp(retStr, "none", strlen("none")) == 0) {
        retStr[0] = '\0';
    }
    return retStr;
}

Z
zhong_ning 已提交
411
static int GetMountFlag(unsigned long* mountflags, const char* targetStr, const char *source)
W
wenjun 已提交
412 413 414 415 416 417 418
{
    if (targetStr == NULL) {
        return 0;
    }

    if (strncmp(targetStr, "nodev", strlen("nodev")) == 0) {
        (*mountflags) |= MS_NODEV;
G
Gymee 已提交
419
    } else if (strncmp(targetStr, "noexec", strlen("noexec")) == 0) {
W
wenjun 已提交
420
        (*mountflags) |= MS_NOEXEC;
G
Gymee 已提交
421
    } else if (strncmp(targetStr, "nosuid", strlen("nosuid")) == 0) {
W
wenjun 已提交
422
        (*mountflags) |= MS_NOSUID;
G
Gymee 已提交
423
    } else if (strncmp(targetStr, "rdonly", strlen("rdonly")) == 0) {
W
wenjun 已提交
424
        (*mountflags) |= MS_RDONLY;
Z
zhong_ning 已提交
425 426 427
    } else if (strncmp(targetStr, "noatime", strlen("noatime")) == 0) {
        (*mountflags) |= MS_NOATIME;
    } else if (strncmp(targetStr, "wait", strlen("wait")) == 0) {
Z
zhong_ning 已提交
428
        WaitForFile(source, WAIT_MAX_COUNT);
G
Gymee 已提交
429 430
    } else {
        return 0;
W
wenjun 已提交
431
    }
G
Gymee 已提交
432
    return 1;
W
wenjun 已提交
433 434 435 436
}

static int CountSpaces(const char* cmdContent, size_t* spaceCnt, size_t* spacePosArr, size_t spacePosArrLen)
{
M
mamingshuai 已提交
437 438
    *spaceCnt = 0;
    size_t strLen = strlen(cmdContent);
W
wenjun 已提交
439
    for (size_t i = 0; i < strLen; ++i) {
M
mamingshuai 已提交
440 441 442
        if (cmdContent[i] == ' ') {
            ++(*spaceCnt);
            if ((*spaceCnt) > spacePosArrLen) {
Z
zhong_ning 已提交
443
                INIT_LOGE("DoMount, too many spaces, bad format for %s.", cmdContent);
M
mamingshuai 已提交
444 445 446
                return 0;
            }
            spacePosArr[(*spaceCnt) - 1] = i;
W
wenjun 已提交
447 448 449
        }
    }

M
mamingshuai 已提交
450 451 452
    if ((*spaceCnt) < SPACES_CNT_IN_CMD_MIN ||           // spaces count should not less than 2(at least 3 items)
        spacePosArr[0] == 0 ||                           // should not start with space
        spacePosArr[(*spaceCnt) - 1] == strLen - 1) {    // should not end with space
Z
zhong_ning 已提交
453
        INIT_LOGE("DoMount, bad format for %s.", cmdContent);
W
wenjun 已提交
454 455 456 457
        return 0;
    }

    // spaces should not be adjacent
M
mamingshuai 已提交
458 459
    for (size_t i = 1; i < (*spaceCnt); ++i) {
        if (spacePosArr[i] == spacePosArr[i - 1] + 1) {
Z
zhong_ning 已提交
460
            INIT_LOGE("DoMount, bad format for %s.", cmdContent);
W
wenjun 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474
            return 0;
        }
    }
    return 1;
}

static void DoMount(const char* cmdContent)
{
    size_t spaceCnt = 0;
    size_t spacePosArr[SPACES_CNT_IN_CMD_MAX] = {0};
    if (!CountSpaces(cmdContent, &spaceCnt, spacePosArr, SPACES_CNT_IN_CMD_MAX)) {
        return;
    }

475 476 477
    // format: fileSystemType source target mountFlag1 mountFlag2... data
    unsigned long mountflags = 0;
    size_t strLen = strlen(cmdContent);
W
wenjun 已提交
478 479
    size_t indexOffset = 0;
    char* fileSysType = CopySubStr(cmdContent, 0, spacePosArr[indexOffset]);
M
mamingshuai 已提交
480 481 482 483
    if (fileSysType == NULL) {
        return;
    }

W
wenjun 已提交
484
    char* source = CopySubStr(cmdContent, spacePosArr[indexOffset] + 1, spacePosArr[indexOffset + 1]);
M
mamingshuai 已提交
485 486 487 488
    if (source == NULL) {
        free(fileSysType);
        return;
    }
W
wenjun 已提交
489 490 491 492 493
    ++indexOffset;

    // maybe only has "filesystype source target", 2 spaces
    size_t targetEndPos = (indexOffset == spaceCnt - 1) ? strLen : spacePosArr[indexOffset + 1];
    char* target = CopySubStr(cmdContent, spacePosArr[indexOffset] + 1, targetEndPos);
M
mamingshuai 已提交
494 495 496 497 498
    if (target == NULL) {
        free(fileSysType);
        free(source);
        return;
    }
W
wenjun 已提交
499 500 501 502 503 504
    ++indexOffset;

    // get mountflags, if fail, the rest part of string will be data
    while (indexOffset < spaceCnt) {
        size_t tmpStrEndPos = (indexOffset == spaceCnt - 1) ? strLen : spacePosArr[indexOffset + 1];
        char* tmpStr = CopySubStr(cmdContent, spacePosArr[indexOffset] + 1, tmpStrEndPos);
Z
zhong_ning 已提交
505
        int ret = GetMountFlag(&mountflags, tmpStr, source);
W
wenjun 已提交
506 507 508 509 510 511 512 513 514 515
        free(tmpStr);
        tmpStr = NULL;

        // get flag failed, the rest part of string will be data
        if (ret == 0) {
            break;
        }
        ++indexOffset;
    }

M
mamingshuai 已提交
516 517 518
    int mountRet;
    if (indexOffset >= spaceCnt) {    // no data
        mountRet = mount(source, target, fileSysType, mountflags, NULL);
W
wenjun 已提交
519
    } else {
M
mamingshuai 已提交
520 521 522 523 524
        const char* dataStr = cmdContent + spacePosArr[indexOffset] + 1;
        mountRet = mount(source, target, fileSysType, mountflags, dataStr);
    }

    if (mountRet != 0) {
Z
zhong_ning 已提交
525
        INIT_LOGE("DoMount, failed for %s, err %d.", cmdContent, errno);
W
wenjun 已提交
526 527 528 529 530 531 532
    }

    free(fileSysType);
    free(source);
    free(target);
}

Z
zhong_ning 已提交
533
#ifndef OHOS_LITE
L
leon 已提交
534
#define OPTIONS_SIZE 128u
L
leon 已提交
535 536 537 538 539 540
static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPtr, int flags)
{
    char options[OPTIONS_SIZE] = {0};
    if (flags == 0) { //  '-f' option
        if (restPtr != NULL && secondPtr != NULL) { // Reset arugments, combine then all.
            if (snprintf_s(options, sizeof(options), OPTIONS_SIZE -1, "%s %s", secondPtr, restPtr) == -1) {
Z
zhong_ning 已提交
541
                return;
L
leon 已提交
542
            }
L
leon 已提交
543
        } else if (secondPtr != NULL) {
L
leon 已提交
544
            if (strncpy_s(options, OPTIONS_SIZE - 1, secondPtr, strlen(secondPtr)) != 0) {
Z
zhong_ning 已提交
545
                return;
L
leon 已提交
546 547 548 549
            }
        }
    } else { // Only restPtr is option
        if (restPtr != NULL) {
Z
zhong_ning 已提交
550
            if (strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)) != 0) {
Z
zhong_ning 已提交
551
                return;
Z
zhong_ning 已提交
552
            }
L
leon 已提交
553 554
        }
    }
Z
zhong_ning 已提交
555
    if (!fileName) {
Z
zhong_ning 已提交
556
        return;
Z
zhong_ning 已提交
557
    }
Z
zhong_ning 已提交
558
    char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char));
Z
zhong_ning 已提交
559 560 561
    if (realPath == NULL) {
        return;
    }
Z
zhong_ning 已提交
562
    realPath = realpath(fileName, realPath);
Z
zhong_ning 已提交
563
    int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
L
leon 已提交
564
    if (fd < 0) {
Z
zhong_ning 已提交
565
        INIT_LOGE("failed to open %s: %d", realPath, errno);
Z
zhong_ning 已提交
566
        free(realPath);
Z
zhong_ning 已提交
567
        return;
L
leon 已提交
568 569 570
    }
    int rc = syscall(__NR_finit_module, fd, options, flags);
    if (rc == -1) {
Z
zhong_ning 已提交
571
        INIT_LOGE("finit_module for %s failed: %d", realPath, errno);
L
leon 已提交
572
    }
Z
zhong_ning 已提交
573
    if (fd >= 0) {
L
leon 已提交
574 575
        close(fd);
    }
Z
zhong_ning 已提交
576
    free(realPath);
L
leon 已提交
577 578 579
    return;
}

Z
zhong_ning 已提交
580 581 582 583 584 585
// format insmod <ko name> [-f] [options]
static void DoInsmod(const char *cmdContent)
{
    char *p = NULL;
    char *restPtr = NULL;
    char *fileName = NULL;
L
leon 已提交
586
    char *line = NULL;
Z
zhong_ning 已提交
587 588 589 590
    int flags = 0;

    size_t count = strlen(cmdContent);
    if (count > OPTIONS_SIZE) {
Z
zhong_ning 已提交
591
        INIT_LOGE("DoInsmod options too long, maybe lost some of options");
Z
zhong_ning 已提交
592 593 594
    }
    line = (char *)malloc(count + 1);
    if (line == NULL) {
Z
zhong_ning 已提交
595
        INIT_LOGE("DoInsmod allocate memory failed.");
Z
zhong_ning 已提交
596 597
        return;
    }
L
leon 已提交
598

Z
zhong_ning 已提交
599
    if (memcpy_s(line, count, cmdContent, count) != EOK) {
Z
zhong_ning 已提交
600
        INIT_LOGE("DoInsmod memcpy failed");
Z
zhong_ning 已提交
601
        free(line);
L
leon 已提交
602
        return;
Z
zhong_ning 已提交
603 604 605 606
    }
    line[count] = '\0';
    do {
        if ((p = strtok_r(line, " ", &restPtr)) == NULL) {
Z
zhong_ning 已提交
607
            INIT_LOGE("DoInsmod cannot get filename.");
Z
zhong_ning 已提交
608
            free(line);
Z
zhong_ning 已提交
609 610 611
            return;
        }
        fileName = p;
Z
zhong_ning 已提交
612
        INIT_LOGI("DoInsmod fileName is [%s].", fileName);
Z
zhong_ning 已提交
613 614 615 616 617 618 619
        if ((p = strtok_r(NULL, " ", &restPtr)) == NULL) {
            break;
        }
        if (!strcmp(p, "-f")) {
            flags = MODULE_INIT_IGNORE_VERMAGIC | MODULE_INIT_IGNORE_MODVERSIONS;
        }
    } while (0);
L
leon 已提交
620
    DoInsmodInternal(fileName, p, restPtr, flags);
Z
zhong_ning 已提交
621 622 623
    if (line != NULL) {
        free(line);
    }
L
leon 已提交
624
    return;
Z
zhong_ning 已提交
625
}
Z
zhong_ning 已提交
626 627 628 629 630

static void DoSetParam(const char* cmdContent)
{
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 2) {
Z
zhong_ning 已提交
631
        INIT_LOGE("DoSetParam failed.");
Z
zhong_ning 已提交
632 633
        goto out;
    }
Z
zhong_ning 已提交
634
    INIT_LOGE("param name: %s, value %s ", ctx->argv[0], ctx->argv[1]);
Z
zhong_ning 已提交
635
    SystemWriteParam(ctx->argv[0], ctx->argv[1]);
Z
zhong_ning 已提交
636 637 638 639 640
out:
    FreeCmd(&ctx);
    return;
}

L
leon 已提交
641
#endif // OHOS_LITE
Z
zhong_ning 已提交
642

M
mamingshuai 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
static bool CheckValidCfg(const char *path)
{
    size_t cfgCnt = sizeof(g_supportCfg) / sizeof(g_supportCfg[0]);
    struct stat fileStat = {0};

    if (stat(path, &fileStat) != 0 || fileStat.st_size <= 0 || fileStat.st_size > LOADCFG_MAX_FILE_LEN) {
        return false;
    }

    for (size_t i = 0; i < cfgCnt; ++i) {
        if (strcmp(path, g_supportCfg[i]) == 0) {
            return true;
        }
    }

    return false;
}

static void DoLoadCfg(const char *path)
{
    char buf[LOADCFG_BUF_SIZE] = {0};
    FILE *fp = NULL;
    size_t maxLoop = 0;
    CmdLine *cmdLine = NULL;
    int len;

    if (path == NULL) {
        return;
    }

Z
zhong_ning 已提交
673
    INIT_LOGI("DoLoadCfg cfg file %s", path);
M
mamingshuai 已提交
674
    if (!CheckValidCfg(path)) {
Z
zhong_ning 已提交
675
        INIT_LOGE("CheckCfg file %s Failed", path);
M
mamingshuai 已提交
676 677 678 679 680
        return;
    }

    fp = fopen(path, "r");
    if (fp == NULL) {
Z
zhong_ning 已提交
681
        INIT_LOGE("open cfg error = %d", errno);
M
mamingshuai 已提交
682 683 684 685 686
        return;
    }

    cmdLine = (CmdLine *)malloc(sizeof(CmdLine));
    if (cmdLine == NULL) {
Z
zhong_ning 已提交
687
        INIT_LOGE("malloc cmdline error");
M
mamingshuai 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
        fclose(fp);
        return;
    }

    while (fgets(buf, LOADCFG_BUF_SIZE, fp) != NULL && maxLoop < LOADCFG_MAX_LOOP) {
        maxLoop++;
        len = strlen(buf);
        if (len < 1) {
            continue;
        }
        if (buf[len - 1] == '\n') {
            buf[len - 1] = '\0'; // we replace '\n' with '\0'
        }
        (void)memset_s(cmdLine, sizeof(CmdLine), 0, sizeof(CmdLine));
        ParseCmdLine(buf, cmdLine);
        DoCmd(cmdLine);
        (void)memset_s(buf, sizeof(char) * LOADCFG_BUF_SIZE, 0, sizeof(char) * LOADCFG_BUF_SIZE);
    }

    free(cmdLine);
    fclose(fp);
}

Z
zhong_ning 已提交
711 712 713 714 715 716
static void DoWrite(const char *cmdContent)
{
    // format: write path content
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    int writeCmdNumber = 2;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != writeCmdNumber) {
Z
zhong_ning 已提交
717
        INIT_LOGE("DoWrite: invalid arguments");
Z
zhong_ning 已提交
718 719 720 721 722 723
        goto out;
    }

    int fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU |
 S_IRGRP | S_IROTH);
    if (fd == -1) {
Z
zhong_ning 已提交
724
        INIT_LOGE("DoWrite: open %s failed: %d", ctx->argv[0], errno);
Z
zhong_ning 已提交
725 726 727 728 729
        goto out;
    }

    size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1]));
    if (ret < 0) {
Z
zhong_ning 已提交
730
        INIT_LOGE("DoWrite: write to file %s failed: %d", ctx->argv[0], errno);
Z
zhong_ning 已提交
731 732 733 734 735 736 737 738 739 740 741 742 743 744
        close(fd);
        goto out;
    }
    close(fd);
out:
    FreeCmd(&ctx);
    return;
}

static void DoRmdir(const char *cmdContent)
{
    // format: rmdir path
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 1) {
Z
zhong_ning 已提交
745
        INIT_LOGE("DoRmdir: invalid arguments");
Z
zhong_ning 已提交
746 747 748 749 750
        goto out;
    }

    int ret = rmdir(ctx->argv[0]);
    if (ret == -1) {
Z
zhong_ning 已提交
751
        INIT_LOGE("DoRmdir: remove %s failed: %d.", ctx->argv[0], errno);
Z
zhong_ning 已提交
752 753 754 755 756 757 758
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

Z
zhong_ning 已提交
759 760 761 762 763 764 765 766 767 768 769 770
static void DoSetrlimit(const char *cmdContent)
{
    char *resource[] = {
        "RLIMIT_CPU", "RLIMIT_FSIZE", "RLIMIT_DATA", "RLIMIT_STACK", "RLIMIT_CORE", "RLIMIT_RSS",
        "RLIMIT_NPROC", "RLIMIT_NOFILE", "RLIMIT_MEMLOCK", "RLIMIT_AS", "RLIMIT_LOCKS", "RLIMIT_SIGPENDING",
        "RLIMIT_MSGQUEUE", "RLIMIT_NICE", "RLIMIT_RTPRIO", "RLIMIT_RTTIME", "RLIM_NLIMITS"
    };
    // format: setrlimit resource curValue maxValue
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    int setrlimitCmdNumber = 3;
    int rlimMaxPos = 2;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != setrlimitCmdNumber) {
Z
zhong_ning 已提交
771
        INIT_LOGE("DoSetrlimit: invalid arguments");
Z
zhong_ning 已提交
772 773 774 775 776 777 778 779 780 781 782 783 784
        goto out;
    }

    struct rlimit limit;
    limit.rlim_cur = atoi(ctx->argv[1]);
    limit.rlim_max = atoi(ctx->argv[rlimMaxPos]);
    int rcs = -1;
    for (unsigned int i = 0 ; i < sizeof(resource) / sizeof(char*); ++i) {
        if (strcmp(ctx->argv[0], resource[i]) == 0) {
            rcs = (int)i;
        }
    }
    if (rcs == -1) {
Z
zhong_ning 已提交
785
        INIT_LOGE("DoSetrlimit failed, resouces :%s not support.", ctx->argv[0]);
Z
zhong_ning 已提交
786 787 788 789
        goto out;
    }
    int ret = setrlimit(rcs, &limit);
    if (ret) {
Z
zhong_ning 已提交
790
        INIT_LOGE("DoSetrlimit failed : %d", errno);
Z
zhong_ning 已提交
791 792 793 794 795 796 797
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

Z
zhong_ning 已提交
798 799 800 801 802
static void DoRm(const char *cmdContent)
{
    // format: rm /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 1) {
Z
zhong_ning 已提交
803
        INIT_LOGE("DoRm: invalid arguments");
Z
zhong_ning 已提交
804 805 806 807
        goto out;
    }
    int ret = unlink(ctx->argv[0]);
    if (ret == -1) {
Z
zhong_ning 已提交
808
        INIT_LOGE("DoRm: unlink %s failed: %d.", ctx->argv[0], errno);
Z
zhong_ning 已提交
809 810 811 812 813 814 815 816 817 818 819
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

static void DoExport(const char *cmdContent)
{
    // format: export xxx /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
Z
zhong_ning 已提交
820 821
    int exportCmdNumber = 2;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != exportCmdNumber) {
Z
zhong_ning 已提交
822
        INIT_LOGE("DoExport: invalid arguments");
Z
zhong_ning 已提交
823 824 825 826
        goto out;
    }
    int ret = setenv(ctx->argv[0], ctx->argv[1], 1);
    if (ret != 0) {
Z
zhong_ning 已提交
827
        INIT_LOGE("DoExport: set %s with %s failed: %d", ctx->argv[0], ctx->argv[1], errno);
Z
zhong_ning 已提交
828 829 830 831 832 833 834 835 836 837 838
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

static void DoExec(const char *cmdContent)
{
    // format: exec /xxx/xxx/xxx xxx
    pid_t pid = fork();
Z
zhong_ning 已提交
839
    if (pid < 0) {
Z
zhong_ning 已提交
840
        INIT_LOGE("DoExec: failed to fork child process to exec \"%s\"", cmdContent);
Z
zhong_ning 已提交
841 842
        return;
    }
Z
zhong_ning 已提交
843
    if (pid == 0) {
Z
zhong_ning 已提交
844 845
        struct CmdArgs *ctx = GetCmd(cmdContent, " ");
        if (ctx == NULL || ctx->argv == NULL) {
Z
zhong_ning 已提交
846
            INIT_LOGE("DoExec: invalid arguments");
Z
zhong_ning 已提交
847 848
            _exit(0x7f);
        }
Z
zhong_ning 已提交
849 850 851 852 853 854
#ifdef OHOS_LITE
        int ret = execve(ctx->argv[0], ctx->argv, NULL);
#else
        int ret = execv(ctx->argv[0], ctx->argv);
#endif
        if (ret == -1) {
Z
zhong_ning 已提交
855
            INIT_LOGE("DoExec: execute \"%s\" failed: %d.", cmdContent, errno);
Z
zhong_ning 已提交
856
        }
Z
zhong_ning 已提交
857 858
        FreeCmd(&ctx);
        _exit(0x7f);
Z
zhong_ning 已提交
859 860 861 862 863 864 865 866 867 868 869
    }
    return;
}

#ifndef __LITEOS__
static void DoSymlink(const char *cmdContent)
{
    // format: symlink /xxx/xxx/xxx /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    int symlinkCmdNumber = 2;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != symlinkCmdNumber) {
Z
zhong_ning 已提交
870
        INIT_LOGE("DoSymlink: invalid arguments.");
Z
zhong_ning 已提交
871 872 873 874 875
        goto out;
    }

    int ret = symlink(ctx->argv[0], ctx->argv[1]);
    if (ret != 0) {
Z
zhong_ning 已提交
876
        INIT_LOGE("DoSymlink: link %s to %s failed: %d", ctx->argv[0], ctx->argv[1], errno);
Z
zhong_ning 已提交
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

static mode_t GetDeviceMode(const char *deviceStr)
{
    switch (*deviceStr) {
        case 'b':
        case 'B':
            return S_IFBLK;
        case 'c':
        case 'C':
            return S_IFCHR;
        case 'f':
        case 'F':
            return S_IFIFO;
        default:
            return -1;
    }
}

static void DoMakeNode(const char *cmdContent)
{
    // format: mknod path b 0644 1 9
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    int mkNodeCmdNumber = 5;
    int deviceTypePos = 1;
    int authorityPos = 2;
    int majorDevicePos = 3;
    int minorDevicePos = 4;
    int decimal = 10;
    int octal = 8;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != mkNodeCmdNumber) {
Z
zhong_ning 已提交
913
        INIT_LOGE("DoMakeNode: invalid arguments");
Z
zhong_ning 已提交
914 915 916 917
        goto out;
    }

    if (!access(ctx->argv[1], F_OK)) {
Z
zhong_ning 已提交
918
        INIT_LOGE("DoMakeNode failed, path has not sexisted");
Z
zhong_ning 已提交
919 920 921 922 923 924 925 926 927
        goto out;
    }
    mode_t deviceMode = GetDeviceMode(ctx->argv[deviceTypePos]);
    unsigned int major = strtoul(ctx->argv[majorDevicePos], NULL, decimal);
    unsigned int minor = strtoul(ctx->argv[minorDevicePos], NULL, decimal);
    mode_t authority = strtoul(ctx->argv[authorityPos], NULL, octal);

    int ret = mknod(ctx->argv[0], deviceMode | authority, makedev(major, minor));
    if (ret != 0) {
Z
zhong_ning 已提交
928
        INIT_LOGE("DoMakeNode: path: %s failed: %d", ctx->argv[0], errno);
Z
zhong_ning 已提交
929 930 931 932 933 934 935 936 937 938 939 940 941 942
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

static void DoMakeDevice(const char *cmdContent)
{
    // format: makedev major minor
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    int makeDevCmdNumber = 2;
    int decimal = 10;
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != makeDevCmdNumber) {
Z
zhong_ning 已提交
943
        INIT_LOGE("DoMakedevice: invalid arugments");
Z
zhong_ning 已提交
944 945 946 947 948 949
        goto out;
    }
    unsigned int major = strtoul(ctx->argv[0], NULL, decimal);
    unsigned int minor = strtoul(ctx->argv[1], NULL, decimal);
    dev_t deviceId = makedev(major, minor);
    if (deviceId < 0) {
Z
zhong_ning 已提交
950
        INIT_LOGE("DoMakedevice \" %s \" failed :%d ", cmdContent, errno);
Z
zhong_ning 已提交
951 952 953 954 955 956 957 958
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}
#endif // __LITEOS__

W
wenjun 已提交
959 960
void DoCmd(const CmdLine* curCmd)
{
Z
zhong_ning 已提交
961 962
    // null curCmd or empty command, just quit.
    if (curCmd == NULL || curCmd->name[0] == '\0') {
W
wenjun 已提交
963 964
        return;
    }
Z
zhong_ning 已提交
965 966
    DoCmdByName(curCmd->name, curCmd->cmdContent);
}
W
wenjun 已提交
967

Z
zhong_ning 已提交
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
void DoCmdByName(const char *name, const char *cmdContent)
{
    if (name == NULL || cmdContent == NULL) {
        return;
    }
    if (strncmp(name, "start ", strlen("start ")) == 0) {
        DoStart(cmdContent);
    } else if (strncmp(name, "mkdir ", strlen("mkdir ")) == 0) {
        DoMkDir(cmdContent);
    } else if (strncmp(name, "stop ", strlen("stop ")) == 0) {
        DoStop(cmdContent);
    } else if (strncmp(name, "reset ", strlen("reset ")) == 0) {
        DoReset(cmdContent);
    } else if (strncmp(name, "copy ", strlen("copy ")) == 0) {
        DoCopy(cmdContent);
    } else if (strncmp(name, "chmod ", strlen("chmod ")) == 0) {
        DoChmod(cmdContent);
    } else if (strncmp(name, "chown ", strlen("chown ")) == 0) {
        DoChown(cmdContent);
    } else if (strncmp(name, "mount ", strlen("mount ")) == 0) {
        DoMount(cmdContent);
    } else if (strncmp(name, "write ", strlen("write ")) == 0) {
        DoWrite(cmdContent);
    } else if (strncmp(name, "rmdir ", strlen("rmdir ")) == 0) {
        DoRmdir(cmdContent);
    } else if (strncmp(name, "rm ", strlen("rm ")) == 0) {
        DoRm(cmdContent);
    } else if (strncmp(name, "export ", strlen("export ")) == 0) {
        DoExport(cmdContent);
Z
zhong_ning 已提交
997 998
    } else if (strncmp(name, "setrlimit ", strlen("setrlimit ")) == 0) {
        DoSetrlimit(cmdContent);
Z
zhong_ning 已提交
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
    } else if (strncmp(name, "exec ", strlen("exec ")) == 0) {
        DoExec(cmdContent);
#ifndef __LITEOS__
    } else if (strncmp(name, "symlink ", strlen("symlink ")) == 0) {
        DoSymlink(cmdContent);
    } else if (strncmp(name, "makedev ", strlen("makedev ")) == 0) {
        DoMakeDevice(cmdContent);
    } else if (strncmp(name, "mknode ", strlen("mknode ")) == 0) {
        DoMakeNode(cmdContent);
#endif
    } else if (strncmp(name, "loadcfg ", strlen("loadcfg ")) == 0) {
        DoLoadCfg(cmdContent);
Z
zhong_ning 已提交
1011
#ifndef OHOS_LITE
Z
zhong_ning 已提交
1012 1013 1014
    } else if (strncmp(name, "insmod ", strlen("insmod ")) == 0) {
        DoInsmod(cmdContent);
    } else if (strncmp(name, "trigger ", strlen("trigger ")) == 0) {
Z
zhong_ning 已提交
1015
        INIT_LOGD("ready to trigger job: %s", name);
Z
zhong_ning 已提交
1016
        DoTriggerExec(cmdContent);
Z
zhong_ning 已提交
1017 1018
    } else if (strncmp(name, "load_persist_params ", strlen("load_persist_params ")) == 0) {
        LoadPersistParams();
Z
zhong_ning 已提交
1019 1020
    } else if (strncmp(name, "setparam ", strlen("setparam ")) == 0) {
        DoSetParam(cmdContent);
Z
zhong_ning 已提交
1021 1022
    } else if (strncmp(name, "load_param ", strlen("load_param ")) == 0) {
        LoadDefaultParams(cmdContent);
Z
zhong_ning 已提交
1023
#endif
Z
zhong_ning 已提交
1024 1025 1026
    } else if (strncmp(name, "reboot ", strlen("reboot ")) == 0) {
        DoReboot(cmdContent);
    }  else {
Z
zhong_ning 已提交
1027
        INIT_LOGE("DoCmd, unknown cmd name %s.", name);
W
wenjun 已提交
1028 1029
    }
}
M
mamingshuai 已提交
1030

Z
zhong_ning 已提交
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
const char *GetMatchCmd(const char *cmdStr)
{
    if (cmdStr == NULL) {
        return NULL;
    }
    size_t supportCmdCnt = sizeof(g_supportedCmds) / sizeof(g_supportedCmds[0]);
    for (size_t i = 0; i < supportCmdCnt; ++i) {
        size_t curCmdNameLen = strlen(g_supportedCmds[i]);
        if (strncmp(g_supportedCmds[i], cmdStr, curCmdNameLen) == 0) {
            return g_supportedCmds[i];
        }
    }
    return NULL;
}