]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
env-util: Add helper to store current log level
authorAdrian Vovk <adrianvovk@gmail.com>
Sat, 20 Jan 2024 00:46:07 +0000 (19:46 -0500)
committerLennart Poettering <lennart@poettering.net>
Wed, 14 Feb 2024 08:21:45 +0000 (09:21 +0100)
This is useful after a fork but before an exec into a binary that uses
systemd's logging utilities. For example, this should be used in dbus
services that fork off worker processes: currently, the log level set by
the LogControl dbus API will be lost because of the exec, and the worker
process will not have the correct log level set.

src/basic/env-util.c
src/basic/env-util.h

index 6061edb90484e13dfafeabeb8b86496d4a266f43..b96fbdff4838f89e5e47464576101f28dde3da68 100644 (file)
@@ -18,6 +18,7 @@
 #include "stdio-util.h"
 #include "string-util.h"
 #include "strv.h"
+#include "syslog-util.h"
 #include "utf8.h"
 
 /* We follow bash for the character set. Different shells have different rules. */
@@ -1030,6 +1031,17 @@ int setenv_systemd_exec_pid(bool update_only) {
         return 1;
 }
 
+int setenv_systemd_log_level(void) {
+        _cleanup_free_ char *val = NULL;
+        int r;
+
+        r = log_level_to_string_alloc(log_get_max_level(), &val);
+        if (r < 0)
+                return r;
+
+        return RET_NERRNO(setenv("SYSTEMD_LOG_LEVEL", val, /* overwrite= */ true));
+}
+
 int getenv_path_list(const char *name, char ***ret_paths) {
         _cleanup_strv_free_ char **l = NULL;
         const char *e;
index ad127de39f8e9614103850adcc074c92511eb7b8..31680b160cc0eda16aa22e6ec7209b2ce0c929c1 100644 (file)
@@ -72,6 +72,7 @@ int set_unset_env(const char *name, const char *value, bool overwrite);
 int putenv_dup(const char *assignment, bool override);
 
 int setenv_systemd_exec_pid(bool update_only);
+int setenv_systemd_log_level(void);
 
 /* Parses and does sanity checks on an environment variable containing
  * PATH-like colon-separated absolute paths */