[make-initrd] ueventd: Allow to configure the log destination

Alexey Gladkov gladkov.alexey at gmail.com
Sat May 6 22:45:02 MSK 2023


Signed-off-by: Alexey Gladkov <gladkov.alexey at gmail.com>
---
 datasrc/ueventd/logging.c | 27 ++++++---------------------
 datasrc/ueventd/ueventd.c | 19 ++++++++++++++++++-
 datasrc/ueventd/ueventd.h |  2 +-
 3 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/datasrc/ueventd/logging.c b/datasrc/ueventd/logging.c
index 3a04a5a9..525ff7fd 100644
--- a/datasrc/ueventd/logging.c
+++ b/datasrc/ueventd/logging.c
@@ -8,9 +8,8 @@
 
 #include "ueventd.h"
 
-#define default_logfile "/var/log/ueventd.log"
-
 int log_priority = LOG_INFO;
+int log_fd = STDERR_FILENO;
 
 int rd_logging_level(const char *name)
 {
@@ -21,24 +20,10 @@ int rd_logging_level(const char *name)
 	return log_priority;
 }
 
-void rd_logging_init(int loglevel)
+void rd_logging_init(int fd, int loglevel)
 {
-	if (!getenv("UEVENTD_USE_STDERR")) {
-		char *rdlog = getenv("RDLOG");
-		const char *logfile = default_logfile;
-
-		if (rdlog && !strcasecmp(rdlog, "console"))
-			logfile = "/dev/console";
-
-		FILE *cons = fopen(logfile, "w+");
-		if (!cons)
-			rd_fatal("open(%s): %m", logfile);
-
-		fclose(stderr);
-		stderr = cons;
-	}
-
 	log_priority = loglevel;
+	log_fd = fd;
 }
 
 void rd_logging_close(void)
@@ -52,12 +37,12 @@ void rd_message(int priority, const char *fmt, ...)
 	if (priority <= log_priority) {
 		time_t ts = time(NULL);
 		struct tm *t = localtime(&ts);
-		fprintf(stderr, "[%04d-%02d-%02d %02d:%02d:%02d] %s: ",
+		dprintf(log_fd, "[%04d-%02d-%02d %02d:%02d:%02d] %s: ",
 		        t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
 		        t->tm_hour, t->tm_min, t->tm_sec,
 		        program_invocation_short_name);
-		vfprintf(stderr, fmt, ap);
-		fprintf(stderr, "\n");
+		vdprintf(log_fd, fmt, ap);
+		dprintf(log_fd, "\n");
 	}
 	va_end(ap);
 }
diff --git a/datasrc/ueventd/ueventd.c b/datasrc/ueventd/ueventd.c
index f4b89cb9..d015d3da 100644
--- a/datasrc/ueventd/ueventd.c
+++ b/datasrc/ueventd/ueventd.c
@@ -21,6 +21,8 @@
 
 #include "ueventd.h"
 
+#define default_logfile "/var/log/ueventd.log"
+
 char *uevent_confdb;
 char *filter_dir;
 char *uevent_dir;
@@ -474,7 +476,22 @@ int main(int argc, char **argv)
 	if (optind == argc)
 		rd_fatal("specify handler program");
 
-	rd_logging_init(loglevel);
+	if (!getenv("UEVENTD_USE_STDERR")) {
+		char *rdlog = getenv("RDLOG");
+		const char *logfile = default_logfile;
+
+		if (rdlog && !strcasecmp(rdlog, "console"))
+			logfile = "/dev/console";
+
+		FILE *cons = fopen(logfile, "w+");
+		if (!cons)
+			rd_fatal("open(%s): %m", logfile);
+
+		fclose(stderr);
+		stderr = cons;
+	}
+
+	rd_logging_init(fileno(stderr), loglevel);
 
 	rd_info("starting server ...");
 
diff --git a/datasrc/ueventd/ueventd.h b/datasrc/ueventd/ueventd.h
index e7ee1a8b..a783d48d 100644
--- a/datasrc/ueventd/ueventd.h
+++ b/datasrc/ueventd/ueventd.h
@@ -56,7 +56,7 @@ extern  int is_dot_dir(struct dirent *ent) __attribute__((nonnull(1)));
 #include <syslog.h>
 #include <stdlib.h>
 
-extern void rd_logging_init(int level);
+extern void rd_logging_init(int log_fd, int level);
 extern void rd_logging_close(void);
 extern int rd_logging_level(const char *lvl)               __attribute__((nonnull(1)));
 extern void rd_message(int priority, const char *fmt, ...) __attribute__((format(printf, 2, 3)));
-- 
2.33.7



More information about the Make-initrd mailing list