init_cmds.c 27.3 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 25
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/stat.h>
Z
zhong_ning 已提交
26 27
#include <sys/sysmacros.h>
#include <sys/wait.h>
W
wenjun 已提交
28
#include <unistd.h>
Z
zhong_ning 已提交
29
#include <fcntl.h>
Z
zhong_ning 已提交
30
#ifndef OHOS_LITE
Z
zhong_ning 已提交
31 32
#include <linux/module.h>
#endif
Z
zhong_ning 已提交
33 34 35
#include <sys/syscall.h>
#include "init_jobs.h"
#include "init_log.h"
Z
zhong_ning 已提交
36
#include "init_reboot.h"
W
wenjun 已提交
37
#include "init_service_manager.h"
Z
zhong_ning 已提交
38
#include "init_utils.h"
W
wenjun 已提交
39
#include "securec.h"
Z
zhong_ning 已提交
40
#ifndef OHOS_LITE
Z
zhong_ning 已提交
41
#include "init_param.h"
Z
zhong_ning 已提交
42
#endif
W
wenjun 已提交
43 44 45 46 47

#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 已提交
48 49 50
#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 已提交
51
#define OCTAL_TYPE 8  // 8 means octal to decimal
Z
zhong_ning 已提交
52
#define MAX_BUFFER 256
Z
zhong_ning 已提交
53
#define AUTHORITY_MAX_SIZE 128
Z
zhong_ning 已提交
54
#define WAIT_MAX_COUNT 10
Z
zhong_ning 已提交
55

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

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

void ParseCmdLine(const char* cmdStr, CmdLine* resCmd)
{
89 90
    size_t cmdLineLen = 0;
    if (cmdStr == NULL || resCmd == NULL || (cmdLineLen = strlen(cmdStr)) == 0) {
W
wenjun 已提交
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 116
        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 已提交
117
        INIT_LOGE("Cannot parse command: %s\n", cmdStr);
W
wenjun 已提交
118 119 120 121 122 123
        (void)memset_s(resCmd, sizeof(*resCmd), 0, sizeof(*resCmd));
    }
}

static void DoStart(const char* cmdContent)
{
Z
zhong_ning 已提交
124
    INIT_LOGD("DoStart %s \n", cmdContent);
W
wenjun 已提交
125 126 127
    StartServiceByName(cmdContent);
}

Z
zhong_ning 已提交
128
static void DoStop(const char* cmdContent)
W
wenjun 已提交
129
{
Z
zhong_ning 已提交
130
    INIT_LOGD("DoStop %s \n", cmdContent);
Z
zhong_ning 已提交
131
    StopServiceByName(cmdContent);
W
wenjun 已提交
132 133
}

Z
zhong_ning 已提交
134
static void DoReset(const char* cmdContent)
W
wenjun 已提交
135
{
Z
zhong_ning 已提交
136
    INIT_LOGD("DoReset %s \n", cmdContent);
Z
zhong_ning 已提交
137 138 139
    DoStop(cmdContent);
    DoStart(cmdContent);
}
W
wenjun 已提交
140

Z
zhong_ning 已提交
141 142 143 144 145 146 147 148 149 150 151
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 已提交
152
        INIT_LOGE("DoCopy failed.\n");
Z
zhong_ning 已提交
153 154 155
        goto out;
    }
    srcFd = open(ctx->argv[0], O_RDONLY);
Z
zhong_ning 已提交
156 157
    INIT_ERROR_CHECK(srcFd >= 0, goto out, "copy open %s fail %d! \n", ctx->argv[0], errno);
    INIT_ERROR_CHECK(stat(ctx->argv[0], &fileStat) == 0, goto out, "stat fail \n");
Z
zhong_ning 已提交
158 159
    mode = fileStat.st_mode;
    dstFd = open(ctx->argv[1], O_WRONLY | O_TRUNC | O_CREAT, mode);
Z
zhong_ning 已提交
160
    INIT_ERROR_CHECK(dstFd >= 0, goto out, "copy open %s fail %d! \n", ctx->argv[1], errno);
Z
zhong_ning 已提交
161 162
    while ((rdLen = read(srcFd, buf, sizeof(buf) - 1)) > 0) {
        rtLen = write(dstFd, buf, rdLen);
Z
zhong_ning 已提交
163
        INIT_ERROR_CHECK(rtLen == rdLen, goto out, "write %s file fail %d! \n", ctx->argv[1], errno);
Z
zhong_ning 已提交
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    }
    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 已提交
181
        INIT_LOGE("DoChown failed.\n");
Z
zhong_ning 已提交
182 183 184 185 186 187 188
        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 已提交
189
        INIT_ERROR_CHECK(owner != (uid_t)-1, goto out, "DoChown decode owner failed.\n");
Z
zhong_ning 已提交
190 191
    } else {
        owner = strtoul(ctx->argv[0], NULL, 0);
W
wenjun 已提交
192 193
    }

Z
zhong_ning 已提交
194 195
    if (isalpha(ctx->argv[1][0])) {
        group = DecodeUid(ctx->argv[1]);
Z
zhong_ning 已提交
196
        INIT_ERROR_CHECK(group != (gid_t)-1, goto out, "DoChown decode group failed.\n");
Z
zhong_ning 已提交
197 198
    } else {
        group = strtoul(ctx->argv[1], NULL, 0);
W
wenjun 已提交
199 200
    }

Z
zhong_ning 已提交
201 202
    int pathPos = 2;
    if (chown(ctx->argv[pathPos], owner, group) != 0) {
Z
zhong_ning 已提交
203
        INIT_LOGE("DoChown, failed for %s, err %d.\n", cmdContent, errno);
W
wenjun 已提交
204
    }
Z
zhong_ning 已提交
205 206 207
out:
    FreeCmd(&ctx);
    return;
W
wenjun 已提交
208 209
}

Z
zhong_ning 已提交
210
static void DoMkDir(const char* cmdContent)
W
wenjun 已提交
211
{
Z
zhong_ning 已提交
212 213 214
    // 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 已提交
215
        INIT_LOGE("DoMkDir failed.\n");
Z
zhong_ning 已提交
216 217 218 219 220 221 222 223 224
        goto out;
    }

    mode_t mode = DEFAULT_DIR_MODE;
    for (size_t i = 0; i < strlen(ctx->argv[0]); ++i) {
        if (ctx->argv[0][i] == '/') {
            ctx->argv[0][i] = '\0';
            if (access(ctx->argv[0], 0) != 0 ) {
                mkdir(ctx->argv[0], mode);
M
mamingshuai 已提交
225
            }
Z
zhong_ning 已提交
226
            ctx->argv[0][i]='/';
W
wenjun 已提交
227 228
        }
    }
Z
zhong_ning 已提交
229 230
    if (access(ctx->argv[0], 0) != 0) {
        if (mkdir(ctx->argv[0], mode) != 0 && errno != EEXIST) {
Z
zhong_ning 已提交
231
            INIT_LOGE("DoMkDir %s failed, err %d.\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
232 233
            goto out;
        }
W
wenjun 已提交
234 235
    }

Z
zhong_ning 已提交
236 237 238
    if (ctx->argc > 1) {
        mode = strtoul(ctx->argv[1], NULL, OCTAL_TYPE);
        if (chmod(ctx->argv[0], mode) != 0) {
Z
zhong_ning 已提交
239
            INIT_LOGE("DoMkDir failed for %s, err %d.\n", cmdContent, errno);
Z
zhong_ning 已提交
240 241 242 243 244 245
        }
        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 已提交
246
            INIT_LOGE("DoMkDir snprintf failed.\n");
Z
zhong_ning 已提交
247
            goto out;
W
wenjun 已提交
248
        }
Z
zhong_ning 已提交
249 250 251 252 253 254 255 256 257 258 259 260
        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 已提交
261
        INIT_LOGE("DoChmod failed.\n");
Z
zhong_ning 已提交
262
        goto out;
W
wenjun 已提交
263 264
    }

Z
zhong_ning 已提交
265 266
    mode_t mode = strtoul(ctx->argv[0], NULL, OCTAL_TYPE);
    if (mode == 0) {
Z
zhong_ning 已提交
267
        INIT_LOGE("DoChmod, strtoul failed for %s, er %d.\n", cmdContent, errno);
Z
zhong_ning 已提交
268 269 270 271
        goto out;
    }

    if (chmod(ctx->argv[1], mode) != 0) {
Z
zhong_ning 已提交
272
        INIT_LOGE("DoChmod, failed for %s, err %d.\n", cmdContent, errno);
W
wenjun 已提交
273
    }
Z
zhong_ning 已提交
274 275 276
out:
    FreeCmd(&ctx);
    return;
W
wenjun 已提交
277 278 279 280 281
}

static char* CopySubStr(const char* srcStr, size_t startPos, size_t endPos)
{
    if (endPos <= startPos) {
Z
zhong_ning 已提交
282
        INIT_LOGE("DoMount, invalid params<%zu, %zu> for %s.\n", endPos, startPos, srcStr);
W
wenjun 已提交
283 284 285 286 287 288
        return NULL;
    }

    size_t mallocLen = endPos - startPos + 1;
    char* retStr = (char*)malloc(mallocLen);
    if (retStr == NULL) {
Z
zhong_ning 已提交
289
        INIT_LOGE("DoMount, malloc failed! malloc size %zu, for %s.\n", mallocLen, srcStr);
W
wenjun 已提交
290 291 292 293 294
        return NULL;
    }

    const char* copyStart = srcStr + startPos;
    if (memcpy_s(retStr, mallocLen, copyStart, endPos - startPos) != EOK) {
Z
zhong_ning 已提交
295
        INIT_LOGE("DoMount, memcpy_s failed for %s.\n", srcStr);
W
wenjun 已提交
296 297 298 299 300 301 302 303 304 305 306 307
        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 已提交
308
static int GetMountFlag(unsigned long* mountflags, const char* targetStr, const char *source)
W
wenjun 已提交
309 310 311 312 313 314 315
{
    if (targetStr == NULL) {
        return 0;
    }

    if (strncmp(targetStr, "nodev", strlen("nodev")) == 0) {
        (*mountflags) |= MS_NODEV;
G
Gymee 已提交
316
    } else if (strncmp(targetStr, "noexec", strlen("noexec")) == 0) {
W
wenjun 已提交
317
        (*mountflags) |= MS_NOEXEC;
G
Gymee 已提交
318
    } else if (strncmp(targetStr, "nosuid", strlen("nosuid")) == 0) {
W
wenjun 已提交
319
        (*mountflags) |= MS_NOSUID;
G
Gymee 已提交
320
    } else if (strncmp(targetStr, "rdonly", strlen("rdonly")) == 0) {
W
wenjun 已提交
321
        (*mountflags) |= MS_RDONLY;
Z
zhong_ning 已提交
322 323 324
    } else if (strncmp(targetStr, "noatime", strlen("noatime")) == 0) {
        (*mountflags) |= MS_NOATIME;
    } else if (strncmp(targetStr, "wait", strlen("wait")) == 0) {
Z
zhong_ning 已提交
325
        WaitForFile(source, WAIT_MAX_COUNT);
G
Gymee 已提交
326 327
    } else {
        return 0;
W
wenjun 已提交
328
    }
G
Gymee 已提交
329
    return 1;
W
wenjun 已提交
330 331 332 333
}

static int CountSpaces(const char* cmdContent, size_t* spaceCnt, size_t* spacePosArr, size_t spacePosArrLen)
{
M
mamingshuai 已提交
334 335
    *spaceCnt = 0;
    size_t strLen = strlen(cmdContent);
W
wenjun 已提交
336
    for (size_t i = 0; i < strLen; ++i) {
M
mamingshuai 已提交
337 338 339
        if (cmdContent[i] == ' ') {
            ++(*spaceCnt);
            if ((*spaceCnt) > spacePosArrLen) {
Z
zhong_ning 已提交
340
                INIT_LOGE("DoMount, too many spaces, bad format for %s.\n", cmdContent);
M
mamingshuai 已提交
341 342 343
                return 0;
            }
            spacePosArr[(*spaceCnt) - 1] = i;
W
wenjun 已提交
344 345 346
        }
    }

M
mamingshuai 已提交
347 348 349
    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 已提交
350
        INIT_LOGE("DoMount, bad format for %s.\n", cmdContent);
W
wenjun 已提交
351 352 353 354
        return 0;
    }

    // spaces should not be adjacent
M
mamingshuai 已提交
355 356
    for (size_t i = 1; i < (*spaceCnt); ++i) {
        if (spacePosArr[i] == spacePosArr[i - 1] + 1) {
Z
zhong_ning 已提交
357
            INIT_LOGE("DoMount, bad format for %s.\n", cmdContent);
W
wenjun 已提交
358 359 360 361 362 363 364 365 366 367 368 369 370 371
            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;
    }

372 373 374
    // format: fileSystemType source target mountFlag1 mountFlag2... data
    unsigned long mountflags = 0;
    size_t strLen = strlen(cmdContent);
W
wenjun 已提交
375 376
    size_t indexOffset = 0;
    char* fileSysType = CopySubStr(cmdContent, 0, spacePosArr[indexOffset]);
M
mamingshuai 已提交
377 378 379 380
    if (fileSysType == NULL) {
        return;
    }

W
wenjun 已提交
381
    char* source = CopySubStr(cmdContent, spacePosArr[indexOffset] + 1, spacePosArr[indexOffset + 1]);
M
mamingshuai 已提交
382 383 384 385
    if (source == NULL) {
        free(fileSysType);
        return;
    }
W
wenjun 已提交
386 387 388 389 390
    ++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 已提交
391 392 393 394 395
    if (target == NULL) {
        free(fileSysType);
        free(source);
        return;
    }
W
wenjun 已提交
396 397 398 399 400 401
    ++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 已提交
402
        int ret = GetMountFlag(&mountflags, tmpStr, source);
W
wenjun 已提交
403 404 405 406 407 408 409 410 411 412
        free(tmpStr);
        tmpStr = NULL;

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

M
mamingshuai 已提交
413 414 415
    int mountRet;
    if (indexOffset >= spaceCnt) {    // no data
        mountRet = mount(source, target, fileSysType, mountflags, NULL);
W
wenjun 已提交
416
    } else {
M
mamingshuai 已提交
417 418 419 420 421
        const char* dataStr = cmdContent + spacePosArr[indexOffset] + 1;
        mountRet = mount(source, target, fileSysType, mountflags, dataStr);
    }

    if (mountRet != 0) {
Z
zhong_ning 已提交
422
        INIT_LOGE("DoMount, failed for %s, err %d.\n", cmdContent, errno);
W
wenjun 已提交
423 424 425 426 427 428 429
    }

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

Z
zhong_ning 已提交
430
#ifndef OHOS_LITE
L
leon 已提交
431
#define OPTIONS_SIZE 128u
L
leon 已提交
432 433 434 435 436 437
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 已提交
438
                return;
L
leon 已提交
439
            }
L
leon 已提交
440
        } else if (secondPtr != NULL) {
L
leon 已提交
441
            if (strncpy_s(options, OPTIONS_SIZE - 1, secondPtr, strlen(secondPtr)) != 0) {
Z
zhong_ning 已提交
442
                return;
L
leon 已提交
443 444 445 446
            }
        }
    } else { // Only restPtr is option
        if (restPtr != NULL) {
Z
zhong_ning 已提交
447
            if (strncpy_s(options, OPTIONS_SIZE - 1, restPtr, strlen(restPtr)) != 0) {
Z
zhong_ning 已提交
448
                return;
Z
zhong_ning 已提交
449
            }
L
leon 已提交
450 451
        }
    }
Z
zhong_ning 已提交
452
    if (!fileName) {
Z
zhong_ning 已提交
453
        return;
Z
zhong_ning 已提交
454
    }
Z
zhong_ning 已提交
455
    char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char));
Z
zhong_ning 已提交
456 457 458
    if (realPath == NULL) {
        return;
    }
Z
zhong_ning 已提交
459
    realPath = realpath(fileName, realPath);
Z
zhong_ning 已提交
460
    int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
L
leon 已提交
461
    if (fd < 0) {
Z
zhong_ning 已提交
462
        INIT_LOGE("failed to open %s: %d\n", realPath, errno);
Z
zhong_ning 已提交
463
        free(realPath);
Z
zhong_ning 已提交
464
        return;
L
leon 已提交
465 466 467
    }
    int rc = syscall(__NR_finit_module, fd, options, flags);
    if (rc == -1) {
Z
zhong_ning 已提交
468
        INIT_LOGE("finit_module for %s failed: %d\n", realPath, errno);
L
leon 已提交
469
    }
Z
zhong_ning 已提交
470
    if (fd >= 0) {
L
leon 已提交
471 472
        close(fd);
    }
Z
zhong_ning 已提交
473
    free(realPath);
L
leon 已提交
474 475 476
    return;
}

Z
zhong_ning 已提交
477 478 479 480 481 482
// format insmod <ko name> [-f] [options]
static void DoInsmod(const char *cmdContent)
{
    char *p = NULL;
    char *restPtr = NULL;
    char *fileName = NULL;
L
leon 已提交
483
    char *line = NULL;
Z
zhong_ning 已提交
484 485 486 487
    int flags = 0;

    size_t count = strlen(cmdContent);
    if (count > OPTIONS_SIZE) {
Z
zhong_ning 已提交
488
        INIT_LOGE("DoInsmod options too long, maybe lost some of options\n");
Z
zhong_ning 已提交
489 490 491
    }
    line = (char *)malloc(count + 1);
    if (line == NULL) {
Z
zhong_ning 已提交
492
        INIT_LOGE("DoInsmod allocate memory failed.\n");
Z
zhong_ning 已提交
493 494
        return;
    }
L
leon 已提交
495

Z
zhong_ning 已提交
496
    if (memcpy_s(line, count, cmdContent, count) != EOK) {
Z
zhong_ning 已提交
497
        INIT_LOGE("DoInsmod memcpy failed\n");
Z
zhong_ning 已提交
498
        free(line);
L
leon 已提交
499
        return;
Z
zhong_ning 已提交
500 501 502 503
    }
    line[count] = '\0';
    do {
        if ((p = strtok_r(line, " ", &restPtr)) == NULL) {
Z
zhong_ning 已提交
504
            INIT_LOGE("DoInsmod cannot get filename.\n");
Z
zhong_ning 已提交
505
            free(line);
Z
zhong_ning 已提交
506 507 508
            return;
        }
        fileName = p;
Z
zhong_ning 已提交
509
        INIT_LOGI("DoInsmod fileName is [%s].\n", fileName);
Z
zhong_ning 已提交
510 511 512 513 514 515 516
        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 已提交
517
    DoInsmodInternal(fileName, p, restPtr, flags);
Z
zhong_ning 已提交
518 519 520
    if (line != NULL) {
        free(line);
    }
L
leon 已提交
521
    return;
Z
zhong_ning 已提交
522
}
Z
zhong_ning 已提交
523 524 525 526 527

static void DoSetParam(const char* cmdContent)
{
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 2) {
Z
zhong_ning 已提交
528
        INIT_LOGE("DoSetParam failed.\n");
Z
zhong_ning 已提交
529 530
        goto out;
    }
Z
zhong_ning 已提交
531 532
    INIT_LOGE("param name: %s, value %s \n", ctx->argv[0], ctx->argv[1]);
    SystemWriteParam(ctx->argv[0], ctx->argv[1]);
Z
zhong_ning 已提交
533 534 535 536 537
out:
    FreeCmd(&ctx);
    return;
}

L
leon 已提交
538
#endif // OHOS_LITE
Z
zhong_ning 已提交
539

M
mamingshuai 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
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 已提交
570
    INIT_LOGI("DoLoadCfg cfg file %s\n", path);
M
mamingshuai 已提交
571
    if (!CheckValidCfg(path)) {
Z
zhong_ning 已提交
572
        INIT_LOGE("CheckCfg file %s Failed\n", path);
M
mamingshuai 已提交
573 574 575 576 577
        return;
    }

    fp = fopen(path, "r");
    if (fp == NULL) {
Z
zhong_ning 已提交
578
        INIT_LOGE("open cfg error = %d\n", errno);
M
mamingshuai 已提交
579 580 581 582 583
        return;
    }

    cmdLine = (CmdLine *)malloc(sizeof(CmdLine));
    if (cmdLine == NULL) {
Z
zhong_ning 已提交
584
        INIT_LOGE("malloc cmdline error");
M
mamingshuai 已提交
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        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 已提交
608 609 610 611 612 613
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 已提交
614
        INIT_LOGE("DoWrite: invalid arguments\n");
Z
zhong_ning 已提交
615 616 617 618 619 620
        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 已提交
621
        INIT_LOGE("DoWrite: open %s failed: %d\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
622 623 624 625 626
        goto out;
    }

    size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1]));
    if (ret < 0) {
Z
zhong_ning 已提交
627
        INIT_LOGE("DoWrite: write to file %s failed: %d\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
628 629 630 631 632 633 634 635 636 637 638 639 640 641
        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 已提交
642
        INIT_LOGE("DoRmdir: invalid arguments\n");
Z
zhong_ning 已提交
643 644 645 646 647
        goto out;
    }

    int ret = rmdir(ctx->argv[0]);
    if (ret == -1) {
Z
zhong_ning 已提交
648
        INIT_LOGE("DoRmdir: remove %s failed: %d.\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
649 650 651 652 653 654 655 656 657 658 659 660
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

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 已提交
661
        INIT_LOGE("DoRm: invalid arguments\n");
Z
zhong_ning 已提交
662 663 664 665
        goto out;
    }
    int ret = unlink(ctx->argv[0]);
    if (ret == -1) {
Z
zhong_ning 已提交
666
        INIT_LOGE("DoRm: unlink %s failed: %d.\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
667 668 669 670 671 672 673 674 675 676 677 678
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}

static void DoExport(const char *cmdContent)
{
    // format: export xxx /xxx/xxx/xxx
    struct CmdArgs *ctx = GetCmd(cmdContent, " ");
    if (ctx == NULL || ctx->argv == NULL || ctx->argc != 2) {
Z
zhong_ning 已提交
679
        INIT_LOGE("DoExport: invalid arguments\n");
Z
zhong_ning 已提交
680 681 682 683
        goto out;
    }
    int ret = setenv(ctx->argv[0], ctx->argv[1], 1);
    if (ret != 0) {
Z
zhong_ning 已提交
684
        INIT_LOGE("DoExport: set %s with %s failed: %d\n", ctx->argv[0], ctx->argv[1], errno);
Z
zhong_ning 已提交
685 686 687 688 689 690 691 692 693 694 695
        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 已提交
696 697 698 699
    if (pid < 0) {
        INIT_LOGE("DoExec: failed to fork child process to exec \"%s\"\n", cmdContent);
        return;
    }
Z
zhong_ning 已提交
700
    if (pid == 0) {
Z
zhong_ning 已提交
701 702 703 704 705
        struct CmdArgs *ctx = GetCmd(cmdContent, " ");
        if (ctx == NULL || ctx->argv == NULL) {
            INIT_LOGE("DoExec: invalid arguments\n");
            _exit(0x7f);
        }
Z
zhong_ning 已提交
706 707 708 709 710 711
#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 已提交
712
            INIT_LOGE("DoExec: execute \"%s\" failed: %d.\n", cmdContent, errno);
Z
zhong_ning 已提交
713
        }
Z
zhong_ning 已提交
714 715
        FreeCmd(&ctx);
        _exit(0x7f);
Z
zhong_ning 已提交
716 717 718 719 720 721 722 723 724 725 726
    }
    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 已提交
727
        INIT_LOGE("DoSymlink: invalid arguments.\n");
Z
zhong_ning 已提交
728 729 730 731 732
        goto out;
    }

    int ret = symlink(ctx->argv[0], ctx->argv[1]);
    if (ret != 0) {
Z
zhong_ning 已提交
733
        INIT_LOGE("DoSymlink: link %s to %s failed: %d\n", ctx->argv[0], ctx->argv[1], errno);
Z
zhong_ning 已提交
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
        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 已提交
770
        INIT_LOGE("DoMakeNode: invalid arguments\n");
Z
zhong_ning 已提交
771 772 773 774
        goto out;
    }

    if (!access(ctx->argv[1], F_OK)) {
Z
zhong_ning 已提交
775
        INIT_LOGE("DoMakeNode failed, path has not sexisted\n");
Z
zhong_ning 已提交
776 777 778 779 780 781 782 783 784
        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 已提交
785
        INIT_LOGE("DoMakeNode: path: %s failed: %d\n", ctx->argv[0], errno);
Z
zhong_ning 已提交
786 787 788 789 790 791 792 793 794 795 796 797 798 799
        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 已提交
800
        INIT_LOGE("DoMakedevice: invalid arugments\n");
Z
zhong_ning 已提交
801 802 803 804 805 806
        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 已提交
807
        INIT_LOGE("DoMakedevice \" %s \" failed :%d \n", cmdContent, errno);
Z
zhong_ning 已提交
808 809 810 811 812 813 814 815
        goto out;
    }
out:
    FreeCmd(&ctx);
    return;
}
#endif // __LITEOS__

W
wenjun 已提交
816 817
void DoCmd(const CmdLine* curCmd)
{
Z
zhong_ning 已提交
818 819
    // null curCmd or empty command, just quit.
    if (curCmd == NULL || curCmd->name[0] == '\0') {
W
wenjun 已提交
820 821
        return;
    }
Z
zhong_ning 已提交
822 823 824
    // INIT_LOGE("curCmd->name:%s, curCmd->cmdContent:%s\n", curCmd->name, curCmd->cmdContent);
    DoCmdByName(curCmd->name, curCmd->cmdContent);
}
W
wenjun 已提交
825

Z
zhong_ning 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
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);
    } 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 已提交
867
#ifndef OHOS_LITE
Z
zhong_ning 已提交
868 869 870
    } else if (strncmp(name, "insmod ", strlen("insmod ")) == 0) {
        DoInsmod(cmdContent);
    } else if (strncmp(name, "trigger ", strlen("trigger ")) == 0) {
Z
zhong_ning 已提交
871
        INIT_LOGD("ready to trigger job: %s\n", name);
Z
zhong_ning 已提交
872
        DoTriggerExec(cmdContent);
Z
zhong_ning 已提交
873 874
    } else if (strncmp(name, "load_persist_params ", strlen("load_persist_params ")) == 0) {
        LoadPersistParams();
Z
zhong_ning 已提交
875 876
    } else if (strncmp(name, "setparam ", strlen("setparam ")) == 0) {
        DoSetParam(cmdContent);
Z
zhong_ning 已提交
877 878
    } else if (strncmp(name, "load_param ", strlen("load_param ")) == 0) {
        LoadDefaultParams(cmdContent);
Z
zhong_ning 已提交
879
#endif
Z
zhong_ning 已提交
880 881 882 883
    } else if (strncmp(name, "reboot ", strlen("reboot ")) == 0) {
        DoReboot(cmdContent);
    }  else {
        INIT_LOGE("DoCmd, unknown cmd name %s.\n", name);
W
wenjun 已提交
884 885
    }
}
M
mamingshuai 已提交
886

Z
zhong_ning 已提交
887 888 889 890 891 892 893 894 895 896 897 898 899 900
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;
}