From 50beef8763b7f651bb6376ee0f4d8403db360e71 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 9 Sep 2021 18:01:57 +0800 Subject: [PATCH 1/7] init: fix codedex Signed-off-by: sun_fan --- services/log/init_log.c | 3 +++ services/src/init_cmds.c | 35 +++++++++++++++++++---------- services/src/init_service_manager.c | 14 +++++++++++- services/src/init_utils.c | 2 +- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/services/log/init_log.c b/services/log/init_log.c index f4cf63178..c585c9491 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -74,6 +74,9 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l time_t logTime; time(&logTime); struct tm *t = gmtime(&logTime); + if (t == NULL) { + return; + } fprintf(stdout, "[%d-%d-%d %d:%d:%d][pid=%d][%s:%d][%s][%s] ", (t->tm_year + BASE_YEAR), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, getpid(), fileName, line, tag, LOG_LEVEL_STR[logLevel]); diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index e4bb8c67f..9a7c1b81f 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -148,7 +148,7 @@ 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); + 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]; @@ -162,13 +162,13 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim) ctx->argc = 0; while (token != NULL) { #ifndef OHOS_LITE - ctx->argv[ctx->argc] = calloc(sizeof(char *), MAX_EACH_CMD_LENGTH + MAX_PARAM_VALUE_LEN); + 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); + 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); @@ -259,18 +259,29 @@ static void DoCopy(const char* cmdContent) int rdLen = 0; int rtLen = 0; char buf[MAX_COPY_BUF_SIZE] = {0}; + char *realPath1 = NULL; + char *realPath2 = NULL; 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) { + if (ctx == NULL || ctx->argv == NULL || ctx->argv[0] == NULL || ctx->argv[1] == NULL || + ctx->argc != DEFAULT_COPY_ARGS_CNT) { INIT_LOGE("DoCopy failed."); goto out; } - srcFd = open(ctx->argv[0], O_RDONLY); + realPath1 = realpath(ctx->argv[0], NULL); + if (realPath1 == NULL) { + goto out; + } + realPath2 = realpath(ctx->argv[1], NULL); + if (realPath2 == NULL) { + goto out; + } + srcFd = open(realPath1, O_RDONLY); 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 "); mode = fileStat.st_mode; - dstFd = open(ctx->argv[1], O_WRONLY | O_TRUNC | O_CREAT, mode); + dstFd = open(realPath2, O_WRONLY | O_TRUNC | O_CREAT, mode); INIT_ERROR_CHECK(dstFd >= 0, goto out, "copy open %s fail %d! ", ctx->argv[1], errno); while ((rdLen = read(srcFd, buf, sizeof(buf) - 1)) > 0) { rtLen = write(dstFd, buf, rdLen); @@ -280,10 +291,10 @@ static void DoCopy(const char* cmdContent) out: FreeCmd(&ctx); ctx = NULL; - close(srcFd); - srcFd = -1; - close(dstFd); - dstFd = -1; + INIT_CHECK(srcFd < 0, close(srcFd); srcFd = -1); + INIT_CHECK(dstFd < 0, close(dstFd); dstFd = -1); + INIT_CHECK(realPath1 == NULL, free(realPath1); realPath1 = NULL); + INIT_CHECK(realPath2 == NULL, free(realPath2); realPath2 = NULL); return; } @@ -718,8 +729,8 @@ static void DoWrite(const char *cmdContent) goto out; } - int fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU | - S_IRGRP | S_IROTH); + int fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | + S_IWUSR); if (fd == -1) { INIT_LOGE("DoWrite: open %s failed: %d", ctx->argv[0], errno); goto out; diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 73927627e..582203b6d 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -155,7 +155,7 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ) { int writepidCnt = 0; cJSON* filedJ = GetArrItem(curArrItem, &writepidCnt, "writepid"); - if (writepidCnt <= 0) { // not item is ok. + if ((writepidCnt <= 0) || (filedJ == NULL)) { // not item is ok. return SERVICE_SUCCESS; } @@ -173,6 +173,9 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ) } char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i)); + if ((fieldStr == NULL) || (fieldStr[0] == '\0')) { + return SERVICE_FAILURE; + } size_t strLen = strlen(fieldStr); curServ->writepidFiles[i] = (char *)malloc(sizeof(char) * strLen + 1); if (curServ->writepidFiles[i] == NULL) { @@ -204,6 +207,9 @@ static int GetGidOneItem(const cJSON *curArrItem, Service *curServ) // gi if (cJSON_IsString(filedJ)) { char* fieldStr = cJSON_GetStringValue(filedJ); + if (fieldStr == NULL) { + return SERVICE_FAILURE; + } gid_t gID = DecodeUid(fieldStr); if (gID == (gid_t)(-1)) { INIT_LOGE("GetGidOneItem, DecodeUid %s error.", fieldStr); @@ -405,6 +411,9 @@ static int GetUidStringNumber(const cJSON *curArrItem, Service *curServ) if (cJSON_IsString(filedJ)) { char* fieldStr = cJSON_GetStringValue(filedJ); + if (fieldStr == NULL) { + return SERVICE_FAILURE; + } int uID = DecodeUid(fieldStr); if (uID < 0) { INIT_LOGE("GetUidStringNumber, DecodeUid %s error.", fieldStr); @@ -474,6 +483,7 @@ static int ParseServiceSocket(char **opt, const int optNum, struct ServiceSocket int ret = memcpy_s(sockopt->name, MAX_SOCK_NAME_LEN, opt[SERVICE_SOCK_NAME], MAX_SOCK_NAME_LEN - 1); if (ret != 0) { free(sockopt->name); + sockopt->name = NULL; return -1; } sockopt->next = NULL; @@ -556,6 +566,7 @@ static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ) curServ->onRestart->cmdLine = (CmdLine *)calloc(cmdCnt, sizeof(CmdLine)); if (curServ->onRestart->cmdLine == NULL) { free(curServ->onRestart); + curServ->onRestart = NULL; return SERVICE_FAILURE; } curServ->onRestart->cmdNum = cmdCnt; @@ -563,6 +574,7 @@ static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ) cJSON* cmdJ = cJSON_GetArrayItem(filedJ, i); if (!cJSON_IsString(cmdJ) || !cJSON_GetStringValue(cmdJ)) { free(curServ->onRestart->cmdLine); + curServ->onRestart->cmdLine = NULL; free(curServ->onRestart); curServ->onRestart = NULL; return SERVICE_FAILURE; diff --git a/services/src/init_utils.c b/services/src/init_utils.c index 86ec49766..98276497e 100644 --- a/services/src/init_utils.c +++ b/services/src/init_utils.c @@ -74,7 +74,7 @@ void CheckAndCreateDir(const char *fileName) { char *path = strndup(fileName, strrchr(fileName, '/') - fileName); if (path != NULL && access(path, F_OK) != 0) { - mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); + mkdir(path, S_IRWXU | S_IRGRP | S_IXGRP); } free(path); } -- Gitee From 5806808bb0ec11f54f9a0ef993f7b71aed94c9a9 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 9 Sep 2021 18:11:17 +0800 Subject: [PATCH 2/7] init: fix codedex . Signed-off-by: sun_fan --- services/src/init_cmds.c | 2 +- services/src/init_service_manager.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 9a7c1b81f..22a62371b 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -148,7 +148,7 @@ 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); + 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]; diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 582203b6d..84ef9d548 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -173,7 +173,7 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ) } char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i)); - if ((fieldStr == NULL) || (fieldStr[0] == '\0')) { + if (fieldStr == NULL) { return SERVICE_FAILURE; } size_t strLen = strlen(fieldStr); -- Gitee From ef442904a1e7160a6a679679924b205a836dc5a3 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 9 Sep 2021 18:22:27 +0800 Subject: [PATCH 3/7] init: fix codedex .. Signed-off-by: sun_fan --- services/src/init_cmds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 22a62371b..e5ce6b85d 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -291,10 +291,10 @@ static void DoCopy(const char* cmdContent) out: FreeCmd(&ctx); ctx = NULL; - INIT_CHECK(srcFd < 0, close(srcFd); srcFd = -1); - INIT_CHECK(dstFd < 0, close(dstFd); dstFd = -1); - INIT_CHECK(realPath1 == NULL, free(realPath1); realPath1 = NULL); - INIT_CHECK(realPath2 == NULL, free(realPath2); realPath2 = NULL); + INIT_ERROR_CHECK(srcFd < 0, close(srcFd); srcFd = -1); + INIT_ERROR_CHECK(dstFd < 0, close(dstFd); dstFd = -1); + INIT_ERROR_CHECK(realPath1 == NULL, free(realPath1); realPath1 = NULL); + INIT_ERROR_CHECK(realPath2 == NULL, free(realPath2); realPath2 = NULL); return; } -- Gitee From 2ce243abf61ff5e10b4cf677fc6ae883c696a244 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 9 Sep 2021 18:48:05 +0800 Subject: [PATCH 4/7] init: fix codedex ... Signed-off-by: sun_fan --- services/log/init_log.h | 24 +++++++++++++++++------- services/src/init_cmds.c | 8 ++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/services/log/init_log.h b/services/log/init_log.h index f46bf8e85..5b24b9075 100644 --- a/services/log/init_log.h +++ b/services/log/init_log.h @@ -73,17 +73,27 @@ void SetHiLogLevel(LogLevel logLevel); void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *fmt, ...); void SetLogLevel(InitLogLevel logLevel); #endif +#define INIT_CHECK(ret, statement) \ + do { \ + if (!(ret)) { \ + statement; \ + } \ + } while (0) #define INIT_ERROR_CHECK(ret, statement, format, ...) \ - if (!(ret)) { \ - INIT_LOGE(format, ##__VA_ARGS__); \ - statement; \ - } + do { \ + if (!(ret)) { \ + INIT_LOGE(format, ##__VA_ARGS__); \ + statement; \ + } \ + } while (0) #define INIT_CHECK_ONLY_RETURN(ret, statement) \ - if (!(ret)) { \ - statement; \ - } + do { \ + if (!(ret)) { \ + statement; \ + } \ + } while (0) #ifdef __cplusplus #if __cplusplus diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index e5ce6b85d..22a62371b 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -291,10 +291,10 @@ static void DoCopy(const char* cmdContent) out: FreeCmd(&ctx); ctx = NULL; - INIT_ERROR_CHECK(srcFd < 0, close(srcFd); srcFd = -1); - INIT_ERROR_CHECK(dstFd < 0, close(dstFd); dstFd = -1); - INIT_ERROR_CHECK(realPath1 == NULL, free(realPath1); realPath1 = NULL); - INIT_ERROR_CHECK(realPath2 == NULL, free(realPath2); realPath2 = NULL); + INIT_CHECK(srcFd < 0, close(srcFd); srcFd = -1); + INIT_CHECK(dstFd < 0, close(dstFd); dstFd = -1); + INIT_CHECK(realPath1 == NULL, free(realPath1); realPath1 = NULL); + INIT_CHECK(realPath2 == NULL, free(realPath2); realPath2 = NULL); return; } -- Gitee From e3c62d32e759f00f3d85255cf6ce8262796227c6 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Thu, 9 Sep 2021 21:46:09 +0800 Subject: [PATCH 5/7] init: fix codedex Signed-off-by: sun_fan --- services/include/init_service_manager.h | 6 +++--- services/src/init_service_manager.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/services/include/init_service_manager.h b/services/include/init_service_manager.h index ae3e7c579..3bf1b35df 100644 --- a/services/include/init_service_manager.h +++ b/services/include/init_service_manager.h @@ -39,11 +39,11 @@ extern "C" { void RegisterServices(Service* services, int servicesCnt); void StartServiceByName(const char* serviceName); void StopServiceByName(const char* serviceName); -void StopAllServices(); -void StopAllServicesBeforeReboot(); +void StopAllServices(void); +void StopAllServicesBeforeReboot(void); void ReapServiceByPID(int pid); void ParseAllServices(const cJSON* fileRoot); -void DumpAllServices(); +void DumpAllServices(void); #ifdef __cplusplus #if __cplusplus diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 84ef9d548..95a52a1b5 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -38,7 +38,7 @@ static Service* g_services = NULL; static int g_servicesCnt = 0; -void DumpAllServices() +void DumpAllServices(void) { INIT_LOGD("Ready to dump all services:"); INIT_LOGD("total service number: %d", g_servicesCnt); @@ -736,7 +736,7 @@ void StopServiceByName(const char* servName) return; } -void StopAllServices() +void StopAllServices(void) { for (int i = 0; i < g_servicesCnt; i++) { if (ServiceStop(&g_services[i]) != SERVICE_SUCCESS) { @@ -745,7 +745,7 @@ void StopAllServices() } } -void StopAllServicesBeforeReboot() +void StopAllServicesBeforeReboot(void) { for (int i = 0; i < g_servicesCnt; i++) { g_services[i].attribute |= SERVICE_ATTR_INVALID; -- Gitee From 628b321e05c772e9cc07487725f93fb5d8b1ea26 Mon Sep 17 00:00:00 2001 From: sun_fan Date: Fri, 10 Sep 2021 11:40:50 +0800 Subject: [PATCH 6/7] init: fix codedex Signed-off-by: sun_fan --- services/log/init_log.c | 15 ---- services/src/init_cmds.c | 105 +++++++++++++--------------- services/src/init_service_manager.c | 46 ++++-------- 3 files changed, 62 insertions(+), 104 deletions(-) diff --git a/services/log/init_log.c b/services/log/init_log.c index c585c9491..a573ef0d7 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -86,19 +86,4 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l vfprintf(stdout, fmt, list); va_end(list); fflush(stdout); - -#if 0 - int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC | O_APPEND ); - if (fd < 1) { - printf("xxxxxxxxxxxxxxx open failed. %d\n", errno); - return; - } - if (write(fd, logInfo, strlen(logInfo)) < -1) { - printf("xxxxxxxxxxxxxxx write failed.%d\n", errno); - close(fd); - return; - } - close(fd); -#endif } - diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index 22a62371b..abdf402d9 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -566,15 +566,15 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt if (!fileName) { return; } - char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char)); + char *realPath = realpath(fileName, NULL); if (realPath == NULL) { return; } - realPath = realpath(fileName, realPath); int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); if (fd < 0) { INIT_LOGE("failed to open %s: %d", realPath, errno); free(realPath); + realPath = NULL; return; } int rc = syscall(__NR_finit_module, fd, options, flags); @@ -585,6 +585,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt close(fd); } free(realPath); + realPath = NULL; return; } @@ -976,66 +977,58 @@ void DoCmd(const CmdLine* curCmd) DoCmdByName(curCmd->name, curCmd->cmdContent); } +struct CmdTable { + char name[MAX_CMD_NAME_LEN]; + void (*DoFuncion)(const char *cmdContent); +}; + +static const struct CmdTable CMD_TABLE[] = { + { "start ", DoStart }, + { "mkdir ", DoMkDir }, + { "stop ", DoStop }, + { "reset ", DoReset }, + { "copy ", DoCopy }, + { "chmod ", DoChmod }, + { "chown ", DoChown }, + { "mount ", DoMount }, + { "write ", DoWrite }, + { "rmdir ", DoRmdir }, + { "rm ", DoRm }, +#ifndef OHOS_LITE + { "symlink ", DoSymlink }, + { "makedev ", DoMakeDevice }, + { "mknode ", DoMakeNode }, + { "insmod ", DoInsmod }, + { "trigger ", DoTriggerExec }, + { "setparam ", DoSetParam }, + { "load_param ", LoadDefaultParams }, +#endif + { "export ", DoExport }, + { "setrlimit ", DoSetrlimit }, + { "exec ", DoExec }, + { "loadcfg ", DoLoadCfg }, + { "reboot ", DoReboot } +}; + 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, "setrlimit ", strlen("setrlimit ")) == 0) { - DoSetrlimit(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); -#ifndef OHOS_LITE - } else if (strncmp(name, "insmod ", strlen("insmod ")) == 0) { - DoInsmod(cmdContent); - } else if (strncmp(name, "trigger ", strlen("trigger ")) == 0) { - INIT_LOGD("ready to trigger job: %s", name); - DoTriggerExec(cmdContent); - } else if (strncmp(name, "load_persist_params ", strlen("load_persist_params ")) == 0) { + if (strncmp(name, "load_persist_params ", strlen("load_persist_params ")) == 0) { LoadPersistParams(); - } else if (strncmp(name, "setparam ", strlen("setparam ")) == 0) { - DoSetParam(cmdContent); - } else if (strncmp(name, "load_param ", strlen("load_param ")) == 0) { - LoadDefaultParams(cmdContent); -#endif - } else if (strncmp(name, "reboot ", strlen("reboot ")) == 0) { - DoReboot(cmdContent); - } else { - INIT_LOGE("DoCmd, unknown cmd name %s.", name); + } else { + size_t cmdCnt = sizeof(CMD_TABLE) / sizeof(CMD_TABLE[0]); + unsigned int i = 0; + for (; i < cmdCnt; ++i) { + if (strncmp(name, CMD_TABLE[i].name, strlen(CMD_TABLE[i].name)) == 0) { + CMD_TABLE[i].DoFuncion(cmdContent); + break; + } + } + if (i == cmdCnt) { + INIT_LOGE("DoCmd, unknown cmd name %s.", name); + } } } diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 95a52a1b5..34469b3ad 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -448,7 +448,6 @@ static int ParseServiceSocket(char **opt, const int optNum, struct ServiceSocket 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; } @@ -472,7 +471,8 @@ static int ParseServiceSocket(char **opt, const int optNum, struct ServiceSocket 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->passcred = strncmp(opt[SERVICE_SOCK_SETOPT], "passcred", + strlen(opt[SERVICE_SOCK_SETOPT])) == 0 ? true : false; if (opt[SERVICE_SOCK_NAME] == NULL) { return -1; } @@ -526,7 +526,7 @@ static int GetServiceSocket(const cJSON* curArrItem, Service* curServ) return SERVICE_FAILURE; } char *sockStr = cJSON_GetStringValue(sockJ); - char *tmpStr[SOCK_OPT_NUMS] = {NULL,}; + char *tmpStr[SOCK_OPT_NUMS] = {NULL}; int num = SplitString(sockStr, tmpStr, SOCK_OPT_NUMS); if (num != SOCK_OPT_NUMS) { return SERVICE_FAILURE; @@ -605,7 +605,7 @@ static int CheckServiceKeyName(const cJSON* curService) break; } } - if(i < keyListSize) { + if (i < keyListSize) { child = child->next; } else { INIT_LOGE("CheckServiceKeyName, key name %s is not found. error.", child->string); @@ -619,31 +619,15 @@ void ParseAllServices(const cJSON* fileRoot) { int servArrSize = 0; cJSON* serviceArr = GetArrItem(fileRoot, &servArrSize, SERVICES_ARR_NAME_IN_JSON); - if (serviceArr == NULL) { - INIT_LOGE("ParseAllServices, get array %s failed.", SERVICES_ARR_NAME_IN_JSON); - return; - } - - INIT_LOGI("servArrSize is %d ", servArrSize); - if (servArrSize > MAX_SERVICES_CNT_IN_FILE) { - INIT_LOGE("ParseAllServices, too many services[cnt %d] detected, should not exceed %d.", - servArrSize, MAX_SERVICES_CNT_IN_FILE); - return; - } - + INIT_ERROR_CHECK(serviceArr != NULL, return, "ParseAllServices, get array %s failed.",SERVICES_ARR_NAME_IN_JSON); + INIT_ERROR_CHECK(servArrSize <= MAX_SERVICES_CNT_IN_FILE, return, "ParseAllServices, + too many services[cnt %d] detected, should not exceed %d.", servArrSize, MAX_SERVICES_CNT_IN_FILE); Service* retServices = (Service*)realloc(g_services, sizeof(Service) * (g_servicesCnt + servArrSize)); - if (retServices == NULL) { - INIT_LOGE("ParseAllServices, realloc for %s arr failed! %d.", SERVICES_ARR_NAME_IN_JSON, servArrSize); - return; - } - // Skip already saved services, + INIT_ERROR_CHECK(retServices != NUL, return, "ParseAllServices, realloc for %s arr failed! %d.", + SERVICES_ARR_NAME_IN_JSON, servArrSize); Service* tmp = retServices + g_servicesCnt; - if (memset_s(tmp, sizeof(Service) * servArrSize, 0, sizeof(Service) * servArrSize) != EOK) { - free(retServices); - retServices = NULL; - return; - } - + INIT_ERROR_CHECK(memset_s(tmp, sizeof(Service) * servArrSize, 0, sizeof(Service) * servArrSize) == EOK, + free(retServices); retServices = NULL; return, "memset_s failed errno: %d", errno); for (int i = 0; i < servArrSize; ++i) { cJSON* curItem = cJSON_GetArrayItem(serviceArr, i); if (CheckServiceKeyName(curItem) != SERVICE_SUCCESS) { @@ -664,15 +648,13 @@ void ParseAllServices(const cJSON* fileRoot) int retb = GetServiceCaps(curItem, &tmp[i]); int retAll = ret1 | ret2 | ret3 | ret4 | ret5 | ret6 | ret7 | ret8 | ret9 | reta | retb; if (retAll != SERVICE_SUCCESS) { - // release resources if it fails ReleaseServiceMem(&tmp[i]); tmp[i].attribute |= SERVICE_ATTR_INVALID; INIT_LOGE("ParseAllServices, parse information for service %s failed. ", tmp[i].name); continue; } else { - INIT_LOGD("ParseAllServices ParseAllServices Service[%d] name=%s, uid=%d, critical=%d, disabled=%d", - i, tmp[i].name, tmp[i].servPerm.uID, tmp[i].attribute & SERVICE_ATTR_CRITICAL ? 1 : 0, - tmp[i].attribute & SERVICE_ATTR_DISABLED ? 1 : 0); + INIT_LOGD("Service[%d] name=%s, uid=%d, critical=%d, disabled=%d", 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) { INIT_LOGE("GetServiceSocket fail "); @@ -685,7 +667,6 @@ void ParseAllServices(const cJSON* fileRoot) INIT_LOGE("GetServiceOnRestart fail "); } } - // Increase service counter. RegisterServices(retServices, servArrSize); } @@ -773,4 +754,3 @@ void ReapServiceByPID(int pid) } } - -- Gitee From f4bd202cf8a2f958b0bb3a067717c46679fe0aa3 Mon Sep 17 00:00:00 2001 From: Cheng Jinsong Date: Fri, 10 Sep 2021 04:16:23 +0000 Subject: [PATCH 7/7] init : fix codedex Signed-off-by: Cheng Jinsong --- services/include/init_utils.h | 2 ++ services/src/init_capability.c | 3 ++- services/src/init_cmds.c | 10 +++++----- services/src/init_service_manager.c | 4 ++-- services/src/uevent.c | 3 ++- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/include/init_utils.h b/services/include/init_utils.h index 3dcdbcbda..2352816f2 100644 --- a/services/include/init_utils.h +++ b/services/include/init_utils.h @@ -26,6 +26,8 @@ extern "C" { #define OCTAL_BASE 8 #define DECIMAL_BASE 10 +#define ARRAY_LENGTH(array) (sizeof(array) / sizeof(array[0])) + int DecodeUid(const char *name); void CheckAndCreateDir(const char *fileName); char* ReadFileToBuf(const char *configFile); diff --git a/services/src/init_capability.c b/services/src/init_capability.c index ca9365bdf..31a0a8326 100644 --- a/services/src/init_capability.c +++ b/services/src/init_capability.c @@ -30,6 +30,7 @@ #include "init_log.h" #include "init_perms.h" +#include "init_utils.h" #define MAX_CAPS_CNT_FOR_ONE_SERVICE 100 @@ -89,7 +90,7 @@ static int GetServiceStringCaps(const cJSON* filedJ, Service* curServ) break; } char* fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i)); - int mapSize = sizeof(g_capStrCapNum) / sizeof(struct CapStrCapNum); // search + int mapSize = ARRAY_LENGTH(g_capStrCapNum); int j = 0; for (; j < mapSize; j++) { if (!strcmp(fieldStr, g_capStrCapNum[j].capStr)) { diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index abdf402d9..39658e5e7 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -205,7 +205,7 @@ void ParseCmdLine(const char* cmdStr, CmdLine* resCmd) return; } - size_t supportCmdCnt = sizeof(g_supportedCmds) / sizeof(g_supportedCmds[0]); + size_t supportCmdCnt = ARRAY_LENGTH(g_supportedCmds); int foundAndSucceed = 0; for (size_t i = 0; i < supportCmdCnt; ++i) { size_t curCmdNameLen = strlen(g_supportedCmds[i]); @@ -654,7 +654,7 @@ out: static bool CheckValidCfg(const char *path) { - size_t cfgCnt = sizeof(g_supportCfg) / sizeof(g_supportCfg[0]); + size_t cfgCnt = ARRAY_LENGTH(g_supportCfg); struct stat fileStat = {0}; if (stat(path, &fileStat) != 0 || fileStat.st_size <= 0 || fileStat.st_size > LOADCFG_MAX_FILE_LEN) { @@ -788,7 +788,7 @@ static void DoSetrlimit(const char *cmdContent) 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) { + for (unsigned int i = 0 ; i < ARRAY_LENGTH(resource); ++i) { if (strcmp(ctx->argv[0], resource[i]) == 0) { rcs = (int)i; } @@ -1018,7 +1018,7 @@ void DoCmdByName(const char *name, const char *cmdContent) if (strncmp(name, "load_persist_params ", strlen("load_persist_params ")) == 0) { LoadPersistParams(); } else { - size_t cmdCnt = sizeof(CMD_TABLE) / sizeof(CMD_TABLE[0]); + size_t cmdCnt = ARRAY_LENGTH(CMD_TABLE); unsigned int i = 0; for (; i < cmdCnt; ++i) { if (strncmp(name, CMD_TABLE[i].name, strlen(CMD_TABLE[i].name)) == 0) { @@ -1037,7 +1037,7 @@ const char *GetMatchCmd(const char *cmdStr) if (cmdStr == NULL) { return NULL; } - size_t supportCmdCnt = sizeof(g_supportedCmds) / sizeof(g_supportedCmds[0]); + size_t supportCmdCnt = ARRAY_LENGTH(g_supportedCmds); for (size_t i = 0; i < supportCmdCnt; ++i) { size_t curCmdNameLen = strlen(g_supportedCmds[i]); if (strncmp(g_supportedCmds[i], cmdStr, curCmdNameLen) == 0) { diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index 34469b3ad..ed59483c6 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -598,8 +598,8 @@ static int CheckServiceKeyName(const cJSON* curService) return SERVICE_FAILURE; } while (child) { - int i = 0; - int keyListSize = sizeof(cfgServiceKeyList) / sizeof(char *); + size_t i = 0; + size_t keyListSize = ARRAY_LENGTH(cfgServiceKeyList); for (; i < keyListSize; i++) { if (!strcmp(child->string, cfgServiceKeyList[i])) { break; diff --git a/services/src/uevent.c b/services/src/uevent.c index 24b998f5e..e7884df41 100644 --- a/services/src/uevent.c +++ b/services/src/uevent.c @@ -29,6 +29,7 @@ #include #include #include "init_log.h" +#include "init_utils.h" #include "list.h" #include "securec.h" @@ -391,7 +392,7 @@ struct DevPermissionMapper DEV_MAPPER[] = { static void AdjustDevicePermission(const char *devPath) { - for (unsigned int i = 0; i < sizeof(DEV_MAPPER) / sizeof(struct DevPermissionMapper); ++i) { + for (size_t i = 0; i < ARRAY_LENGTH(DEV_MAPPER); ++i) { if (strcmp(devPath, DEV_MAPPER[i].devName) == 0) { if (chmod(devPath, DEV_MAPPER[i].devMode) != 0) { INIT_LOGE("AdjustDevicePermission, failed for %s, err %d.", devPath, errno); -- Gitee