journal-send: introduce journal_stream_path helper

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

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

View 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; \
})