journald-server.c revision 8a0889dfdafa3054c894e54852d8a9e3a7e8390b
/*-*- 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>
#ifdef HAVE_LOGIND
#include <systemd/sd-login.h>
#endif
#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 200
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];
char _cleanup_free_ *p = NULL;
const char *f;
int r;
JournalMetrics *m;
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;
}
s->cached_available_space = avail;
return avail;
}
static void server_read_file_gid(Server *s) {
const char *adm = "adm";
int r;
assert(s);
if (s->file_gid_valid)
return;
if (r < 0)
/* if we couldn't read the gid, then it will be 0, but that's
* fine and we shouldn't try to resolve the group again, so
* let's just pretend it worked right-away. */
s->file_gid_valid = true;
}
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;
}
}
acl_calc_mask(&acl) < 0) {
goto finish;
}
#endif
}
char *p;
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, s->system_journal, &f);
free(p);
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_vacuum(Server *s) {
char *p;
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) {
if (!p) {
log_oom();
return;
}
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)
free(p);
}
if (s->runtime_journal) {
if (!p) {
log_oom();
return;
}
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)
free(p);
}
s->cached_available_space_timestamp = 0;
}
int r;
char *path;
if (r < 0)
return NULL;
if (r < 0)
return NULL;
init_path[0] = 0;
} else {
path = process_path;
process_path = NULL;
}
return path;
}
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)) {
return;
}
server_rotate(s);
server_vacuum(s);
f = find_journal(s, uid);
if (!f)
return;
log_debug("Retrying write.");
if (r < 0)
}
static void dispatch_message_real(
Server *s,
const char *unit_id) {
char idbuf[33];
int r;
char *t;
bool loginuid_valid = false, owner_valid = false;
assert(s);
assert(n > 0);
assert(n + N_IOVEC_META_FIELDS <= m);
if (ucred) {
if (r >= 0) {
free(t);
if (comm)
}
if (r >= 0) {
free(t);
if (exe)
}
if (r >= 0) {
free(t);
if (cmdline)
}
if (r >= 0)
if (r >= 0) {
loginuid_valid = true;
}
if (t) {
free(t);
if (cgroup)
}
#ifdef HAVE_LOGIND
free(t);
if (session)
}
owner_valid = true;
}
#endif
free(t);
free(t);
} else if (unit_id) {
if (session)
else
}
if (unit)
#ifdef HAVE_SELINUX
if (label) {
if (selinux_context) {
}
} else {
if (selinux_context)
}
}
#endif
}
if (tv) {
(unsigned long long) timeval_load(tv)) >= 0)
}
/* 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) {
free(t);
if (hostname)
}
assert(n <= m);
journal_uid = owner;
else
journal_uid = 0;
}
int n = 0;
assert(s);
}
}
void server_dispatch_message(
Server *s,
const char *unit_id,
int priority) {
int rl;
char *c;
assert(s);
if (n == 0)
return;
return;
if (!ucred)
goto finish;
if (!path)
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)
if (!fn)
return -ENOMEM;
if (!fn)
return -ENOMEM;
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) {
char fb[FORMAT_BYTES_MAX];
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) {
char fb[FORMAT_BYTES_MAX];
server_fix_perms(s, s->runtime_journal, 0);
}
}
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);
log_debug("Retrying write.");
if (r < 0) {
goto finish;
}
}
s->runtime_journal = NULL;
if (r >= 0)
if (j)
sd_journal_close(j);
return r;
}
assert(s);
struct signalfd_siginfo sfsi;
ssize_t n;
log_error("Got invalid event from epoll.");
return -EIO;
}
if (n != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
return 1;
return -errno;
}
return 1;
}
server_rotate(s);
server_vacuum(s);
return 1;
}
return 0;
int r;
log_error("Got invalid event from epoll.");
return -EIO;
}
r = server_read_dev_kmsg(s);
if (r < 0)
return r;
return 1;
log_error("Got invalid event from epoll.");
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;
}
}
}
char *e;
if (n > 0 && n_fds == 0) {
if (e)
*e = 0;
else
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;
log_error("Got invalid event from epoll.");
return -EIO;
}
return 1;
} else {
log_error("Got invalid event from epoll.");
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;
}
char _cleanup_free_ *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, s);
if (r < 0)
return r;
}
int server_init(Server *s) {
int n, r, fd;
assert(s);
zero(*s);
s->compress = true;
s->seal = true;
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 = 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->rate_limit)
if (s->kernel_seqnum)
if (s->mmap)
mmap_cache_unref(s->mmap);
if (s->udev)
udev_unref(s->udev);
}