journald-server.c revision 8a7935a23bfec14beb73e004beb01273254cf789
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <sys/signalfd.h>
#include <libudev.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-messages.h>
#include <systemd/sd-daemon.h>
#include "fileio.h"
#include "mkdir.h"
#include "hashmap.h"
#include "journal-file.h"
#include "socket-util.h"
#include "cgroup-util.h"
#include "list.h"
#include "virt.h"
#include "missing.h"
#include "conf-parser.h"
#include "journal-internal.h"
#include "journal-vacuum.h"
#include "journal-authenticate.h"
#include "journald-server.h"
#include "journald-rate-limit.h"
#include "journald-kmsg.h"
#include "journald-syslog.h"
#include "journald-stream.h"
#include "journald-console.h"
#include "journald-native.h"
#ifdef HAVE_ACL
#include "acl-util.h"
#endif
#ifdef HAVE_SELINUX
#endif
#define USER_JOURNALS_MAX 1024
#define DEFAULT_RATE_LIMIT_BURST 1000
static const char* const storage_table[] = {
[STORAGE_AUTO] = "auto",
[STORAGE_VOLATILE] = "volatile",
[STORAGE_PERSISTENT] = "persistent",
[STORAGE_NONE] = "none"
};
DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
static const char* const split_mode_table[] = {
[SPLIT_NONE] = "none",
[SPLIT_UID] = "uid",
[SPLIT_LOGIN] = "login"
};
DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode, "Failed to parse split mode setting");
char ids[33];
_cleanup_free_ char *p = NULL;
int r;
const char *f;
JournalMetrics *m;
&& !verbose)
return s->cached_available_space;
r = sd_id128_get_machine(&machine);
if (r < 0)
return 0;
if (s->system_journal) {
m = &s->system_metrics;
} else {
m = &s->runtime_metrics;
}
assert(m);
if (!p)
return 0;
d = opendir(p);
if (!d)
return 0;
return 0;
for (;;) {
union dirent_storage buf;
if (r != 0)
break;
if (!de)
break;
continue;
continue;
continue;
}
if (verbose) {
"%s journal is using %s (max %s, leaving %s of free %s, current limit %s).",
}
return s->cached_available_space;
}
int r;
#ifdef HAVE_ACL
#endif
assert(f);
if (r < 0)
#ifdef HAVE_ACL
if (uid <= 0)
return;
if (!acl) {
return;
}
if (r <= 0) {
goto finish;
}
}
/* We do not recalculate the mask unconditionally here,
* so that the fchmod() mask above stays intact. */
calc_acl_mask_if_needed(&acl) < 0) {
goto finish;
}
#endif
}
_cleanup_free_ char *p = NULL;
int r;
JournalFile *f;
assert(s);
/* We split up user logs only on /var, not on /run. If the
* runtime file is open, we write to it exclusively, in order
* to guarantee proper order as soon as we flush /run to
* /var and close the runtime file. */
if (s->runtime_journal)
return s->runtime_journal;
if (uid <= 0)
return s->system_journal;
r = sd_id128_get_machine(&machine);
if (r < 0)
return s->system_journal;
if (f)
return f;
return s->system_journal;
/* Too many open? Then let's close one */
f = hashmap_steal_first(s->user_journals);
assert(f);
}
r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &f);
if (r < 0)
return s->system_journal;
server_fix_perms(s, f, uid);
if (r < 0) {
return s->system_journal;
}
return f;
}
void server_rotate(Server *s) {
JournalFile *f;
void *k;
Iterator i;
int r;
log_debug("Rotating...");
if (s->runtime_journal) {
if (r < 0)
if (s->runtime_journal)
else
else
server_fix_perms(s, s->runtime_journal, 0);
}
if (s->system_journal) {
if (r < 0)
if (s->system_journal)
else
else
server_fix_perms(s, s->system_journal, 0);
}
HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
if (r < 0)
if (f)
else
else {
hashmap_replace(s->user_journals, k, f);
server_fix_perms(s, f, PTR_TO_UINT32(k));
}
}
}
void server_sync(Server *s) {
static const struct itimerspec sync_timer_disable = {};
JournalFile *f;
void *k;
Iterator i;
int r;
if (s->system_journal) {
r = journal_file_set_offline(s->system_journal);
if (r < 0)
}
HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
r = journal_file_set_offline(f);
if (r < 0)
}
if (r < 0)
log_error("Failed to disable max timer: %m");
s->sync_scheduled = false;
}
void server_vacuum(Server *s) {
char ids[33];
int r;
log_debug("Vacuuming...");
s->oldest_file_usec = 0;
r = sd_id128_get_machine(&machine);
if (r < 0) {
return;
}
if (s->system_journal) {
r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
if (r < 0 && r != -ENOENT)
}
if (s->runtime_journal) {
r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free, s->max_retention_usec, &s->oldest_file_usec);
if (r < 0 && r != -ENOENT)
}
s->cached_available_space_timestamp = 0;
}
bool shall_try_append_again(JournalFile *f, int r) {
/* -E2BIG Hit configured limit
-EFBIG Hit fs limit
-EDQUOT Quota limit hit
-ENOSPC Disk full
-EHOSTDOWN Other machine
-EBUSY Unclean shutdown
-EPROTONOSUPPORT Unsupported feature
-EBADMSG Corrupted
-ENODATA Truncated
-ESHUTDOWN Already archived */
else if (r == -EHOSTDOWN)
else if (r == -EBUSY)
else if (r == -EPROTONOSUPPORT)
else
return false;
return true;
}
JournalFile *f;
bool vacuumed = false;
int r;
assert(s);
assert(n > 0);
f = find_journal(s, uid);
if (!f)
return;
if (journal_file_rotate_suggested(f, s->max_file_usec)) {
server_rotate(s);
server_vacuum(s);
vacuumed = true;
f = find_journal(s, uid);
if (!f)
return;
}
if (r >= 0) {
return;
}
if (vacuumed || !shall_try_append_again(f, r)) {
unsigned i;
for (i = 0; i < n; i++)
return;
}
server_rotate(s);
server_vacuum(s);
f = find_journal(s, uid);
if (!f)
return;
log_debug("Retrying write.");
if (r < 0) {
unsigned i;
for (i = 0; i < n; i++)
log_error("Failed to write entry (%d items, %zu bytes) despite vacuuming, ignoring: %s", n, size, strerror(-r));
} else
}
static void dispatch_message_real(
Server *s,
const char *unit_id,
int priority,
pid_t object_pid) {
char *x;
int r;
char *t, *c;
bool owner_valid = false;
#ifdef HAVE_AUDIT
#endif
assert(s);
assert(n > 0);
if (ucred) {
if (r >= 0) {
x = strappenda("_COMM=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
if (r >= 0) {
x = strappenda("_EXE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
if (r >= 0) {
x = strappenda("_CMDLINE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
if (r >= 0) {
x = strappenda("_CAP_EFFECTIVE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
#ifdef HAVE_AUDIT
if (r >= 0) {
}
if (r >= 0) {
}
#endif
if (r >= 0) {
x = strappenda("_SYSTEMD_CGROUP=", c);
IOVEC_SET_STRING(iovec[n++], x);
r = cg_path_get_session(c, &t);
if (r >= 0) {
free(t);
}
if (cg_path_get_owner_uid(c, &owner) >= 0) {
owner_valid = true;
}
if (cg_path_get_unit(c, &t) >= 0) {
x = strappenda("_SYSTEMD_UNIT=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
IOVEC_SET_STRING(iovec[n++], x);
}
if (cg_path_get_user_unit(c, &t) >= 0) {
x = strappenda("_SYSTEMD_USER_UNIT=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
IOVEC_SET_STRING(iovec[n++], x);
}
if (cg_path_get_slice(c, &t) >= 0) {
x = strappenda("_SYSTEMD_SLICE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
free(c);
}
#ifdef HAVE_SELINUX
if (label) {
IOVEC_SET_STRING(iovec[n++], x);
} else {
IOVEC_SET_STRING(iovec[n++], x);
}
}
#endif
}
assert(n <= m);
if (object_pid) {
if (r >= 0) {
}
if (r >= 0) {
}
r = get_process_comm(object_pid, &t);
if (r >= 0) {
x = strappenda("OBJECT_COMM=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
r = get_process_exe(object_pid, &t);
if (r >= 0) {
x = strappenda("OBJECT_EXE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
r = get_process_cmdline(object_pid, 0, false, &t);
if (r >= 0) {
x = strappenda("OBJECT_CMDLINE=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
#ifdef HAVE_AUDIT
if (r >= 0) {
}
if (r >= 0) {
}
#endif
if (r >= 0) {
x = strappenda("OBJECT_SYSTEMD_CGROUP=", c);
IOVEC_SET_STRING(iovec[n++], x);
r = cg_path_get_session(c, &t);
if (r >= 0) {
x = strappenda("OBJECT_SYSTEMD_SESSION=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
if (cg_path_get_owner_uid(c, &owner) >= 0) {
}
if (cg_path_get_unit(c, &t) >= 0) {
x = strappenda("OBJECT_SYSTEMD_UNIT=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
if (cg_path_get_user_unit(c, &t) >= 0) {
x = strappenda("OBJECT_SYSTEMD_USER_UNIT=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
free(c);
}
}
assert(n <= m);
if (tv) {
}
/* Note that strictly speaking storing the boot id here is
* redundant since the entry includes this in-line
* anyway. However, we need this indexed, too. */
r = sd_id128_get_boot(&id);
if (r >= 0) {
}
r = sd_id128_get_machine(&id);
if (r >= 0) {
}
t = gethostname_malloc();
if (t) {
x = strappenda("_HOSTNAME=", t);
free(t);
IOVEC_SET_STRING(iovec[n++], x);
}
assert(n <= m);
/* Split up strictly by any UID */
/* Split up by login UIDs, this avoids creation of
* individual journals for system UIDs. We do this
* only if the realuid is not root, in order not to
* accidentally leak privileged information to the
* user that is logged by a privileged process that is
* part of an unprivileged session.*/
journal_uid = owner;
else
journal_uid = 0;
}
int n = 0;
assert(s);
}
}
void server_dispatch_message(
Server *s,
const char *unit_id,
int priority,
pid_t object_pid) {
int rl, r;
char *c;
assert(s);
if (n == 0)
return;
return;
/* Stop early in case the information will not be stored
* in a journal. */
if (s->storage == STORAGE_NONE)
return;
if (!ucred)
goto finish;
if (r < 0)
goto finish;
*
* So let's cut of everything past the third /, since that is
* where user directories start */
if (c) {
if (c) {
if (c)
*c = 0;
}
}
if (rl == 0)
return;
/* Write a suppression message if we suppressed something */
if (rl > 1)
}
static int system_journal_open(Server *s) {
int r;
char *fn;
char ids[33];
r = sd_id128_get_machine(&machine);
if (r < 0) {
return r;
}
if (!s->system_journal &&
/* If in auto mode: first try to create the machine
* path, but not the prefix.
*
* the machine path */
if (s->storage == STORAGE_PERSISTENT)
r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
if (r >= 0)
server_fix_perms(s, s->system_journal, 0);
else if (r < 0) {
r = 0;
}
}
if (!s->runtime_journal &&
(s->storage != STORAGE_NONE)) {
if (!fn)
return -ENOMEM;
if (s->system_journal) {
/* Try to open the runtime journal, but only
* if it already exists, so that we can flush
* it into the system journal */
r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
if (r < 0) {
if (r != -ENOENT)
r = 0;
}
} else {
/* OK, we really need the runtime journal, so create
* it if necessary. */
r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
if (r < 0) {
return r;
}
}
if (s->runtime_journal)
server_fix_perms(s, s->runtime_journal, 0);
}
available_space(s, true);
return r;
}
int server_flush_to_var(Server *s) {
int r;
sd_journal *j = NULL;
assert(s);
if (s->storage != STORAGE_AUTO &&
s->storage != STORAGE_PERSISTENT)
return 0;
if (!s->runtime_journal)
return 0;
if (!s->system_journal)
return 0;
log_debug("Flushing to /var...");
r = sd_id128_get_machine(&machine);
if (r < 0)
return r;
r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
if (r < 0) {
return r;
}
SD_JOURNAL_FOREACH(j) {
JournalFile *f;
f = j->current_file;
assert(f && f->current_offset > 0);
if (r < 0) {
goto finish;
}
if (r >= 0)
continue;
if (!shall_try_append_again(s->system_journal, r)) {
goto finish;
}
server_rotate(s);
server_vacuum(s);
if (!s->system_journal) {
log_notice("Didn't flush runtime journal since rotation of system journal wasn't successful.");
r = -EIO;
goto finish;
}
log_debug("Retrying write.");
if (r < 0) {
goto finish;
}
}
s->runtime_journal = NULL;
if (r >= 0)
sd_journal_close(j);
return r;
}
assert(s);
struct signalfd_siginfo sfsi;
ssize_t n;
return -EIO;
}
if (n != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
return 1;
return -errno;
}
server_sync(s);
return 1;
}
server_rotate(s);
server_vacuum(s);
return 1;
}
return 0;
int r;
uint64_t t;
log_debug("Got sync request from epoll.");
if (r < 0)
return 0;
server_sync(s);
return 1;
int r;
log_warning("/dev/kmsg buffer overrun, some messages lost.");
return -EIO;
}
r = server_read_dev_kmsg(s);
if (r < 0)
return r;
return 1;
return -EIO;
}
for (;;) {
union {
/* We use NAME_MAX space for the
* SELinux label here. The kernel
* currently enforces no limit, but
* according to suggestions from the
* SELinux people this will change and
* it will probably be identical to
* NAME_MAX. For now we use that, but
* this should be updated one day when
* the final limit is known.*/
CMSG_SPACE(sizeof(struct timeval)) +
CMSG_SPACE(sizeof(int)) + /* fd */
} control;
ssize_t n;
int v;
unsigned n_fds = 0;
log_error("SIOCINQ failed: %m");
return -errno;
}
if (s->buffer_size < (size_t) v) {
void *b;
size_t l;
if (!b) {
log_error("Couldn't increase buffer.");
return -ENOMEM;
}
s->buffer_size = l;
s->buffer = b;
}
if (n < 0) {
return 1;
log_error("recvmsg() failed: %m");
return -errno;
}
}
}
if (n > 0 && n_fds == 0) {
s->buffer[n] = 0;
} else if (n_fds > 0)
log_warning("Got file descriptors via syslog socket. Ignoring.");
} else {
if (n > 0 && n_fds == 0)
else if (n == 0 && n_fds == 1)
else if (n_fds > 0)
log_warning("Got too many file descriptors via native socket. Ignoring.");
}
}
return 1;
return -EIO;
}
return 1;
} else {
return -EIO;
}
/* If it is none of the well-known fds, it must be an
* stdout stream fd. Note that this is a bit ugly here
* (since we rely that none of the well-known fds
* could be interpreted as pointer), but nonetheless
* safe, since the well-known fds would never get an
* fd > 4096, i.e. beyond the first memory page */
if (stdout_stream_process(stream) <= 0)
return 1;
}
log_error("Unknown event.");
return 0;
}
static int open_signalfd(Server *s) {
struct epoll_event ev;
assert(s);
if (s->signal_fd < 0) {
log_error("signalfd(): %m");
return -errno;
}
log_error("epoll_ctl(): %m");
return -errno;
}
return 0;
}
static int server_parse_proc_cmdline(Server *s) {
char *w, *state;
int r;
size_t l;
if (detect_container(NULL) > 0)
return 0;
if (r < 0) {
return 0;
}
_cleanup_free_ char *word;
if (!word)
return -ENOMEM;
if (r < 0)
else
s->forward_to_syslog = r;
if (r < 0)
else
s->forward_to_kmsg = r;
if (r < 0)
else
s->forward_to_console = r;
log_warning("Invalid systemd.journald parameter. Ignoring.");
}
return 0;
}
static int server_parse_config_file(Server *s) {
static const char fn[] = "/etc/systemd/journald.conf";
int r;
assert(s);
if (!f) {
return 0;
return -errno;
}
(void*) journald_gperf_lookup, false, false, s);
if (r < 0)
return r;
}
static int server_open_sync_timer(Server *s) {
int r;
struct epoll_event ev;
assert(s);
if (s->sync_timer_fd < 0)
return -errno;
if (r < 0) {
log_error("Failed to add idle timer fd to epoll object: %m");
return -errno;
}
return 0;
}
int r;
assert(s);
/* Immediately sync to disk when this is of priority CRIT, ALERT, EMERG */
server_sync(s);
return 0;
}
if (s->sync_scheduled)
return 0;
if (s->sync_interval_usec) {
struct itimerspec sync_timer_enable = {};
if (r < 0)
return -errno;
}
s->sync_scheduled = true;
return 0;
}
int server_init(Server *s) {
int n, r, fd;
assert(s);
zero(*s);
s->compress = true;
s->seal = true;
s->sync_scheduled = false;
s->forward_to_syslog = true;
s->max_level_store = LOG_DEBUG;
s->max_level_syslog = LOG_DEBUG;
s->max_level_kmsg = LOG_NOTICE;
s->max_level_console = LOG_INFO;
if (!!s->rate_limit_interval ^ !!s->rate_limit_burst) {
log_debug("Setting both rate limit interval and burst from %llu,%u to 0,0",
(long long unsigned) s->rate_limit_interval,
s->rate_limit_burst);
s->rate_limit_interval = s->rate_limit_burst = 0;
}
if (!s->user_journals)
return log_oom();
s->mmap = mmap_cache_new();
if (!s->mmap)
return log_oom();
if (s->epoll_fd < 0) {
log_error("Failed to create epoll object: %m");
return -errno;
}
n = sd_listen_fds(true);
if (n < 0) {
return n;
}
if (s->native_fd >= 0) {
log_error("Too many native sockets passed.");
return -EINVAL;
}
if (s->stdout_fd >= 0) {
log_error("Too many stdout sockets passed.");
return -EINVAL;
}
if (s->syslog_fd >= 0) {
return -EINVAL;
}
} else {
log_error("Unknown socket passed.");
return -EINVAL;
}
}
r = server_open_syslog_socket(s);
if (r < 0)
return r;
r = server_open_native_socket(s);
if (r < 0)
return r;
r = server_open_stdout_socket(s);
if (r < 0)
return r;
r = server_open_dev_kmsg(s);
if (r < 0)
return r;
r = server_open_kernel_seqnum(s);
if (r < 0)
return r;
r = server_open_sync_timer(s);
if (r < 0)
return r;
r = open_signalfd(s);
if (r < 0)
return r;
if (!s->udev)
return -ENOMEM;
s->rate_limit_burst);
if (!s->rate_limit)
return -ENOMEM;
r = system_journal_open(s);
if (r < 0)
return r;
return 0;
}
void server_maybe_append_tags(Server *s) {
#ifdef HAVE_GCRYPT
JournalFile *f;
Iterator i;
usec_t n;
n = now(CLOCK_REALTIME);
if (s->system_journal)
HASHMAP_FOREACH(f, s->user_journals, i)
#endif
}
void server_done(Server *s) {
JournalFile *f;
assert(s);
while (s->stdout_streams)
if (s->system_journal)
if (s->runtime_journal)
while ((f = hashmap_steal_first(s->user_journals)))
if (s->epoll_fd >= 0)
if (s->signal_fd >= 0)
if (s->syslog_fd >= 0)
if (s->native_fd >= 0)
if (s->stdout_fd >= 0)
if (s->dev_kmsg_fd >= 0)
if (s->sync_timer_fd >= 0)
if (s->rate_limit)
if (s->kernel_seqnum)
if (s->mmap)
mmap_cache_unref(s->mmap);
if (s->udev)
udev_unref(s->udev);
}