init_service_manager.c 25.8 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
 * 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.
 */

#include "init_service_manager.h"
#include <ctype.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>

#include "cJSON.h"
#include "init_adapter.h"
#include "init_jobs.h"
#include "init_log.h"
#include "init_perms.h"
#include "init_read_cfg.h"
#include "init_capability.h"
#include "init_service_socket.h"
#include "init_utils.h"
#include "securec.h"

// All serivce processes that init will fork+exec.
static Service* g_services = NULL;
static int g_servicesCnt = 0;

void DumpAllServices()
{
Z
zhong_ning 已提交
43 44
    INIT_LOGD("Ready to dump all services:\n");
    INIT_LOGD("total service number: %d\n", g_servicesCnt);
Z
zhong_ning 已提交
45
    for (int i = 0; i < g_servicesCnt; i++) {
Z
zhong_ning 已提交
46 47
        INIT_LOGD("\tservice name: [%s]\n", g_services[i].name);
        INIT_LOGD("\tpath :");
Z
zhong_ning 已提交
48
        for (int j = 0; j < g_services[i].pathArgsCnt; j++) {
Z
zhong_ning 已提交
49
            INIT_LOGD(" %s", g_services[i].pathArgs[j]);
Z
zhong_ning 已提交
50
        }
Z
zhong_ning 已提交
51
        INIT_LOGD("\n");
Z
zhong_ning 已提交
52
    }
Z
zhong_ning 已提交
53
    INIT_LOGD("Dump all services finished\n");
Z
zhong_ning 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 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
}

void RegisterServices(Service* services, int servicesCnt)
{
    g_services = services;
    g_servicesCnt += servicesCnt;
}

static void ReleaseServiceMem(Service* curServ)
{
    if (curServ->pathArgs != NULL) {
        for (int i = 0; i < curServ->pathArgsCnt; ++i) {
            if (curServ->pathArgs[i] != NULL) {
                free(curServ->pathArgs[i]);
                curServ->pathArgs[i] = NULL;
            }
        }
        free(curServ->pathArgs);
        curServ->pathArgs = NULL;
    }
    curServ->pathArgsCnt = 0;

    if (curServ->servPerm.caps != NULL) {
        free(curServ->servPerm.caps);
        curServ->servPerm.caps = NULL;
    }
    curServ->servPerm.capsCnt = 0;

    for (int i = 0; i < MAX_WRITEPID_FILES; i++) {
        if (curServ->writepidFiles[i] != NULL) {
            free(curServ->writepidFiles[i]);
            curServ->writepidFiles[i] = NULL;
        }
    }

    if (curServ->servPerm.gIDArray != NULL) {
        free(curServ->servPerm.gIDArray);
        curServ->servPerm.gIDArray = NULL;
    }
    curServ->servPerm.gIDCnt = 0;
}

static int GetServiceName(const cJSON* curArrItem, Service* curServ)
{
    char* fieldStr = cJSON_GetStringValue(cJSON_GetObjectItem(curArrItem, "name"));
    if (fieldStr == NULL) {
Z
zhong_ning 已提交
100
        INIT_LOGE("GetServiceName cJSON_GetStringValue error\n");
Z
zhong_ning 已提交
101 102 103 104 105
        return SERVICE_FAILURE;
    }

    size_t strLen = strlen(fieldStr);
    if (strLen == 0 || strLen > MAX_SERVICE_NAME) {
Z
zhong_ning 已提交
106
        INIT_LOGE("GetServiceName strLen = %d, error\n", strLen);
Z
zhong_ning 已提交
107 108 109 110
        return SERVICE_FAILURE;
    }

    if (memcpy_s(curServ->name, MAX_SERVICE_NAME, fieldStr, strLen) != EOK) {
Z
zhong_ning 已提交
111
        INIT_LOGE("GetServiceName memcpy_s error\n");
Z
zhong_ning 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
        return SERVICE_FAILURE;
    }
    curServ->name[strLen] = '\0';
    return SERVICE_SUCCESS;
}

static int IsForbidden(const char* fieldStr)
{
    size_t fieldLen = strlen(fieldStr);
    size_t forbidStrLen = strlen(BIN_SH_NOT_ALLOWED);
    if (fieldLen == forbidStrLen) {
        if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, fieldLen) == 0) {
            return 1;
        }
        return 0;
    } else if (fieldLen > forbidStrLen) {
        // "/bin/shxxxx" is valid but "/bin/sh xxxx" is invalid
        if (strncmp(fieldStr, BIN_SH_NOT_ALLOWED, forbidStrLen) == 0) {
            if (fieldStr[forbidStrLen] == ' ') {
                return 1;
            }
        }
        return 0;
    } else {
        return 0;
    }
}

// TODO: move this function to common files
static cJSON* GetArrItem(const cJSON* fileRoot, int* arrSize, const char* arrName)
{
    cJSON* arrItem = cJSON_GetObjectItemCaseSensitive(fileRoot, arrName);
    if (!cJSON_IsArray(arrItem)) {
Z
zhong_ning 已提交
145
        INIT_LOGE("GetArrItem, item %s is not an array!\n", arrName);
Z
zhong_ning 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
        return NULL;
    }

    *arrSize = cJSON_GetArraySize(arrItem);
    if (*arrSize <= 0) {
        return NULL;
    }
    return arrItem;
}

static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ)        // writepid
{
    int writepidCnt = 0;
    cJSON* filedJ = GetArrItem(curArrItem, &writepidCnt, "writepid");
    if (writepidCnt <= 0) {             // not item is ok.
        return SERVICE_SUCCESS;
    }

    if (writepidCnt > MAX_WRITEPID_FILES) {
Z
zhong_ning 已提交
165
        INIT_LOGE("GetWritepidStrings, too many writepid[cnt %d] for one service, should not exceed %d.\n",
Z
zhong_ning 已提交
166 167 168 169 170 171 172
            writepidCnt, MAX_WRITEPID_FILES);
        return SERVICE_FAILURE;
    }

    for (int i = 0; i < writepidCnt; ++i) {
        if (!cJSON_GetArrayItem(filedJ, i) || !cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i))
            || strlen(cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i))) <= 0) {      // check all errors
Z
zhong_ning 已提交
173
            INIT_LOGE("GetWritepidStrings, parse item[%d] error.\n", i);
Z
zhong_ning 已提交
174 175 176 177 178 179 180
            return SERVICE_FAILURE;
        }

        char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i));
        size_t strLen = strlen(fieldStr);
        curServ->writepidFiles[i] = (char *)malloc(sizeof(char) * strLen + 1);
        if (curServ->writepidFiles[i] == NULL) {
Z
zhong_ning 已提交
181
            INIT_LOGE("GetWritepidStrings, malloc item[%d] error.\n", i);
Z
zhong_ning 已提交
182 183 184
            return SERVICE_FAILURE;
        }
        if (memcpy_s(curServ->writepidFiles[i], strLen + 1, fieldStr, strLen) != EOK) {
Z
zhong_ning 已提交
185
            INIT_LOGE("GetWritepidStrings, memcpy_s error.\n");
Z
zhong_ning 已提交
186 187 188 189 190 191 192 193 194 195 196 197
            return SERVICE_FAILURE;
        }
        curServ->writepidFiles[i][strLen] = '\0';
    }

    return SERVICE_SUCCESS;
}

static int GetGidOneItem(const cJSON *curArrItem, Service *curServ)        // gid one item
{
    cJSON* filedJ = cJSON_GetObjectItem(curArrItem, GID_STR_IN_CFG);
    if (filedJ == NULL) {
Z
zhong_ning 已提交
198 199
        INIT_LOGE("GetGidOneItem, gid is not found too, but ok.\n");
        return SERVICE_SUCCESS;             // not found
Z
zhong_ning 已提交
200 201 202 203
    }
    curServ->servPerm.gIDCnt = 1;
    curServ->servPerm.gIDArray = (gid_t *)malloc(sizeof(gid_t));
    if (curServ->servPerm.gIDArray == NULL) {
Z
zhong_ning 已提交
204
        INIT_LOGE("GetGidOneItem, can't malloc, error.\n");
Z
zhong_ning 已提交
205 206 207 208 209 210 211
        return SERVICE_FAILURE;
    }

    if (cJSON_IsString(filedJ)) {
        char* fieldStr = cJSON_GetStringValue(filedJ);
        gid_t gID = DecodeUid(fieldStr);
        if (gID == (gid_t)(-1)) {
Z
zhong_ning 已提交
212
            INIT_LOGE("GetGidOneItem, DecodeUid %s error.\n", fieldStr);
Z
zhong_ning 已提交
213 214 215 216 217 218 219 220 221
            return SERVICE_FAILURE;
        }
        curServ->servPerm.gIDArray[0] = gID;
        return SERVICE_SUCCESS;
    }

    if (cJSON_IsNumber(filedJ)) {
        gid_t gID = (int)cJSON_GetNumberValue(filedJ);
        if (gID < 0) {
Z
zhong_ning 已提交
222
            INIT_LOGE("GetGidOneItem, gID = %d error.\n", gID);
Z
zhong_ning 已提交
223 224 225 226 227 228
            return SERVICE_FAILURE;
        }
        curServ->servPerm.gIDArray[0] = gID;
        return SERVICE_SUCCESS;
    }

Z
zhong_ning 已提交
229
    INIT_LOGE("GetGidOneItem, this gid is neither a string nor a number, error.\n");
Z
zhong_ning 已提交
230 231 232 233 234 235 236 237
    return SERVICE_FAILURE;
}

static int GetGidArray(const cJSON *curArrItem, Service *curServ)        // gid array
{
    int gIDCnt = 0;
    cJSON* filedJ = GetArrItem(curArrItem, &gIDCnt, GID_STR_IN_CFG);        // "gid" must have 1 item.
    if (gIDCnt <= 0) {              // not a array, but maybe a item?
Z
zhong_ning 已提交
238
        INIT_LOGE("GetGidArray, gid is not a list.\n");
Z
zhong_ning 已提交
239 240 241 242
        return GetGidOneItem(curArrItem, curServ);
    }

    if (gIDCnt > NGROUPS_MAX + 1) {
Z
zhong_ning 已提交
243
        INIT_LOGE("GetGidArray, too many gids[cnt %d] for one service, should not exceed %d.\n",
Z
zhong_ning 已提交
244 245 246 247 248 249
            gIDCnt, NGROUPS_MAX + 1);
        return SERVICE_FAILURE;
    }

    curServ->servPerm.gIDArray = (gid_t *)malloc(sizeof(gid_t) * gIDCnt);
    if (curServ->servPerm.gIDArray == NULL) {
Z
zhong_ning 已提交
250
        INIT_LOGE("GetGidArray malloc error\n");
Z
zhong_ning 已提交
251 252 253 254 255 256 257
        return SERVICE_FAILURE;
    }
    curServ->servPerm.gIDCnt = gIDCnt;
    int i = 0;
    for (; i < gIDCnt; ++i) {
        if (cJSON_GetArrayItem(filedJ, i) == NULL || !cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i))
            || strlen(cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i))) <= 0) {      // check all errors
Z
zhong_ning 已提交
258
            INIT_LOGE("GetGidArray, parse item[%d] as string, error.\n", i);
Z
zhong_ning 已提交
259 260 261 262 263
            break;
        }
        char* fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i));
        gid_t gID = DecodeUid(fieldStr);
        if ((gID) == (gid_t)(-1)) {
Z
zhong_ning 已提交
264
            INIT_LOGE("GetGidArray, DecodeUid item[%d] error.\n", i);
Z
zhong_ning 已提交
265 266 267 268 269 270 271 272 273
            return SERVICE_FAILURE;
        }
        curServ->servPerm.gIDArray[i] = gID;
    }
    if (i == gIDCnt) {
        return SERVICE_SUCCESS;
    }
    for (i = 0; i < gIDCnt; ++i) {
        if (cJSON_GetArrayItem(filedJ, i) == NULL || !cJSON_IsNumber(cJSON_GetArrayItem(filedJ, i))) {
Z
zhong_ning 已提交
274
            INIT_LOGE("GetGidArray, parse item[%d] as number, error.\n", i);
Z
zhong_ning 已提交
275 276 277 278
            break;
        }
        gid_t gID = (int)cJSON_GetNumberValue(cJSON_GetArrayItem(filedJ, i));
        if (gID < 0) {
Z
zhong_ning 已提交
279
            INIT_LOGE("GetGidArray gID = %d, error\n", gID);
Z
zhong_ning 已提交
280 281 282 283 284 285 286 287 288 289 290 291
            break;
        }
        curServ->servPerm.gIDArray[i] = gID;
    }
    int ret = i == gIDCnt ? SERVICE_SUCCESS : SERVICE_FAILURE;
    return ret;
}

static int GetServicePathAndArgs(const cJSON* curArrItem, Service* curServ)
{
    cJSON* pathItem = cJSON_GetObjectItem(curArrItem, "path");
    if (!cJSON_IsArray(pathItem)) {
Z
zhong_ning 已提交
292
        INIT_LOGE("GetServicePathAndArgs path item not found or not a array\n");
Z
zhong_ning 已提交
293 294 295 296 297
        return SERVICE_FAILURE;
    }

    int arrSize = cJSON_GetArraySize(pathItem);
    if (arrSize <= 0 || arrSize > MAX_PATH_ARGS_CNT) {  // array size invalid
Z
zhong_ning 已提交
298
        INIT_LOGE("GetServicePathAndArgs arrSize = %d, error\n", arrSize);
Z
zhong_ning 已提交
299 300 301 302 303
        return SERVICE_FAILURE;
    }

    curServ->pathArgs = (char**)malloc((arrSize + 1) * sizeof(char*));
    if (curServ->pathArgs == NULL) {
Z
zhong_ning 已提交
304
        INIT_LOGE("GetServicePathAndArgs malloc 1 error\n");
Z
zhong_ning 已提交
305 306 307 308 309 310 311 312 313 314 315 316
        return SERVICE_FAILURE;
    }
    for (int i = 0; i < arrSize + 1; ++i) {
        curServ->pathArgs[i] = NULL;
    }
    curServ->pathArgsCnt = arrSize + 1;

    for (int i = 0; i < arrSize; ++i) {
        char* curParam = cJSON_GetStringValue(cJSON_GetArrayItem(pathItem, i));
        if (curParam == NULL || strlen(curParam) > MAX_ONE_ARG_LEN) {
            // resources will be released by function: ReleaseServiceMem
            if (curParam == NULL) {
Z
zhong_ning 已提交
317
                INIT_LOGE("GetServicePathAndArgs curParam == NULL, error\n");
Z
zhong_ning 已提交
318
            } else {
Z
zhong_ning 已提交
319
                INIT_LOGE("GetServicePathAndArgs strlen = %d, error\n", strlen(curParam));
Z
zhong_ning 已提交
320 321 322 323 324 325
            }
            return SERVICE_FAILURE;
        }

        if (i == 0 && IsForbidden(curParam)) {
            // resources will be released by function: ReleaseServiceMem
Z
zhong_ning 已提交
326
            INIT_LOGE("GetServicePathAndArgs i == 0 && IsForbidden, error\n");
Z
zhong_ning 已提交
327 328 329 330 331 332 333
            return SERVICE_FAILURE;
        }

        size_t paramLen = strlen(curParam);
        curServ->pathArgs[i] = (char*)malloc(paramLen + 1);
        if (curServ->pathArgs[i] == NULL) {
            // resources will be released by function: ReleaseServiceMem
Z
zhong_ning 已提交
334
            INIT_LOGE("GetServicePathAndArgs i == 0 && IsForbidden, error\n");
Z
zhong_ning 已提交
335 336 337 338 339
            return SERVICE_FAILURE;
        }

        if (memcpy_s(curServ->pathArgs[i], paramLen + 1, curParam, paramLen) != EOK) {
            // resources will be released by function: ReleaseServiceMem
Z
zhong_ning 已提交
340
            INIT_LOGE("GetServicePathAndArgs malloc 2 error.\n");
Z
zhong_ning 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
            return SERVICE_FAILURE;
        }
        curServ->pathArgs[i][paramLen] = '\0';
    }
    return SERVICE_SUCCESS;
}

static int GetServiceNumber(const cJSON* curArrItem, Service* curServ, const char* targetField)
{
    cJSON* filedJ = cJSON_GetObjectItem(curArrItem, targetField);
    if (filedJ == NULL && (strncmp(targetField, CRITICAL_STR_IN_CFG, strlen(CRITICAL_STR_IN_CFG)) == 0
        || strncmp(targetField, DISABLED_STR_IN_CFG, strlen(DISABLED_STR_IN_CFG)) == 0
        || strncmp(targetField, ONCE_STR_IN_CFG, strlen(ONCE_STR_IN_CFG)) == 0
        || strncmp(targetField, IMPORTANT_STR_IN_CFG, strlen(IMPORTANT_STR_IN_CFG)) == 0)) {
        return SERVICE_SUCCESS;             // not found "critical","disabled","once","importance" item is ok
    }

    if (!cJSON_IsNumber(filedJ)) {
Z
zhong_ning 已提交
359
        INIT_LOGE("GetServiceNumber, %s is null or is not a number, error.\n", targetField);
Z
zhong_ning 已提交
360 361 362 363
        return SERVICE_FAILURE;
    }

    int value = (int)cJSON_GetNumberValue(filedJ);
Z
zhong_ning 已提交
364 365 366
    // important value allow < 0
    if (strncmp(targetField, IMPORTANT_STR_IN_CFG, strlen(IMPORTANT_STR_IN_CFG)) != 0) {
        if (value < 0) {
Z
zhong_ning 已提交
367
            INIT_LOGE("GetServiceNumber, value = %d, error.\n", value);
Z
zhong_ning 已提交
368 369
            return SERVICE_FAILURE;
        }
Z
zhong_ning 已提交
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
    }

    if (strncmp(targetField, ONCE_STR_IN_CFG, strlen(ONCE_STR_IN_CFG)) == 0) {
        if (value != 0) {
            curServ->attribute |= SERVICE_ATTR_ONCE;
        }
    } else if (strncmp(targetField, IMPORTANT_STR_IN_CFG, strlen(IMPORTANT_STR_IN_CFG)) == 0) {
        if (value != 0) {
            curServ->attribute |= SERVICE_ATTR_IMPORTANT;
        }
    } else if (strncmp(targetField, CRITICAL_STR_IN_CFG, strlen(CRITICAL_STR_IN_CFG)) == 0) {       // set critical
        curServ->attribute &= ~SERVICE_ATTR_CRITICAL;
        if (value == 1) {
            curServ->attribute |= SERVICE_ATTR_CRITICAL;
        }
    } else if (strncmp(targetField, DISABLED_STR_IN_CFG, strlen(DISABLED_STR_IN_CFG)) == 0) {       // set disabled
        curServ->attribute &= ~SERVICE_ATTR_DISABLED;
        if (value == 1) {
            curServ->attribute |= SERVICE_ATTR_DISABLED;
        }
    } else {
Z
zhong_ning 已提交
391
        INIT_LOGE("GetServiceNumber, item = %s, not expected, error.\n", targetField);
Z
zhong_ning 已提交
392 393 394 395 396 397 398 399 400
        return SERVICE_FAILURE;
    }
    return SERVICE_SUCCESS;
}

static int GetUidStringNumber(const cJSON *curArrItem, Service *curServ)
{
    cJSON* filedJ = cJSON_GetObjectItem(curArrItem, UID_STR_IN_CFG);
    if (filedJ == NULL) {
Z
zhong_ning 已提交
401 402
        INIT_LOGE("GetUidStringNumber, %s not found, but ok.\n", UID_STR_IN_CFG);
        return SERVICE_SUCCESS;             // uID not found, but ok.
Z
zhong_ning 已提交
403 404 405 406 407 408
    }

    if (cJSON_IsString(filedJ)) {
        char* fieldStr = cJSON_GetStringValue(filedJ);
        int uID = DecodeUid(fieldStr);
        if (uID < 0) {
Z
zhong_ning 已提交
409
            INIT_LOGE("GetUidStringNumber, DecodeUid %s error.\n", fieldStr);
Z
zhong_ning 已提交
410 411 412 413 414 415 416 417 418
            return SERVICE_FAILURE;
        }
        curServ->servPerm.uID = uID;
        return SERVICE_SUCCESS;
    }

    if (cJSON_IsNumber(filedJ)) {
        int uID = (int)cJSON_GetNumberValue(filedJ);
        if (uID < 0) {
Z
zhong_ning 已提交
419
            INIT_LOGE("GetUidStringNumber, uID = %d error.\n", uID);
Z
zhong_ning 已提交
420 421 422 423 424 425
            return SERVICE_FAILURE;
        }
        curServ->servPerm.uID = uID;
        return SERVICE_SUCCESS;
    }

Z
zhong_ning 已提交
426
    INIT_LOGE("GetUidStringNumber, this uid is neither a string nor a number, error.\n");
Z
zhong_ning 已提交
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    return SERVICE_FAILURE;
}

static int SplitString(char *srcPtr, char **dstPtr)
{
    if (!srcPtr) {
        return -1;
    }
    char *buf = NULL;
    dstPtr[0] = strtok_r(srcPtr, " ", &buf);
    int i = 0;
    while (dstPtr[i])
    {
        i++;
        dstPtr[i] = strtok_r(NULL, " ", &buf);
    }
    dstPtr[i] = "\0";
    int num = i;
Z
zhong_ning 已提交
445 446
    for (int j = 0; j < num; j++) {
        INIT_LOGI("dstPtr[%d] is %s \n", j, dstPtr[j]);
Z
zhong_ning 已提交
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
    }
    return num;
}

#define MAX_SOCK_NAME_LEN  16
#define SOCK_OPT_NUMS  6
enum SockOptionTab
{
    SERVICE_SOCK_NAME = 0,
    SERVICE_SOCK_TYPE,
    SERVICE_SOCK_PERM,
    SERVICE_SOCK_UID,
    SERVICE_SOCK_GID,
    SERVICE_SOCK_SETOPT
};

static int ParseServiceSocket(char **opt, const int optNum, struct ServiceSocket *sockopt)
{
Z
zhong_ning 已提交
465
    INIT_LOGI("ParseServiceSocket\n");
Z
zhong_ning 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
    if (optNum != SOCK_OPT_NUMS) {
        return -1;
    }
    sockopt->name = (char *)calloc(MAX_SOCK_NAME_LEN, sizeof(char));
    if (sockopt->name == NULL) {
        return -1;
    }
    if (opt[SERVICE_SOCK_NAME] == NULL) {
        return -1;
    }
    int ret = memcpy_s(sockopt->name, MAX_SOCK_NAME_LEN, opt[SERVICE_SOCK_NAME], MAX_SOCK_NAME_LEN - 1);
    if (ret != 0) {
        return -1;
    }

    if (opt[SERVICE_SOCK_TYPE] == NULL) {
        return -1;
    }
    sockopt->type =
        strncmp(opt[SERVICE_SOCK_TYPE], "stream", strlen(opt[SERVICE_SOCK_TYPE])) == 0 ? SOCK_STREAM :
        (strncmp(opt[SERVICE_SOCK_TYPE], "dgram", strlen(opt[SERVICE_SOCK_TYPE])) == 0 ? SOCK_DGRAM : SOCK_SEQPACKET);

    if (opt[SERVICE_SOCK_PERM] == NULL) {
        return -1;
    }
Z
zhong_ning 已提交
491
    sockopt->perm = strtoul(opt[SERVICE_SOCK_PERM], 0, 8);  //¡Áa?¡¥?a8????
Z
zhong_ning 已提交
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520

    if (opt[SERVICE_SOCK_UID] == NULL) {
        return -1;
    }
    int uuid = DecodeUid(opt[SERVICE_SOCK_UID]);
    if (uuid < 0) {
        return -1;
    }
    sockopt->uid = uuid;

    if (opt[SERVICE_SOCK_GID] == NULL) {
        return -1;
    }
    int ggid = DecodeUid(opt[SERVICE_SOCK_GID]);
    if (ggid < 0) {
        return -1;
    }
    sockopt->gid = ggid;

    if (opt[SERVICE_SOCK_SETOPT] == NULL) {
        return -1;
    }
    sockopt->passcred = strncmp(opt[SERVICE_SOCK_SETOPT], "passcred", strlen(opt[SERVICE_SOCK_SETOPT])) == 0 ? true : false;
    sockopt->next = NULL;
    return 0;
}

static int GetServiceSocket(const cJSON* curArrItem, Service* curServ)
{
Z
zhong_ning 已提交
521
    INIT_LOGI("GetServiceSocket \n");
Z
zhong_ning 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
    cJSON* filedJ = cJSON_GetObjectItem(curArrItem, "socket");
    if (!cJSON_IsArray(filedJ)) {
        return SERVICE_FAILURE;
    }

    int sockCnt = cJSON_GetArraySize(filedJ);
    if (sockCnt <= 0) {
        return SERVICE_FAILURE;
    }
    curServ->socketCfg = NULL;
    for (int i = 0; i < sockCnt; ++i) {
        cJSON* sockJ = cJSON_GetArrayItem(filedJ, i);
        if (!cJSON_IsString(sockJ) || !cJSON_GetStringValue(sockJ)) {
            return SERVICE_FAILURE;
        }
        char *sockStr = cJSON_GetStringValue(sockJ);
        char *tmpStr[SOCK_OPT_NUMS] = {NULL,};
        int num = SplitString(sockStr, tmpStr);
        if (num != SOCK_OPT_NUMS) {
            return SERVICE_FAILURE;
        }
        struct ServiceSocket *socktmp = (struct ServiceSocket *)calloc(1, sizeof(struct ServiceSocket));
        if (!socktmp) {
            return SERVICE_FAILURE;
        }
        int ret = ParseServiceSocket(tmpStr, SOCK_OPT_NUMS, socktmp);
        if (ret < 0) {
            return SERVICE_FAILURE;
        }
        if (curServ->socketCfg == NULL) {
            curServ->socketCfg = socktmp;
        } else {
            socktmp->next = curServ->socketCfg->next;
            curServ->socketCfg->next = socktmp;
        }
    }
    return SERVICE_SUCCESS;
}

static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ)
{
Z
zhong_ning 已提交
563
    INIT_LOGI("GetServiceOnRestart \n");
Z
zhong_ning 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
    cJSON* filedJ = cJSON_GetObjectItem(curArrItem, "onrestart");
    if (!cJSON_IsArray(filedJ)) {
            return SERVICE_FAILURE;
    }
    int cmdCnt = cJSON_GetArraySize(filedJ);
    if (cmdCnt <= 0) {
        return SERVICE_FAILURE;
    }
    curServ->onRestart = (struct OnRestartCmd *)calloc(1, sizeof(struct OnRestartCmd));
    if (curServ->onRestart == NULL) {
        return SERVICE_FAILURE;
    }
    curServ->onRestart->cmdLine = (CmdLine *)calloc(cmdCnt, sizeof(CmdLine));
    if (curServ->onRestart->cmdLine == NULL) {
        return SERVICE_FAILURE;
    }
    curServ->onRestart->cmdNum = cmdCnt;
    for (int i = 0; i < cmdCnt; ++i) {
        cJSON* cmdJ = cJSON_GetArrayItem(filedJ, i);
        if (!cJSON_IsString(cmdJ) || !cJSON_GetStringValue(cmdJ)) {
            return SERVICE_FAILURE;
        }
        char *cmdStr = cJSON_GetStringValue(cmdJ);
        ParseCmdLine(cmdStr, &curServ->onRestart->cmdLine[i]);
    }
    return SERVICE_SUCCESS;
}

static int CheckServiceKeyName(const cJSON* curService)
{
    char *cfgServiceKeyList[] = {"name", "path", "uid", "gid", "once",
        "importance", "caps", "disabled", "writepid", "critical", "socket",
    };
    if (curService == NULL) {
        return SERVICE_FAILURE;
    }
    cJSON *child = curService->child;
    if (child == NULL) {
        return SERVICE_FAILURE;
    }
    while (child) {
        int i = 0;
        int keyListSize = sizeof(cfgServiceKeyList) / sizeof(char *);
        for (; i < keyListSize; i++) {
            if (!strcmp(child->string, cfgServiceKeyList[i])) {
                break;
            }
        }
        if(i < keyListSize) {
            child = child->next;
        } else {
Z
zhong_ning 已提交
615
            INIT_LOGE("CheckServiceKeyName, key name %s is not found. error.\n", child->string);
Z
zhong_ning 已提交
616 617 618 619 620 621 622 623 624 625 626
            return SERVICE_FAILURE;
        }
    }
    return SERVICE_SUCCESS;
}

void ParseAllServices(const cJSON* fileRoot)
{
    int servArrSize = 0;
    cJSON* serviceArr = GetArrItem(fileRoot, &servArrSize, SERVICES_ARR_NAME_IN_JSON);
    if (serviceArr == NULL) {
Z
zhong_ning 已提交
627
        INIT_LOGE("ParseAllServices, get array %s failed.\n", SERVICES_ARR_NAME_IN_JSON);
Z
zhong_ning 已提交
628 629 630
        return;
    }

Z
zhong_ning 已提交
631
    INIT_LOGI("servArrSize is %d \n", servArrSize);
Z
zhong_ning 已提交
632
    if (servArrSize > MAX_SERVICES_CNT_IN_FILE) {
Z
zhong_ning 已提交
633
        INIT_LOGE("ParseAllServices, too many services[cnt %d] detected, should not exceed %d.\n",
Z
zhong_ning 已提交
634 635 636 637 638 639
            servArrSize, MAX_SERVICES_CNT_IN_FILE);
        return;
    }

    Service* retServices = (Service*)realloc(g_services, sizeof(Service) * (g_servicesCnt + servArrSize));
    if (retServices == NULL) {
Z
zhong_ning 已提交
640
        INIT_LOGE("ParseAllServices, realloc for %s arr failed! %d.\n", SERVICES_ARR_NAME_IN_JSON, servArrSize);
Z
zhong_ning 已提交
641 642 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
        return;
    }
    // Skip already saved services,
    Service* tmp = retServices + g_servicesCnt;
    if (memset_s(tmp, sizeof(Service) * servArrSize, 0, sizeof(Service) * servArrSize) != EOK) {
        free(retServices);
        retServices = NULL;
        return;
    }

    for (int i = 0; i < servArrSize; ++i) {
        cJSON* curItem = cJSON_GetArrayItem(serviceArr, i);
        if (CheckServiceKeyName(curItem) != SERVICE_SUCCESS) {
            ReleaseServiceMem(&tmp[i]);
            tmp[i].attribute |= SERVICE_ATTR_INVALID;
            continue;
        }
        int ret1 = GetServiceName(curItem, &tmp[i]);
        int ret2 = GetServicePathAndArgs(curItem, &tmp[i]);
        int ret3 = GetUidStringNumber(curItem, &tmp[i]);                        // uid in string or number form
        int ret4 = GetGidArray(curItem, &tmp[i]);                               // gid array
        int ret5 = GetServiceNumber(curItem, &tmp[i], ONCE_STR_IN_CFG);
        int ret6 = GetServiceNumber(curItem, &tmp[i], IMPORTANT_STR_IN_CFG);
        int ret7 = GetServiceNumber(curItem, &tmp[i], CRITICAL_STR_IN_CFG);     // critical
        int ret8 = GetServiceNumber(curItem, &tmp[i], DISABLED_STR_IN_CFG);     // disabled
        int ret9 = GetWritepidStrings(curItem, &tmp[i]);                        // writepid
        int reta = GetServiceCaps(curItem, &tmp[i]);
        int retAll = ret1 | ret2 | ret3 | ret4 | ret5 | ret6 | ret7 | ret8 | ret9 | reta;
        if (retAll != SERVICE_SUCCESS) {
            // release resources if it fails
            ReleaseServiceMem(&tmp[i]);
            tmp[i].attribute |= SERVICE_ATTR_INVALID;
Z
zhong_ning 已提交
673
            INIT_LOGE("ParseAllServices, parse information for service %s failed. ", tmp[i].name);
Z
zhong_ning 已提交
674 675
            continue;
        } else {
Z
zhong_ning 已提交
676
            INIT_LOGD("ParseAllServices ParseAllServices Service[%d] name=%s, uid=%d, critical=%d, disabled=%d\n",
Z
zhong_ning 已提交
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
                 i, tmp[i].name, tmp[i].servPerm.uID, tmp[i].attribute & SERVICE_ATTR_CRITICAL ? 1 : 0,
                 tmp[i].attribute & SERVICE_ATTR_DISABLED ? 1 : 0);
        }
        if (GetServiceSocket(curItem, &tmp[i]) != SERVICE_SUCCESS) {
            // free list
        }
        if (GetServiceOnRestart(curItem, &tmp[i]) != SERVICE_SUCCESS) {
            // free
        }
    }
    // Increase service counter.
    RegisterServices(retServices, servArrSize);
}

static int FindServiceByName(const char* servName)
{
    if (servName == NULL) {
        return -1;
    }

    for (int i = 0; i < g_servicesCnt; ++i) {
        if (strlen(g_services[i].name) == strlen(servName) &&
            strncmp(g_services[i].name, servName, strlen(g_services[i].name)) == 0) {
            return i;
        }
    }
    return -1;
}

void StartServiceByName(const char* servName)
{
    // find service by name
    int servIdx = FindServiceByName(servName);
    if (servIdx < 0) {
Z
zhong_ning 已提交
711
        INIT_LOGE("StartServiceByName, cannot find service %s.\n", servName);
Z
zhong_ning 已提交
712 713 714 715
        return;
    }

    if (ServiceStart(&g_services[servIdx]) != SERVICE_SUCCESS) {
Z
zhong_ning 已提交
716
        INIT_LOGE("StartServiceByName, service %s start failed!\n", g_services[servIdx].name);
Z
zhong_ning 已提交
717 718 719 720 721 722 723 724 725 726
    }

    return;
}

void StopServiceByName(const char* servName)
{
    // find service by name
    int servIdx = FindServiceByName(servName);
    if (servIdx < 0) {
Z
zhong_ning 已提交
727
        INIT_LOGE("StopServiceByName, cannot find service %s.\n", servName);
Z
zhong_ning 已提交
728 729 730 731
        return;
    }

    if (ServiceStop(&g_services[servIdx]) != SERVICE_SUCCESS) {
Z
zhong_ning 已提交
732
        INIT_LOGE("StopServiceByName, service %s start failed!\n", g_services[servIdx].name);
Z
zhong_ning 已提交
733 734 735 736 737 738 739 740
    }

    return;
}

void StopAllServices()
{
    for (int i = 0; i < g_servicesCnt; i++) {
Z
zhong_ning 已提交
741 742 743 744
        if (strcmp(g_services[i].name, "console") != 0 && strcmp(g_services[i].name, "ueventd") != 0) {
            if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) {
                INIT_LOGE("[Init] StopAllServices, service %s stop failed!\n", g_services[i].name);
            }
Z
zhong_ning 已提交
745 746 747 748 749 750 751 752 753 754
        }
    }
}

void StopAllServicesBeforeReboot()
{
    for (int i = 0; i < g_servicesCnt; i++) {
        g_services[i].attribute |= SERVICE_ATTR_INVALID;
        if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) {
            INIT_LOGE("StopAllServicesBeforeReboot, service %s stop failed!\n", g_services[i].name);
Z
zhong_ning 已提交
755 756 757 758 759 760 761 762 763 764 765 766
        }
    }
}

void ReapServiceByPID(int pid)
{
    for (int i = 0; i < g_servicesCnt; i++) {
        if (g_services[i].pid == pid) {
            if (g_services[i].attribute & SERVICE_ATTR_IMPORTANT) {
                // important process exit, need to reboot system
                g_services[i].pid = -1;
                StopAllServices();
Z
zhong_ning 已提交
767
//                RebootSystem();
Z
zhong_ning 已提交
768 769 770 771 772 773 774 775
            }
            ServiceReap(&g_services[i]);
            break;
        }
    }
}