]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-journal: introduce sd_journal_stream_fd_with_namespace
authorMike Yuan <me@yhndnzj.com>
Wed, 13 Mar 2024 10:50:23 +0000 (18:50 +0800)
committerMike Yuan <me@yhndnzj.com>
Thu, 14 Mar 2024 06:25:52 +0000 (14:25 +0800)
man/rules/meson.build
man/sd_journal_stream_fd.xml
src/libsystemd/libsystemd.sym
src/libsystemd/sd-journal/journal-send.c
src/systemd/sd-journal.h

index 1f07e606c9689f75f6ac1bf16f4ac9771132f099..465ea4e4b3ece15cf278a337f50607efd8e620b1 100644 (file)
@@ -795,7 +795,7 @@ manpages = [
    'sd_journal_seek_realtime_usec',
    'sd_journal_seek_tail'],
   ''],
- ['sd_journal_stream_fd', '3', [], ''],
+ ['sd_journal_stream_fd', '3', ['sd_journal_stream_fd_with_namespace'], ''],
  ['sd_listen_fds',
   '3',
   ['SD_LISTEN_FDS_START', 'sd_listen_fds_with_names'],
index 13939ff19e4e5dadfda32e38c61f047d196d77ec..ff0d2eedd939c6c014f4474098bb2761bcc6c286 100644 (file)
@@ -17,6 +17,7 @@
 
   <refnamediv>
     <refname>sd_journal_stream_fd</refname>
+    <refname>sd_journal_stream_fd_with_namespace</refname>
     <refpurpose>Create log stream file descriptor to the journal</refpurpose>
   </refnamediv>
 
         <paramdef>int <parameter>level_prefix</parameter></paramdef>
       </funcprototype>
 
+      <funcprototype>
+        <funcdef>int <function>sd_journal_stream_fd_with_namespace</function></funcdef>
+        <paramdef>const char *<parameter>name_space</parameter></paramdef>
+        <paramdef>const char *<parameter>identifier</parameter></paramdef>
+        <paramdef>int <parameter>priority</parameter></paramdef>
+        <paramdef>int <parameter>level_prefix</parameter></paramdef>
+      </funcprototype>
+
     </funcsynopsis>
   </refsynopsisdiv>
 
   <refsect1>
     <title>Description</title>
 
-    <para><function>sd_journal_stream_fd()</function> may be used to
-    create a log stream file descriptor. Log messages written to this
-    file descriptor as simple newline-separated text strings are
-    written to the journal. This file descriptor can be used
-    internally by applications or be made standard output or standard
-    error of other processes executed.</para>
+    <para><function>sd_journal_stream_fd()</function> may be used to create a log stream file descriptor.
+    Log messages written to this file descriptor as simple newline-separated text strings are written
+    to the journal. This file descriptor can be used internally by applications or be made standard output
+    or standard error of other processes executed.</para>
 
-    <para><function>sd_journal_stream_fd()</function> takes a short
-    program identifier string as first argument, which will be written
-    to the journal as SYSLOG_IDENTIFIER= field for each log entry
+    <para><function>sd_journal_stream_fd()</function> takes a short program identifier string as
+    first argument, which will be written to the journal as SYSLOG_IDENTIFIER= field for each log entry
     (see
     <citerefentry><refentrytitle>systemd.journal-fields</refentrytitle><manvolnum>7</manvolnum></citerefentry>
-    for more information). The second argument shall be the default
-    priority level for all messages. The priority level is one of
+    for more information). The second argument shall be the default priority level for all messages.
+    The priority level is one of
     <constant>LOG_EMERG</constant>, <constant>LOG_ALERT</constant>,
     <constant>LOG_CRIT</constant>, <constant>LOG_ERR</constant>,
     <constant>LOG_WARNING</constant>, <constant>LOG_NOTICE</constant>,
     <citerefentry><refentrytitle>sd-daemon</refentrytitle><manvolnum>3</manvolnum></citerefentry>
     for more information.</para>
 
-    <para>It is recommended that applications log UTF-8 messages only
-    with this API, but this is not enforced.</para>
+    <para><function>sd_journal_stream_fd_with_namespace()</function> is similar to
+    <function>sd_journal_stream_fd()</function>, but takes an additional <parameter>name_space</parameter> parameter
+    that specifies which journal namespace to operate on. If specified as <constant>NULL</constant> the call
+    is identical to <function>sd_journal_stream_fd()</function>. For details about journal namespaces, see
+    <citerefentry><refentrytitle>systemd-journald.service</refentrytitle><manvolnum>8</manvolnum></citerefentry>.</para>
+
+    <para>It is recommended that applications log UTF-8 messages only with this API, but this is not enforced.</para>
 
-    <para>Each invocation of <function>sd_journal_stream_fd()</function> allocates a new log stream file descriptor,
-    that is not shared with prior or later invocations. The file descriptor is write-only (its reading direction is
-    shut down), and <constant>O_NONBLOCK</constant> is turned off initially.</para>
+    <para>Each invocation of these functions allocates a new log stream file descriptor,
+    that is not shared with prior or later invocations. The file descriptor is write-only (its reading direction
+    is shut down), and <constant>O_NONBLOCK</constant> is turned off initially.</para>
   </refsect1>
 
   <refsect1>
     <title>Return Value</title>
 
-    <para>The call returns a valid write-only file descriptor on
-    success or a negative errno-style error code.</para>
+    <para>The call returns a valid write-only file descriptor on success or a negative errno-style error code.</para>
   </refsect1>
 
   <refsect1>
     <title>Signal safety</title>
 
-    <para><function>sd_journal_stream_fd()</function> is "async signal safe" in the meaning of <citerefentry
+    <para><function>sd_journal_stream_fd()</function> and <function>sd_journal_stream_fd_with_namespace()</function>
+    are "async signal safe" in the meaning of <citerefentry
     project='man-pages'><refentrytitle>signal-safety</refentrytitle><manvolnum>7</manvolnum></citerefentry>.
     </para>
   </refsect1>
   <refsect1>
     <title>History</title>
     <para><function>sd_journal_stream_fd()</function> was added in version 187.</para>
+    <para><function>sd_journal_stream_fd_with_namespace()</function> was added in version 256.</para>
   </refsect1>
 
   <refsect1>
index 22cf48c5f8db7b05c3479edf595d243a360b9a46..d23da4c1515c84cd4e54fea36a117c3478d1f9ad 100644 (file)
@@ -839,4 +839,5 @@ LIBSYSTEMD_256 {
 global:
         sd_bus_creds_get_pidfd_dup;
         sd_bus_creds_new_from_pidfd;
+        sd_journal_stream_fd_with_namespace;
 } LIBSYSTEMD_255;
index be23b2fe75d631f03f032f984ba2f5eb0e020049..650581addf41bee70a18c2c0394180b60393ba95 100644 (file)
@@ -398,20 +398,28 @@ _public_ int sd_journal_perror(const char *message) {
         return fill_iovec_perror_and_send(message, 0, iovec);
 }
 
-_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
+_public_ int sd_journal_stream_fd_with_namespace(
+                const char *name_space,
+                const char *identifier,
+                int priority,
+                int level_prefix) {
+
         _cleanup_close_ int fd = -EBADF;
-        char *header;
-        size_t l;
+        const char *path;
         int r;
 
         assert_return(priority >= 0, -EINVAL);
         assert_return(priority <= 7, -EINVAL);
 
+        path = journal_stream_path(name_space);
+        if (!path)
+                return -EINVAL;
+
         fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
         if (fd < 0)
                 return -errno;
 
-        r = connect_unix_path(fd, AT_FDCWD, "/run/systemd/journal/stdout");
+        r = connect_unix_path(fd, AT_FDCWD, path);
         if (r < 0)
                 return r;
 
@@ -422,6 +430,9 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
 
         identifier = strempty(identifier);
 
+        char *header;
+        size_t l;
+
         l = strlen(identifier);
         header = newa(char, l + 1 + 1 + 2 + 2 + 2 + 2 + 2);
 
@@ -446,6 +457,10 @@ _public_ int sd_journal_stream_fd(const char *identifier, int priority, int leve
         return TAKE_FD(fd);
 }
 
+_public_ int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix) {
+        return sd_journal_stream_fd_with_namespace(NULL, identifier, priority, level_prefix);
+}
+
 _public_ int sd_journal_print_with_location(int priority, const char *file, const char *line, const char *func, const char *format, ...) {
         int r;
         va_list ap;
index e4a67f048b8524111d3e1274d96a81844b8d8903..7434051ce1fb92a1d8b7aea760d50a63d3c0d5b3 100644 (file)
@@ -57,6 +57,7 @@ int sd_journal_perror_with_location(const char *file, const char *line, const ch
 #endif
 
 int sd_journal_stream_fd(const char *identifier, int priority, int level_prefix);
+int sd_journal_stream_fd_with_namespace(const char *name_space, const char *identifier, int priority, int level_prefix);
 
 /* Browse journal stream */