提交 5b5de9f7 编写于 作者: O openharmony_ci 提交者: Gitee

!122 fix:fix init bugs

Merge pull request !122 from 熊磊/init917_2
......@@ -14,49 +14,48 @@
*/
#include "init_reboot.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include "init_log.h"
#include "param.h"
#include "securec.h"
#include "sys_param.h"
#define SYS_POWER_CTRL "sys.powerctrl="
#define MAX_REBOOT_NAME_SIZE 100
#define MAX_REBOOT_VAUE_SIZE 500
// Refer to parameter limit, value size should not bigger than 96
#define MAX_REBOOT_OPTION_SIZE PARAM_VALUE_LEN_MAX
int DoReboot(const char *cmdContent)
int DoReboot(const char *option)
{
uid_t uid1 = getuid();
uid_t uid2 = geteuid();
if (uid1 != 0 || uid2 != 0) {
INIT_LOGE("uid1=%d, uid2=%d, user MUST be root, error!", uid1, uid2);
uid_t uid = getuid();
uid_t euid = geteuid();
if (uid != 0 || euid != 0) {
INIT_LOGE("User or effective user MUST be root, abort!");
return -1;
}
char value[MAX_REBOOT_VAUE_SIZE];
if (cmdContent == NULL || strlen(cmdContent) == 0) {
if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, strlen("reboot") + 1, "%s", "reboot") < 0) {
INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough");
char value[MAX_REBOOT_OPTION_SIZE];
if (option == NULL || strlen(option) == 0) {
if (snprintf_s(value, MAX_REBOOT_OPTION_SIZE, strlen("reboot") + 1, "%s", "reboot") < 0) {
INIT_LOGE("reboot options too large, overflow");
return -1;
}
if (SystemSetParameter("sys.powerctrl", value) != 0) {
INIT_LOGE("DoReboot Api SystemSetParameter error");
INIT_LOGE("Set parameter to trigger reboot command \" %s \" failed", value);
return -1;
}
return 0;
}
size_t length = strlen(cmdContent);
if (length > MAX_REBOOT_VAUE_SIZE) {
INIT_LOGE("DoReboot api error, cmdContent = %s, length = %d.", cmdContent, length);
int length = strlen(option);
if (length > MAX_REBOOT_OPTION_SIZE) {
INIT_LOGE("Reboot option \" %s \" is too large, overflow", option);
return -1;
}
if (snprintf_s(value, MAX_REBOOT_NAME_SIZE, MAX_REBOOT_NAME_SIZE - 1, "%s%s", "reboot,", cmdContent) < 0) {
INIT_LOGE("DoReboot api error, MAX_REBOOT_NAME_SIZE is not enough");
if (snprintf_s(value, MAX_REBOOT_OPTION_SIZE, MAX_REBOOT_OPTION_SIZE - 1, "%s%s", "reboot,", option) < 0) {
INIT_LOGE("Failed to copy boot option \" %s \"", option);
return -1;
}
if (SystemSetParameter("sys.powerctrl", value) != 0) {
INIT_LOGE("DoReboot Api SystemSetParameter error");
INIT_LOGE("Set parameter to trigger reboot command \" %s \" failed", value);
return -1;
}
return 0;
......
......@@ -31,14 +31,15 @@ if (defined(ohos_lite)) {
"src/init_service_manager.c",
"src/init_service_socket.c",
"src/init_signal_handler.c",
"src/init_utils.c",
"src/main.c",
"utils/init_utils.c",
]
include_dirs = [
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/param/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/utils",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//base/startup/syspara_lite/interfaces/kits",
......@@ -99,13 +100,13 @@ if (defined(ohos_lite)) {
"src/init_service_manager.c",
"src/init_service_socket.c",
"src/init_signal_handler.c",
"src/init_utils.c",
"src/main.c",
]
include_dirs = [
"//base/startup/init_lite/services/include/param",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/utils",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//third_party/libuv/include",
......@@ -113,6 +114,7 @@ if (defined(ohos_lite)) {
deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/param:param_service",
"//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
......
......@@ -30,7 +30,6 @@ extern "C" {
void MountBasicFs(void);
void CreateDeviceNode(void);
int MakeSocketDir(const char *path, mode_t mode);
void CloseStdio(void);
#ifdef __cplusplus
......
......@@ -68,8 +68,6 @@ typedef struct {
int pid;
int crashCnt;
time_t firstCrashTime;
int criticalCrashCnt; // count for critical
time_t firstCriticalCrashTime; // record for critical
char *writepidFiles[MAX_WRITEPID_FILES];
unsigned int attribute;
int importance;
......
......@@ -27,7 +27,6 @@
#include <sys/types.h>
#include <unistd.h>
#include "init_utils.h"
#include "param_utils.h"
#include "sys_param.h"
......
......@@ -27,7 +27,6 @@
#include <unistd.h>
#include "init_cmds.h"
#include "init_utils.h"
#include "param_manager.h"
#include "trigger_checker.h"
......
......@@ -77,18 +77,3 @@ void CreateDeviceNode(void)
}
}
int MakeSocketDir(const char *path, mode_t mode)
{
int rc = mkdir("/dev/unix/", mode);
if (rc < 0 && errno != EEXIST) {
INIT_LOGE("Create %s failed. %d", path, errno);
return -1;
}
rc = mkdir("/dev/unix/socket/", mode);
if (rc < 0 && errno != EEXIST) {
INIT_LOGE("Create %s failed. %d", path, errno);
return -1;
}
return rc;
}
......@@ -175,13 +175,13 @@ struct CmdArgs *GetCmd(const char *cmdContent, const char *delim, int argsCount)
ctx->argc = 0;
token = strstr(p, delim);
if (token == NULL) { // No whitespaces
// Make surce there is enough memory to store parameter value
// Make sure there is enough memory to store parameter value
allocSize = (size_t)(cmdLength + MAX_PARAM_VALUE_LEN + 1);
return CopyCmd(ctx, p, allocSize);
}
while (token != NULL) {
// Too more arguments, treat rest of data as one argument
// Too many arguments, treat rest of data as one argument
if (ctx->argc == (argsCount - 1)) {
break;
}
......@@ -223,23 +223,18 @@ static void WriteCommon(const char *file, char *buffer, int flags, mode_t mode)
return;
}
char realPath[PATH_MAX] = {0};
char *rp = realpath(file, realPath);
if (rp == NULL) {
INIT_LOGE("Failed resolve real path name of %s", rp);
return;
}
int fd = open(rp, flags, mode);
if (fd >= 0) {
size_t totalSize = strlen(buffer);
size_t written = WriteAll(fd, buffer, totalSize);
if (written != totalSize) {
INIT_LOGE("Write %lu bytes to file failed", totalSize, file);
if (Realpath(file, realPath, sizeof(realPath)) != NULL) {
int fd = open(realPath, flags, mode);
if (fd >= 0) {
size_t totalSize = strlen(buffer);
size_t written = WriteAll(fd, buffer, totalSize);
if (written != totalSize) {
INIT_LOGE("Write %lu bytes to file failed", totalSize, file);
}
close(fd);
}
close(fd);
fd = -1;
}
fd = -1;
}
static void DoSetDomainname(const char *cmdContent, int maxArg)
......@@ -437,22 +432,18 @@ static void DoCopy(const char *cmdContent, int maxArg)
FreeCmd(ctx);
return;
}
char *sourceFile = realpath(ctx->argv[0], NULL);
char *targetFile = realpath(ctx->argv[1], NULL);
if (sourceFile == NULL || targetFile == NULL) {
INIT_LOGE("Failed resolve real path name in copy command");
char srcPath[PATH_MAX] = {0};
char dstPath[PATH_MAX] = {0};
if ((Realpath(ctx->argv[0], srcPath, sizeof(srcPath)) == NULL) ||
(Realpath(ctx->argv[1], dstPath, sizeof(dstPath)) == NULL)) {
FreeCmd(ctx);
ctx = NULL;
return;
}
DoCopyInernal(sourceFile, targetFile);
DoCopyInernal(srcPath, dstPath);
FreeCmd(ctx);
free(sourceFile);
free(targetFile);
ctx = NULL;
sourceFile = NULL;
targetFile = NULL;
return;
}
......@@ -539,7 +530,7 @@ static void DoMkDir(const char *cmdContent, int maxArg)
} while (0);
if (rc < 0) {
INIT_LOGE("Run command mkdir %s failed err = %d", ctx->argv[0], errno);
INIT_LOGE("Run command mkdir %s failed err = %d ", ctx->argv[0], errno);
(void)rmdir(ctx->argv[0]);
}
FreeCmd(ctx);
......@@ -740,15 +731,13 @@ static void DoInsmodInternal(const char *fileName, const char *secondPtr, const
if (fileName == NULL) {
return;
}
char *realPath = realpath(fileName, NULL);
if (realPath == NULL) {
char realPath[PATH_MAX] = {0};
if (Realpath(fileName, realPath, sizeof(realPath)) == NULL) {
return;
}
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);
......@@ -758,8 +747,6 @@ static void DoInsmodInternal(const char *fileName, const char *secondPtr, const
if (fd >= 0) {
close(fd);
}
free(realPath);
realPath = NULL;
return;
}
......@@ -904,13 +891,13 @@ static void DoLoadCfg(const char *path, int maxArg)
return;
}
INIT_ERROR_CHECK(path != NULL, return, "CheckCfg path is NULL.");
char *realPath = realpath(path, NULL);
INIT_CHECK_ONLY_RETURN(realPath != NULL);
char realPath[PATH_MAX] = {0};
if (Realpath(path, realPath, sizeof(realPath)) == NULL) {
return;
}
fp = fopen(realPath, "r");
if (fp == NULL) {
INIT_LOGE("open cfg error = %d", errno);
free(realPath);
realPath = NULL;
return;
}
......@@ -918,8 +905,6 @@ static void DoLoadCfg(const char *path, int maxArg)
if (cmdLine == NULL) {
INIT_LOGE("malloc cmdline error");
fclose(fp);
free(realPath);
realPath = NULL;
return;
}
......@@ -937,8 +922,6 @@ static void DoLoadCfg(const char *path, int maxArg)
DoCmd(cmdLine);
(void)memset_s(buf, sizeof(char) * LOADCFG_BUF_SIZE, 0, sizeof(char) * LOADCFG_BUF_SIZE);
}
free(realPath);
realPath = NULL;
free(cmdLine);
fclose(fp);
}
......
......@@ -16,6 +16,7 @@
#include "init_reboot.h"
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -44,29 +45,24 @@ static bool RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdat
INIT_LOGE("path or boot is NULL.");
return false;
}
char *realPath = realpath(path, NULL);
if (realPath == NULL) {
char realPath[PATH_MAX] = {0};
if (Realpath(path, realPath, sizeof(realPath)) == NULL) {
return false;
}
FILE* fp = fopen(realPath, "rb+");
FILE *fp = fopen(realPath, "rb+");
if (fp == NULL) {
INIT_LOGE("open %s failed", path);
free(realPath);
realPath = NULL;
return false;
}
size_t ret = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp);
if (ret < 0) {
INIT_LOGE("write to misc failed");
free(realPath);
realPath = NULL;
fclose(fp);
(void)fclose(fp);
return false;
}
free(realPath);
realPath = NULL;
fclose(fp);
(void)fclose(fp);
return true;
}
......@@ -76,29 +72,23 @@ static bool RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessag
INIT_LOGE("path or boot is NULL.");
return false;
}
char *realPath = realpath(path, NULL);
if (realPath == NULL) {
char realPath[PATH_MAX] = {0};
if (Realpath(path, realPath, sizeof(realPath)) == NULL) {
return false;
}
FILE* fp = fopen(realPath, "rb");
FILE *fp = fopen(realPath, "rb");
if (fp == NULL) {
INIT_LOGE("open %s failed", path);
free(realPath);
realPath = NULL;
return false;
}
size_t ret = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
if (ret <= 0) {
INIT_LOGE("read to misc failed");
free(realPath);
realPath = NULL;
fclose(fp);
(void)fclose(fp);
return false;
}
free(realPath);
realPath = NULL;
fclose(fp);
(void)fclose(fp);
return true;
}
......@@ -120,13 +110,13 @@ static int GetMountStatusForMountPoint(const char *mountPoint)
buffer[n - 1] = '\0';
}
if (strstr(buffer, mountPoint) != NULL) {
fclose(fp);
(void)fclose(fp);
return 1;
}
}
// Cannot find it from system.
fclose(fp);
(void)fclose(fp);
return 0;
}
......@@ -155,8 +145,24 @@ static int CheckAndRebootToUpdater(const char *valueData, const char *cmd, const
return -1;
}
static int CheckRebootValue(const char **cmdParams, const char *valueData)
{
size_t i = 0;
for (; i < ARRAY_LENGTH(cmdParams); i++) {
if (strncmp(valueData, cmdParams[i], strlen(cmdParams[i])) == 0) {
break;
}
}
if (i >= ARRAY_LENGTH(cmdParams)) {
INIT_LOGE("DoReboot valueData = %s, parameters error.", valueData);
return -1;
}
return 0;
}
void DoReboot(const char *value)
{
#ifndef OHOS_LITE
static const char *g_cmdParams[] = {
"shutdown", "updater", "updater:", "flashing", "flashing:", "NoArgument", "bootloader"
};
......@@ -174,14 +180,7 @@ void DoReboot(const char *value)
valueData = value + strlen("reboot,");
}
if (valueData != NULL) {
size_t i = 0;
for (; i < ARRAY_LENGTH(g_cmdParams); i++) {
if (strncmp(valueData, g_cmdParams[i], strlen(g_cmdParams[i])) == 0) {
break;
}
}
if (i >= ARRAY_LENGTH(g_cmdParams)) {
INIT_LOGE("DoReboot value = %s, parameters error.", value);
if (CheckRebootValue(g_cmdParams, valueData) < 0) {
return;
}
}
......@@ -206,5 +205,11 @@ void DoReboot(const char *value)
ret = CheckAndRebootToUpdater(valueData, "flashing", "flashing:", "boot_flashing");
}
INIT_LOGI("DoReboot value = %s %s.", value, (ret == 0) ? "success" : "fail");
#else
int ret = reboot(RB_AUTOBOOT);
if (ret != 0) {
INIT_LOGE("reboot failed! syscall ret %d, err %d.", ret, errno);
}
#endif
return;
}
......@@ -17,6 +17,7 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
......@@ -39,6 +40,7 @@
#include "init_param.h"
#endif
#include "init_perms.h"
#include "init_reboot.h"
#include "init_service_socket.h"
#include "init_utils.h"
#include "securec.h"
......@@ -253,29 +255,24 @@ int ServiceStop(Service *service)
return SERVICE_SUCCESS;
}
// the service need to be restarted, if it crashed more than 4 times in 4 minutes
void CheckCritical(Service *service)
static bool CalculateCrashTime(Service *service, int crashTimeLimit, int crashCountLimit)
{
if (service == NULL) {
return;
}
if (service->attribute & SERVICE_ATTR_CRITICAL) { // critical
// crash time and count check
time_t curTime = time(NULL);
if (service->criticalCrashCnt == 0) {
service->firstCriticalCrashTime = curTime;
++service->criticalCrashCnt;
} else if (difftime(curTime, service->firstCriticalCrashTime) > CRITICAL_CRASH_TIME_LIMIT) {
service->firstCriticalCrashTime = curTime;
service->criticalCrashCnt = 1;
} else {
++service->criticalCrashCnt;
if (service->criticalCrashCnt > CRITICAL_CRASH_COUNT_LIMIT) {
INIT_LOGE("reap critical service %s, crash too many times! Need reboot!", service->name);
RebootSystem();
}
INIT_ERROR_CHECK(service != NULL && crashTimeLimit > 0 && crashCountLimit > 0, return 0,
"Service name=%s, input params error.", service->name);
time_t curTime = time(NULL);
if (service->crashCnt == 0) {
service->firstCrashTime = curTime;
++service->crashCnt;
} else if (difftime(curTime, service->firstCrashTime) > crashTimeLimit) {
service->firstCrashTime = curTime;
service->crashCnt = 1;
} else {
++service->crashCnt;
if (service->crashCnt > crashCountLimit) {
return false;
}
}
return true;
}
static int ExecRestartCmd(const Service *service)
......@@ -328,25 +325,18 @@ void ServiceReap(Service *service)
return;
}
}
// the service that does not need to be restarted restarts, indicating that it has crashed
if (!(service->attribute & SERVICE_ATTR_NEED_RESTART)) {
// crash time and count check
time_t curTime = time(NULL);
if (service->crashCnt == 0) {
service->firstCrashTime = curTime;
++service->crashCnt;
} else if (difftime(curTime, service->firstCrashTime) > CRASH_TIME_LIMIT) {
service->firstCrashTime = curTime;
service->crashCnt = 1;
} else {
++service->crashCnt;
if (service->crashCnt > CRASH_COUNT_LIMIT) {
INIT_LOGE("reap service %s, crash too many times!", service->name);
return;
}
if (service->attribute & SERVICE_ATTR_CRITICAL) { // critical
if (CalculateCrashTime(service, CRITICAL_CRASH_TIME_LIMIT, CRITICAL_CRASH_COUNT_LIMIT) == false) {
INIT_LOGE("Critical service \" %s \" crashed %d times, rebooting system",
service->name, CRITICAL_CRASH_COUNT_LIMIT);
DoReboot("reboot");
}
} else if (!(service->attribute & SERVICE_ATTR_NEED_RESTART)) {
if (CalculateCrashTime(service, CRASH_TIME_LIMIT, CRASH_COUNT_LIMIT) == false) {
INIT_LOGE("Service name=%s, crash %d times, no more start.", service->name, CRASH_COUNT_LIMIT);
return;
}
}
CheckCritical(service);
if (service->onRestart != NULL) {
INIT_CHECK_ONLY_ELOG(ExecRestartCmd(service) == SERVICE_SUCCESS, "SetOnRestart fail ");
}
......
......@@ -36,6 +36,7 @@
#include "device.h"
#include "init_param.h"
#endif
#include "init_utils.h"
static const pid_t INIT_PROCESS_PID = 1;
......@@ -90,7 +91,7 @@ int main(int argc, char **argv)
MountBasicFs();
CreateDeviceNode();
EnableDevKmsg();
MakeSocketDir("/dev/unix/socket/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
MakeDirRecursive("/dev/unix/socket", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
#endif
SignalInitModule();
......
......@@ -31,6 +31,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/param/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/utils",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//base/startup/syspara_lite/interfaces/kits",
......@@ -50,7 +51,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/src/init_service_manager.c",
"//base/startup/init_lite/services/src/init_service_socket.c",
"//base/startup/init_lite/services/src/init_signal_handler.c",
"//base/startup/init_lite/services/src/init_utils.c",
"//base/startup/init_lite/services/utils/init_utils.c",
"cmd_func_test.cpp",
]
......
# Copyright (c) 2021 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.
import("//build/ohos.gni")
ohos_static_library("libinit_utils") {
sources = [ "init_utils.c" ]
include_dirs = [
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/utils",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//third_party/bounds_checking_function:libsec_static",
]
part_name = "init"
}
......@@ -13,20 +13,19 @@
* limitations under the License.
*/
#include "init_utils.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include "init_log.h"
#include "init_utils.h"
#include "securec.h"
#define WAIT_MAX_COUNT 10
......@@ -74,10 +73,10 @@ uid_t DecodeUid(const char *name)
}
}
char* ReadFileToBuf(const char *configFile)
char *ReadFileToBuf(const char *configFile)
{
char* buffer = NULL;
FILE* fd = NULL;
char *buffer = NULL;
FILE *fd = NULL;
struct stat fileStat = {0};
if (configFile == NULL || *configFile == '\0') {
return NULL;
......@@ -133,9 +132,10 @@ int SplitString(char *srcPtr, char **dstPtr, int maxNum)
void WaitForFile(const char *source, unsigned int maxCount)
{
if (maxCount > WAIT_MAX_COUNT) {
unsigned int maxCountTmp = maxCount;
if (maxCountTmp > WAIT_MAX_COUNT) {
INIT_LOGE("WaitForFile max time is 5s");
maxCount = WAIT_MAX_COUNT;
maxCountTmp = WAIT_MAX_COUNT;
}
struct stat sourceInfo;
const unsigned int waitTime = 500000;
......@@ -143,10 +143,10 @@ void WaitForFile(const char *source, unsigned int maxCount)
do {
usleep(waitTime);
count++;
} while ((stat(source, &sourceInfo) < 0) && (errno == ENOENT) && (count < maxCount));
} while ((stat(source, &sourceInfo) < 0) && (errno == ENOENT) && (count < maxCountTmp));
float secTime = ConvertMicrosecondToSecond(waitTime);
if (count == maxCount) {
INIT_LOGE("wait for file:%s failed after %f.", source, maxCount * secTime);
if (count == maxCountTmp) {
INIT_LOGE("wait for file:%s failed after %f.", source, maxCountTmp * secTime);
}
return;
}
......@@ -173,4 +173,78 @@ size_t WriteAll(int fd, char *buffer, size_t size)
left -= written;
}
return size - left;
}
\ No newline at end of file
}
char *Realpath(const char *source, char *resolvedPath, size_t resolvedPathSize)
{
if ((source == NULL) || (resolvedPath == NULL) || (resolvedPathSize != PATH_MAX)) {
return NULL;
}
if (realpath(source, resolvedPath) == NULL) {
if (errno != ENOENT) {
INIT_LOGE("Fail resolve %s real path err=%d", source, errno);
return NULL;
}
}
return resolvedPath;
}
int MakeDir(const char *dir, mode_t mode)
{
int rc = -1;
if (dir == NULL || *dir == '\0') {
errno = EINVAL;
return rc;
}
rc = mkdir(dir, mode);
if (rc < 0 && errno != EEXIST) {
INIT_LOGE("Create directory \" %s \" failed, err = %d", dir, errno);
return rc;
}
// create dir success or it already exist.
return 0;
}
int MakeDirRecursive(const char *dir, mode_t mode)
{
int rc = -1;
char buffer[PATH_MAX] = {};
const char *p = NULL;
if (dir == NULL || *dir == '\0') {
errno = EINVAL;
return rc;
}
p = dir;
char *slash = strchr(dir, '/');
while (slash != NULL) {
int gap = slash - p;
p = slash + 1;
if (gap == 0) {
slash = strchr(p, '/');
continue;
}
if (gap < 0) { // end with '/'
break;
}
if (memcpy_s(buffer, PATH_MAX, dir, p - dir - 1) != 0) {
return -1;
}
rc = MakeDir(buffer, mode);
if (rc < 0) {
return rc;
}
slash = strchr(p, '/');
}
return MakeDir(dir, mode);
}
int StringToInt(const char *str, int defaultValue)
{
if (str == NULL || *str == '\0') {
return defaultValue;
}
errno = 0;
int value = (int)strtoul(str, NULL, DECIMAL_BASE);
return errno != 0 ? defaultValue : value;
}
......@@ -15,7 +15,8 @@
#ifndef INIT_UTILS_H
#define INIT_UTILS_H
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#ifdef __cplusplus
......@@ -30,10 +31,15 @@ extern "C" {
#define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
uid_t DecodeUid(const char *name);
char* ReadFileToBuf(const char *configFile);
char *ReadFileToBuf(const char *configFile);
int SplitString(char *srcPtr, char **dstPtr, int maxNum);
void WaitForFile(const char *source, unsigned int maxCount);
size_t WriteAll(int fd, char *buffer, size_t size);
char *Realpath(const char *source, char *resolvedPath, size_t resolvedPathSize);
int StringToInt(const char *str, int defaultValue);
int MakeDirRecursive(const char *dir, mode_t mode);
int MakeDir(const char *dir, mode_t mode);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -22,18 +22,19 @@ if (!defined(ohos_lite)) {
"//base/startup/init_lite/ueventd/ueventd_firmware_handler.c",
"//base/startup/init_lite/ueventd/ueventd_read_cfg.c",
"//base/startup/init_lite/ueventd/ueventd_socket.c",
"//base/startup/init_lite/ueventd/ueventd_utils.c",
]
include_dirs = [
"include",
".",
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/utils",
]
deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static",
]
......
......@@ -14,13 +14,16 @@
*/
#include "ueventd_device_handler.h"
#include <errno.h>
#include <libgen.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "init_utils.h"
#include "list.h"
#include "ueventd.h"
#include "ueventd_read_cfg.h"
......@@ -219,8 +222,8 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
return NULL;
}
char *bus = realpath(subsystem, NULL);
if (bus != NULL) {
char bus[PATH_MAX] = {0};
if (Realpath(subsystem, bus, sizeof(bus)) != NULL) {
if (STRINGEQUAL(bus, "/sys/bus/platform")) {
INIT_LOGD("Find a platform device: %s", parent);
parent = FindPlatformDeviceName(parent);
......
......@@ -14,6 +14,7 @@
*/
#include "ueventd_read_cfg.h"
#include <ctype.h>
#include <limits.h>
#include <string.h>
......@@ -22,6 +23,7 @@
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "init_utils.h"
#include "list.h"
#include "ueventd_utils.h"
#include "securec.h"
......@@ -159,7 +161,7 @@ static int ParseDeviceConfig(char *p)
}
config->name = strdup(items[DEVICE_CONFIG_NAME_NUM]); // device node
errno = 0;
config->mode = strtoul(items[DEVICE_CONFIG_MODE_NUM], NULL, OCTONARY);
config->mode = strtoul(items[DEVICE_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
if (errno != 0) {
INIT_LOGE("Invalid mode in config file for device node %s. use default mode", config->name);
config->mode = DEVMODE;
......@@ -197,7 +199,7 @@ static int ParseSysfsConfig(char *p)
config->sysPath = strdup(items[SYS_CONFIG_PATH_NUM]); // sys path
config->attr = strdup(items[SYS_CONFIG_ATTR_NUM]); // attribute
errno = 0;
config->mode = strtoul(items[SYS_CONFIG_MODE_NUM], NULL, OCTONARY);
config->mode = strtoul(items[SYS_CONFIG_MODE_NUM], NULL, OCTAL_BASE);
if (errno != 0) {
INIT_LOGE("Invalid mode in config file for sys path %s. use default mode", config->sysPath);
config->mode = DEVMODE;
......@@ -327,12 +329,11 @@ void ParseUeventdConfigFile(const char *file)
if (INVALIDSTRING(file)) {
return;
}
char *config = realpath(file, NULL);
if (config == NULL) {
char config[PATH_MAX] = {0};
if (Realpath(file, config, sizeof(config)) == NULL) {
return;
}
int fd = open(config, O_RDONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
free(config);
if (fd < 0) {
INIT_LOGE("Read from %s failed", file);
return;
......
/*
* Copyright (c) 2021 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 "ueventd_utils.h"
#include <errno.h>
#include <limits.h>
#include <stdlib.h>
#include <unistd.h>
#include "securec.h"
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"
int MakeDir(const char *dir, mode_t mode)
{
int rc = -1;
if (INVALIDSTRING(dir)) {
errno = EINVAL;
return rc;
}
rc = mkdir(dir, mode);
if (rc < 0 && errno != EEXIST) {
INIT_LOGE("Create directory \" %s \" failed, err = %d", dir, errno);
return rc;
}
// create dir success or it already exist.
return 0;
}
int MakeDirRecursive(const char *dir, mode_t mode)
{
int rc = -1;
char buffer[PATH_MAX] = {};
const char *p = NULL;
if (INVALIDSTRING(dir)) {
errno = EINVAL;
return rc;
}
char *slash = strchr(dir, '/');
p = dir;
while (slash != NULL) {
int gap = slash - p;
p = slash + 1;
if (gap == 0) {
slash = strchr(p, '/');
continue;
}
if (gap < 0) { // end with '/'
break;
}
if (memcpy_s(buffer, PATH_MAX, dir, p - dir -1) != 0) {
return -1;
}
rc = MakeDir(buffer, mode);
if (rc < 0) {
return rc;
}
slash = strchr(p, '/');
}
return MakeDir(dir, mode);
}
int StringToInt(const char *str, int defaultValue)
{
if (INVALIDSTRING(str)) {
return defaultValue;
}
errno = 0;
int value = (int)strtoul(str, NULL, DECIMALISM);
return errno != 0 ? defaultValue : value;
}
......@@ -18,9 +18,8 @@
#include <fcntl.h>
#include <string.h>
#include <sys/stat.h>
#include "init_utils.h"
#define DECIMALISM 10
#define OCTONARY 8
#define DEVICE_FILE_SIZE 128U
#define SYSPATH_SIZE 512U
#define BLOCKDEVICE_LINKS 3
......@@ -33,7 +32,4 @@
// Default device mode is 0660
#define DEVMODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
int MakeDirRecursive(const char *dir, mode_t mode);
int MakeDir(const char *dir, mode_t mode);
int StringToInt(const char *str, int defaultValue);
#endif // BASE_STARTUP_INITLITE_UEVENTD_UTILS_H
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册