提交 364f1581 编写于 作者: X xionglei6

init: fix codedex

Signed-off-by: Nxionglei6 <xionglei6@huawei.com>
上级 7ee6dc2b
......@@ -152,8 +152,12 @@ void FsManagerLogInit(LogTarget target, const char *fileName)
break;
case LOG_TO_FILE:
if (fileName != NULL && *fileName != '\0') {
g_logFile = fopen(fileName, "a+");
setbuf(g_logFile, NULL);
char *realPath = GetRealPath(fileName);
if (realPath != NULL) {
g_logFile = fopen(realPath, "a+");
setbuf(g_logFile, NULL);
free(realPath);
}
// Do not check return values. The log writte function will do this.
}
g_logFunc = FsManagerLogToFile;
......
......@@ -191,12 +191,13 @@ Fstab *ReadFstabFromFile(const char *file, bool procMounts)
ssize_t readn = 0;
Fstab *fstab = NULL;
if (file == NULL) {
char *realPath = GetRealPath(file);
if (realPath == NULL) {
FSMGR_LOGE("Invalid file");
return NULL;
}
FILE *fp = fopen(file, "r");
FILE *fp = fopen(realPath, "r");
free(realPath);
if (fp == NULL) {
FSMGR_LOGE("Open %s failed, err = %d", file, errno);
return NULL;
......@@ -250,7 +251,7 @@ FstabItem *FindFstabItemForMountPoint(Fstab fstab, const char *mp)
FstabItem *item = NULL;
if (mp != NULL) {
for (item = fstab.head; item != NULL; item = item->next) {
if (strcmp(item->mountPoint, mp) == 0) {
if ((item->mountPoint != NULL) && (strcmp(item->mountPoint, mp) == 0)) {
break;
}
}
......
......@@ -55,6 +55,7 @@ static std::string GetMiscDevicePath()
FstabItem *misc = FindFstabItemForMountPoint(*fstab, "/misc");
if (misc == nullptr) {
std::cout << "Cannot find misc partition from fstab\n";
ReleaseFstab(fstab);
return miscDev;
}
miscDev = misc->deviceName;
......@@ -86,22 +87,27 @@ static void WriteLogoContent(int fd, const std::string &logoPath, uint32_t size)
std::cout << "path is null or size illegal\n";
return;
}
FILE* rgbFile = fopen(logoPath.c_str(), "rb");
FILE *rgbFile = fopen(logoPath.c_str(), "rb");
if (rgbFile == nullptr) {
std::cout << "cannot find pic file\n";
return;
}
char* buffer = (char*)malloc(size);
char *buffer = (char*)malloc(size);
if (buffer == nullptr) {
(void)fclose(rgbFile);
return;
}
uint32_t ret = fread(buffer, 1, size, rgbFile);
if (ret < 0) {
(void)fclose(rgbFile);
free(buffer);
return;
}
ret = write(fd, buffer, size);
if (ret != size) {
(void)fclose(rgbFile);
free(buffer);
return;
}
......@@ -194,6 +200,7 @@ static void WriteLogoToMisc(const std::string &logoPath)
int fd1 = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (lseek(fd1, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to seek file\n";
close(fd1);
return;
}
......
......@@ -56,6 +56,7 @@ static char *AddOneArg(const char *param, size_t paramLen)
char *BuildStringFromCmdArg(const struct CmdArgs *ctx, int startIndex)
{
INIT_ERROR_CHECK(ctx != NULL, return NULL, "Failed to get cmd args ");
char *options = (char *)calloc(1, OPTIONS_SIZE + 1);
INIT_ERROR_CHECK(options != NULL, return NULL, "Failed to get memory ");
options[0] = '\0';
......@@ -336,7 +337,7 @@ static int GetMountFlag(unsigned long *mountflag, const char *targetStr, const c
INIT_CHECK_RETURN_VALUE(targetStr != NULL && mountflag != NULL, 0);
struct {
char *flagName;
int value;
unsigned long value;
} mountFlagMap[] = {
{ "noatime", MS_NOATIME },
{ "noexec", MS_NOEXEC },
......@@ -416,18 +417,16 @@ static void DoWrite(const struct CmdArgs *ctx)
int fd = -1;
if (realPath != NULL) {
fd = open(realPath, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR);
free(realPath);
realPath = NULL;
} else {
fd = open(ctx->argv[0], O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, S_IRUSR | S_IWUSR);
}
if (fd >= 0) {
size_t ret = write(fd, ctx->argv[1], strlen(ctx->argv[1]));
INIT_CHECK_ONLY_ELOG(ret >= 0, "DoWrite: write to file %s failed: %d", ctx->argv[0], errno);
close(fd);
}
if (realPath != NULL) {
free(realPath);
}
realPath = NULL;
close(fd);
}
static void DoRmdir(const struct CmdArgs *ctx)
......
......@@ -50,7 +50,7 @@ static int ParseInitCfg(const char *configFile, void *context)
static void ParseAllImports(const cJSON *root)
{
char *tmpParamValue = malloc(PARAM_VALUE_LEN_MAX + 1);
char *tmpParamValue = calloc(sizeof(char), PARAM_VALUE_LEN_MAX + 1);
INIT_ERROR_CHECK(tmpParamValue != 0, return, "Failed to alloc memory for param");
cJSON *importAttr = cJSON_GetObjectItemCaseSensitive(root, "import");
......
......@@ -326,6 +326,7 @@ static int AddServiceSocket(cJSON *json, Service *service)
sockopt->gid = DecodeUid(opt[SERVICE_SOCK_GID]);
if (sockopt->uid == (uid_t)-1 || sockopt->gid == (uid_t)-1) {
free(sockopt);
sockopt = NULL;
INIT_LOGE("Invalid uid %d or gid %d", sockopt->uid, sockopt->gid);
return SERVICE_FAILURE;
}
......@@ -399,6 +400,7 @@ static int AddServiceFile(cJSON *json, Service *service)
fileOpt->gid = DecodeUid(opt[SERVICE_FILE_GID]);
if (fileOpt->uid == (uid_t)-1 || fileOpt->gid == (gid_t)-1) {
free(fileOpt);
fileOpt = NULL;
INIT_LOGE("Invalid uid %d or gid %d", fileOpt->uid, fileOpt->gid);
return SERVICE_FAILURE;
}
......@@ -641,10 +643,13 @@ Service *GetServiceByPid(pid_t pid)
Service *GetServiceByName(const char *servName)
{
INIT_ERROR_CHECK(servName != NULL, return NULL, "Failed get servName");
ListNode *node = g_serviceSpace.services.next;
while (node != &g_serviceSpace.services) {
Service *service = ListEntry(node, Service, node);
INIT_CHECK_RETURN_VALUE(strcmp(service->name, servName) != 0, service);
if ((service != NULL) && (service->name != NULL)) {
INIT_CHECK_RETURN_VALUE(strcmp(service->name, servName) != 0, service);
}
node = node->next;
}
return NULL;
......
......@@ -18,7 +18,7 @@
#include "init_log.h"
#include "securec.h"
int MountRequriedPartitions(Fstab *fstab)
int MountRequriedPartitions(const Fstab *fstab)
{
INIT_ERROR_CHECK(fstab != NULL, return -1, "Failed fstab is NULL");
int rc = -1;
......
......@@ -23,7 +23,7 @@
extern "C" {
#endif
#endif
int MountRequriedPartitions(Fstab *fstab);
int MountRequriedPartitions(const Fstab *fstab);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -38,16 +38,16 @@ static int RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdate
INIT_CHECK_RETURN_VALUE(realPath != NULL, -1);
int ret = 0;
FILE *fp = fopen(realPath, "rb+");
free(realPath);
realPath = NULL;
if (fp != NULL) {
size_t writeLen = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp);
INIT_ERROR_CHECK(writeLen == 1, ret = -1, "Failed to write misc for reboot");
(void)fclose(fp);
} else {
ret = -1;
INIT_LOGE("Failed to open %s", path);
}
free(realPath);
realPath = NULL;
(void)fclose(fp);
return ret;
}
......@@ -57,17 +57,16 @@ static int RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessage
INIT_CHECK_RETURN_VALUE(realPath != NULL, -1);
int ret = 0;
FILE *fp = fopen(realPath, "rb");
free(realPath);
realPath = NULL;
if (fp != NULL) {
size_t readLen = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
INIT_ERROR_CHECK(readLen > 0, ret = -1, "Failed to read misc for reboot");
(void)fclose(fp);
} else {
ret = -1;
INIT_LOGE("Failed to open %s", path);
}
free(realPath);
realPath = NULL;
(void)fclose(fp);
return ret;
}
......
......@@ -135,6 +135,15 @@ static int MountToNewTarget(const char *target)
return 0;
}
static void FreeRootDir(DIR *oldRoot, dev_t dev)
{
if (oldRoot != NULL) {
FreeOldRoot(oldRoot, dev);
closedir(oldRoot);
oldRoot = NULL;
}
}
// Switch root from ramdisk to system
int SwitchRoot(const char *newRoot)
{
......@@ -157,35 +166,39 @@ int SwitchRoot(const char *newRoot)
struct stat newRootStat = {};
if (stat(newRoot, &newRootStat) != 0) {
INIT_LOGE("Failed to get new root \" %s \" stat", newRoot);
FreeRootDir(oldRoot, oldRootStat.st_dev);
return -1;
}
if (oldRootStat.st_dev == newRootStat.st_dev) {
INIT_LOGW("Try to switch root in same device, skip switching root");
FreeRootDir(oldRoot, oldRootStat.st_dev);
return 0;
}
if (MountToNewTarget(newRoot) < 0) {
INIT_LOGE("Failed to move mount to new root \" %s \" stat", newRoot);
FreeRootDir(oldRoot, oldRootStat.st_dev);
return -1;
}
// OK, we've done move mount.
// Now mount new root.
if (chdir(newRoot) < 0) {
INIT_LOGE("Failed to change directory to %s, err = %d", newRoot, errno);
FreeRootDir(oldRoot, oldRootStat.st_dev);
return -1;
}
if (mount(newRoot, "/", NULL, MS_MOVE, NULL) < 0) {
INIT_LOGE("Failed to mount moving %s to %s, err = %d", newRoot, "/", errno);
FreeRootDir(oldRoot, oldRootStat.st_dev);
return -1;
}
if (chroot(".") < 0) {
INIT_LOGE("Failed to change root directory");
FreeRootDir(oldRoot, oldRootStat.st_dev);
return -1;
}
FreeOldRoot(oldRoot, oldRootStat.st_dev);
closedir(oldRoot);
oldRoot = NULL;
FreeRootDir(oldRoot, oldRootStat.st_dev);
return 0;
}
......@@ -66,7 +66,7 @@ int UeventdSocketInit(void)
ssize_t ReadUeventMessage(int sockFd, char *buffer, size_t length)
{
ssize_t n = -1;
struct msghdr msghdr;
struct msghdr msghdr = {};
struct iovec iov;
struct sockaddr_nl addr;
char credMsg[CMSG_SPACE(sizeof(struct ucred))];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册