提交 284caa22 编写于 作者: S sun_fan

init: modify code style

Signed-off-by: Nsun_fan <sun_fan1@hoperun.com>
上级 d5437075
......@@ -50,7 +50,7 @@ struct CmdArgs {
char **argv;
};
int GetParamValue(char *symValue, char *paramValue, unsigned int paramLen);
int GetParamValue(const char *symValue, char *paramValue, unsigned int paramLen);
struct CmdArgs* GetCmd(const char *cmdContent, const char *delim, int argsCount);
void FreeCmd(struct CmdArgs **cmd);
......
......@@ -53,7 +53,7 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...)
return;
}
if (tag == NULL) {
return;
tag = "Init";
}
va_list list;
va_start(list, fmt);
......
......@@ -22,8 +22,8 @@
#include <unistd.h>
#include "init_log.h"
#define DEFAULT_RW_MODE 0666
#define DEFAULT_NO_AUTHORITY_MODE 0600
#define DEFAULT_RW_MODE S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP | S_IROTH | S_IWOTH
#define DEFAULT_NO_AUTHORITY_MODE S_IWUSR | S_IRUSR
#define DEVICE_ID_THIRD 3
#define DEVICE_ID_EIGHTH 8
#define DEVICE_ID_NINTH 9
......
......@@ -42,7 +42,7 @@
#include "init_utils.h"
#include "securec.h"
#define DEFAULT_DIR_MODE 0755 // mkdir, default mode
#define DEFAULT_DIR_MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH // 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
......@@ -88,7 +88,7 @@ static const char* g_supportedCmds[] = {
};
#ifndef OHOS_LITE
int GetParamValue(char *symValue, char *paramValue, unsigned int paramLen)
int GetParamValue(const char *symValue, char *paramValue, unsigned int paramLen)
{
if ((symValue == NULL) || (paramValue == NULL) || (paramLen == 0)) {
return -1;
......@@ -143,7 +143,7 @@ int GetParamValue(char *symValue, char *paramValue, unsigned int paramLen)
}
#else
// For ite ohos, do not support parameter operation. just do string copy
inline int GetParamValue(char *symValue, char *paramValue, unsigned int paramLen)
inline int GetParamValue(const char *symValue, char *paramValue, unsigned int paramLen)
{
return (strncpy_s(paramValue, paramLen, symValue, strlen(symValue)) == EOK) ? 0 : -1;
}
......@@ -777,7 +777,7 @@ static void DoLoadCfg(const char *path)
return;
}
while (fgets(buf, LOADCFG_BUF_SIZE, fp) != NULL && maxLoop < LOADCFG_MAX_LOOP) {
while (fgets(buf, LOADCFG_BUF_SIZE - 1, fp) != NULL && maxLoop < LOADCFG_MAX_LOOP) {
maxLoop++;
len = strlen(buf);
if (len < 1) {
......
......@@ -29,6 +29,9 @@ static int ExtractCfgFile(char **cfgFile, char *content)
return -1;
}
size_t cfgFileLen = strlen(content) + MAX_PARAM_VALUE_LEN + 1;
if (cfgFileLen <= 0) {
return -1;
}
if ((*cfgFile = malloc(cfgFileLen)) == NULL) {
INIT_LOGW("Failed to allocate memory to import cfg file. err = %d", errno);
return -1;
......
......@@ -113,7 +113,7 @@ static int GetMountStatusForMountPoint(const char *mountPoint)
return 1;
}
while (fgets(buffer, sizeof(buffer), fp) != NULL) {
while (fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
n = strlen(buffer);
if (buffer[n - 1] == '\n') {
buffer[n - 1] = '\0';
......
......@@ -200,14 +200,22 @@ int ServiceStart(Service *service)
if (service->writepidFiles[i] == NULL) {
continue;
}
FILE *fd = fopen(service->writepidFiles[i], "wb");
char *realPath = realpath(service->writepidFiles[i], NULL);
if (realPath == NULL) {
continue;
}
FILE *fd = fopen(realPath, "wb");
if (fd == NULL) {
INIT_LOGE("start service writepidFiles %s invalid.", service->writepidFiles[i]);
free(realPath);
realPath = NULL;
continue;
}
if (fwrite(pidString, 1, strlen(pidString), fd) != strlen(pidString)) {
INIT_LOGE("start service writepid error.file:%s pid:%s", service->writepidFiles[i], pidString);
}
free(realPath);
realPath = NULL;
fclose(fd);
INIT_LOGE("ServiceStart writepid filename=%s, childPid=%s, ok", service->writepidFiles[i],
pidString);
......
......@@ -178,14 +178,12 @@ static int GetWritepidStrings(const cJSON *curArrItem, Service *curServ)
}
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
INIT_LOGE("GetWritepidStrings, parse item[%d] error.", i);
cJSON *item = cJSON_GetArrayItem(filedJ, i);
if (item == NULL) {
return SERVICE_FAILURE;
}
char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i));
if (fieldStr == NULL) {
char *fieldStr = cJSON_GetStringValue(item);
if ((fieldStr == NULL) || (fieldStr[0] == '\0')) {
return SERVICE_FAILURE;
}
size_t strLen = strlen(fieldStr);
......@@ -267,12 +265,14 @@ static int GetGidArray(const cJSON *curArrItem, Service *curServ) // gid
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
INIT_LOGE("GetGidArray, parse item[%d] as string, error.", i);
cJSON *item = cJSON_GetArrayItem(filedJ, i);
if (item == NULL) {
break;
}
char *fieldStr = cJSON_GetStringValue(item);
if ((fieldStr == NULL) || (fieldStr[0] == '\0')) {
break;
}
char *fieldStr = cJSON_GetStringValue(cJSON_GetArrayItem(filedJ, i));
gid_t gID = DecodeUid(fieldStr);
if ((gID) == (gid_t)(-1)) {
INIT_LOGE("GetGidArray, DecodeUid item[%d] error.", i);
......@@ -284,11 +284,14 @@ static int GetGidArray(const cJSON *curArrItem, Service *curServ) // gid
return SERVICE_SUCCESS;
}
for (i = 0; i < gIDCnt; ++i) {
if (cJSON_GetArrayItem(filedJ, i) == NULL || !cJSON_IsNumber(cJSON_GetArrayItem(filedJ, i))) {
INIT_LOGE("GetGidArray, parse item[%d] as number, error.", i);
cJSON *item = cJSON_GetArrayItem(filedJ, i);
if (item == NULL) {
break;
}
if (!cJSON_IsNumber(item)) {
break;
}
gid_t gID = (int)cJSON_GetNumberValue(cJSON_GetArrayItem(filedJ, i));
gid_t gID = (int)cJSON_GetNumberValue(item);
if (gID < 0) {
INIT_LOGE("GetGidArray gID = %d, error", gID);
break;
......@@ -533,7 +536,7 @@ static int GetServiceSocket(const cJSON* curArrItem, Service* curServ)
}
curServ->socketCfg = NULL;
for (int i = 0; i < sockCnt; ++i) {
cJSON* sockJ = cJSON_GetArrayItem(filedJ, i);
cJSON *sockJ = cJSON_GetArrayItem(filedJ, i);
if (!cJSON_IsString(sockJ) || !cJSON_GetStringValue(sockJ)) {
return SERVICE_FAILURE;
}
......@@ -549,6 +552,8 @@ static int GetServiceSocket(const cJSON* curArrItem, Service* curServ)
}
int ret = ParseServiceSocket(tmpStr, SOCK_OPT_NUMS, socktmp);
if (ret < 0) {
free(socktmp);
socktmp = NULL;
return SERVICE_FAILURE;
}
if (curServ->socketCfg == NULL) {
......
......@@ -92,7 +92,7 @@ static int CreateSocket(struct ServiceSocket *sockopt)
return sockopt->sockFd;
}
static int SetSocketEnv(int fd, char *name)
static int SetSocketEnv(int fd, const char *name)
{
if (name == NULL) {
return -1;
......
......@@ -65,11 +65,11 @@ int DecodeUid(const char *name)
}
return result;
} else {
struct passwd *pwd = getpwnam(name);
if (pwd == NULL) {
struct passwd *userInf = getpwnam(name);
if (userInf == NULL) {
return -1;
}
return pwd->pw_uid;
return userInf->pw_uid;
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册