提交 59b51bd1 编写于 作者: O openharmony_ci 提交者: Gitee

!161 fix: uevent param fsmanager_log

Merge pull request !161 from 熊磊/init1111
......@@ -14,6 +14,7 @@
import("//build/ohos.gni")
fs_manager_sources = [
"fs_manager_log.c",
"fstab.c",
"fstab_mount.c",
]
......
/*
* 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 "fs_manager/fs_manager_log.h"
#include "init_log.h"
#include "securec.h"
#define LOG_BUFFER_MAX (1024)
static FILE *g_logFile = NULL;
static FsMgrLogLevel g_defaultLogLevel = FSMGR_DEBUG;
FsManagerLogFunc g_logFunc = FsManagerLogToStd;
static InitLogLevel ConvertToInitLog(FsMgrLogLevel level)
{
switch (level) {
case FSMGR_VERBOSE: // fall through
case FSMGR_DEBUG:
return INIT_DEBUG;
case FSMGR_INFO:
return INIT_INFO;
case FSMGR_WARNING:
return INIT_WARN;
case FSMGR_ERROR:
return INIT_ERROR;
case FSMGR_FATAL:
return INIT_FATAL;
// Unexpected log level, set it as lowest
default:
return INIT_DEBUG;
}
}
static const char *ConvertToKernelLog(FsMgrLogLevel level)
{
switch (level) {
case FSMGR_VERBOSE: // fall through
case FSMGR_DEBUG:
return "<7>";
case FSMGR_INFO:
return "<6>";
case FSMGR_WARNING:
return "<4>";
case FSMGR_ERROR:
case FSMGR_FATAL:
return "<3>";
// Unexpected log level, set level as lowest
default:
return "<7>";
}
}
// Wrap init log
static void FsManagerLogToKernel(FsMgrLogLevel level, const char *fileName, int line, const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
char tmpFmt[LOG_BUFFER_MAX];
if (vsnprintf_s(tmpFmt, LOG_BUFFER_MAX, LOG_BUFFER_MAX - 1, fmt, vargs) == -1) {
return;
}
char logInfo[LOG_BUFFER_MAX];
if (snprintf_s(logInfo, LOG_BUFFER_MAX, LOG_BUFFER_MAX - 1, "[%s:%d]%s", fileName, line, tmpFmt) == -1) {
return;
}
InitLog(fileName, ConvertToInitLog(level), ConvertToKernelLog(level), logInfo);
va_end(vargs);
return;
}
static void WriteLogToFile(FILE *fp, const char *fileName, int line, const char *fmt, ...)
{
// Sanity checks
INIT_CHECK_ONLY_RETURN(fp != NULL && fmt != NULL);
va_list vargs;
va_start(vargs, fmt);
va_end(vargs);
char fullLogMsg[LOG_BUFFER_MAX];
if (snprintf_s(fullLogMsg, LOG_BUFFER_MAX, LOG_BUFFER_MAX - 1, "[%s:%d][%s] %s",
fileName == NULL ? "" : fileName, line, "fs_manager", fmt) == -1) {
return;
}
(void)fprintf(fp, "%s", fullLogMsg);
return;
}
void FsManagerLogToStd(FsMgrLogLevel level, const char *fileName, int line, const char *fmt, ...)
{
if (level < g_defaultLogLevel) {
return;
}
FILE *fp = NULL;
if (level >= FSMGR_ERROR) {
fp = stderr;
} else {
fp = stdout;
}
va_list vargs;
va_start(vargs, fmt);
char tmpFmt[LOG_BUFFER_MAX];
if (vsnprintf_s(tmpFmt, LOG_BUFFER_MAX, LOG_BUFFER_MAX - 1, fmt, vargs) == -1) {
return;
}
WriteLogToFile(fp, fileName, line, tmpFmt, vargs);
va_end(vargs);
return;
}
static void FsManagerLogToFile(FsMgrLogLevel level, const char *fileName, int line, const char *fmt, ...)
{
if (level < g_defaultLogLevel) {
return;
}
va_list vargs;
va_start(vargs, fmt);
// If @g_logFile is NULL, output the log to standard I/O.
if (g_logFile == NULL) {
FsManagerLogToStd(level, fileName, line, fmt, vargs);
} else {
char tmpFmt[LOG_BUFFER_MAX];
if (vsnprintf_s(tmpFmt, LOG_BUFFER_MAX, LOG_BUFFER_MAX - 1, fmt, vargs) == -1) {
return;
}
WriteLogToFile(g_logFile, fileName, line, tmpFmt, vargs);
}
va_end(vargs);
return;
}
void FsManagerLogInit(LogTarget target, const char *fileName)
{
setbuf(stderr, NULL);
setbuf(stdout, NULL);
switch (target) {
case LOG_TO_KERNEL:
g_logFunc = FsManagerLogToKernel;
break;
case LOG_TO_FILE:
if (fileName != NULL && *fileName != '\0') {
g_logFile = fopen(fileName, "a+");
setbuf(g_logFile, NULL);
// Do not check return values. The log writte function will do this.
}
g_logFunc = FsManagerLogToFile;
break;
case LOG_TO_STDIO:
g_logFunc = FsManagerLogToStd;
break;
default:
// Output log to standard I/O by default
g_logFunc = FsManagerLogToStd;
break;
}
return;
}
void FsManagerLogDeInit(void)
{
if (g_logFile != NULL) {
fclose(g_logFile);
g_logFile = NULL;
}
return;
}
......@@ -25,6 +25,7 @@
#include <sys/types.h>
#include <unistd.h>
#include "fs_manager/fs_manager.h"
#include "fs_manager/fs_manager_log.h"
#include "init_log.h"
#include "init_utils.h"
#include "securec.h"
......@@ -134,37 +135,37 @@ static int ParseFstabPerLine(char *str, Fstab *fstab, bool procMounts)
if ((item = (FstabItem *)calloc(1, sizeof(FstabItem))) == NULL) {
errno = ENOMEM;
printf("[fs_manager] Allocate memory for FS table item failed, err = %d\n", errno);
FSMGR_LOGE("Allocate memory for FS table item failed, err = %d", errno);
return -1;
}
do {
if ((p = strtok_r(str, separator, &rest)) == NULL) {
fprintf(stderr, "Failed to parse block device.\n");
FSMGR_LOGE("Failed to parse block device.");
break;
}
item->deviceName = strdup(p);
if ((p = strtok_r(NULL, separator, &rest)) == NULL) {
fprintf(stderr, "Failed to parse mount point.\n");
FSMGR_LOGE("Failed to parse mount point.");
break;
}
item->mountPoint = strdup(p);
if ((p = strtok_r(NULL, separator, &rest)) == NULL) {
fprintf(stderr, "Failed to parse fs type.\n");
FSMGR_LOGE("Failed to parse fs type.");
break;
}
item->fsType = strdup(p);
if ((p = strtok_r(NULL, separator, &rest)) == NULL) {
fprintf(stderr, "Failed to parse mount options.\n");
FSMGR_LOGE("Failed to parse mount options.");
break;
}
item->mountOptions = strdup(p);
if ((p = strtok_r(NULL, separator, &rest)) == NULL) {
fprintf(stderr, "Failed to parse fs manager flags.\n");
FSMGR_LOGE("Failed to parse fs manager flags.");
break;
}
// @fsManagerFlags only for fstab
......@@ -191,18 +192,18 @@ Fstab *ReadFstabFromFile(const char *file, bool procMounts)
Fstab *fstab = NULL;
if (file == NULL) {
printf("[fs_manager], invalid file\n");
FSMGR_LOGE("Invalid file");
return NULL;
}
FILE *fp = fopen(file, "r");
if (fp == NULL) {
printf("[fs_manager] Open %s failed, err = %d", file, errno);
FSMGR_LOGE("Open %s failed, err = %d", file, errno);
return NULL;
}
if ((fstab = (Fstab *)calloc(1, sizeof(Fstab))) == NULL) {
printf("[fs_manager] Allocate memory for FS table failed, err = %d\n", errno);
FSMGR_LOGE("Allocate memory for FS table failed, err = %d", errno);
fclose(fp);
fp = NULL;
return NULL;
......@@ -232,7 +233,7 @@ Fstab *ReadFstabFromFile(const char *file, bool procMounts)
}
// If one line in fstab file parsed with a failure. just give a warning
// and skip it.
printf("[fs_manager][warning] Cannot parse file \" %s \" at line %zu. skip it\n", file, ln);
FSMGR_LOGW("Cannot parse file \" %s \" at line %zu. skip it", file, ln);
continue;
}
}
......@@ -267,8 +268,8 @@ FstabItem *FindFstabItemForPath(Fstab fstab, const char *path)
char tmp[PATH_MAX] = {0};
char *dir = NULL;
if (strncpy_s(tmp, PATH_MAX -1, path, strlen(path)) != EOK) {
printf("[fs_manager][error], Failed to copy path\n");
if (strncpy_s(tmp, PATH_MAX - 1, path, strlen(path)) != EOK) {
FSMGR_LOGE("Failed to copy path.");
return NULL;
}
......@@ -360,7 +361,7 @@ unsigned long GetMountFlags(char *mountFlag, char *fsSpecificData, size_t fsSpec
flags |= ParseDefaultMountFlag(p);
} else {
if (strncat_s(fsSpecificData, fsSpecificDataSize - 1, p, strlen(p)) != EOK) {
printf("[fs_manager][warning], Failed to append mount flag \" %s \", ignore it\n", p);
FSMGR_LOGW("Failed to append mount flag \" %s \", ignore it.", p);
continue;
}
if (i == flagCount - 1) { // last flags, do not need to append ','
......@@ -368,7 +369,7 @@ unsigned long GetMountFlags(char *mountFlag, char *fsSpecificData, size_t fsSpec
}
// Combined each mount flag with ','
if (strncat_s(fsSpecificData, fsSpecificDataSize - 1, ",", 1) != EOK) {
printf("[fs_manager][warning], Failed to append comma\n");
FSMGR_LOGW("Failed to append comma.");
break; // If cannot add ',' to the end of flags, there is not reason to continue.
}
}
......
......@@ -14,7 +14,6 @@
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
......@@ -24,6 +23,7 @@
#include <sys/wait.h>
#include <unistd.h>
#include "fs_manager/fs_manager.h"
#include "fs_manager/fs_manager_log.h"
#include "init_log.h"
#include "init_utils.h"
#include "securec.h"
......@@ -71,7 +71,7 @@ static int ExecCommand(int argc, char **argv)
int status;
waitpid(pid, &status, 0);
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
printf("Command %s failed with status %d", argv[0], WEXITSTATUS(status));
FSMGR_LOGE("Command %s failed with status %d", argv[0], WEXITSTATUS(status));
}
return WEXITSTATUS(status);
}
......@@ -83,7 +83,7 @@ int DoFormat(const char *devPath, const char *fsType)
}
if (!IsSupportedFilesystem(fsType)) {
printf("[fs_manager][error] Do not support filesystem \" %s \"\n", fsType);
FSMGR_LOGE("Do not support filesystem \" %s \"", fsType);
return -1;
}
int ret = 0;
......@@ -91,7 +91,7 @@ int DoFormat(const char *devPath, const char *fsType)
if (strcmp(fsType, "ext4") == 0) {
const unsigned int blockSize = 4096;
if (snprintf_s(blockSizeBuffer, BLOCK_SIZE_BUFFER, BLOCK_SIZE_BUFFER - 1, "%u", blockSize) == -1) {
printf("[fs_manager][error] Failed to build block size buffer\n");
FSMGR_LOGE("Failed to build block size buffer");
return -1;
}
char *formatCmds[] = {
......@@ -117,7 +117,6 @@ MountStatus GetMountStatusForMountPoint(const char *mp)
return MOUNT_ERROR;
}
char buffer[FS_MANAGER_BUFFER_SIZE] = {0};
size_t n = 0;
const int expectedItems = 6;
int count = 0;
char **mountItems = NULL;
......@@ -129,7 +128,7 @@ MountStatus GetMountStatusForMountPoint(const char *mp)
return status;
}
while (fgets(buffer, sizeof(buffer) - 1, fp) != NULL) {
n = strlen(buffer);
size_t n = strlen(buffer);
if (buffer[n - 1] == '\n') {
buffer[n - 1] = '\0';
}
......@@ -161,11 +160,11 @@ static int Mount(const char *source, const char *target, const char *fsType,
int rc = -1;
if (source == NULL || target == NULL || fsType == NULL) {
printf("[fs_manager][error] Invalid argment for mount\n");
FSMGR_LOGE("Invalid argment for mount.");
return -1;
}
if (stat(target, &st) != 0 && errno != ENOENT) {
printf("[fs_manager][error] Cannot get stat of \" %s \", err = %d\n", target, errno);
FSMGR_LOGE("Cannot get stat of \" %s \", err = %d", target, errno);
return -1;
}
if ((st.st_mode & S_IFMT) == S_IFLNK) { // link, delete it.
......@@ -173,14 +172,14 @@ static int Mount(const char *source, const char *target, const char *fsType,
}
if (mkdir(target, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) < 0) {
if (errno != EEXIST) {
printf("[fs_manager][error] Failed to create dir \" %s \", err = %d\n", target, errno);
FSMGR_LOGE("Failed to create dir \" %s \", err = %d", target, errno);
return -1;
}
}
errno = 0;
while ((rc = mount(source, target, fsType, flags, data)) != 0) {
if (errno == EAGAIN) {
printf("[fs_manager][warning] Mount %s to %s failed. try again", source, target);
FSMGR_LOGE("Mount %s to %s failed. try again", source, target);
continue;
} else {
break;
......@@ -199,7 +198,7 @@ int MountOneItem(FstabItem *item)
mountFlags = GetMountFlags(item->mountOptions, fsSpecificData, sizeof(fsSpecificData));
if (!IsSupportedFilesystem(item->fsType)) {
printf("[fs_manager][error]Unsupported file system \" %s \"", item->fsType);
FSMGR_LOGE("Unsupported file system \" %s \"", item->fsType);
return -1;
}
if (FM_MANAGER_WAIT_ENABLED(item->fsManagerFlags)) {
......@@ -207,9 +206,9 @@ int MountOneItem(FstabItem *item)
}
int rc = Mount(item->deviceName, item->mountPoint, item->fsType, mountFlags, fsSpecificData);
if (rc != 0) {
INIT_LOGE("Mount %s to %s failed %d", item->deviceName, item->mountPoint, errno);
FSMGR_LOGE("Mount %s to %s failed %d", item->deviceName, item->mountPoint, errno);
} else {
INIT_LOGI("Mount %s to %s successful", item->deviceName, item->mountPoint);
FSMGR_LOGI("Mount %s to %s successful", item->deviceName, item->mountPoint);
}
return rc;
}
......@@ -232,14 +231,9 @@ int CheckRequiredAndMount(FstabItem *item, bool required)
return rc;
}
int MountAllWithFstabFile(const char *fstabFile, bool required)
int MountAllWithFstab(const Fstab *fstab, bool required)
{
if (fstabFile == NULL || *fstabFile == '\0') {
return -1;
}
Fstab *fstab = NULL;
if ((fstab = ReadFstabFromFile(fstabFile, false)) == NULL) {
printf("[fs_manager][error] Read fstab file \" %s \" failed\n", fstabFile);
if (fstab == NULL) {
return -1;
}
......@@ -251,6 +245,21 @@ int MountAllWithFstabFile(const char *fstabFile, bool required)
break;
}
}
return rc;
}
int MountAllWithFstabFile(const char *fstabFile, bool required)
{
if (fstabFile == NULL || *fstabFile == '\0') {
return -1;
}
Fstab *fstab = NULL;
if ((fstab = ReadFstabFromFile(fstabFile, false)) == NULL) {
FSMGR_LOGE("[fs_manager][error] Read fstab file \" %s \" failed\n", fstabFile);
return -1;
}
int rc = MountAllWithFstab(fstab, required);
ReleaseFstab(fstab);
fstab = NULL;
return rc;
......@@ -263,30 +272,29 @@ int UmountAllWithFstabFile(const char *fstabFile)
}
Fstab *fstab = NULL;
if ((fstab = ReadFstabFromFile(fstabFile, false)) == NULL) {
printf("[fs_manager][error] Read fstab file \" %s \" failed\n", fstabFile);
FSMGR_LOGE("Read fstab file \" %s \" failed.", fstabFile);
return -1;
}
FstabItem *item = NULL;
int rc = -1;
for (item = fstab->head; item != NULL; item = item->next) {
printf("[fs_manager][info]Umount %s\n", item->mountPoint);
FSMGR_LOGI("Umount %s.", item->mountPoint);
MountStatus status = GetMountStatusForMountPoint(item->mountPoint);
if (status == MOUNT_ERROR) {
printf("[fs_manager][warning] Cannot get mount status of mount point \" %s \"\n", item->mountPoint);
FSMGR_LOGW("Cannot get mount status of mount point \" %s \"", item->mountPoint);
continue; // Cannot get mount status, just ignore it and try next one.
} else if (status == MOUNT_UMOUNTED) {
printf("[fs_manager][info] Mount point \" %s \" already unmounted. device path: %s, fs type: %s\n",
FSMGR_LOGI("Mount point \" %s \" already unmounted. device path: %s, fs type: %s.",
item->mountPoint, item->deviceName, item->fsType);
continue;
} else {
rc = umount(item->mountPoint);
if (rc == -1) {
printf("[fs_manager][error] Umount %s failed, device path: %s, fs type: %s, err = %d",
FSMGR_LOGE("Umount %s failed, device path: %s, fs type: %s, err = %d.",
item->mountPoint, item->deviceName, item->fsType, errno);
} else {
printf("[fs_manager][info] Umount %s successfully\n",
item->mountPoint);
FSMGR_LOGE("Umount %s successfully.", item->mountPoint);
}
}
}
......
......@@ -65,7 +65,8 @@ bool IsSupportedFilesystem(const char *fsType);
int DoFormat(const char *devPath, const char *fsType);
int MountOneItem(FstabItem *item);
MountStatus GetMountStatusForMountPoint(const char *mp);
int MountAllWithFstabFile(const char *file, bool required);
int MountAllWithFstabFile(const char *fstabFile, bool required);
int MountAllWithFstab(const Fstab *fstab, bool required);
int UmountAllWithFstabFile(const char *file);
unsigned long GetMountFlags(char *mountFlag, char *fsSpecificFlags, size_t fsSpecificFlagSize);
#ifdef __cplusplus
......
/*
* 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.
*/
#ifndef STARTUP_FS_MANAGER_LOG_H
#define STARTUP_FS_MANAGER_LOG_H
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
typedef enum LOGTARGET {
LOG_TO_KERNEL,
LOG_TO_FILE,
LOG_TO_STDIO
} LogTarget;
typedef enum FsMgrLogLevel {
FSMGR_VERBOSE,
FSMGR_DEBUG,
FSMGR_INFO,
FSMGR_WARNING,
FSMGR_ERROR,
FSMGR_FATAL
} FsMgrLogLevel;
void FsManagerLogInit(LogTarget target, const char *fileName);
void FsManagerLogDeInit(void);
void FsManagerLogToStd(FsMgrLogLevel level, const char *fileName, int line, const char *fmt, ...);
typedef void (*FsManagerLogFunc)(FsMgrLogLevel level, const char *fileName, int line, const char *fmt, ...);
extern FsManagerLogFunc g_logFunc;
#define FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__))
#define FSMGR_LOGV(fmt, ...) g_logFunc(FSMGR_VERBOSE, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#define FSMGR_LOGD(fmt, ...) g_logFunc(FSMGR_DEBUG, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#define FSMGR_LOGI(fmt, ...) g_logFunc(FSMGR_INFO, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#define FSMGR_LOGW(fmt, ...) g_logFunc(FSMGR_WARNING, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#define FSMGR_LOGE(fmt, ...) g_logFunc(FSMGR_ERROR, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#define FSMGR_LOGF(fmt, ...) g_logFunc(FSMGR_FATAL, (FILE_NAME), (__LINE__), fmt"\n", ##__VA_ARGS__)
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif // STARTUP_FS_MANAGER_LOG_H
......@@ -4,8 +4,7 @@
"init": {
"module_list": [
"//base/startup/init_lite/services:startup_init",
"//base/startup/init_lite/ueventd:ueventd",
"//base/startup/init_lite/ueventd:ueventd.config",
"//base/startup/init_lite/ueventd:startup_ueventd",
"//base/startup/init_lite/watchdog:watchdog",
"//base/startup/init_lite/services/cmds/misc:misc_daemon"
],
......
......@@ -116,6 +116,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/init/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/interfaces/innerkits/include",
"//base/startup/init_lite/ueventd/include",
"//third_party/cJSON",
"//third_party/bounds_checking_function/include",
"//third_party/libuv/include",
......@@ -124,6 +125,7 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/interfaces/innerkits/fs_manager:libfsmanager_static",
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/param:param_service",
"//base/startup/init_lite/ueventd:libueventd_static",
"//third_party/bounds_checking_function:libsec_static",
"//third_party/cJSON:cjson_static",
]
......
......@@ -69,7 +69,7 @@ static void ClearLogo(int fd)
return;
}
char buffer[8] = {0}; // logo magic number + logo size is 8
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 -1) / BLOCK_SZIE_1;
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
if (lseek(fd, addrOffset * BLOCK_SZIE_1, SEEK_SET) < 0) {
std::cout << "Failed to clean file\n";
return;
......@@ -180,7 +180,7 @@ static void WriteLogoToMisc(const std::string &logoPath)
return;
}
int fd = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, 0644);
int fd = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
std::cout << "Failed to open " << miscDev << ", err = " << errno << std::endl;
return;
......@@ -191,7 +191,7 @@ static void WriteLogoToMisc(const std::string &logoPath)
}
close(fd);
int addrOffset = (PARTITION_INFO_POS + PARTITION_INFO_MAX_LENGTH + BLOCK_SZIE_1 - 1) / BLOCK_SZIE_1;
int fd1 = open(miscDev.c_str(), O_RDWR | O_CLOEXEC, 0644);
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";
return;
......
......@@ -33,6 +33,8 @@ extern "C" {
#define ARRAY_LENGTH(array) (sizeof((array)) / sizeof((array)[0]))
uid_t DecodeUid(const char *name);
char *ReadFileToBuf(const char *configFile);
int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length);
char *ReadFileData(const char *fileName);
int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum);
void WaitForFile(const char *source, unsigned int maxCount);
size_t WriteAll(int fd, const char *buffer, size_t size);
......
......@@ -16,7 +16,6 @@
#include <errno.h>
#include <semaphore.h>
#include <stdio.h>
#include <sys/prctl.h>
#include <sys/reboot.h>
#include <unistd.h>
......
......@@ -12,9 +12,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined OHOS_LITE && !defined __LINUX__
......
......@@ -19,7 +19,6 @@
#include <fcntl.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
......
......@@ -17,7 +17,6 @@
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __MUSL__
......@@ -38,8 +37,8 @@
#include "securec.h"
#ifdef WITH_SELINUX
# include "init_selinux_param.h"
# include <selinux/selinux.h>
#include "init_selinux_param.h"
#include <selinux/selinux.h>
#endif // WITH_SELINUX
#ifndef TIOCSCTTY
......@@ -172,7 +171,7 @@ static int WritePid(const Service *service)
return SERVICE_SUCCESS;
}
void SetSecon(Service * service)
void SetSecon(Service *service)
{
#ifdef WITH_SELINUX
if (*(service->secon)) {
......
......@@ -14,7 +14,6 @@
*/
#include "init_service_file.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
......
......@@ -14,10 +14,7 @@
*/
#include "init_service_manager.h"
#include <ctype.h>
#include <limits.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
......@@ -378,7 +375,7 @@ static int AddServiceFile(cJSON *json, Service *service)
return SERVICE_FAILURE;
}
if (opt[SERVICE_FILE_NAME] == NULL || opt[SERVICE_FILE_FLAGS] == NULL || opt[SERVICE_FILE_PERM] == NULL ||
opt[SERVICE_FILE_UID] == NULL || opt[SERVICE_FILE_GID] == NULL) {
opt[SERVICE_FILE_UID] == NULL || opt[SERVICE_FILE_GID] == NULL) {
INIT_LOGE("Invalid file opt");
return SERVICE_FAILURE;
}
......
......@@ -14,7 +14,6 @@
*/
#include "init_service_socket.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
......
......@@ -19,7 +19,6 @@
#include <stdlib.h>
#include <string.h>
#include "init.h"
#include "init_log.h"
#include "init_utils.h"
#include "securec.h"
......
......@@ -14,7 +14,6 @@
*/
#include "init_jobs_internal.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
......
......@@ -13,7 +13,6 @@
* limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/reboot.h>
......
......@@ -14,7 +14,6 @@
*/
#include "init_service.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
......
......@@ -13,11 +13,9 @@
* limitations under the License.
*/
#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
#include "init_adapter.h"
#include "init_log.h"
#include "init_service_manager.h"
void ReapService(Service *service)
......
......@@ -12,12 +12,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include "init.h"
#include "init_log.h"
static const pid_t INIT_PROCESS_PID = 1;
int main(int argc, char* const argv[])
int main(int argc, char * const argv[])
{
int isSecondStage = 0;
// Number of command line parameters is 2
......
......@@ -15,6 +15,7 @@
#include "init.h"
#include <errno.h>
#include <poll.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/sysmacros.h>
......@@ -23,14 +24,17 @@
#include <linux/major.h>
#include "device.h"
#include "fs_manager/fs_manager.h"
#include "init_log.h"
#include "init_mount.h"
#include "init_param.h"
#include "init_utils.h"
#include "securec.h"
#include "switch_root.h"
#include "ueventd.h"
#include "ueventd_socket.h"
#ifdef WITH_SELINUX
# include <policycoreutils.h>
#include <policycoreutils.h>
#endif // WITH_SELINUX
void SystemInit(void)
......@@ -50,49 +54,77 @@ void LogInit(void)
}
#ifndef DISABLE_INIT_TWO_STAGES
static pid_t StartUeventd(void)
static char **GetRequiredDevices(Fstab fstab, int *requiredNum)
{
char *const argv[] = {
"/bin/ueventd",
NULL,
};
pid_t pid = fork();
if (pid < 0) {
INIT_LOGE("Failed to fork child process");
int num = 0;
FstabItem *item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
num++;
}
item = item->next;
}
if (pid == 0) {
if (execv(argv[0], argv) != 0) {
INIT_LOGE("service %s execve failed! err %d.", argv[0], errno);
char **devices = (char **)calloc(num, sizeof(char *));
INIT_ERROR_CHECK(devices != NULL, return NULL, "Failed calloc err=%d", errno);
int i = 0;
item = fstab.head;
while (item != NULL) {
if (FM_MANAGER_REQUIRED_ENABLED(item->fsManagerFlags)) {
devices[i] = strdup(item->deviceName);
INIT_ERROR_CHECK(devices[i] != NULL, FreeStringVector(devices, num); return NULL,
"Failed strdup err=%d", errno);
i++;
}
_exit(0x7f); // 0x7f: user specified
item = item->next;
}
return pid;
*requiredNum = num;
return devices;
}
static void StartInitSecondStage(void)
static int StartUeventd(char **requiredDevices, int num)
{
pid_t ueventPid = StartUeventd();
if (ueventPid < 0) {
INIT_LOGE("Failed to start ueventd");
abort();
INIT_ERROR_CHECK(requiredDevices != NULL && num > 0, return -1, "Failed parameters");
int ueventSockFd = UeventdSocketInit();
if (ueventSockFd < 0) {
INIT_LOGE("Failed to create uevent socket");
return -1;
}
if (MountRequriedPartitions() < 0) {
// If mount required partitions failure.
// There is no necessary to continue.
// Just abort
INIT_LOGE("Mount requried partitions failed");
abort();
RetriggerUevent(ueventSockFd, requiredDevices, num);
return 0;
}
static void StartInitSecondStage(void)
{
const char *fstabFile = "/etc/fstab.required";
Fstab *fstab = NULL;
INIT_ERROR_CHECK(access(fstabFile, F_OK) == 0, abort(), "Failed get fstab.required");
fstab = ReadFstabFromFile(fstabFile, false);
INIT_ERROR_CHECK(fstab != NULL, abort(), "Read fstab file \" %s \" failed\n", fstabFile);
int requiredNum = 0;
char **devices = GetRequiredDevices(*fstab, &requiredNum);
if (devices != NULL && requiredNum > 0) {
int ret = StartUeventd(devices, requiredNum);
if (ret == 0) {
ret = MountRequriedPartitions(fstab);
}
FreeStringVector(devices, requiredNum);
devices = NULL;
ReleaseFstab(fstab);
fstab = NULL;
if (ret < 0) {
// If mount required partitions failure.
// There is no necessary to continue.
// Just abort
INIT_LOGE("Mount requried partitions failed");
abort();
}
}
// Kill ueventd, because init second stage will start it again.
(void)kill(ueventPid, SIGKILL);
// The init process in ramdisk has done its job.
// It's ready to switch to system partition.
// The system partition mounted in first stage to /usr
// Because the directory /system is in use. we cannot use it.
// After switch root. /usr will become new root.
SwitchRoot("/usr");
// Execute init second stage
char *const args[] = {
char * const args[] = {
"/bin/init",
"--second-stage",
NULL,
......
......@@ -14,12 +14,10 @@
*/
#include "init_cmds.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <net/if.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
......@@ -33,7 +31,7 @@
#include <linux/module.h>
#include "fs_manager/fs_manager.h"
#include "init.h"
#include "fs_manager/fs_manager_log.h"
#include "init_jobs_internal.h"
#include "init_log.h"
#include "init_param.h"
......@@ -252,12 +250,14 @@ static void DoMakeDevice(const struct CmdArgs *ctx)
static void DoMountFstabFile(const struct CmdArgs *ctx)
{
INIT_LOGI("Mount partitions from fstab file \" %s \"", ctx->argv[0]);
FsManagerLogInit(LOG_TO_KERNEL, "");
(void)MountAllWithFstabFile(ctx->argv[0], 0);
}
static void DoUmountFstabFile(const struct CmdArgs *ctx)
{
INIT_LOGI("Umount partitions from fstab file \" %s \"", ctx->argv[0]);
FsManagerLogInit(LOG_TO_KERNEL, "");
int rc = UmountAllWithFstabFile(ctx->argv[0]);
if (rc < 0) {
INIT_LOGE("Run command umount_fstab failed");
......
......@@ -13,29 +13,16 @@
* limitations under the License.
*/
#include "device.h"
#include "fs_manager/fs_manager.h"
#include "init.h"
#include "init_cmds.h"
#include "init_log.h"
#include "securec.h"
int MountRequriedPartitions(void)
int MountRequriedPartitions(Fstab *fstab)
{
const char *fstabFiles[] = {"/etc/fstab.required", NULL};
int i = 0;
INIT_ERROR_CHECK(fstab != NULL, return -1, "Failed fstab is NULL");
int rc = -1;
while (fstabFiles[i] != NULL) {
if (access(fstabFiles[i], F_OK) == 0) {
INIT_LOGI("Mount required partition from %s", fstabFiles[i]);
rc = MountAllWithFstabFile(fstabFiles[i], 1);
} else {
INIT_LOGE("Cannot access fstab file \" %s \"", fstabFiles[i]);
}
if (rc == 0) {
break;
}
i++;
}
INIT_LOGI("Mount required partitions");
rc = MountAllWithFstab(fstab, 1);
return rc;
}
\ No newline at end of file
......@@ -16,12 +16,14 @@
#ifndef BASE_STARTUP_INITLITE_MOUNT_H
#define BASE_STARTUP_INITLITE_MOUNT_H
#include "fs_manager/fs_manager.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
int MountRequriedPartitions(void);
int MountRequriedPartitions(Fstab *fstab);
#ifdef __cplusplus
#if __cplusplus
}
......
......@@ -13,7 +13,6 @@
* limitations under the License.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mount.h>
#include <sys/reboot.h>
......
......@@ -14,7 +14,6 @@
*/
#include "init_service.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/param.h>
......
......@@ -13,7 +13,6 @@
* limitations under the License.
*/
#include <signal.h>
#include <stdio.h>
#include <sys/wait.h>
#include "init_adapter.h"
......
......@@ -41,7 +41,7 @@ void SetInitLogLevel(InitLogLevel logLevel)
#ifdef OHOS_LITE
static LogLevel ConvertToHiLog(InitLogLevel level)
{
switch (level) {
switch (level) {
case INIT_DEBUG:
return LOG_DEBUG;
case INIT_INFO:
......
......@@ -73,8 +73,12 @@ static void OnReceiveAlloc(uv_handle_t *handle, size_t suggestedSize, uv_buf_t *
static void OnWriteResponse(uv_write_t *req, int status)
{
UNUSED(status);
PARAM_CHECK(req != NULL, return, "Invalid req");
PARAM_LOGD("OnWriteResponse handle: %p", req);
write_req_t *writer = (write_req_t *)req;
if (writer != NULL) {
free(writer->buf.base);
free(writer);
}
}
static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf)
......@@ -86,9 +90,20 @@ static void OnReceiveRequest(uv_stream_t *handle, ssize_t nread, const uv_buf_t
}
return;
}
PARAM_LOGD("OnReceiveRequest %d nread %d", buf->len, nread);
LibuvStreamTask *client = PARAM_ENTRY(handle, LibuvStreamTask, stream);
if (client->recvMessage != NULL) {
client->recvMessage(&client->base.worker, (const ParamMessage *)buf->base);
if (client->recvMessage == NULL) {
free(buf->base);
return;
}
ssize_t curr = 0;
while (curr < nread) {
const ParamMessage *msg = (const ParamMessage *)(buf->base + curr);
if ((ssize_t)(msg->msgSize + curr) > nread) {
break;
}
client->recvMessage(&client->base.worker, msg);
curr += msg->msgSize;
}
free(buf->base);
}
......@@ -220,13 +235,15 @@ int ParamTaskSendMsg(const ParamTaskPtr stream, const ParamMessage *msg)
return -1;
}
#ifndef STARTUP_INIT_TEST
write_req_t *req = malloc(sizeof(write_req_t));
PARAM_CHECK(req != NULL, LibuvFreeMsg(stream, msg);
return -1, "Failed to create request");
LibuvStreamTask *worker = (LibuvStreamTask *)stream;
uv_buf_t buf = uv_buf_init((char *)msg, msg->msgSize);
int ret = uv_write(&worker->writer, (uv_stream_t *)&worker->stream.pipe, &buf, 1, OnWriteResponse);
req->buf = uv_buf_init((char *)msg, msg->msgSize);
int ret = uv_write(&req->writer, (uv_stream_t *)&worker->stream.pipe, &req->buf, 1, OnWriteResponse);
PARAM_CHECK(ret >= 0, LibuvFreeMsg(stream, msg);
return -1, "Failed to uv_write2 ret %s", uv_strerror(ret));
#endif
LibuvFreeMsg(stream, msg);
return 0;
}
......@@ -329,4 +346,4 @@ int ParamServiceStop(void)
uv_fs_unlink(uv_default_loop(), &req, PIPE_NAME, NULL);
uv_stop(uv_default_loop());
return 0;
}
\ No newline at end of file
}
......@@ -28,13 +28,17 @@ typedef struct {
uint16_t userDataOffset;
} LibuvBaseTask;
typedef struct {
uv_write_t writer;
uv_buf_t buf;
} write_req_t;
typedef struct {
LibuvBaseTask base;
RecvMessage recvMessage;
union {
uv_pipe_t pipe;
} stream;
uv_write_t writer;
} LibuvStreamTask;
typedef struct {
......
......@@ -225,14 +225,14 @@ int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
if (ret != PARAM_CODE_NOT_FOUND && ret != 0) {
PARAM_CHECK(ret == 0, return ret, "Forbid to wait parameter %s", name);
}
if (timeout == 0) {
if (timeout <= 0) {
timeout = DEFAULT_PARAM_WAIT_TIMEOUT;
}
uint32_t msgSize = sizeof(ParamMessage) + sizeof(ParamMsgContent) + sizeof(ParamMsgContent) + sizeof(uint32_t);
msgSize = (msgSize < RECV_BUFFER_MAX) ? RECV_BUFFER_MAX : msgSize;
uint32_t offset = 0;
ParamMessage *request = NULL;
if (value != NULL) {
if (value != NULL && strlen(value) > 0) {
msgSize += PARAM_ALIGN(strlen(value) + 1);
request = (ParamMessage *)CreateParamMessage(MSG_WAIT_PARAM, name, msgSize);
PARAM_CHECK(request != NULL, return -1, "Failed to malloc for wait");
......
......@@ -110,8 +110,6 @@ typedef struct {
char value[PARAM_BUFFER_SIZE];
} SubStringInfo;
#define MAX_DATA_BUFFER 2048
char *ReadFileData(const char *fileName);
void CheckAndCreateDir(const char *fileName);
int GetSubStringInfo(const char *buff, uint32_t buffLen, char delimiter, SubStringInfo *info, int subStrNumber);
#ifdef __cplusplus
......
......@@ -38,29 +38,6 @@ void CheckAndCreateDir(const char *fileName)
free(path);
}
char *ReadFileData(const char *fileName)
{
if (fileName == NULL) {
return NULL;
}
char *buffer = NULL;
int fd = -1;
do {
fd = open(fileName, O_RDONLY); // 阶段早,不能使用realpath
PARAM_CHECK(fd >= 0, break, "Failed to read file %s", fileName);
buffer = (char *)malloc(MAX_DATA_BUFFER);
PARAM_CHECK(buffer != NULL, break, "Failed to allocate memory for %s", fileName);
ssize_t readLen = read(fd, buffer, MAX_DATA_BUFFER - 1);
PARAM_CHECK(readLen > 0, break, "Failed to read data for %s", fileName);
buffer[readLen] = '\0';
} while (0);
if (fd != -1) {
close(fd);
}
return buffer;
}
static void TrimString(char *string, uint32_t currLen)
{
for (int i = currLen - 1; i >= 0; i--) {
......
......@@ -35,6 +35,7 @@ static ParamWorkSpace g_paramWorkSpace = { 0, {}, NULL, {}, NULL, NULL };
static void OnClose(ParamTaskPtr client)
{
PARAM_LOGD("OnClose %p", client);
ParamWatcher *watcher = (ParamWatcher *)ParamGetTaskUserData(client);
ClearWatcherTrigger(watcher);
ListRemove(&watcher->node);
......@@ -241,6 +242,7 @@ static int SystemSetParam(const char *name, const char *value, const ParamSecuri
// watcher stoped
if (strcmp(name, "init.svc.param_watcher") == 0 && strcmp(value, "stopped") == 0) {
ParamWatcher *watcher = GetParamWatcher(NULL);
PARAM_LOGD("ClearWatcherTrigger");
ClearWatcherTrigger(watcher);
}
return ret;
......@@ -286,7 +288,9 @@ static int SendWatcherNotifyMessage(const TriggerExtData *extData, int cmd, cons
msg->id.msgId = extData->watcherId;
msg->msgSize = sizeof(ParamMessage) + offset;
PARAM_LOGD("SendWatcherNotifyMessage watcherId %d msgSize %d para: %s", extData->watcherId, msg->msgSize, content);
PARAM_LOGD("SendWatcherNotifyMessage cmd %s, watcherId %d msgSize %d para: %s",
(cmd == CMD_INDEX_FOR_PARA_WAIT) ? "wait" : "watcher",
extData->watcherId, msg->msgSize, content);
ParamTaskSendMsg(extData->watcher->stream, msg);
return 0;
}
......@@ -426,10 +430,10 @@ PARAM_STATIC int ProcessMessage(const ParamTaskPtr worker, const ParamMessage *m
ret = HandleParamWaitAdd(&g_paramWorkSpace, worker, msg);
break;
case MSG_ADD_WATCHER:
ret = HandleParamWatcherAdd(&g_paramWorkSpace, worker, (const ParamMessage *)msg);
ret = HandleParamWatcherAdd(&g_paramWorkSpace, worker, msg);
break;
case MSG_DEL_WATCHER:
ret = HandleParamWatcherDel(&g_paramWorkSpace, worker, (const ParamMessage *)msg);
ret = HandleParamWatcherDel(&g_paramWorkSpace, worker, msg);
break;
default:
break;
......@@ -518,51 +522,10 @@ static void TimerCallback(ParamTaskPtr timer, void *context)
static int GetParamValueFromBuffer(const char *name, const char *buffer, char *value, int length)
{
char *endData = (char *)buffer + strlen(buffer);
size_t bootLen = strlen(OHOS_BOOT);
char *tmp = strstr(buffer, name + bootLen);
do {
if (tmp == NULL) {
return -1;
}
tmp = tmp + strlen(name) - bootLen;
while (tmp < endData && *tmp == ' ') {
tmp++;
}
if (*tmp == '=') {
break;
}
tmp = strstr(tmp + 1, name + bootLen);
} while (tmp < endData);
tmp++;
size_t i = 0;
size_t endIndex = 0;
while (tmp < endData && *tmp == ' ') {
tmp++;
}
for (; i < (size_t)length; tmp++) {
if (tmp >= endData) {
endIndex = i;
break;
}
if (*tmp == ' ') {
endIndex = i;
}
if (*tmp == '=') {
if (endIndex != 0) { // for root=uuid=xxxx
break;
}
i = 0;
endIndex = 0;
continue;
}
value[i++] = *tmp;
}
if (i >= (size_t)length) {
return -1;
}
value[endIndex] = '\0';
return 0;
const char *tmpName = name + bootLen;
int ret = GetProcCmdlineValue(tmpName, buffer, value, length);
return ret;
}
static int LoadParamFromCmdLine(void)
......@@ -579,7 +542,7 @@ static int LoadParamFromCmdLine(void)
OHOS_BOOT"uuid",
OHOS_BOOT"aaaaa",
OHOS_BOOT"rootfstype",
OHOS_BOOT"blkdevparts",
OHOS_BOOT"blkdevparts"
#endif
};
char *data = ReadFileData(PARAM_CMD_LINE);
......@@ -595,6 +558,7 @@ static int LoadParamFromCmdLine(void)
ret = CheckParamName(cmdLines[i], 0);
PARAM_CHECK(ret == 0, return -1, "Invalid name %s", cmdLines[i]);
uint32_t dataIndex = 0;
PARAM_LOGE("**** cmdLines[%d] %s, value %s", i, cmdLines[i], value);
ret = WriteParam(&g_paramWorkSpace.paramSpace, cmdLines[i], value, &dataIndex, 0);
PARAM_CHECK(ret == 0, return -1, "Failed to write param %s %s", cmdLines[i], value);
} else {
......
......@@ -126,7 +126,7 @@ static int ExecuteTiggerImmediately(TriggerNode *trigger, const char *content, u
{
PARAM_CHECK(trigger != NULL, return -1, "Invalid trigger");
PARAM_CHECK(g_triggerWorkSpace.cmdExec != NULL, return -1, "Invalid cmdExec");
PARAM_LOGI("ExecuteTiggerImmediately trigger %s", trigger->name);
PARAM_LOGD("ExecuteTiggerImmediately trigger %s", trigger->name);
CommandNode *cmd = GetNextCmdNode(trigger, NULL);
while (cmd != NULL) {
g_triggerWorkSpace.cmdExec(trigger, cmd, content, size);
......
......@@ -129,7 +129,8 @@ int32_t WatcherManagerKits::AddWatcher(const std::string &keyPrefix, ParameterCh
{
WATCHER_LOGI("AddWatcher keyPrefix %s", keyPrefix.c_str());
ParamWatcherKitPtr watcher = GetParamWatcher(keyPrefix);
WATCHER_CHECK(watcher == nullptr, return 0, "Has been watched by keyPrefix %s", keyPrefix.c_str());
WATCHER_CHECK(watcher == nullptr, return watcher->GetWatcherId(),
"Has been watched by keyPrefix %s", keyPrefix.c_str());
watcher = new ParamWatcher(keyPrefix, callback, context);
WATCHER_CHECK(watcher != nullptr, return -1, "Failed to create watcher for %s", keyPrefix.c_str());
......
......@@ -291,7 +291,7 @@ int WatcherManager::GetServerFd(bool retry)
do {
serverFd_ = socket(PF_UNIX, SOCK_STREAM, 0);
int flags = fcntl(serverFd_, F_GETFL, 0);
(void)fcntl(serverFd_, F_SETFL, flags &~ O_NONBLOCK);
(void)fcntl(serverFd_, F_SETFL, flags & ~ O_NONBLOCK);
ret = ConntectServer(serverFd_, CLIENT_PIPE_NAME);
if (ret == 0) {
break;
......
......@@ -31,6 +31,8 @@
#include "securec.h"
#define MAX_BUF_SIZE 1024
#define MAX_DATA_BUFFER 2048
#ifdef STARTUP_UT
#define LOG_FILE_NAME "/media/sf_ubuntu/test/log.txt"
#else
......@@ -113,6 +115,79 @@ char *ReadFileToBuf(const char *configFile)
return buffer;
}
char *ReadFileData(const char *fileName)
{
if (fileName == NULL) {
return NULL;
}
char *buffer = NULL;
int fd = -1;
do {
fd = open(fileName, O_RDONLY);
INIT_ERROR_CHECK(fd >= 0, break, "Failed to read file %s", fileName);
buffer = (char *)malloc(MAX_DATA_BUFFER); // fsmanager not create, can not get fileStat st_size
INIT_ERROR_CHECK(buffer != NULL, break, "Failed to allocate memory for %s", fileName);
ssize_t readLen = read(fd, buffer, MAX_DATA_BUFFER - 1);
INIT_ERROR_CHECK(readLen > 0, break, "Failed to read data for %s", fileName);
buffer[readLen] = '\0';
} while (0);
if (fd != -1) {
close(fd);
}
return buffer;
}
int GetProcCmdlineValue(const char *name, const char *buffer, char *value, int length)
{
INIT_ERROR_CHECK(name != NULL && buffer != NULL && value != NULL, return -1, "Failed get parameters");
char *endData = (char *)buffer + strlen(buffer);
char *tmp = strstr(buffer, name);
do {
if (tmp == NULL) {
return -1;
}
tmp = tmp + strlen(name);
while (tmp < endData && *tmp == ' ') {
tmp++;
}
if (*tmp == '=') {
break;
}
tmp = strstr(tmp + 1, name);
} while (tmp < endData);
tmp++;
size_t i = 0;
size_t endIndex = 0;
while (tmp < endData && *tmp == ' ') {
tmp++;
}
for (; i < (size_t)length; tmp++) {
if (tmp >= endData) {
endIndex = i;
break;
}
if (*tmp == ' ') {
endIndex = i;
}
if (*tmp == '=') {
if (endIndex != 0) { // for root=uuid=xxxx
break;
}
i = 0;
endIndex = 0;
continue;
}
value[i++] = *tmp;
}
if (i >= (size_t)length) {
return -1;
}
value[endIndex] = '\0';
return 0;
}
int SplitString(char *srcPtr, const char *del, char **dstPtr, int maxNum)
{
INIT_CHECK_RETURN_VALUE(srcPtr != NULL && dstPtr != NULL && del != NULL, -1);
......
......@@ -34,6 +34,7 @@ ohos_unittest("init_ut") {
sources = [
"//base/startup/init_lite/interfaces/innerkits/dynamic_service/dynamic_service.c",
"//base/startup/init_lite/interfaces/innerkits/file/init_file.c",
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fs_manager_log.c",
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab.c",
"//base/startup/init_lite/interfaces/innerkits/fs_manager/fstab_mount.c",
"//base/startup/init_lite/interfaces/innerkits/reboot/init_reboot_innerkits.c",
......@@ -127,7 +128,7 @@ ohos_unittest("init_ut") {
"//foundation/distributedschedule/safwk/interfaces/innerkits/safwk",
"//foundation/distributedschedule/samgr/adapter/interfaces/innerkits/include",
"//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include",
"//base/startup/init_lite/ueventd",
"//base/startup/init_lite/ueventd/include",
"//utils/native/base/include",
"//utils/system/safwk/native/include",
"//third_party/bounds_checking_function/include",
......
......@@ -21,11 +21,11 @@
#include <sys/types.h>
#include "cJSON.h"
#include "gtest/gtest.h"
#include "init_cmds.h"
#include "init_jobs_internal.h"
#include "init_service_manager.h"
#include "securec.h"
#include "gtest/gtest.h"
using namespace std;
using namespace testing::ext;
......@@ -171,11 +171,10 @@ void DoCmd(const TestCmdLine *resCmd)
}
/*
** @tc.name: cmdFuncParseCmdTest_001
** @tc.desc: parse function, nullptr test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncParseCmdTest_001
* @tc.desc: parse function, nullptr test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0)
{
// do not crash
......@@ -183,11 +182,10 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_001, TestSize.Level0)
};
/*
** @tc.name: cmdFuncParseCmdTest_002
** @tc.desc: parse function, invalid strings test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncParseCmdTest_002
* @tc.desc: parse function, invalid strings test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -211,11 +209,10 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_002, TestSize.Level0)
}
/*
** @tc.name: cmdFuncParseCmdTest_003
** @tc.desc: parse function, cmd content empty test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncParseCmdTest_003
* @tc.desc: parse function, cmd content empty test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -229,11 +226,10 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_003, TestSize.Level0)
}
/*
** @tc.name: cmdFuncParseCmdTest_004
** @tc.desc: parse function, cmd content too long test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncParseCmdTest_004
* @tc.desc: parse function, cmd content too long test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -270,11 +266,10 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_004, TestSize.Level0)
}
/*
** @tc.name: cmdFuncParseCmdTest_005
** @tc.desc: parse function, parse success test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncParseCmdTest_005
* @tc.desc: parse function, parse success test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -302,11 +297,10 @@ HWTEST_F(StartupInitUTest, cmdFuncParseCmdTest_005, TestSize.Level0)
};
/*
** @tc.name: cmdFuncDoCmdTest_001
** @tc.desc: do cmd function, nullptr test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_001
* @tc.desc: do cmd function, nullptr test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0)
{
// do not crash here
......@@ -314,11 +308,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_001, TestSize.Level0)
}
/*
** @tc.name: cmdFuncDoCmdTest_002
** @tc.desc: do cmd function, do start fail test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_002
* @tc.desc: do cmd function, do start fail test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -334,11 +327,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_002, TestSize.Level0)
}
/*
** @tc.name: cmdFuncDoCmdTest_003
** @tc.desc: do cmd function, do mkdir fail test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_003
* @tc.desc: do cmd function, do mkdir fail test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -380,11 +372,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_003, TestSize.Level0)
}
/*
** @tc.name: cmdFuncDoCmdTest_004
** @tc.desc: do cmd function, do chmod fail test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_004
* @tc.desc: do cmd function, do chmod fail test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -437,11 +428,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_004, TestSize.Level0)
}
/*
** @tc.name: cmdFuncDoCmdTest_005
** @tc.desc: do cmd function, do chown fail test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_005
* @tc.desc: do cmd function, do chown fail test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -474,11 +464,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_005, TestSize.Level0)
}
/*
** @tc.name: cmdFuncDoCmdTest_006
** @tc.desc: do cmd function, do success test
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cmdFuncDoCmdTest_006
* @tc.desc: do cmd function, do success test
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0)
{
TestCmdLine curCmdLine;
......@@ -542,11 +531,10 @@ HWTEST_F(StartupInitUTest, cmdFuncDoCmdTest_006, TestSize.Level0)
}
/*
** @tc.name: cfgCheckStat_001
** @tc.desc: init.cfg file state check
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cfgCheckStat_001
* @tc.desc: init.cfg file state check
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cfgCheckStat_001, TestSize.Level0)
{
struct stat fileStat = {0};
......@@ -807,11 +795,10 @@ static void CheckJobs(const cJSON* fileRoot)
}
/*
** @tc.name: cfgCheckContent_001
** @tc.desc: init.cfg file content check
** @tc.type: FUNC
** @tc.require:
**/
* @tc.name: cfgCheckContent_001
* @tc.desc: init.cfg file content check
* @tc.type: FUNC
*/
HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0)
{
char *fileBuf = ReadFileToBuf();
......@@ -836,7 +823,6 @@ HWTEST_F(StartupInitUTest, cfgCheckContent_001, TestSize.Level0)
* @tc.name: CreateIllegalCfg
* @tc.desc: Create illegal Config file for testing
* @tc.type: FUNC
* @tc.require:
*/
static void CreateIllegalCfg()
{
......@@ -860,7 +846,6 @@ static void CreateIllegalCfg()
* @tc.name: cmdFuncDoLoadCfgTest_001
* @tc.desc: parse function, parse success test
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0)
{
......@@ -876,7 +861,6 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_001, TestSize.Level0)
* @tc.name: cmdFuncDoLoadCfgTest_002
* @tc.desc: fstab.cfg file fail test
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0)
{
......@@ -916,7 +900,6 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_002, TestSize.Level0)
* @tc.name: cmdFuncDoLoadCfgTest_003
* @tc.desc: fstab.cfg file success test
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0)
{
......@@ -965,7 +948,6 @@ HWTEST_F(StartupInitUTest, cmdFuncDoLoadCfgTest_003, TestSize.Level0)
* @tc.name: cmdJobTest_001
* @tc.desc: job functions test
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0)
{
......@@ -985,7 +967,6 @@ HWTEST_F(StartupInitUTest, cmdJobTest_001, TestSize.Level0)
* @tc.name: cmdJobTest_002
* @tc.desc: job functions test
* @tc.type: FUNC
* @tc.require:
*/
HWTEST_F(StartupInitUTest, cmdJobTest_002, TestSize.Level0)
{
......
......@@ -14,6 +14,7 @@
*/
#include <cerrno>
#include <unistd.h>
#include "fs_manager/fs_manager.h"
#include "init_unittest.h"
#include "init_mount.h"
#include "securec.h"
......@@ -31,7 +32,12 @@ public:
HWTEST_F(MountUnitTest, TestMountRequriedPartitions, TestSize.Level0)
{
int ret = MountRequriedPartitions();
EXPECT_EQ(ret, -1);
const char *fstabFiles = "/etc/fstab.required";
Fstab *fstab = NULL;
fstab = ReadFstabFromFile(fstabFiles, false);
if (fstab != NULL) {
int ret = MountRequriedPartitions(fstab);
EXPECT_EQ(ret, -1);
}
}
} // namespace init_ut
......@@ -17,6 +17,8 @@
#include <sys/mount.h>
#include "dynamic_service.h"
#include "fs_manager/fs_manager.h"
#include "fs_manager/fs_manager_log.h"
#include "init_log.h"
#include "init_unittest.h"
#include "securec.h"
......@@ -147,4 +149,19 @@ HWTEST_F(InnerkitsUnitTest, GetMountFlags_unitest, TestSize.Level1)
ReleaseFstab(fstab);
fstab = nullptr;
}
HWTEST_F(InnerkitsUnitTest, TestFsManagerLog, TestSize.Level1)
{
FsManagerLogInit(LOG_TO_KERNEL, FILE_NAME);
FSMGR_LOGE("Fsmanager log to kernel.");
FsManagerLogInit(LOG_TO_STDIO, "");
FSMGR_LOGE("Fsmanager log to stdio.");
string logPath = "/data/init_ut/fs_log.txt";
auto fp = std::unique_ptr<FILE, decltype(&fclose)>(fopen(logPath.c_str(), "at+"), fclose);
EXPECT_TRUE(fp != nullptr);
sync();
FsManagerLogInit(LOG_TO_FILE, logPath.c_str());
FSMGR_LOGE("Fsmanager log to file.");
FsManagerLogDeInit();
}
} // namespace init_ut
......@@ -10,6 +10,7 @@
# 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.
if (defined(ohos_lite)) {
if (ohos_kernel_type == "linux") {
executable("ueventd_linux") {
......@@ -28,12 +29,11 @@ if (defined(ohos_lite)) {
defines += [ "_GNU_SOURCE" ]
include_dirs = [
".",
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/include",
"//base/startup/init_lite/services/utils",
"//kernel/linux-4.19/include/uapi",
"//base/startup/init_lite/ueventd/include",
]
deps = [
......@@ -56,32 +56,49 @@ if (defined(ohos_lite)) {
}
} else {
import("//build/ohos.gni")
service_ueventd_sources = [
"//base/startup/init_lite/services/utils/list.c",
"//base/startup/init_lite/ueventd/ueventd.c",
"//base/startup/init_lite/ueventd/ueventd_device_handler.c",
"//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",
]
service_ueventd_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",
"//base/startup/init_lite/ueventd/include",
]
service_ueventd_deps = [
"//base/startup/init_lite/services/log:init_log",
"//base/startup/init_lite/services/utils:libinit_utils",
"//third_party/bounds_checking_function:libsec_static",
]
ohos_executable("ueventd") {
sources = [
"//base/startup/init_lite/services/utils/list.c",
"//base/startup/init_lite/ueventd/ueventd.c",
"//base/startup/init_lite/ueventd/ueventd_device_handler.c",
"//base/startup/init_lite/ueventd/ueventd_firmware_handler.c",
"//base/startup/init_lite/ueventd/ueventd_main.c",
"//base/startup/init_lite/ueventd/ueventd_read_cfg.c",
"//base/startup/init_lite/ueventd/ueventd_socket.c",
group("startup_ueventd") {
deps = [
"//base/startup/init_lite/ueventd:libueventd_static",
"//base/startup/init_lite/ueventd:ueventd",
"//base/startup/init_lite/ueventd:ueventd.config",
]
}
ohos_static_library("libueventd_static") {
sources = service_ueventd_sources
include_dirs = service_ueventd_include
deps = service_ueventd_deps
}
ohos_executable("ueventd") {
sources = [ "//base/startup/init_lite/ueventd/ueventd_main.c" ]
include_dirs = [
".",
"//third_party/bounds_checking_function/include",
"//base/startup/init_lite/ueventd/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",
]
deps = [ "//base/startup/init_lite/ueventd:libueventd_static" ]
install_images = [
"system",
"updater",
......
......@@ -60,8 +60,13 @@ typedef enum SUBYSTEM {
SUBSYSTEM_OTHERS = 3,
} SUBSYSTEMTYPE;
#define CMDLINE_VALUE_LEN_MAX 512
extern char bootDevice[CMDLINE_VALUE_LEN_MAX];
const char *ActionString(ACTION action);
void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *uevent);
void RetriggerUevent(int sockFd);
void ProcessUevent(int sockFd);
#endif // BASE_STARTUP_INITLITE_UEVENTD_H
\ No newline at end of file
void RetriggerUevent(int sockFd, char **devices, int num);
void ProcessUevent(int sockFd, char **devices, int num);
#endif // BASE_STARTUP_INITLITE_UEVENTD_H
......@@ -29,9 +29,12 @@
#include "securec.h"
#define INIT_LOG_TAG "ueventd"
#include "init_log.h"
#include "init_utils.h"
// buffer size refer to kernel kobject uevent
#define UEVENT_BUFFER_SIZE (2048 + 1)
char bootDevice[CMDLINE_VALUE_LEN_MAX] = { 0 };
#define WRITE_SIZE 4
static const char *actions[] = {
[ACTION_ADD] = "add",
......@@ -116,6 +119,28 @@ static void HandleUevent(const struct Uevent *uevent)
}
}
static void HandleUeventRequired(const struct Uevent *uevent, char **devices, int num)
{
INIT_ERROR_CHECK(devices != NULL && num > 0, return, "Fault paramters");
if (uevent->action == ACTION_ADD) {
ChangeSysAttributePermissions(uevent->syspath);
}
SUBSYSTEMTYPE type = GetSubsystemType(uevent->subsystem);
if (type == SUBSYSTEM_BLOCK) {
for (int i = 0; i < num; i++) {
if (uevent->partitionName == NULL) {
break;
}
if (strstr(devices[i], uevent->partitionName) != NULL) {
INIT_LOGI("Handle block device partitionName %s", uevent->partitionName);
HandleBlockDeviceEvent(uevent);
break;
}
}
}
}
static void AddUevent(struct Uevent *uevent, const char *event, size_t len)
{
if (uevent == NULL || event == NULL || len == 0) {
......@@ -177,7 +202,7 @@ void ParseUeventMessage(const char *buffer, ssize_t length, struct Uevent *ueven
}
}
void ProcessUevent(int sockFd)
void ProcessUevent(int sockFd, char **devices, int num)
{
// One more bytes for '\0'
char ueventBuffer[UEVENT_BUFFER_SIZE] = {};
......@@ -189,12 +214,16 @@ void ProcessUevent(int sockFd)
INIT_LOGD("Ignore unexpected uevent");
return;
}
HandleUevent(&uevent);
if (devices != NULL && num > 0) {
HandleUeventRequired(&uevent, devices, num);
} else {
HandleUevent(&uevent);
}
}
}
static int g_triggerDone = 0;
static void DoTrigger(const char *ueventPath, int sockFd)
static void DoTrigger(const char *ueventPath, int sockFd, char **devices, int num)
{
if (ueventPath == NULL || ueventPath[0] == '\0') {
return;
......@@ -204,7 +233,7 @@ static void DoTrigger(const char *ueventPath, int sockFd)
if (fd < 0) {
INIT_LOGE("Open \" %s \" failed, err = %d", ueventPath, errno);
} else {
ssize_t n = write(fd, "add\n", 4);
ssize_t n = write(fd, "add\n", WRITE_SIZE);
if (n < 0) {
INIT_LOGE("Write \" %s \" failed, err = %d", ueventPath, errno);
close(fd);
......@@ -212,13 +241,13 @@ static void DoTrigger(const char *ueventPath, int sockFd)
close(fd);
// uevent triggered, now handle it.
if (sockFd >= 0) {
ProcessUevent(sockFd);
ProcessUevent(sockFd, devices, num);
}
}
}
}
static void Trigger(const char *path, int sockFd)
static void Trigger(const char *path, int sockFd, char **devices, int num)
{
if (path == NULL) {
return;
......@@ -237,7 +266,7 @@ static void Trigger(const char *path, int sockFd)
if (snprintf_s(pathBuffer, PATH_MAX, PATH_MAX - 1, "%s/%s", path, dirent->d_name) == -1) {
continue;
}
Trigger(pathBuffer, sockFd);
Trigger(pathBuffer, sockFd, devices, num);
} else {
if (strcmp(dirent->d_name, "uevent") != 0) {
continue;
......@@ -247,18 +276,25 @@ static void Trigger(const char *path, int sockFd)
INIT_LOGW("Cannnot build uevent path under %s", path);
continue;
}
DoTrigger(ueventBuffer, sockFd);
DoTrigger(ueventBuffer, sockFd, devices, num);
}
}
closedir(dir);
}
void RetriggerUevent(int sockFd)
void RetriggerUevent(int sockFd, char **devices, int num)
{
char *buffer = ReadFileData("/proc/cmdline");
int ret = GetProcCmdlineValue("default_boot_device", buffer, bootDevice, CMDLINE_VALUE_LEN_MAX);
INIT_CHECK_ONLY_ELOG(ret == 0, "Failed get default_boot_device value from cmdline");
if (!g_triggerDone) {
Trigger("/sys/block", sockFd);
Trigger("/sys/class", sockFd);
Trigger("/sys/devices", sockFd);
Trigger("/sys/block", sockFd, devices, num);
Trigger("/sys/class", sockFd, devices, num);
Trigger("/sys/devices", sockFd, devices, num);
g_triggerDone = 1;
}
if (buffer != NULL) {
free(buffer);
}
}
......@@ -48,7 +48,7 @@ static void CreateSymbolLinks(const char *deviceNode, char **symLinks)
}
const char *linkDir = dirname(linkBuf);
if (MakeDirRecursive(linkDir, DIRMODE) < 0) {
INIT_LOGE("[uevent] Failed to create dir \" %s \", err = %d", linkDir, errno);
INIT_LOGE("[uevent] Failed to create dir \" %s \", err = %d", linkDir, errno);
}
errno = 0;
int rc = symlink(deviceNode, linkName);
......@@ -170,6 +170,24 @@ static char *FindPlatformDeviceName(char *path)
return NULL;
}
static void BuildBootDeviceSymbolLink(char **links, int linkNum, const char *partitionName)
{
if (linkNum > BLOCKDEVICE_LINKS - 1) {
INIT_LOGW("Too many links, ignore");
return;
}
links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE);
if (links[linkNum] == NULL) {
INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
return;
}
if (snprintf_s(links[linkNum], DEVICE_FILE_SIZE, DEVICE_FILE_SIZE - 1,
"/dev/block/by-name/%s", partitionName) == -1) {
INIT_LOGE("Failed to build link");
}
}
static void BuildDeviceSymbolLinks(char **links, int linkNum, const char *parent,
const char *partitionName, const char *deviceName)
{
......@@ -177,7 +195,6 @@ static void BuildDeviceSymbolLinks(char **links, int linkNum, const char *parent
INIT_LOGW("Too many links, ignore");
return;
}
links[linkNum] = calloc(sizeof(char), DEVICE_FILE_SIZE);
if (links[linkNum] == NULL) {
INIT_LOGE("Failed to allocate memory for link, err = %d", errno);
......@@ -218,7 +235,6 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
INIT_LOGE("Failed to build sys path for device %s", uevent->syspath);
return NULL;
}
char **links = calloc(sizeof(char *), BLOCKDEVICE_LINKS);
int linkNum = 0;
if (links == NULL) {
......@@ -234,7 +250,6 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
INIT_LOGE("Failed to build subsystem path for device \" %s \"", uevent->syspath);
return NULL;
}
char *bus = GetRealPath(subsystem);
if (bus == NULL) {
parent = dirname(parent);
......@@ -247,6 +262,10 @@ static char **GetBlockDeviceSymbolLinks(const struct Uevent *uevent)
BuildDeviceSymbolLinks(links, linkNum, parent, uevent->partitionName, uevent->deviceName);
}
linkNum++;
if (STRINGEQUAL(parent, bootDevice)) {
BuildBootDeviceSymbolLink(links, linkNum, uevent->partitionName);
linkNum++;
}
}
free(bus);
parent = dirname(parent);
......
......@@ -17,7 +17,6 @@
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include "ueventd.h"
#define INIT_LOG_TAG "ueventd"
......
......@@ -15,7 +15,6 @@
#include <poll.h>
#include "ueventd.h"
#include "ueventd_device_handler.h"
#include "ueventd_read_cfg.h"
#include "ueventd_socket.h"
#define INIT_LOG_TAG "ueventd"
......@@ -35,7 +34,7 @@ int main(int argc, char **argv)
return -1;
}
RetriggerUevent(ueventSockFd);
RetriggerUevent(ueventSockFd, NULL, 0); // Not require boot devices
struct pollfd pfd = {};
pfd.events = POLLIN;
pfd.fd = ueventSockFd;
......@@ -47,7 +46,7 @@ int main(int argc, char **argv)
continue;
}
if (pfd.revents & POLLIN) {
ProcessUevent(ueventSockFd);
ProcessUevent(ueventSockFd, NULL, 0); // Not require boot devices
}
}
return 0;
......
......@@ -204,7 +204,7 @@ int ParseUeventConfig(char *buffer)
callback = funcMapper[type].func;
return 0;
}
return callback != NULL ? callback(p) : -1;
return (callback != NULL) ? callback(p) : -1;
}
static void DoUeventConfigParse(char *buffer, size_t length)
......@@ -332,7 +332,7 @@ bool IsMatch(const char *target, const char *pattern)
while (*p == '*') {
p++;
}
return *p == '\0' ? true : false;
return (*p == '\0') ? true : false;
}
void GetDeviceNodePermissions(const char *devNode, uid_t *uid, gid_t *gid, mode_t *mode)
......
......@@ -16,7 +16,6 @@
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
......@@ -66,13 +65,13 @@ int main(int argc, const char *argv[])
if (argc >= 2) { // Argument nums greater than or equal to 2.
interval = atoi(argv[1]);
}
interval = interval > 0 ? interval : DEFAULT_INTERVAL;
interval = (interval > 0) ? interval : DEFAULT_INTERVAL;
int gap = 0;
if (argc >= 3) { // Argument nums greater than or equal to 3.
gap = atoi(argv[2]); // 2 second parameter.
}
gap = gap > 0 ? gap : DEFAULT_GAP;
gap = (gap > 0) ? gap : DEFAULT_GAP;
INIT_LOGI("watchdoge started (interval %d, margin %d), fd = %d\n", interval, gap, fd);
......@@ -86,7 +85,7 @@ int main(int argc, const char *argv[])
if (ret) {
INIT_LOGE("Failed to get timeout\n");
} else {
interval = timeoutGet > gap ? timeoutGet - gap : 1;
interval = (timeoutGet > gap) ? timeoutGet - gap : 1;
}
while (1) {
ioctl(fd, WDIOC_KEEPALIVE);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册