From dda883b204392ce3e7e7620aeefc2853557f57a2 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Thu, 2 Sep 2021 17:06:47 +0800 Subject: [PATCH 1/3] enable log to kmsg Signed-off-by: zhong_ning --- services/include/device.h | 1 + services/log/init_log.c | 90 ++++++++++++++++++++++++--------------- services/log/init_log.h | 36 ++++++++++------ services/src/device.c | 19 ++++++--- services/src/main.c | 4 ++ 5 files changed, 98 insertions(+), 52 deletions(-) diff --git a/services/include/device.h b/services/include/device.h index 73265fe21..b129056eb 100644 --- a/services/include/device.h +++ b/services/include/device.h @@ -31,6 +31,7 @@ extern "C" { void MountBasicFs(); void CreateDeviceNode(); int MakeSocketDir(const char *path, mode_t mode); +void CloseStdio(); #ifdef __cplusplus #if __cplusplus diff --git a/services/log/init_log.c b/services/log/init_log.c index aa0307903..ed783d482 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -16,20 +16,16 @@ #include "init_log.h" #include #include -#ifdef OHOS_LITE -#include "hilog/log.h" -#endif #include -#include -#include +#include #include -#include + #include "securec.h" #define UNUSED(x) (void)(x) -#define MAX_FORMAT_SIZE 2048 -#define MAX_LOG_SIZE 2048 +#define MAX_LOG_SIZE 1024 #define BASE_YEAR 1900 +#define UNLIKELY(x) __builtin_expect(!!(x), 0) static InitLogLevel g_logLevel = INIT_INFO; static const char *LOG_LEVEL_STR[] = { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" }; @@ -57,8 +53,8 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...) } va_list list; va_start(list, fmt); - char tmpFmt[MAX_FORMAT_SIZE]; - if (vsnprintf_s(tmpFmt, MAX_FORMAT_SIZE, MAX_FORMAT_SIZE - 1, fmt, list) == -1) { + char tmpFmt[MAX_LOG_SIZE]; + if (vsnprintf_s(tmpFmt, MAX_LOG_SIZE, MAX_LOG_SIZE - 1, fmt, list) == -1) { va_end(list); return; } @@ -68,7 +64,31 @@ void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...) } #endif -void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *fmt, ...) +static int g_fd = -1; +void OpenLogDevice() +{ + int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP); + if (fd >= 0) { + g_fd = fd; + } + return; +} + +void EnableDevKmsg() +{ + /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions*/ + int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP); + if (fd < 0) { + return; + } + char *kmsgStatus = "on"; + write(fd, kmsgStatus, strlen(kmsgStatus) + 1); + close(fd); + fd = -1; + return; +} +void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *kLevel, + const char *fmt, ...) { if (logLevel < g_logLevel) { return; @@ -76,34 +96,36 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l if (tag == NULL) { return; } - time_t second = time(0); - struct tm *t = localtime(&second); - if (t == NULL) { - printf("time is NULL.\n"); + + if (UNLIKELY(g_fd < 0)) { + OpenLogDevice(); + if (g_fd < 0) { + return; + } + } + va_list vargs; + va_start(vargs, fmt); + char tmpFmt[MAX_LOG_SIZE]; + if (vsnprintf_s(tmpFmt, MAX_LOG_SIZE, MAX_LOG_SIZE - 1, fmt, vargs) == -1) { + close(g_fd); + g_fd = -1; return; } - fprintf(stdout, "[%d-%d-%d %d:%d:%d][pid=%d][%s:%d][%s][%s] ", - (t->tm_year + BASE_YEAR), (t->tm_mon + 1), t->tm_mday, t->tm_hour, t->tm_min, t->tm_sec, - getpid(), fileName, line, tag, LOG_LEVEL_STR[logLevel]); - - va_list list; - va_start(list, fmt); - vfprintf(stdout, fmt, list); - va_end(list); - fflush(stdout); -#if 0 - int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC | O_APPEND ); - if (fd < 1) { - printf("xxxxxxxxxxxxxxx open failed. %d\n", errno); + char logInfo[MAX_LOG_SIZE]; + if (snprintf_s(logInfo, MAX_LOG_SIZE, MAX_LOG_SIZE - 1, "%s[pid=%d][%s:%d][%s][%s] %s", + kLevel, getpid(), fileName, line, tag, LOG_LEVEL_STR[logLevel], tmpFmt) == -1) { + close(g_fd); + g_fd = -1; return; } - if (write(fd, logInfo, strlen(logInfo)) < -1) { - printf("xxxxxxxxxxxxxxx write failed.%d\n", errno); - close(fd); - return; + va_end(vargs); + + if (write(g_fd, logInfo, strlen(logInfo)) < 0) { + close(g_fd); + g_fd = -1; } - close(fd); -#endif + return; } + diff --git a/services/log/init_log.h b/services/log/init_log.h index 8c7e08518..ce5bbe230 100644 --- a/services/log/init_log.h +++ b/services/log/init_log.h @@ -42,7 +42,7 @@ typedef enum InitLogLevel { #include "hilog/log.h" #undef LOG_DOMAIN -#define LOG_DOMAIN 0xD000719 +#define LOG_DOMAIN 0xD000719 #define INIT_LOGD(fmt, ...) InitToHiLog(INIT_LOG_TAG, LOG_DEBUG, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__) #define INIT_LOGI(fmt, ...) InitToHiLog(INIT_LOG_TAG, LOG_INFO, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__) @@ -54,24 +54,34 @@ typedef enum InitLogLevel { #define STARTUP_LOGI(LABEL, fmt, ...) InitToHiLog(LABEL, LOG_INFO, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__) #define STARTUP_LOGE(LABEL, fmt, ...) InitToHiLog(LABEL, LOG_ERROR, "%s : "fmt, (__FUNCTION__), ##__VA_ARGS__) - void InitToHiLog(const char *tag, LogLevel logLevel, const char *fmt, ...); void SetHiLogLevel(LogLevel logLevel); #else #define __FILE_NAME__ (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) -#define INIT_LOGD(fmt, ...) InitLog(INIT_LOG_TAG, INIT_DEBUG, (__FILE_NAME__), (__LINE__), fmt"\n", ##__VA_ARGS__) -#define INIT_LOGI(fmt, ...) InitLog(INIT_LOG_TAG, INIT_INFO, (__FILE_NAME__), (__LINE__), fmt"\n", ##__VA_ARGS__) -#define INIT_LOGW(fmt, ...) InitLog(INIT_LOG_TAG, INIT_WARN, (__FILE_NAME__), (__LINE__), fmt"\n", ##__VA_ARGS__) -#define INIT_LOGE(fmt, ...) InitLog(INIT_LOG_TAG, INIT_ERROR, (__FILE_NAME__), (__LINE__), fmt"\n", ##__VA_ARGS__) -#define INIT_LOGF(fmt, ...) InitLog(INIT_LOG_TAG, INIT_FATAL, (__FILE_NAME__), (__LINE__), fmt"\n", ##__VA_ARGS__) - -#define STARTUP_LOGD(LABEL, fmt, ...) InitLog(LABEL, INIT_DEBUG, (__FILE_NAME__), (__LINE__), fmt "\n", ##__VA_ARGS__) -#define STARTUP_LOGI(LABEL, fmt, ...) InitLog(LABEL, INIT_INFO, (__FILE_NAME__), (__LINE__), fmt "\n", ##__VA_ARGS__) -#define STARTUP_LOGE(LABEL, fmt, ...) InitLog(LABEL, INIT_ERROR, (__FILE_NAME__), (__LINE__), fmt "\n", ##__VA_ARGS__) - -void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *fmt, ...); +#define INIT_LOGD(fmt, ...) InitLog(INIT_LOG_TAG, INIT_DEBUG, (__FILE_NAME__), (__LINE__), \ + "<7>", fmt"\n", ##__VA_ARGS__) +#define INIT_LOGI(fmt, ...) InitLog(INIT_LOG_TAG, INIT_INFO, (__FILE_NAME__), (__LINE__), \ + "<6>", fmt"\n", ##__VA_ARGS__) +#define INIT_LOGW(fmt, ...) InitLog(INIT_LOG_TAG, INIT_WARN, (__FILE_NAME__), (__LINE__), \ + "<4>", fmt"\n", ##__VA_ARGS__) +#define INIT_LOGE(fmt, ...) InitLog(INIT_LOG_TAG, INIT_ERROR, (__FILE_NAME__), (__LINE__), \ + "<3>", fmt"\n", ##__VA_ARGS__) +#define INIT_LOGF(fmt, ...) InitLog(INIT_LOG_TAG, INIT_FATAL, (__FILE_NAME__), (__LINE__), \ + "<3>", fmt"\n", ##__VA_ARGS__) + +#define STARTUP_LOGD(LABEL, fmt, ...) InitLog(LABEL, INIT_DEBUG, (__FILE_NAME__), (__LINE__), \ + "<7>", fmt "\n", ##__VA_ARGS__); +#define STARTUP_LOGI(LABEL, fmt, ...) InitLog(LABEL, INIT_INFO, (__FILE_NAME__), (__LINE__), \ + "<6>", fmt "\n", ##__VA_ARGS__); +#define STARTUP_LOGE(LABEL, fmt, ...) InitLog(LABEL, INIT_ERROR, (__FILE_NAME__), (__LINE__), \ + "<3>", fmt "\n", ##__VA_ARGS__); + +void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *kLevel, + const char *fmt, ...); void SetLogLevel(InitLogLevel logLevel); +void OpenLogDevice(); +void EnableDevKmsg(); #endif #define INIT_ERROR_CHECK(ret, statement, format, ...) \ diff --git a/services/src/device.c b/services/src/device.c index fabc880bc..1c1311ca2 100644 --- a/services/src/device.c +++ b/services/src/device.c @@ -15,8 +15,9 @@ #include "device.h" #include +#include #include -#include +#include #include #include #include @@ -27,30 +28,38 @@ #define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) #define DEFAULT_NO_AUTHORITY_MODE (S_IWUSR | S_IRUSR) +void CloseStdio() +{ + int fd = open("/dev/null", O_RDWR | O_CLOEXEC); + if (fd < 0) { + return; + } + dup2(fd, 0); + dup2(fd, 1); + dup2(fd, 2); + close(fd); +} + void MountBasicFs() { if (mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755") != 0) { INIT_LOGE("Mount tmpfs failed. %s", strerror(errno)); } -#ifndef __LITEOS__ if (mkdir("/dev/pts", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) != 0) { INIT_LOGE("mkdir /dev/pts failed. %s", strerror(errno)); } if (mount("devpts", "/dev/pts", "devpts", 0, NULL) != 0) { INIT_LOGE("Mount devpts failed. %s", strerror(errno)); } -#endif if (mount("proc", "/proc", "proc", 0, "hidepid=2") != 0) { INIT_LOGE("Mount procfs failed. %s", strerror(errno)); } if (mount("sysfs", "/sys", "sysfs", 0, NULL) != 0) { INIT_LOGE("Mount sysfs failed. %s", strerror(errno)); } -#ifndef __LITEOS__ if (mount("selinuxfs", "/sys/fs/selinux", "selinuxfs", 0, NULL) != 0) { INIT_LOGE("Mount selinuxfs failed. %s", strerror(errno)); } -#endif } void CreateDeviceNode() diff --git a/services/src/main.c b/services/src/main.c index 619eb911c..2bce7f335 100644 --- a/services/src/main.c +++ b/services/src/main.c @@ -70,6 +70,9 @@ int main(int argc, char * const argv[]) INIT_LOGE("set UV_THREADPOOL_SIZE error : %d.", errno); } + CloseStdio(); + OpenLogDevice(); + #endif #ifdef OHOS_DEBUG struct timespec tmEnter; @@ -90,6 +93,7 @@ int main(int argc, char * const argv[]) // 2. Mount basic filesystem and create common device node. MountBasicFs(); CreateDeviceNode(); + EnableDevKmsg(); MakeSocketDir("/dev/unix/socket/", S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); #endif -- Gitee From 35ccde7607af5851c0a1967730291c7a899d536c Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Thu, 2 Sep 2021 18:23:32 +0800 Subject: [PATCH 2/3] fix code style Signed-off-by: zhong_ning --- services/log/init_log.c | 4 ++-- services/src/device.c | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/services/log/init_log.c b/services/log/init_log.c index ed783d482..b45bb76d8 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -19,7 +19,6 @@ #include #include #include - #include "securec.h" #define UNUSED(x) (void)(x) @@ -76,7 +75,7 @@ void OpenLogDevice() void EnableDevKmsg() { - /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions*/ + /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */ int fd = open("/proc/sys/kernel/printk_devkmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IRGRP); if (fd < 0) { return; @@ -87,6 +86,7 @@ void EnableDevKmsg() fd = -1; return; } + void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int line, const char *kLevel, const char *fmt, ...) { diff --git a/services/src/device.c b/services/src/device.c index 1c1311ca2..d5a01c342 100644 --- a/services/src/device.c +++ b/services/src/device.c @@ -17,16 +17,14 @@ #include #include #include -#include -#include #include #include #include -#include #include "init_log.h" #define DEFAULT_RW_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) #define DEFAULT_NO_AUTHORITY_MODE (S_IWUSR | S_IRUSR) +#define STDERR_HANDLE 2 void CloseStdio() { @@ -36,7 +34,7 @@ void CloseStdio() } dup2(fd, 0); dup2(fd, 1); - dup2(fd, 2); + dup2(fd, STDERR_HANDLE); close(fd); } -- Gitee From e1bc41ed306955f22bcfda0936d53d316dc0c136 Mon Sep 17 00:00:00 2001 From: zhong_ning Date: Thu, 2 Sep 2021 18:45:21 +0800 Subject: [PATCH 3/3] delete blank line Signed-off-by: zhong_ning --- services/log/init_log.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/services/log/init_log.c b/services/log/init_log.c index b45bb76d8..158d29b03 100644 --- a/services/log/init_log.c +++ b/services/log/init_log.c @@ -127,5 +127,3 @@ void InitLog(const char *tag, InitLogLevel logLevel, const char *fileName, int l } return; } - - -- Gitee