From a10f335bd67b00ae25ccf4ff11bb8c666dc0c04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A8=8A=E6=99=AF=E4=B9=90?= Date: Tue, 15 Jul 2025 18:11:05 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B8=A2=E7=8B=97=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 樊景乐 --- nwebspawn.cfg | 2 +- standard/appspawn_kickdog.c | 3 ++- util/include/appspawn_utils.h | 25 ++++++++++++++++++ util/src/appspawn_utils.c | 48 +++++++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 2 deletions(-) diff --git a/nwebspawn.cfg b/nwebspawn.cfg index f29e3c89..a0cbe42d 100644 --- a/nwebspawn.cfg +++ b/nwebspawn.cfg @@ -7,7 +7,7 @@ "--sandbox-switch on --bundle-name com.ohos.nwebspawn.startup --app-operate-type operate ", "--render-command command --app-launch-type singleton --app-visible true"], "uid" : "nwebspawn", - "gid" : ["nwebspawn"], + "gid" : ["nwebspawn", "system"], "setuid" : true, "caps" : ["CAP_SYS_ADMIN", "CAP_SETGID", "CAP_SETUID", "CAP_KILL"], "socket" : [{ diff --git a/standard/appspawn_kickdog.c b/standard/appspawn_kickdog.c index a471c7d1..3123f2f5 100644 --- a/standard/appspawn_kickdog.c +++ b/standard/appspawn_kickdog.c @@ -73,7 +73,8 @@ static void DealSpawnWatchdog(AppSpawnContent *content, bool isOpen) if (isOpen) { content->wdgOpened = (result != -1); } - APPSPAWN_DUMP_LOGI("%{public}s %{public}s %{public}d", + + KMSSG_LOGI("%s %s %d", (content->mode == MODE_FOR_NWEB_SPAWN) ? "Nweb" : "Apps", isOpen ? "enable" : "kick", result); } diff --git a/util/include/appspawn_utils.h b/util/include/appspawn_utils.h index 3317f1ba..e5d71b54 100644 --- a/util/include/appspawn_utils.h +++ b/util/include/appspawn_utils.h @@ -83,6 +83,10 @@ extern "C" { #define INVALID_PERMISSION_INDEX (-1) +#ifndef UNLIKELY +#define UNLIKELY(x) __builtin_expect(!!(x), 0) +#endif + typedef struct { int flag; const char *ldPreload; @@ -92,6 +96,14 @@ typedef struct { const char *hwasanOptions; } EnvConfig; +typedef enum AppSpawnLogLevel { + APPSPAWN_KMSG_DEBUG = 0, + APPSPAWN_KMSG_INFO, + APPSPAWN_KMSG_WARN, + APPSPAWN_KMSG_ERROR, + APPSPAWN_KMSG_FATAL +} AppSpawnLevel; + #define MAX_ENV_VALUE_LEN 1024 typedef struct TagAppSpawnCommonEnv { @@ -156,6 +168,8 @@ int ConvertEnvValue(const char *srcEnv, char *dstEnv, int len); int EnableNewNetNamespace(void); +void KMsgLog(const char* fileName, int line, int kLevel, const char* tag, const char* fmt, ...); + #ifndef APP_FILE_NAME #define APP_FILE_NAME (strrchr((__FILE__), '/') ? strrchr((__FILE__), '/') + 1 : (__FILE__)) #endif @@ -203,6 +217,17 @@ int EnableNewNetNamespace(void); HILOG_INFO(HILOG_MODULE_HIVIEW, fmt, ##__VA_ARGS__) #endif +#define KMSSG_LOGV(fmt, ...) \ + KMsgLog((APP_FILE_NAME), (__LINE__), APPSPAWN_KMSG_DEBUG, APPSPAWN_LABEL, fmt"\n", ##__VA_ARGS__) +#define KMSSG_LOGI(fmt, ...) \ + KMsgLog((APP_FILE_NAME), (__LINE__), APPSPAWN_KMSG_INFO, APPSPAWN_LABEL, fmt"\n", ##__VA_ARGS__) +#define KMSSG_LOGW(fmt, ...) \ + KMsgLog((APP_FILE_NAME), (__LINE__), APPSPAWN_KMSG_WARN, APPSPAWN_LABEL, fmt"\n", ##__VA_ARGS__) +#define KMSSG_LOGE(fmt, ...) \ + KMsgLog((APP_FILE_NAME), (__LINE__), APPSPAWN_KMSG_ERROR, APPSPAWN_LABEL, fmt"\n", ##__VA_ARGS__) +#define KMSSG_LOGF(fmt, ...) \ + KMsgLog((APP_FILE_NAME), (__LINE__), APPSPAWN_KMSG_FATAL, APPSPAWN_LABEL, fmt"\n", ##__VA_ARGS__) + #define APPSPAWN_CHECK(retCode, exper, fmt, ...) \ if (!(retCode)) { \ APPSPAWN_LOGE(fmt, ##__VA_ARGS__); \ diff --git a/util/src/appspawn_utils.c b/util/src/appspawn_utils.c index 20d8ca2e..9da800a4 100644 --- a/util/src/appspawn_utils.c +++ b/util/src/appspawn_utils.c @@ -435,3 +435,51 @@ int EnableNewNetNamespace(void) close(fd); return (ret >= 0) ? 0 : APPSPAWN_SYSTEM_ERROR; } + +static int g_fd = -1; +void KLogOpenLogDevice(void) +{ + int fd = open("/dev/kmsg", O_WRONLY | O_CLOEXEC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); + if (fd >= 0) { + g_fd = fd; + } + return; +} + +void KMsgLog(const char* fileName, int line, int kLevel, const char* tag, const char* fmt, ...) +{ + static const char *LOG_LEVEL_STR[] = { "DEBUG", "INFO", "WARNING", "ERROR", "FATAL" }; + static const char *LOG_KLEVEL_STR[] = { "<7>", "<6>", "<4>", "<3>", "<3>" }; + if (UNLIKELY(g_fd < 0)) { + KLogOpenLogDevice(); + if (g_fd < 0) { + return; + } + } + va_list vargs; + va_start(vargs, fmt); + + char tmpFmt[MAX_ENV_VALUE_LEN] = {0}; + if (vsnprintf_s(tmpFmt, sizeof(tmpFmt), sizeof(tmpFmt) - 1, fmt, vargs) == -1) { + tmpFmt[sizeof(tmpFmt) - 2] = '\n'; + tmpFmt[sizeof(tmpFmt) - 1] = '\0'; + } + + char logInfo[MAX_ENV_VALUE_LEN] = {0}; + if (snprintf_s(logInfo, sizeof(logInfo), sizeof(logInfo) - 1, "%s[pid=%d][%s:%d][%s][%s]:%s", + LOG_KLEVEL_STR[kLevel], getpid(), fileName, line, tag, LOG_LEVEL_STR[kLevel], tmpFmt) == -1) { + logInfo[sizeof(logInfo) - 2] = '\n'; // 2 add \n to tail + logInfo[sizeof(logInfo) - 1] = '\0'; + close(g_fd); + g_fd = -1; + va_end(vargs); + return; + } + + if (write(g_fd, logInfo, strlen(logInfo)) < 0) { + printf("%s\n", logInfo); + close(g_fd); + g_fd = -1; + } + va_end(vargs); +} \ No newline at end of file -- Gitee