mirror of
https://github.com/systemd/systemd.git
synced 2025-03-31 14:50:15 +03:00
journald: allow running multiple instances of journald
If we do, we operate on a separate set of logs and runtime objects The namespace is configured via argv[1]. Fixes: #12123 Fixes: #10230 #9519 (These latter two issues ask for slightly different stuff, but the usecases generally can be solved by running separate instances of journald now, hence also declaring that as "Fixes:")
This commit is contained in:
parent
d6f46470f5
commit
b1852c48c1
@ -2,10 +2,15 @@
|
||||
|
||||
#include <syslog.h>
|
||||
|
||||
#include "sd-id128.h"
|
||||
|
||||
#include "glob-util.h"
|
||||
#include "hexdecoct.h"
|
||||
#include "macro.h"
|
||||
#include "path-util.h"
|
||||
#include "string-table.h"
|
||||
#include "syslog-util.h"
|
||||
#include "unit-name.h"
|
||||
|
||||
int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
|
||||
int a = 0, b = 0, c = 0;
|
||||
@ -96,3 +101,31 @@ DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
|
||||
bool log_level_is_valid(int level) {
|
||||
return level >= 0 && level <= LOG_DEBUG;
|
||||
}
|
||||
|
||||
/* The maximum size for a log namespace length. This is the file name size limit 255 minus the size of a
|
||||
* formatted machine ID minus a separator char */
|
||||
#define LOG_NAMESPACE_MAX (NAME_MAX - (SD_ID128_STRING_MAX - 1) - 1)
|
||||
|
||||
bool log_namespace_name_valid(const char *s) {
|
||||
/* Let's make sure the namespace fits in a filename that is prefixed with the machine ID and a dot
|
||||
* (so that /var/log/journal/<machine-id>.<namespace> can be created based on it). Also make sure it
|
||||
* is suitable as unit instance name, and does not contain fishy characters. */
|
||||
|
||||
if (!filename_is_valid(s))
|
||||
return false;
|
||||
|
||||
if (strlen(s) > LOG_NAMESPACE_MAX)
|
||||
return false;
|
||||
|
||||
if (!unit_instance_is_valid(s))
|
||||
return false;
|
||||
|
||||
if (!string_is_safe(s))
|
||||
return false;
|
||||
|
||||
/* Let's avoid globbing for now */
|
||||
if (string_is_glob(s))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -12,3 +12,5 @@ int log_level_from_string(const char *s);
|
||||
bool log_level_is_valid(int level);
|
||||
|
||||
int syslog_parse_priority(const char **p, int *priority, bool with_facility);
|
||||
|
||||
bool log_namespace_name_valid(const char *s);
|
||||
|
@ -780,7 +780,9 @@ void client_context_acquire_default(Server *s) {
|
||||
log_warning_errno(r, "Failed to acquire our own context, ignoring: %m");
|
||||
}
|
||||
|
||||
if (!s->pid1_context) {
|
||||
if (!s->namespace && !s->pid1_context) {
|
||||
/* Acquire PID1's context, but only if we are in non-namespaced mode, since PID 1 is only
|
||||
* going to log to the non-namespaced journal instance. */
|
||||
|
||||
r = client_context_acquire(s, 1, NULL, NULL, 0, NULL, &s->pid1_context);
|
||||
if (r < 0)
|
||||
|
@ -417,6 +417,7 @@ fail:
|
||||
|
||||
int server_open_kernel_seqnum(Server *s) {
|
||||
_cleanup_close_ int fd = -1;
|
||||
const char *fn;
|
||||
uint64_t *p;
|
||||
int r;
|
||||
|
||||
@ -428,9 +429,10 @@ int server_open_kernel_seqnum(Server *s) {
|
||||
if (!s->read_kmsg)
|
||||
return 0;
|
||||
|
||||
fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
|
||||
fn = strjoina(s->runtime_directory, "/kernel-seqnum");
|
||||
fd = open(fn, O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
|
||||
if (fd < 0) {
|
||||
log_error_errno(errno, "Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
|
||||
log_error_errno(errno, "Failed to open %s, ignoring: %m", fn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -450,17 +450,21 @@ void server_process_native_file(
|
||||
}
|
||||
}
|
||||
|
||||
int server_open_native_socket(Server *s) {
|
||||
|
||||
static const union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
.un.sun_path = "/run/systemd/journal/socket",
|
||||
};
|
||||
int server_open_native_socket(Server *s, const char *native_socket) {
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(native_socket);
|
||||
|
||||
if (s->native_fd < 0) {
|
||||
union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
};
|
||||
|
||||
r = sockaddr_un_set_path(&sa.un, native_socket);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Unable to use namespace path %s for AF_UNIX socket: %m", native_socket);
|
||||
|
||||
s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (s->native_fd < 0)
|
||||
return log_error_errno(errno, "socket() failed: %m");
|
||||
|
@ -20,4 +20,4 @@ void server_process_native_file(
|
||||
const char *label,
|
||||
size_t label_len);
|
||||
|
||||
int server_open_native_socket(Server *s);
|
||||
int server_open_native_socket(Server *s, const char *native_socket);
|
||||
|
@ -44,6 +44,7 @@
|
||||
#include "missing_audit.h"
|
||||
#include "mkdir.h"
|
||||
#include "parse-util.h"
|
||||
#include "path-util.h"
|
||||
#include "proc-cmdline.h"
|
||||
#include "process-util.h"
|
||||
#include "rm-rf.h"
|
||||
@ -293,8 +294,18 @@ static int open_journal(
|
||||
return r;
|
||||
}
|
||||
|
||||
static bool flushed_flag_is_set(void) {
|
||||
return access("/run/systemd/journal/flushed", F_OK) >= 0;
|
||||
static bool flushed_flag_is_set(Server *s) {
|
||||
const char *fn;
|
||||
|
||||
assert(s);
|
||||
|
||||
/* We don't support the "flushing" concept for namespace instances, we assume them to always have
|
||||
* access to /var */
|
||||
if (s->namespace)
|
||||
return true;
|
||||
|
||||
fn = strjoina(s->runtime_directory, "/flushed");
|
||||
return access(fn, F_OK) >= 0;
|
||||
}
|
||||
|
||||
static int system_journal_open(Server *s, bool flush_requested, bool relinquish_requested) {
|
||||
@ -303,7 +314,7 @@ static int system_journal_open(Server *s, bool flush_requested, bool relinquish_
|
||||
|
||||
if (!s->system_journal &&
|
||||
IN_SET(s->storage, STORAGE_PERSISTENT, STORAGE_AUTO) &&
|
||||
(flush_requested || flushed_flag_is_set()) &&
|
||||
(flush_requested || flushed_flag_is_set(s)) &&
|
||||
!relinquish_requested) {
|
||||
|
||||
/* If in auto mode: first try to create the machine path, but not the prefix.
|
||||
@ -988,6 +999,9 @@ static void dispatch_message_real(
|
||||
if (!isempty(s->hostname_field))
|
||||
iovec[n++] = IOVEC_MAKE_STRING(s->hostname_field);
|
||||
|
||||
if (!isempty(s->namespace_field))
|
||||
iovec[n++] = IOVEC_MAKE_STRING(s->namespace_field);
|
||||
|
||||
assert(n <= m);
|
||||
|
||||
if (s->split_mode == SPLIT_UID && c && uid_is_valid(c->uid))
|
||||
@ -1100,10 +1114,11 @@ void server_dispatch_message(
|
||||
}
|
||||
|
||||
int server_flush_to_var(Server *s, bool require_flag_file) {
|
||||
sd_journal *j = NULL;
|
||||
char ts[FORMAT_TIMESPAN_MAX];
|
||||
usec_t start;
|
||||
sd_journal *j = NULL;
|
||||
const char *fn;
|
||||
unsigned n = 0;
|
||||
usec_t start;
|
||||
int r, k;
|
||||
|
||||
assert(s);
|
||||
@ -1111,10 +1126,13 @@ int server_flush_to_var(Server *s, bool require_flag_file) {
|
||||
if (!IN_SET(s->storage, STORAGE_AUTO, STORAGE_PERSISTENT))
|
||||
return 0;
|
||||
|
||||
if (!s->runtime_journal)
|
||||
if (s->namespace) /* Flushing concept does not exist for namespace instances */
|
||||
return 0;
|
||||
|
||||
if (require_flag_file && !flushed_flag_is_set())
|
||||
if (!s->runtime_journal) /* Nothing to flush? */
|
||||
return 0;
|
||||
|
||||
if (require_flag_file && !flushed_flag_is_set(s))
|
||||
return 0;
|
||||
|
||||
(void) system_journal_open(s, true, false);
|
||||
@ -1122,7 +1140,7 @@ int server_flush_to_var(Server *s, bool require_flag_file) {
|
||||
if (!s->system_journal)
|
||||
return 0;
|
||||
|
||||
log_debug("Flushing to /var...");
|
||||
log_debug("Flushing to %s...", s->system_storage.path);
|
||||
|
||||
start = now(CLOCK_MONOTONIC);
|
||||
|
||||
@ -1182,33 +1200,39 @@ finish:
|
||||
s->runtime_journal = journal_file_close(s->runtime_journal);
|
||||
|
||||
if (r >= 0)
|
||||
(void) rm_rf("/run/log/journal", REMOVE_ROOT);
|
||||
(void) rm_rf(s->runtime_storage.path, REMOVE_ROOT);
|
||||
|
||||
sd_journal_close(j);
|
||||
|
||||
server_driver_message(s, 0, NULL,
|
||||
LOG_MESSAGE("Time spent on flushing to /var is %s for %u entries.",
|
||||
LOG_MESSAGE("Time spent on flushing to %s is %s for %u entries.",
|
||||
s->system_storage.path,
|
||||
format_timespan(ts, sizeof(ts), now(CLOCK_MONOTONIC) - start, 0),
|
||||
n),
|
||||
NULL);
|
||||
|
||||
k = touch("/run/systemd/journal/flushed");
|
||||
fn = strjoina(s->runtime_directory, "/flushed");
|
||||
k = touch(fn);
|
||||
if (k < 0)
|
||||
log_warning_errno(k, "Failed to touch /run/systemd/journal/flushed, ignoring: %m");
|
||||
log_warning_errno(k, "Failed to touch %s, ignoring: %m", fn);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
static int server_relinquish_var(Server *s) {
|
||||
const char *fn;
|
||||
assert(s);
|
||||
|
||||
if (s->storage == STORAGE_NONE)
|
||||
return 0;
|
||||
|
||||
if (s->namespace) /* Concept does not exist for namespaced instances */
|
||||
return -EOPNOTSUPP;
|
||||
|
||||
if (s->runtime_journal && !s->system_journal)
|
||||
return 0;
|
||||
|
||||
log_debug("Relinquishing /var...");
|
||||
log_debug("Relinquishing %s...", s->system_storage.path);
|
||||
|
||||
(void) system_journal_open(s, false, true);
|
||||
|
||||
@ -1216,8 +1240,9 @@ static int server_relinquish_var(Server *s) {
|
||||
ordered_hashmap_clear_with_destructor(s->user_journals, journal_file_close);
|
||||
set_clear_with_destructor(s->deferred_closes, journal_file_close);
|
||||
|
||||
if (unlink("/run/systemd/journal/flushed") < 0 && errno != ENOENT)
|
||||
log_warning_errno(errno, "Failed to unlink /run/systemd/journal/flushed, ignoring: %m");
|
||||
fn = strjoina(s->runtime_directory, "/flushed");
|
||||
if (unlink(fn) < 0 && errno != ENOENT)
|
||||
log_warning_errno(errno, "Failed to unlink %s, ignoring: %m", fn);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -1355,6 +1380,11 @@ static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *
|
||||
|
||||
assert(s);
|
||||
|
||||
if (s->namespace) {
|
||||
log_error("Received SIGUSR1 signal from PID " PID_FMT ", but flushing runtime journals not supported for namespaced instances.", si->ssi_pid);
|
||||
return 0;
|
||||
}
|
||||
|
||||
log_info("Received SIGUSR1 signal from PID " PID_FMT ", as request to flush runtime journal.", si->ssi_pid);
|
||||
server_full_flush(s);
|
||||
|
||||
@ -1362,6 +1392,7 @@ static int dispatch_sigusr1(sd_event_source *es, const struct signalfd_siginfo *
|
||||
}
|
||||
|
||||
static void server_full_rotate(Server *s) {
|
||||
const char *fn;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
@ -1375,9 +1406,10 @@ static void server_full_rotate(Server *s) {
|
||||
patch_min_use(&s->runtime_storage);
|
||||
|
||||
/* Let clients know when the most recent rotation happened. */
|
||||
r = write_timestamp_file_atomic("/run/systemd/journal/rotated", now(CLOCK_MONOTONIC));
|
||||
fn = strjoina(s->runtime_directory, "/rotated");
|
||||
r = write_timestamp_file_atomic(fn, now(CLOCK_MONOTONIC));
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to write /run/systemd/journal/rotated, ignoring: %m");
|
||||
log_warning_errno(r, "Failed to write %s, ignoring: %m", fn);
|
||||
}
|
||||
|
||||
static int dispatch_sigusr2(sd_event_source *es, const struct signalfd_siginfo *si, void *userdata) {
|
||||
@ -1403,6 +1435,7 @@ static int dispatch_sigterm(sd_event_source *es, const struct signalfd_siginfo *
|
||||
}
|
||||
|
||||
static void server_full_sync(Server *s) {
|
||||
const char *fn;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
@ -1410,9 +1443,10 @@ static void server_full_sync(Server *s) {
|
||||
server_sync(s);
|
||||
|
||||
/* Let clients know when the most recent sync happened. */
|
||||
r = write_timestamp_file_atomic("/run/systemd/journal/synced", now(CLOCK_MONOTONIC));
|
||||
fn = strjoina(s->runtime_directory, "/synced");
|
||||
r = write_timestamp_file_atomic(fn, now(CLOCK_MONOTONIC));
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to write /run/systemd/journal/synced, ignoring: %m");
|
||||
log_warning_errno(r, "Failed to write %s, ignoring: %m", fn);
|
||||
|
||||
return;
|
||||
}
|
||||
@ -1577,8 +1611,28 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat
|
||||
}
|
||||
|
||||
static int server_parse_config_file(Server *s) {
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
|
||||
if (s->namespace) {
|
||||
const char *namespaced;
|
||||
|
||||
/* If we are running in namespace mode, load the namespace specific configuration file, and nothing else */
|
||||
namespaced = strjoina(PKGSYSCONFDIR "/journald@", s->namespace, ".conf");
|
||||
|
||||
r = config_parse(
|
||||
NULL,
|
||||
namespaced, NULL,
|
||||
"Journal\0",
|
||||
config_item_perf_lookup, journald_gperf_lookup,
|
||||
CONFIG_PARSE_WARN, s);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return config_parse_many_nulstr(PKGSYSCONFDIR "/journald.conf",
|
||||
CONF_PATHS_NULSTR("systemd/journald.conf.d"),
|
||||
"Journal\0",
|
||||
@ -1921,6 +1975,8 @@ static int vl_method_flush_to_var(Varlink *link, JsonVariant *parameters, Varlin
|
||||
|
||||
if (json_variant_elements(parameters) > 0)
|
||||
return varlink_error_invalid_parameter(link, parameters);
|
||||
if (s->namespace)
|
||||
return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL);
|
||||
|
||||
log_info("Received client request to flush runtime journal.");
|
||||
server_full_flush(s);
|
||||
@ -1936,14 +1992,17 @@ static int vl_method_relinquish_var(Varlink *link, JsonVariant *parameters, Varl
|
||||
|
||||
if (json_variant_elements(parameters) > 0)
|
||||
return varlink_error_invalid_parameter(link, parameters);
|
||||
if (s->namespace)
|
||||
return varlink_error(link, "io.systemd.Journal.NotSupportedByNamespaces", NULL);
|
||||
|
||||
log_info("Received client request to relinquish /var access.");
|
||||
log_info("Received client request to relinquish %s access.", s->system_storage.path);
|
||||
server_relinquish_var(s);
|
||||
|
||||
return varlink_reply(link, NULL);
|
||||
}
|
||||
|
||||
static int server_open_varlink(Server *s) {
|
||||
const char *fn;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
@ -1963,7 +2022,9 @@ static int server_open_varlink(Server *s) {
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
r = varlink_server_listen_address(s->varlink_server, "/run/systemd/journal/io.systemd.journal", 0600);
|
||||
fn = strjoina(s->runtime_directory, "/io.systemd.journal");
|
||||
|
||||
r = varlink_server_listen_address(s->varlink_server, fn, 0600);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@ -1974,10 +2035,31 @@ static int server_open_varlink(Server *s) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int server_init(Server *s) {
|
||||
static int set_namespace(Server *s, const char *namespace) {
|
||||
assert(s);
|
||||
|
||||
if (!namespace)
|
||||
return 0;
|
||||
|
||||
if (!log_namespace_name_valid(namespace))
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Specified namespace name not valid, refusing: %s", namespace);
|
||||
|
||||
s->namespace = strdup(namespace);
|
||||
if (!s->namespace)
|
||||
return log_oom();
|
||||
|
||||
s->namespace_field = strjoin("_NAMESPACE=", namespace);
|
||||
if (!s->namespace_field)
|
||||
return log_oom();
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int server_init(Server *s, const char *namespace) {
|
||||
const char *native_socket, *syslog_socket, *stdout_socket, *e;
|
||||
_cleanup_fdset_free_ FDSet *fds = NULL;
|
||||
int n, r, fd;
|
||||
bool no_sockets;
|
||||
int n, r, fd;
|
||||
|
||||
assert(s);
|
||||
|
||||
@ -1993,7 +2075,6 @@ int server_init(Server *s) {
|
||||
.compress.enabled = true,
|
||||
.compress.threshold_bytes = (uint64_t) -1,
|
||||
.seal = true,
|
||||
.read_kmsg = true,
|
||||
|
||||
.watchdog_usec = USEC_INFINITY,
|
||||
|
||||
@ -2019,14 +2100,25 @@ int server_init(Server *s) {
|
||||
.system_storage.name = "System Journal",
|
||||
};
|
||||
|
||||
r = set_namespace(s, namespace);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* By default, only read from /dev/kmsg if are the main namespace */
|
||||
s->read_kmsg = !s->namespace;
|
||||
s->storage = s->namespace ? STORAGE_PERSISTENT : STORAGE_AUTO;
|
||||
|
||||
journal_reset_metrics(&s->system_storage.metrics);
|
||||
journal_reset_metrics(&s->runtime_storage.metrics);
|
||||
|
||||
server_parse_config_file(s);
|
||||
|
||||
r = proc_cmdline_parse(parse_proc_cmdline_item, s, PROC_CMDLINE_STRIP_RD_PREFIX);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
|
||||
if (!s->namespace) {
|
||||
/* Parse kernel command line, but only if we are not a namespace instance */
|
||||
r = proc_cmdline_parse(parse_proc_cmdline_item, s, PROC_CMDLINE_STRIP_RD_PREFIX);
|
||||
if (r < 0)
|
||||
log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
|
||||
}
|
||||
|
||||
if (!!s->ratelimit_interval != !!s->ratelimit_burst) { /* One set to 0 and the other not? */
|
||||
log_debug("Setting both rate limit interval and burst from "USEC_FMT",%u to 0,0",
|
||||
@ -2034,7 +2126,17 @@ int server_init(Server *s) {
|
||||
s->ratelimit_interval = s->ratelimit_burst = 0;
|
||||
}
|
||||
|
||||
(void) mkdir_p("/run/systemd/journal", 0755);
|
||||
e = getenv("RUNTIME_DIRECTORY");
|
||||
if (e)
|
||||
s->runtime_directory = strdup(e);
|
||||
else if (s->namespace)
|
||||
s->runtime_directory = strjoin("/run/systemd/journal.", s->namespace);
|
||||
else
|
||||
s->runtime_directory = strdup("/run/systemd/journal");
|
||||
if (!s->runtime_directory)
|
||||
return log_oom();
|
||||
|
||||
(void) mkdir_p(s->runtime_directory, 0755);
|
||||
|
||||
s->user_journals = ordered_hashmap_new(NULL);
|
||||
if (!s->user_journals)
|
||||
@ -2056,9 +2158,13 @@ int server_init(Server *s) {
|
||||
if (n < 0)
|
||||
return log_error_errno(n, "Failed to read listening file descriptors from environment: %m");
|
||||
|
||||
native_socket = strjoina(s->runtime_directory, "/socket");
|
||||
stdout_socket = strjoina(s->runtime_directory, "/stdout");
|
||||
syslog_socket = strjoina(s->runtime_directory, "/dev-log");
|
||||
|
||||
for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
|
||||
|
||||
if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
|
||||
if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, native_socket, 0) > 0) {
|
||||
|
||||
if (s->native_fd >= 0)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
@ -2066,7 +2172,7 @@ int server_init(Server *s) {
|
||||
|
||||
s->native_fd = fd;
|
||||
|
||||
} else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
|
||||
} else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, stdout_socket, 0) > 0) {
|
||||
|
||||
if (s->stdout_fd >= 0)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
@ -2074,8 +2180,7 @@ int server_init(Server *s) {
|
||||
|
||||
s->stdout_fd = fd;
|
||||
|
||||
} else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0 ||
|
||||
sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/dev-log", 0) > 0) {
|
||||
} else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, syslog_socket, 0) > 0) {
|
||||
|
||||
if (s->syslog_fd >= 0)
|
||||
return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
|
||||
@ -2118,17 +2223,17 @@ int server_init(Server *s) {
|
||||
/* always open stdout, syslog, native, and kmsg sockets */
|
||||
|
||||
/* systemd-journald.socket: /run/systemd/journal/stdout */
|
||||
r = server_open_stdout_socket(s);
|
||||
r = server_open_stdout_socket(s, stdout_socket);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* systemd-journald-dev-log.socket: /run/systemd/journal/dev-log */
|
||||
r = server_open_syslog_socket(s);
|
||||
r = server_open_syslog_socket(s, syslog_socket);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
/* systemd-journald.socket: /run/systemd/journal/socket */
|
||||
r = server_open_native_socket(s);
|
||||
r = server_open_native_socket(s, native_socket);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
@ -2172,9 +2277,21 @@ int server_init(Server *s) {
|
||||
server_cache_boot_id(s);
|
||||
server_cache_machine_id(s);
|
||||
|
||||
s->runtime_storage.path = path_join("/run/log/journal", SERVER_MACHINE_ID(s));
|
||||
s->system_storage.path = path_join("/var/log/journal", SERVER_MACHINE_ID(s));
|
||||
if (!s->runtime_storage.path || !s->system_storage.path)
|
||||
if (s->namespace)
|
||||
s->runtime_storage.path = strjoin("/run/log/journal/", SERVER_MACHINE_ID(s), ".", s->namespace);
|
||||
else
|
||||
s->runtime_storage.path = strjoin("/run/log/journal/", SERVER_MACHINE_ID(s));
|
||||
if (!s->runtime_storage.path)
|
||||
return log_oom();
|
||||
|
||||
e = getenv("LOGS_DIRECTORY");
|
||||
if (e)
|
||||
s->system_storage.path = strdup(e);
|
||||
else if (s->namespace)
|
||||
s->system_storage.path = strjoin("/var/log/journal/", SERVER_MACHINE_ID(s), ".", s->namespace);
|
||||
else
|
||||
s->system_storage.path = strjoin("/var/log/journal/", SERVER_MACHINE_ID(s));
|
||||
if (!s->system_storage.path)
|
||||
return log_oom();
|
||||
|
||||
(void) server_connect_notify(s);
|
||||
@ -2203,6 +2320,9 @@ void server_maybe_append_tags(Server *s) {
|
||||
void server_done(Server *s) {
|
||||
assert(s);
|
||||
|
||||
free(s->namespace);
|
||||
free(s->namespace_field);
|
||||
|
||||
set_free_with_destructor(s->deferred_closes, journal_file_close);
|
||||
|
||||
while (s->stdout_streams)
|
||||
@ -2253,6 +2373,7 @@ void server_done(Server *s) {
|
||||
free(s->hostname_field);
|
||||
free(s->runtime_storage.path);
|
||||
free(s->system_storage.path);
|
||||
free(s->runtime_directory);
|
||||
|
||||
mmap_cache_unref(s->mmap);
|
||||
}
|
||||
|
@ -60,6 +60,8 @@ typedef struct JournalStorage {
|
||||
} JournalStorage;
|
||||
|
||||
struct Server {
|
||||
char *namespace;
|
||||
|
||||
int syslog_fd;
|
||||
int native_fd;
|
||||
int stdout_fd;
|
||||
@ -147,6 +149,8 @@ struct Server {
|
||||
char machine_id_field[sizeof("_MACHINE_ID=") + 32];
|
||||
char boot_id_field[sizeof("_BOOT_ID=") + 32];
|
||||
char *hostname_field;
|
||||
char *namespace_field;
|
||||
char *runtime_directory;
|
||||
|
||||
/* Cached cgroup root, so that we don't have to query that all the time */
|
||||
char *cgroup_root;
|
||||
@ -172,7 +176,7 @@ struct Server {
|
||||
#define SERVER_MACHINE_ID(s) ((s)->machine_id_field + STRLEN("_MACHINE_ID="))
|
||||
|
||||
/* Extra fields for any log messages */
|
||||
#define N_IOVEC_META_FIELDS 22
|
||||
#define N_IOVEC_META_FIELDS 23
|
||||
|
||||
/* Extra fields for log messages that contain OBJECT_PID= (i.e. log about another process) */
|
||||
#define N_IOVEC_OBJECT_FIELDS 18
|
||||
@ -204,7 +208,7 @@ CONFIG_PARSER_PROTOTYPE(config_parse_split_mode);
|
||||
const char *split_mode_to_string(SplitMode s) _const_;
|
||||
SplitMode split_mode_from_string(const char *s) _pure_;
|
||||
|
||||
int server_init(Server *s);
|
||||
int server_init(Server *s, const char *namespace);
|
||||
void server_done(Server *s);
|
||||
void server_sync(Server *s);
|
||||
int server_vacuum(Server *s, bool verbose);
|
||||
|
@ -157,11 +157,12 @@ static int stdout_stream_save(StdoutStream *s) {
|
||||
return log_warning_errno(errno, "Failed to stat connected stream: %m");
|
||||
|
||||
/* We use device and inode numbers as identifier for the stream */
|
||||
if (asprintf(&s->state_file, "/run/systemd/journal/streams/%lu:%lu", (unsigned long) st.st_dev, (unsigned long) st.st_ino) < 0)
|
||||
r = asprintf(&s->state_file, "%s/streams/%lu:%lu", s->server->runtime_directory, (unsigned long) st.st_dev, (unsigned long) st.st_ino);
|
||||
if (r < 0)
|
||||
return log_oom();
|
||||
}
|
||||
|
||||
(void) mkdir_p("/run/systemd/journal/streams", 0755);
|
||||
(void) mkdir_parents(s->state_file, 0755);
|
||||
|
||||
r = fopen_temporary(s->state_file, &f, &temp_path);
|
||||
if (r < 0)
|
||||
@ -692,7 +693,7 @@ static int stdout_stream_load(StdoutStream *stream, const char *fname) {
|
||||
assert(fname);
|
||||
|
||||
if (!stream->state_file) {
|
||||
stream->state_file = path_join("/run/systemd/journal/streams", fname);
|
||||
stream->state_file = path_join(stream->server->runtime_directory, "streams", fname);
|
||||
if (!stream->state_file)
|
||||
return log_oom();
|
||||
}
|
||||
@ -781,14 +782,16 @@ static int stdout_stream_restore(Server *s, const char *fname, int fd) {
|
||||
int server_restore_streams(Server *s, FDSet *fds) {
|
||||
_cleanup_closedir_ DIR *d = NULL;
|
||||
struct dirent *de;
|
||||
const char *path;
|
||||
int r;
|
||||
|
||||
d = opendir("/run/systemd/journal/streams");
|
||||
path = strjoina(s->runtime_directory, "/streams");
|
||||
d = opendir(path);
|
||||
if (!d) {
|
||||
if (errno == ENOENT)
|
||||
return 0;
|
||||
|
||||
return log_warning_errno(errno, "Failed to enumerate /run/systemd/journal/streams: %m");
|
||||
return log_warning_errno(errno, "Failed to enumerate %s: %m", path);
|
||||
}
|
||||
|
||||
FOREACH_DIRENT(de, d, goto fail) {
|
||||
@ -816,8 +819,7 @@ int server_restore_streams(Server *s, FDSet *fds) {
|
||||
/* No file descriptor? Then let's delete the state file */
|
||||
log_debug("Cannot restore stream file %s", de->d_name);
|
||||
if (unlinkat(dirfd(d), de->d_name, 0) < 0)
|
||||
log_warning_errno(errno, "Failed to remove /run/systemd/journal/streams/%s: %m",
|
||||
de->d_name);
|
||||
log_warning_errno(errno, "Failed to remove %s%s: %m", path, de->d_name);
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -834,16 +836,21 @@ fail:
|
||||
return log_error_errno(errno, "Failed to read streams directory: %m");
|
||||
}
|
||||
|
||||
int server_open_stdout_socket(Server *s) {
|
||||
static const union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
.un.sun_path = "/run/systemd/journal/stdout",
|
||||
};
|
||||
int server_open_stdout_socket(Server *s, const char *stdout_socket) {
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(stdout_socket);
|
||||
|
||||
if (s->stdout_fd < 0) {
|
||||
union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
};
|
||||
|
||||
r = sockaddr_un_set_path(&sa.un, stdout_socket);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Unable to use namespace path %s for AF_UNIX socket: %m", stdout_socket);
|
||||
|
||||
s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (s->stdout_fd < 0)
|
||||
return log_error_errno(errno, "socket() failed: %m");
|
||||
|
@ -6,7 +6,7 @@ typedef struct StdoutStream StdoutStream;
|
||||
#include "fdset.h"
|
||||
#include "journald-server.h"
|
||||
|
||||
int server_open_stdout_socket(Server *s);
|
||||
int server_open_stdout_socket(Server *s, const char *stdout_socket);
|
||||
int server_restore_streams(Server *s, FDSet *fds);
|
||||
|
||||
void stdout_stream_free(StdoutStream *s);
|
||||
|
@ -32,9 +32,8 @@ static void forward_syslog_iovec(
|
||||
const struct ucred *ucred,
|
||||
const struct timeval *tv) {
|
||||
|
||||
static const union sockaddr_union sa = {
|
||||
union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
.un.sun_path = "/run/systemd/journal/syslog",
|
||||
};
|
||||
struct msghdr msghdr = {
|
||||
.msg_iov = (struct iovec *) iovec,
|
||||
@ -47,11 +46,20 @@ static void forward_syslog_iovec(
|
||||
struct cmsghdr cmsghdr;
|
||||
uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
|
||||
} control;
|
||||
const char *j;
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(iovec);
|
||||
assert(n_iovec > 0);
|
||||
|
||||
j = strjoina(s->runtime_directory, "/syslog");
|
||||
r = sockaddr_un_set_path(&sa.un, j);
|
||||
if (r < 0) {
|
||||
log_debug_errno(r, "Forwarding socket path %s too long for AF_UNIX, not forwarding: %m", j);
|
||||
return;
|
||||
}
|
||||
|
||||
if (ucred) {
|
||||
zero(control);
|
||||
msghdr.msg_control = &control;
|
||||
@ -446,17 +454,21 @@ void server_process_syslog_message(
|
||||
server_dispatch_message(s, iovec, n, m, context, tv, priority, 0);
|
||||
}
|
||||
|
||||
int server_open_syslog_socket(Server *s) {
|
||||
|
||||
static const union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
.un.sun_path = "/run/systemd/journal/dev-log",
|
||||
};
|
||||
int server_open_syslog_socket(Server *s, const char *syslog_socket) {
|
||||
int r;
|
||||
|
||||
assert(s);
|
||||
assert(syslog_socket);
|
||||
|
||||
if (s->syslog_fd < 0) {
|
||||
union sockaddr_union sa = {
|
||||
.un.sun_family = AF_UNIX,
|
||||
};
|
||||
|
||||
r = sockaddr_un_set_path(&sa.un, syslog_socket);
|
||||
if (r < 0)
|
||||
return log_error_errno(r, "Unable to use namespace path %s for AF_UNIX socket: %m", syslog_socket);
|
||||
|
||||
s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
|
||||
if (s->syslog_fd < 0)
|
||||
return log_error_errno(errno, "socket() failed: %m");
|
||||
|
@ -10,6 +10,6 @@ size_t syslog_parse_identifier(const char **buf, char **identifier, char **pid);
|
||||
void server_forward_syslog(Server *s, int priority, const char *identifier, const char *message, const struct ucred *ucred, const struct timeval *tv);
|
||||
|
||||
void server_process_syslog_message(Server *s, const char *buf, size_t buf_len, const struct ucred *ucred, const struct timeval *tv, const char *label, size_t label_len);
|
||||
int server_open_syslog_socket(Server *s);
|
||||
int server_open_syslog_socket(Server *s, const char *syslog_socket);
|
||||
|
||||
void server_maybe_warn_forward_syslog_missed(Server *s);
|
||||
|
@ -14,14 +14,17 @@
|
||||
#include "sigbus.h"
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
const char *namespace;
|
||||
Server server;
|
||||
int r;
|
||||
|
||||
if (argc > 1) {
|
||||
log_error("This program does not take arguments.");
|
||||
if (argc > 2) {
|
||||
log_error("This program takes one or no arguments.");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
namespace = argc > 1 ? empty_to_null(argv[1]) : NULL;
|
||||
|
||||
log_set_prohibit_ipc(true);
|
||||
log_set_target(LOG_TARGET_AUTO);
|
||||
log_set_facility(LOG_SYSLOG);
|
||||
@ -32,7 +35,7 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
sigbus_install();
|
||||
|
||||
r = server_init(&server);
|
||||
r = server_init(&server, namespace);
|
||||
if (r < 0)
|
||||
goto finish;
|
||||
|
||||
@ -40,7 +43,11 @@ int main(int argc, char *argv[]) {
|
||||
server_flush_to_var(&server, true);
|
||||
server_flush_dev_kmsg(&server);
|
||||
|
||||
log_debug("systemd-journald running as pid "PID_FMT, getpid_cached());
|
||||
if (server.namespace)
|
||||
log_debug("systemd-journald running as PID "PID_FMT" for namespace '%s'.", getpid_cached(), server.namespace ?: "<system>");
|
||||
else
|
||||
log_debug("systemd-journald running as PID "PID_FMT" for the system.", getpid_cached());
|
||||
|
||||
server_driver_message(&server, 0,
|
||||
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_START_STR,
|
||||
LOG_MESSAGE("Journal started"),
|
||||
@ -99,7 +106,11 @@ int main(int argc, char *argv[]) {
|
||||
server_maybe_warn_forward_syslog_missed(&server);
|
||||
}
|
||||
|
||||
log_debug("systemd-journald stopped as pid "PID_FMT, getpid_cached());
|
||||
if (server.namespace)
|
||||
log_debug("systemd-journald stopped as PID "PID_FMT" for namespace '%s'.", getpid_cached(), server.namespace ?: "<system>");
|
||||
else
|
||||
log_debug("systemd-journald stopped as PID "PID_FMT" for the system.", getpid_cached());
|
||||
|
||||
server_driver_message(&server, 0,
|
||||
"MESSAGE_ID=" SD_MESSAGE_JOURNAL_STOP_STR,
|
||||
LOG_MESSAGE("Journal stopped"),
|
||||
|
Loading…
x
Reference in New Issue
Block a user