From c6d1a9b4f7ea0919d9aedcf992d0ff960934daf0 Mon Sep 17 00:00:00 2001
From: Esben Haabendal <esben@geanix.com>
Date: Fri, 10 Mar 2023 08:22:56 +0100
Subject: [PATCH] openharmony-standard: Make param service log to journal

This avoids param service log information to end up spamming the console.

Signed-off-by: Esben Haabendal <esben@geanix.com>
---
 .../openharmony/files/param.service           |  1 +
 .../init_lite-log-to-stderr.patch             | 77 +++++++++++++++++++
 .../openharmony/openharmony-standard_3.0.bb   |  1 +
 3 files changed, 79 insertions(+)
 create mode 100644 recipes-openharmony/openharmony/openharmony-standard-3.0/init_lite-log-to-stderr.patch

diff --git a/recipes-openharmony/openharmony/files/param.service b/recipes-openharmony/openharmony/files/param.service
index 3c73749d..3a9189b9 100644
--- a/recipes-openharmony/openharmony/files/param.service
+++ b/recipes-openharmony/openharmony/files/param.service
@@ -5,6 +5,7 @@ Description=OpenHarmony Param Service
 Type=notify
 ExecStartPre=install -m 755 -d /run/openharmony/param
 ExecStartPre=install -m 755 -d /data/param
+StandardError=journal
 ExecStart=/usr/bin/param_service
 
 [Install]
diff --git a/recipes-openharmony/openharmony/openharmony-standard-3.0/init_lite-log-to-stderr.patch b/recipes-openharmony/openharmony/openharmony-standard-3.0/init_lite-log-to-stderr.patch
new file mode 100644
index 00000000..9d3d7e43
--- /dev/null
+++ b/recipes-openharmony/openharmony/openharmony-standard-3.0/init_lite-log-to-stderr.patch
@@ -0,0 +1,77 @@
+# SPDX-FileCopyrightText: Huawei Inc.
+#
+# SPDX-License-Identifier: Apache-2.0
+
+This patch is for //base/startup/init_lite repository of OpenHarmony 3.0
+
+Write log output to stderr instead of /dev/kmsg, which can be directed to
+systemd journal when run as a systemd service. This is for the param service,
+which is a dependency of hilog, which we can therefore not log to.
+
+Signed-off-by: Esben Haabendal <esben@geanix.com>
+Upstream-Status: Pending
+
+diff --git a/services/log/init_log.c b/services/log/init_log.c
+index 852be3d43b89..7314c6fb8c40 100644
+--- a/services/log/init_log.c
++++ b/services/log/init_log.c
+@@ -17,6 +17,7 @@
+ #include <errno.h>
+ #include <fcntl.h>
+ #include <stdarg.h>
++#include <unistd.h>
+ #include <sys/stat.h>
+ #include <time.h>
+ #include "securec.h"
+@@ -65,16 +66,6 @@ void InitToHiLog(LogLevel logLevel, const char *fmt, ...)
+ }
+ #endif
+ 
+-static int g_fd = -1;
+-void OpenLogDevice(void)
+-{
+-    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(void)
+ {
+     /* printk_devkmsg default value is ratelimit, We need to set "on" and remove the restrictions */
+@@ -96,33 +87,20 @@ void InitLog(InitLogLevel logLevel, const char *fileName, int line, const char *
+         return;
+     }
+ 
+-    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;
+     }
+ 
+     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, INIT_LOG_TAG, LOG_LEVEL_STR[logLevel], tmpFmt) == -1) {
+-        close(g_fd);
+-        g_fd = -1;
+         return;
+     }
+     va_end(vargs);
+ 
+-    if (write(g_fd, logInfo, strlen(logInfo)) < 0) {
+-        close(g_fd);
+-        g_fd = -1;
+-    }
++    write(STDERR_FILENO, logInfo, strlen(logInfo));
+     return;
+ }
diff --git a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
index 8141ba42..a63b901d 100644
--- a/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
+++ b/recipes-openharmony/openharmony/openharmony-standard_3.0.bb
@@ -74,6 +74,7 @@ SRC_URI += "file://init_lite-silence-GetControlFromEnv-spam.patch;patchdir=${S}/
 SRC_URI += "file://param_service_standalone.patch;patchdir=${S}/base/startup/init_lite"
 SRC_URI += "file://param_service-sd-notify.patch;patchdir=${S}/base/startup/init_lite"
 SRC_URI += "file://param-paths.patch;patchdir=${S}/base/startup/init_lite"
+SRC_URI += "file://init_lite-log-to-stderr.patch;patchdir=${S}/base/startup/init_lite"
 SRC_URI += "file://param_service-Add-to-startup-l2-part.patch;patchdir=${S}/base/startup/appspawn_standard"
 
 SRC_URI += "file://samgr-sd-notify.patch;patchdir=${S}/foundation/distributedschedule/samgr"
-- 
GitLab