From: Mike Yuan Date: Wed, 13 Mar 2024 10:50:23 +0000 (+0800) Subject: sd-journal: introduce sd_journal_stream_fd_with_namespace X-Git-Tag: v256-rc1~529^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=32104d3251117a1aaf68e65db4d6942d5ecd722b;p=thirdparty%2Fsystemd.git sd-journal: introduce sd_journal_stream_fd_with_namespace --- diff --git a/man/rules/meson.build b/man/rules/meson.build index 1f07e606c96..465ea4e4b3e 100644 --- a/man/rules/meson.build +++ b/man/rules/meson.build @@ -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'], diff --git a/man/sd_journal_stream_fd.xml b/man/sd_journal_stream_fd.xml index 13939ff19e4..ff0d2eedd93 100644 --- a/man/sd_journal_stream_fd.xml +++ b/man/sd_journal_stream_fd.xml @@ -17,6 +17,7 @@ sd_journal_stream_fd + sd_journal_stream_fd_with_namespace Create log stream file descriptor to the journal @@ -31,26 +32,31 @@ int level_prefix + + int sd_journal_stream_fd_with_namespace + const char *name_space + const char *identifier + int priority + int level_prefix + + Description - sd_journal_stream_fd() 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. + sd_journal_stream_fd() 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. - sd_journal_stream_fd() takes a short - program identifier string as first argument, which will be written - to the journal as SYSLOG_IDENTIFIER= field for each log entry + sd_journal_stream_fd() 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 systemd.journal-fields7 - 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 LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, @@ -63,25 +69,30 @@ sd-daemon3 for more information. - It is recommended that applications log UTF-8 messages only - with this API, but this is not enforced. + sd_journal_stream_fd_with_namespace() is similar to + sd_journal_stream_fd(), but takes an additional name_space parameter + that specifies which journal namespace to operate on. If specified as NULL the call + is identical to sd_journal_stream_fd(). For details about journal namespaces, see + systemd-journald.service8. + + It is recommended that applications log UTF-8 messages only with this API, but this is not enforced. - Each invocation of sd_journal_stream_fd() 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 O_NONBLOCK is turned off initially. + 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 O_NONBLOCK is turned off initially. Return Value - The call returns a valid write-only file descriptor on - success or a negative errno-style error code. + The call returns a valid write-only file descriptor on success or a negative errno-style error code. Signal safety - sd_journal_stream_fd() is "async signal safe" in the meaning of sd_journal_stream_fd() and sd_journal_stream_fd_with_namespace() + are "async signal safe" in the meaning of signal-safety7. @@ -106,6 +117,7 @@ History sd_journal_stream_fd() was added in version 187. + sd_journal_stream_fd_with_namespace() was added in version 256. diff --git a/src/libsystemd/libsystemd.sym b/src/libsystemd/libsystemd.sym index 22cf48c5f8d..d23da4c1515 100644 --- a/src/libsystemd/libsystemd.sym +++ b/src/libsystemd/libsystemd.sym @@ -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; diff --git a/src/libsystemd/sd-journal/journal-send.c b/src/libsystemd/sd-journal/journal-send.c index be23b2fe75d..650581addf4 100644 --- a/src/libsystemd/sd-journal/journal-send.c +++ b/src/libsystemd/sd-journal/journal-send.c @@ -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; diff --git a/src/systemd/sd-journal.h b/src/systemd/sd-journal.h index e4a67f048b8..7434051ce1f 100644 --- a/src/systemd/sd-journal.h +++ b/src/systemd/sd-journal.h @@ -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 */