]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journal-send: introduce journal_stream_path helper
authorMike Yuan <me@yhndnzj.com>
Wed, 13 Mar 2024 10:43:53 +0000 (18:43 +0800)
committerMike Yuan <me@yhndnzj.com>
Thu, 14 Mar 2024 06:25:52 +0000 (14:25 +0800)
src/core/exec-invoke.c
src/libsystemd/sd-journal/journal-send.h

index 0db13bfa8807d3c7c960ff393762aa3e4dcfccd2..a1401f3e4903a9586778cffadfacb0f1472c5e87 100644 (file)
@@ -41,6 +41,7 @@
 #include "hexdecoct.h"
 #include "io-util.h"
 #include "iovec-util.h"
+#include "journal-send.h"
 #include "missing_ioprio.h"
 #include "missing_prctl.h"
 #include "missing_securebits.h"
@@ -159,9 +160,11 @@ static int connect_journal_socket(
         const char *j;
         int r;
 
-        j = log_namespace ?
-                strjoina("/run/systemd/journal.", log_namespace, "/stdout") :
-                "/run/systemd/journal/stdout";
+        assert(fd >= 0);
+
+        j = journal_stream_path(log_namespace);
+        if (!j)
+                return -EINVAL;
 
         if (gid_is_valid(gid)) {
                 oldgid = getgid();
index 24315e249b32d9d0f7a88189d487dd98ac0d0305..6fe6325090ec768c08c58af37babfbe1aa33a152 100644 (file)
@@ -2,6 +2,23 @@
 #pragma once
 
 #include <stdbool.h>
+#include <stddef.h>
+
+#include "syslog-util.h"
 
 int journal_fd_nonblock(bool nonblock);
 void close_journal_fd(void);
+
+/* We declare sd_journal_stream_fd() as async-signal-safe. So instead of strjoin(), which calls malloc()
+ * internally, use a macro + alloca(). */
+#define journal_stream_path(log_namespace)                                              \
+        ({                                                                              \
+                const char *_ns = (log_namespace), *_ret;                               \
+                if (!_ns)                                                               \
+                        _ret = "/run/systemd/journal/stdout";                           \
+                else if (log_namespace_name_valid(_ns))                                 \
+                        _ret = strjoina("/run/systemd/journal.", _ns, "/stdout");       \
+                else                                                                    \
+                        _ret = NULL;                                                    \
+                _ret;                                                                   \
+        })