sd-journal: introduce sd_journal_stream_fd_with_namespace

This commit is contained in:
Mike Yuan 2024-03-13 18:50:23 +08:00
parent 2a11593178
commit 32104d3251
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3
5 changed files with 53 additions and 24 deletions

View 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'],

View 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>
@ -31,26 +32,31 @@
<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>,
@ -63,25 +69,30 @@
<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>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>It is recommended that applications log UTF-8 messages only with this API, but this is not enforced.</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>
@ -106,6 +117,7 @@
<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>

View 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;

View 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;

View 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 */