提交 9cf4e745 编写于 作者: S sun_fan

init: fix code style

Signed-off-by: Nsun_fan <sun_fan@hoperun.com>
上级 cd200898
...@@ -52,6 +52,9 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...) ...@@ -52,6 +52,9 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...)
if (logLevel < g_hiLogLevel) { if (logLevel < g_hiLogLevel) {
return; return;
} }
if (tag == NULL) {
return;
}
va_list list; va_list list;
va_start(list, fmt); va_start(list, fmt);
char tmpFmt[MAX_FORMAT_SIZE]; char tmpFmt[MAX_FORMAT_SIZE];
...@@ -70,7 +73,9 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l ...@@ -70,7 +73,9 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l
if (logLevel < g_logLevel) { if (logLevel < g_logLevel) {
return; return;
} }
if (tag == NULL) {
return;
}
time_t second = time(0); time_t second = time(0);
struct tm *t = localtime(&second); struct tm *t = localtime(&second);
if (t == NULL) { if (t == NULL) {
......
...@@ -187,7 +187,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) ...@@ -187,7 +187,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount)
token = strstr(p, delim); token = strstr(p, delim);
if (token == NULL) { // No whitespaces if (token == NULL) { // No whitespaces
// Make surce there is enough memory to store parameter value // Make surce there is enough memory to store parameter value
allocSize = (size_t)(cmdLength + MAX_PARAM_VALUE_LEN); allocSize = (size_t)(cmdLength + MAX_PARAM_VALUE_LEN + 1);
ctx->argv[ctx->argc] = calloc(sizeof(char), allocSize); ctx->argv[ctx->argc] = calloc(sizeof(char), allocSize);
INIT_CHECK_ONLY_RETURN(ctx->argv[ctx->argc] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(ctx->argv[ctx->argc] != NULL, FreeCmd(&ctx); return NULL);
INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[ctx->argc], allocSize) == 0, INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[ctx->argc], allocSize) == 0,
...@@ -204,7 +204,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) ...@@ -204,7 +204,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount)
break; break;
} }
*token = '\0'; // replace it with '\0'; *token = '\0'; // replace it with '\0';
allocSize = (size_t)((p - token) + MAX_PARAM_VALUE_LEN); allocSize = (size_t)((token - p) + MAX_PARAM_VALUE_LEN + 1);
ctx->argv[index] = calloc(sizeof(char), allocSize); ctx->argv[index] = calloc(sizeof(char), allocSize);
INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL);
INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0, INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0,
...@@ -222,7 +222,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount) ...@@ -222,7 +222,7 @@ struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount)
if (p < tmpCmd + cmdLength) { if (p < tmpCmd + cmdLength) {
// no more white space or encounter max argument count // no more white space or encounter max argument count
size_t restSize = tmpCmd + cmdLength - p; size_t restSize = tmpCmd + cmdLength - p;
allocSize = restSize + MAX_PARAM_VALUE_LEN; allocSize = restSize + MAX_PARAM_VALUE_LEN + 1;
ctx->argv[index] = calloc(sizeof(char), allocSize); ctx->argv[index] = calloc(sizeof(char), allocSize);
INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL); INIT_CHECK_ONLY_RETURN(ctx->argv[index] != NULL, FreeCmd(&ctx); return NULL);
INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0, INIT_CHECK_ONLY_RETURN(GetParamValue(p, ctx->argv[index], allocSize) == 0,
...@@ -308,18 +308,29 @@ static void DoCopy(const char* cmdContent) ...@@ -308,18 +308,29 @@ static void DoCopy(const char* cmdContent)
int rtLen = 0; int rtLen = 0;
int argsCount = 2; int argsCount = 2;
char buf[MAX_COPY_BUF_SIZE] = {0}; char buf[MAX_COPY_BUF_SIZE] = {0};
char *realPath1 = NULL;
char *realPath2 = NULL;
mode_t mode = 0; mode_t mode = 0;
struct stat fileStat = {0}; struct stat fileStat = {0};
struct CmdArgs *ctx = GetCmd(cmdContent, " ", argsCount); struct CmdArgs *ctx = GetCmd(cmdContent, " ", argsCount);
if (ctx == NULL || ctx->argv == NULL || ctx->argv[0] == 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."); INIT_LOGE("DoCopy failed.");
goto out; 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(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 "); INIT_ERROR_CHECK(stat(ctx->argv[0], &fileStat) == 0, goto out, "stat fail ");
mode = fileStat.st_mode; 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); 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) { while ((rdLen = read(srcFd, buf, sizeof(buf) - 1)) > 0) {
rtLen = write(dstFd, buf, rdLen); rtLen = write(dstFd, buf, rdLen);
...@@ -337,6 +348,14 @@ out: ...@@ -337,6 +348,14 @@ out:
close(dstFd); close(dstFd);
dstFd = -1; dstFd = -1;
} }
if (realPath1) {
free(realPath1);
realPath1 = NULL;
}
if (realPath2) {
free(realPath2);
realPath2 = NULL;
}
return; return;
} }
...@@ -611,19 +630,15 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt ...@@ -611,19 +630,15 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt
if (fileName == NULL) { if (fileName == NULL) {
return; return;
} }
char *realPath = (char *)calloc(MAX_BUFFER, sizeof(char)); char *realPath = realpath(fileName, NULL);
if (realPath == NULL) { if (realPath == NULL) {
return; return;
} }
realPath = realpath(fileName, realPath);
if (realPath == NULL) {
free(realPath);
return;
}
int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC); int fd = open(realPath, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (fd < 0) { if (fd < 0) {
INIT_LOGE("failed to open %s: %d", realPath, errno); INIT_LOGE("failed to open %s: %d", realPath, errno);
free(realPath); free(realPath);
realPath = NULL;
return; return;
} }
int rc = syscall(__NR_finit_module, fd, options, flags); int rc = syscall(__NR_finit_module, fd, options, flags);
...@@ -634,6 +649,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt ...@@ -634,6 +649,7 @@ static void DoInsmodInternal(const char *fileName, char *secondPtr, char *restPt
close(fd); close(fd);
} }
free(realPath); free(realPath);
realPath = NULL;
return; return;
} }
...@@ -740,9 +756,15 @@ static void DoLoadCfg(const char *path) ...@@ -740,9 +756,15 @@ static void DoLoadCfg(const char *path)
INIT_LOGE("CheckCfg path is NULL."); INIT_LOGE("CheckCfg path is NULL.");
return; return;
} }
fp = fopen(path, "r"); char *realPath = realpath(path, NULL);
if (realPath == NULL) {
return;
}
fp = fopen(realPath, "r");
if (fp == NULL) { if (fp == NULL) {
INIT_LOGE("open cfg error = %d", errno); INIT_LOGE("open cfg error = %d", errno);
free(realPath);
realPath = NULL;
return; return;
} }
...@@ -750,6 +772,8 @@ static void DoLoadCfg(const char *path) ...@@ -750,6 +772,8 @@ static void DoLoadCfg(const char *path)
if (cmdLine == NULL) { if (cmdLine == NULL) {
INIT_LOGE("malloc cmdline error"); INIT_LOGE("malloc cmdline error");
fclose(fp); fclose(fp);
free(realPath);
realPath = NULL;
return; return;
} }
...@@ -767,7 +791,8 @@ static void DoLoadCfg(const char *path) ...@@ -767,7 +791,8 @@ static void DoLoadCfg(const char *path)
DoCmd(cmdLine); DoCmd(cmdLine);
(void)memset_s(buf, sizeof(char) * LOADCFG_BUF_SIZE, 0, sizeof(char) * LOADCFG_BUF_SIZE); (void)memset_s(buf, sizeof(char) * LOADCFG_BUF_SIZE, 0, sizeof(char) * LOADCFG_BUF_SIZE);
} }
free(realPath);
realPath = NULL;
free(cmdLine); free(cmdLine);
fclose(fp); fclose(fp);
} }
...@@ -781,19 +806,28 @@ static void DoWrite(const char *cmdContent) ...@@ -781,19 +806,28 @@ static void DoWrite(const char *cmdContent)
INIT_LOGE("DoWrite: invalid arguments"); INIT_LOGE("DoWrite: invalid arguments");
goto out; goto out;
} }
char *realPath = realpath(ctx->argv[0], NULL);
int fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU | S_IRGRP | S_IROTH); if (realPath == NULL) {
goto out;
}
int fd = open(realPath, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRWXU | S_IRGRP | S_IROTH);
if (fd == -1) { if (fd == -1) {
INIT_LOGE("DoWrite: open %s failed: %d", ctx->argv[0], errno); INIT_LOGE("DoWrite: open %s failed: %d", ctx->argv[0], errno);
free(realPath);
realPath = NULL;
goto out; goto out;
} }
size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1])); size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1]));
if (ret < 0) { if (ret < 0) {
INIT_LOGE("DoWrite: write to file %s failed: %d", ctx->argv[0], errno); INIT_LOGE("DoWrite: write to file %s failed: %d", ctx->argv[0], errno);
free(realPath);
realPath = NULL;
close(fd); close(fd);
goto out; goto out;
} }
free(realPath);
realPath = NULL;
close(fd); close(fd);
out: out:
FreeCmd(&ctx); FreeCmd(&ctx);
......
...@@ -43,19 +43,28 @@ static bool RBMiscWriteUpdaterMessage(const char *path, struct RBMiscUpdateMessa ...@@ -43,19 +43,28 @@ static bool RBMiscWriteUpdaterMessage(const char *path, struct RBMiscUpdateMessa
INIT_LOGE("path or boot is NULL."); INIT_LOGE("path or boot is NULL.");
return false; return false;
} }
FILE* fp = fopen(path, "rb+"); char *realPath = realpath(path, NULL);
if (realPath == NULL) {
return false;
}
FILE* fp = fopen(realPath, "rb+");
if (fp == NULL) { if (fp == NULL) {
INIT_LOGE("open %s failed", path); INIT_LOGE("open %s failed", path);
free(realPath);
realPath = NULL;
return false; return false;
} }
size_t ret = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp); size_t ret = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp);
if (ret < 0) { if (ret < 0) {
INIT_LOGE("write to misc failed"); INIT_LOGE("write to misc failed");
free(realPath);
realPath = NULL;
fclose(fp); fclose(fp);
return false; return false;
} }
free(realPath);
realPath = NULL;
fclose(fp); fclose(fp);
return true; return true;
} }
...@@ -66,18 +75,28 @@ static bool RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessag ...@@ -66,18 +75,28 @@ static bool RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessag
INIT_LOGE("path or boot is NULL."); INIT_LOGE("path or boot is NULL.");
return false; return false;
} }
FILE* fp = fopen(path, "rb"); char *realPath = realpath(path, NULL);
if (realPath == NULL) {
return false;
}
FILE* fp = fopen(realPath, "rb");
if (fp == NULL) { if (fp == NULL) {
INIT_LOGE("open %s failed", path); INIT_LOGE("open %s failed", path);
free(realPath);
realPath = NULL;
return false; return false;
} }
size_t ret = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp); size_t ret = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
if (ret <= 0) { if (ret <= 0) {
INIT_LOGE("read to misc failed"); INIT_LOGE("read to misc failed");
free(realPath);
realPath = NULL;
fclose(fp); fclose(fp);
return false; return false;
} }
free(realPath);
realPath = NULL;
fclose(fp); fclose(fp);
return true; return true;
} }
...@@ -166,7 +185,10 @@ void DoReboot(const char *value) ...@@ -166,7 +185,10 @@ void DoReboot(const char *value)
return; return;
} }
const int commandSize = 12; const int commandSize = 12;
snprintf(msg.command, MAX_COMMAND_SIZE, "%s", "boot_updater"); if (snprintf_s(msg.command, MAX_COMMAND_SIZE, MAX_COMMAND_SIZE - 1, "%s", "boot_updater") == -1) {
INIT_LOGE("DoReboot updater: RBMiscWriteUpdaterMessage error");
return;
}
msg.command[commandSize] = 0; msg.command[commandSize] = 0;
if (strlen(valueData) > strlen("updater:") && strncmp(valueData, "updater:", strlen("updater:")) == 0) { if (strlen(valueData) > strlen("updater:") && strncmp(valueData, "updater:", strlen("updater:")) == 0) {
......
...@@ -185,6 +185,9 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ) ...@@ -185,6 +185,9 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ)
} }
char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i)); char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i));
if (fieldStr == NULL) {
return SERVICE_FAILURE;
}
size_t strLen = strlen(fieldStr); size_t strLen = strlen(fieldStr);
curServ->writepidFiles[i] = (char *)malloc(sizeof(char) * strLen + 1); curServ->writepidFiles[i] = (char *)malloc(sizeof(char) * strLen + 1);
if (curServ->writepidFiles[i] == NULL) { if (curServ->writepidFiles[i] == NULL) {
...@@ -246,7 +249,7 @@ static int GetGidArray(const cJSON *curArrItem, Service *curServ) // gid ...@@ -246,7 +249,7 @@ static int GetGidArray(const cJSON *curArrItem, Service *curServ) // gid
{ {
int gIDCnt = 0; int gIDCnt = 0;
cJSON *filedJ = GetArrItem(curArrItem, &gIDCnt, GID_STR_IN_CFG); // "gid" must have 1 item. cJSON *filedJ = GetArrItem(curArrItem, &gIDCnt, GID_STR_IN_CFG); // "gid" must have 1 item.
if ((gIDCnt <= 0) && (filedJ == NULL)) { // not a array, but maybe a item? if ((gIDCnt <= 0) || (filedJ == NULL)) { // not a array, but maybe a item?
return GetGidOneItem(curArrItem, curServ); return GetGidOneItem(curArrItem, curServ);
} }
...@@ -578,6 +581,7 @@ static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ) ...@@ -578,6 +581,7 @@ static int GetServiceOnRestart(const cJSON* curArrItem, Service* curServ)
curServ->onRestart->cmdLine = (CmdLine *)calloc(cmdCnt, sizeof(CmdLine)); curServ->onRestart->cmdLine = (CmdLine *)calloc(cmdCnt, sizeof(CmdLine));
if (curServ->onRestart->cmdLine == NULL) { if (curServ->onRestart->cmdLine == NULL) {
free(curServ->onRestart); free(curServ->onRestart);
curServ->onRestart = NULL;
return SERVICE_FAILURE; return SERVICE_FAILURE;
} }
curServ->onRestart->cmdNum = cmdCnt; curServ->onRestart->cmdNum = cmdCnt;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册