journald.c revision cee5e9a7ca75f05d4a0f7759566e657587a500d5
/*-*- 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 <errno.h>
#include <sys/signalfd.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.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 "mkdir.h"
#include "hashmap.h"
#include "journal-file.h"
#include "socket-util.h"
#include "cgroup-util.h"
#include "list.h"
#include "journal-rate-limit.h"
#include "journal-internal.h"
#include "conf-parser.h"
#include "journald.h"
#include "virt.h"
#include "missing.h"
#ifdef HAVE_ACL
#include "acl-util.h"
#endif
#ifdef HAVE_SELINUX
#endif
#define USER_JOURNALS_MAX 1024
#define STDOUT_STREAMS_MAX 4096
#define DEFAULT_RATE_LIMIT_BURST 200
#define N_IOVEC_META_FIELDS 17
#define N_IOVEC_KERNEL_FIELDS 64
typedef enum StdoutStreamState {
struct StdoutStream {
int fd;
#ifdef HAVE_SELINUX
#endif
char *identifier;
char *unit_id;
int priority;
bool level_prefix:1;
bool forward_to_syslog:1;
bool forward_to_kmsg:1;
bool forward_to_console:1;
};
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");
char ids[33], *p;
const char *f;
int r;
DIR *d;
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);
free(p);
if (!d)
return 0;
goto finish;
for (;;) {
if (r != 0)
break;
if (!de)
break;
continue;
continue;
continue;
}
s->cached_available_space = avail;
closedir(d);
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;
char ids[33];
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;
if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
return s->system_journal;
/* Too many open? Then let's close one */
f = hashmap_steal_first(s->user_journals);
assert(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;
}
static void server_rotate(Server *s) {
JournalFile *f;
void *k;
Iterator i;
int r;
log_info("Rotating...");
if (s->runtime_journal) {
r = journal_file_rotate(&s->runtime_journal);
if (r < 0)
if (s->runtime_journal)
else
else
server_fix_perms(s, s->runtime_journal, 0);
}
if (s->system_journal) {
r = journal_file_rotate(&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) {
r = journal_file_rotate(&f);
if (r < 0)
if (f->path)
else
else {
hashmap_replace(s->user_journals, k, f);
}
}
}
static void server_vacuum(Server *s) {
char *p;
char ids[33];
int r;
log_info("Vacuuming...");
r = sd_id128_get_machine(&machine);
if (r < 0) {
return;
}
if (s->system_journal) {
log_oom();
return;
}
if (r < 0 && r != -ENOENT)
free(p);
}
if (s->runtime_journal) {
log_oom();
return;
}
if (r < 0 && r != -ENOENT)
free(p);
}
s->cached_available_space_timestamp = 0;
}
int r;
if (r < 0)
return NULL;
if (r < 0) {
return NULL;
}
init_path[0] = 0;
char *p;
if (!p) {
return NULL;
}
path = p;
} else {
path = process_path;
process_path = NULL;
}
return path;
}
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)) {
log_info("Journal header limits reached or header out-of-date, rotating.");
server_rotate(s);
server_vacuum(s);
vacuumed = true;
f = find_journal(s, uid);
if (!f)
return;
}
for (;;) {
if (r >= 0)
return;
if (vacuumed ||
(r != -E2BIG && /* hit limit */
r != -EFBIG && /* hit fs limit */
r != -EDQUOT && /* quota hit */
r != -ENOSPC && /* disk full */
r != -EBADMSG && /* corrupted */
r != -ENODATA && /* truncated */
r != -EHOSTDOWN && /* other machine */
r != -EPROTONOSUPPORT && /* unsupported feature */
r != -EBUSY && /* unclean shutdown */
r != -ESHUTDOWN /* already archived */)) {
return;
}
log_info("Allocation limit reached, rotating.");
else if (r == -EHOSTDOWN)
log_info("Journal file from other machine, rotating.");
else if (r == -EBUSY)
log_info("Unlcean shutdown, rotating.");
else
log_warning("Journal file corrupted, rotating.");
server_rotate(s);
server_vacuum(s);
vacuumed = true;
f = find_journal(s, uid);
if (!f)
return;
log_info("Retrying write.");
}
}
static void dispatch_message_real(
Server *s,
const char *unit_id) {
char idbuf[33];
int r;
char *t;
assert(s);
assert(n > 0);
assert(n + N_IOVEC_META_FIELDS <= m);
if (ucred) {
#ifdef HAVE_LOGIND
#endif
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)
if (t) {
free(t);
if (cgroup)
}
#ifdef HAVE_LOGIND
free(t);
if (session)
}
#endif
free(t);
} else if (unit_id)
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);
}
int n = 0;
assert(s);
}
static void dispatch_message(Server *s,
const char *unit_id,
int priority) {
int rl;
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
* wher 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 void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
union {
} control;
union sockaddr_union sa;
assert(s);
if (ucred) {
}
* the SO_TIMESTAMP auxiliary data, and hence we don't. */
return;
/* The socket is full? I guess the syslog implementation is
* too slow, and we shouldn't wait for that... */
return;
struct ucred u;
/* Hmm, presumably the sender process vanished
* by now, so let's fix it as good as we
* can, and retry */
u = *ucred;
return;
return;
}
log_debug("Failed to forward syslog message: %m");
}
static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
assert(s);
return;
}
static void forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
int n = 0;
time_t t;
assert(s);
return;
/* First: priority field */
/* Second: timestamp */
if (!tm)
return;
return;
/* Third: identifier and PID */
if (ucred) {
if (!identifier) {
}
if (identifier)
} else if (identifier) {
}
/* Fourth: message */
}
static int fixup_priority(int priority) {
if ((priority & LOG_FACMASK) == 0)
return priority;
}
static void forward_kmsg(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
int n = 0;
assert(s);
return;
if (_unlikely_(s->dev_kmsg_fd < 0))
return;
/* Never allow messages with kernel facility to be written to
* kmsg, regardless where the data comes from. */
/* First: priority field */
/* Second: identifier and PID */
if (ucred) {
if (!identifier) {
}
if (identifier)
} else if (identifier) {
}
/* Fourth: message */
}
static void forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
char header_pid[16];
int n = 0, fd;
const char *tty;
assert(s);
return;
/* First: identifier and PID */
if (ucred) {
if (!identifier) {
}
if (identifier)
} else if (identifier) {
}
/* Third: message */
if (fd < 0) {
goto finish;
}
}
const char *p;
char *t;
size_t l, e;
p = *buf;
p += strspn(p, WHITESPACE);
l = strcspn(p, WHITESPACE);
if (l <= 0 ||
p[l-1] != ':')
return;
e = l;
l--;
if (p[l-1] == ']') {
size_t k = l-1;
for (;;) {
if (p[k] == '[') {
if (t)
*pid = t;
l = k;
break;
}
if (k == 0)
break;
k--;
}
}
t = strndup(p, l);
if (t)
*identifier = t;
*buf = p + e;
}
static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len) {
char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
unsigned n = 0;
const char *orig;
assert(s);
if (s->forward_to_syslog)
skip_syslog_date((char**) &buf);
if (s->forward_to_kmsg)
if (s->forward_to_console)
if (priority & LOG_FACMASK)
if (identifier) {
if (syslog_identifier)
}
if (pid) {
if (syslog_pid)
}
if (message)
}
static bool valid_user_field(const char *p, size_t l) {
const char *a;
/* We kinda enforce POSIX syntax recommendations for
environment variables here, but make a couple of additional
requirements.
/* No empty field names */
if (l <= 0)
return false;
/* Don't allow names longer than 64 chars */
if (l > 64)
return false;
/* Variables starting with an underscore are protected */
if (p[0] == '_')
return false;
/* Don't allow digits as first character */
if (p[0] >= '0' && p[0] <= '9')
return false;
/* Only allow A-Z0-9 and '_' */
for (a = p; a < p + l; a++)
if (!((*a >= 'A' && *a <= 'Z') ||
(*a >= '0' && *a <= '9') ||
*a == '_'))
return false;
return true;
}
static void process_native_message(
Server *s,
unsigned n = 0, m = 0, j, tn = (unsigned) -1;
const char *p;
assert(s);
p = buffer;
while (remaining > 0) {
const char *e, *q;
if (!e) {
/* Trailing noise, let's ignore it, and flush what we collected */
log_debug("Received message with trailing noise, ignoring.");
break;
}
if (e == p) {
/* Entry separator */
n = 0;
p++;
remaining--;
continue;
}
if (*p == '.' || *p == '#') {
/* Ignore control commands for now, and
* comments too. */
remaining -= (e - p) + 1;
p = e + 1;
continue;
}
/* A property follows */
if (n+N_IOVEC_META_FIELDS >= m) {
struct iovec *c;
unsigned u;
if (!c) {
log_oom();
break;
}
iovec = c;
m = u;
}
q = memchr(p, '=', e - p);
if (q) {
if (valid_user_field(p, q - p)) {
size_t l;
l = e - p;
/* If the field name starts with an
* underscore, skip the variable,
* since that indidates a trusted
* field */
n++;
/* We need to determine the priority
* of this entry for the rate limiting
* logic */
if (l == 10 &&
p[9] >= '0' && p[9] <= '9')
else if (l == 17 &&
p[16] >= '0' && p[16] <= '9')
else if (l == 18 &&
p[16] >= '0' && p[16] <= '9' &&
p[17] >= '0' && p[17] <= '9')
else if (l >= 19 &&
char *t;
if (t) {
identifier = t;
}
} else if (l >= 8 &&
char *t;
if (t) {
message = t;
}
}
}
remaining -= (e - p) + 1;
p = e + 1;
continue;
} else {
uint64_t l;
char *k;
log_debug("Failed to parse message, ignoring.");
break;
}
log_debug("Failed to parse message, ignoring.");
break;
}
k = malloc((e - p) + 1 + l);
if (!k) {
log_oom();
break;
}
memcpy(k, p, e - p);
k[e - p] = '=';
if (valid_user_field(p, e - p)) {
n++;
} else
free(k);
}
}
if (n <= 0)
goto finish;
tn = n++;
if (message) {
if (s->forward_to_syslog)
if (s->forward_to_kmsg)
if (s->forward_to_console)
}
for (j = 0; j < n; j++) {
if (j == tn)
continue;
}
}
static void process_native_file(
Server *s,
int fd,
void *p;
ssize_t n;
assert(s);
/* Data is in the passed file, since it didn't fit in a
* datagram. We can't map the file here, since clients might
* then truncate it and trigger a SIGBUS for us. So let's
* stupidly read it */
log_error("Failed to stat passed file, ignoring: %m");
return;
}
log_error("File passed is not regular. Ignoring.");
return;
}
return;
log_error("File passed too large. Ignoring.");
return;
}
if (!p) {
log_oom();
return;
}
if (n < 0)
else if (n > 0)
free(p);
}
static int stdout_stream_log(StdoutStream *s, const char *p) {
unsigned n = 0;
int priority;
assert(s);
assert(p);
if (isempty(p))
return 0;
if (s->level_prefix)
parse_syslog_priority((char**) &p, &priority);
if (priority & LOG_FACMASK)
if (s->identifier) {
if (syslog_identifier)
}
if (message)
#ifdef HAVE_SELINUX
if (s->security_context) {
label = (char*) s->security_context;
}
#endif
dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
return 0;
}
static int stdout_stream_line(StdoutStream *s, char *p) {
int r;
assert(s);
assert(p);
p = strstrip(p);
switch (s->state) {
case STDOUT_STREAM_IDENTIFIER:
if (isempty(p))
s->identifier = NULL;
else {
s->identifier = strdup(p);
if (!s->identifier)
return log_oom();
}
s->state = STDOUT_STREAM_UNIT_ID;
return 0;
case STDOUT_STREAM_UNIT_ID:
if (isempty(p))
else {
if (!s->unit_id)
return log_oom();
}
}
s->state = STDOUT_STREAM_PRIORITY;
return 0;
case STDOUT_STREAM_PRIORITY:
log_warning("Failed to parse log priority line.");
return -EINVAL;
}
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse level prefix line.");
return -EINVAL;
}
s->level_prefix = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse forward to syslog line.");
return -EINVAL;
}
s->forward_to_syslog = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse copy to kmsg line.");
return -EINVAL;
}
s->forward_to_kmsg = !!r;
return 0;
r = parse_boolean(p);
if (r < 0) {
log_warning("Failed to parse copy to console line.");
return -EINVAL;
}
s->forward_to_console = !!r;
s->state = STDOUT_STREAM_RUNNING;
return 0;
case STDOUT_STREAM_RUNNING:
return stdout_stream_log(s, p);
}
assert_not_reached("Unknown stream state");
}
char *p;
int r;
assert(s);
p = s->buffer;
for (;;) {
char *end;
if (end)
} else
break;
*end = 0;
r = stdout_stream_line(s, p);
if (r < 0)
return r;
p += skip;
}
if (force_flush && remaining > 0) {
p[remaining] = 0;
r = stdout_stream_line(s, p);
if (r < 0)
return r;
p += remaining;
remaining = 0;
}
if (p > s->buffer) {
}
return 0;
}
static int stdout_stream_process(StdoutStream *s) {
ssize_t l;
int r;
assert(s);
if (l < 0) {
return 0;
log_warning("Failed to read from stream: %m");
return -errno;
}
if (l == 0) {
r = stdout_stream_scan(s, true);
if (r < 0)
return r;
return 0;
}
s->length += l;
r = stdout_stream_scan(s, false);
if (r < 0)
return r;
return 1;
}
static void stdout_stream_free(StdoutStream *s) {
assert(s);
if (s->server) {
s->server->n_stdout_streams --;
}
if (s->fd >= 0) {
if (s->server)
close_nointr_nofail(s->fd);
}
#ifdef HAVE_SELINUX
if (s->security_context)
freecon(s->security_context);
#endif
free(s->identifier);
free(s);
}
static int stdout_stream_new(Server *s) {
int fd, r;
struct epoll_event ev;
assert(s);
if (fd < 0) {
return 0;
log_error("Failed to accept stdout connection: %m");
return -errno;
}
if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
log_warning("Too many stdout streams, refusing connection.");
return 0;
}
if (!stream) {
return log_oom();
}
log_error("Failed to determine peer credentials: %m");
r = -errno;
goto fail;
}
#ifdef HAVE_SELINUX
log_error("Failed to determine peer security context: %m");
#endif
log_error("Failed to shutdown writing side of socket: %m");
r = -errno;
goto fail;
}
log_error("Failed to add stream to event loop: %m");
r = -errno;
goto fail;
}
s->n_stdout_streams ++;
return 0;
fail:
return r;
}
pid_t t;
return false;
return t == getpid();
}
char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL;
int priority, r;
unsigned n = 0, z = 0, j;
assert(s);
assert(p);
if (l <= 0)
return;
e = memchr(p, ',', l);
if (!e)
return;
*e = 0;
return;
return;
l -= (e - p) + 1;
p = e + 1;
e = memchr(p, ',', l);
if (!e)
return;
*e = 0;
r = safe_atou64(p, &serial);
if (r < 0)
return;
if (s->kernel_seqnum) {
/* We already read this one? */
if (serial < *s->kernel_seqnum)
return;
/* Did we lose any? */
if (serial > *s->kernel_seqnum)
driver_message(s, SD_MESSAGE_JOURNAL_MISSED, "Missed %llu kernel messages", (unsigned long long) serial - *s->kernel_seqnum - 1);
/* Make sure we never read this one again. Note that
* we always store the next message serial we expect
* here, simply because this makes handling the first
* message with serial 0 easy. */
}
l -= (e - p) + 1;
p = e + 1;
f = memchr(p, ';', l);
if (!f)
return;
/* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
e = memchr(p, ',', l);
if (!e || f < e)
e = f;
*e = 0;
r = parse_usec(p, &usec);
if (r < 0)
return;
l -= (f - p) + 1;
p = f + 1;
e = memchr(p, '\n', l);
if (!e)
return;
*e = 0;
pl = e - p;
l -= (e - p) + 1;
k = e + 1;
for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) {
char *m;
/* Meta data fields attached */
if (*k != ' ')
break;
k ++, l --;
e = memchr(k, '\n', l);
if (!e)
return;
*e = 0;
m = cunescape_length_with_prefix(k, e - k, "_KERNEL_");
if (!m)
break;
IOVEC_SET_STRING(iovec[n++], m);
z++;
l -= (e - k) + 1;
k = e + 1;
}
(unsigned long long) usec) >= 0)
else {
/* Avoid any messages we generated ourselves via
* log_info() and friends. */
goto finish;
if (identifier) {
if (syslog_identifier)
}
if (pid) {
if (syslog_pid)
}
}
if (message)
for (j = 0; j < z; j++)
}
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->system_metrics, 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 */
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->runtime_metrics, NULL, &s->runtime_journal);
if (r < 0) {
return r;
}
}
if (s->runtime_journal) {
server_fix_perms(s, s->runtime_journal, 0);
}
}
return r;
}
static int server_flush_to_var(Server *s) {
int r;
sd_journal *j;
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_info("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 == -E2BIG) {
log_info("Allocation limit reached.");
server_rotate(s);
server_vacuum(s);
}
if (r < 0) {
goto finish;
}
}
s->runtime_journal = NULL;
if (r >= 0)
return r;
}
static int server_read_dev_kmsg(Server *s) {
ssize_t l;
assert(s);
assert(s->dev_kmsg_fd >= 0);
if (l == 0)
return 0;
if (l < 0) {
* return EINVAL when we try. So handle this cleanly,
* but don' try to ever read from it again. */
return 0;
}
return 0;
log_error("Failed to read from kernel: %m");
return -errno;
}
dev_kmsg_record(s, buffer, l);
return 1;
}
static int server_flush_dev_kmsg(Server *s) {
int r;
assert(s);
if (s->dev_kmsg_fd < 0)
return 0;
if (!s->dev_kmsg_readable)
return 0;
for (;;) {
r = server_read_dev_kmsg(s);
if (r < 0)
return r;
if (r == 0)
break;
}
return 0;
}
assert(s);
struct signalfd_siginfo sfsi;
ssize_t n;
log_info("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_info("Got invalid event from epoll.");
return -EIO;
}
r = server_read_dev_kmsg(s);
if (r < 0)
return r;
return 1;
log_info("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_info("Got invalid event from epoll.");
return -EIO;
}
return 1;
} else {
log_info("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_syslog_socket(Server *s) {
union sockaddr_union sa;
int one, r;
struct epoll_event ev;
assert(s);
if (s->syslog_fd < 0) {
if (s->syslog_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
} else
one = 1;
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
#ifdef HAVE_SELINUX
one = 1;
if (r < 0)
log_warning("SO_PASSSEC failed: %m");
#endif
one = 1;
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
log_error("Failed to add syslog server fd to epoll object: %m");
return -errno;
}
return 0;
}
static int open_native_socket(Server*s) {
union sockaddr_union sa;
int one, r;
struct epoll_event ev;
assert(s);
if (s->native_fd < 0) {
if (s->native_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
} else
one = 1;
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
#ifdef HAVE_SELINUX
one = 1;
if (r < 0)
log_warning("SO_PASSSEC failed: %m");
#endif
one = 1;
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
log_error("Failed to add native server fd to epoll object: %m");
return -errno;
}
return 0;
}
static int open_stdout_socket(Server *s) {
union sockaddr_union sa;
int r;
struct epoll_event ev;
assert(s);
if (s->stdout_fd < 0) {
if (s->stdout_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
if (r < 0) {
log_error("bind() failed: %m");
return -errno;
}
log_error("liste() failed: %m");
return -errno;
}
} else
log_error("Failed to add stdout server fd to epoll object: %m");
return -errno;
}
return 0;
}
static int open_dev_kmsg(Server *s) {
struct epoll_event ev;
assert(s);
if (s->dev_kmsg_fd < 0) {
log_warning("Failed to open /dev/kmsg, ignoring: %m");
return 0;
}
/* This will fail with EPERM on older kernels where
return 0;
return -errno;
}
s->dev_kmsg_readable = true;
return 0;
}
static int open_kernel_seqnum(Server *s) {
int fd;
uint64_t *p;
assert(s);
/* We store the seqnum we last read in an mmaped file. That
* way we can just use it like a variable, but it is
* persistant and automatically flushed at reboot. */
fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
if (fd < 0) {
log_error("Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
return 0;
}
log_error("Failed to allocate sequential number file, ignoring: %m");
return 0;
}
if (p == MAP_FAILED) {
log_error("Failed to map sequential number file, ignoring: %m");
return 0;
}
s->kernel_seqnum = p;
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) {
int r;
size_t l;
if (detect_container(NULL) > 0)
return 0;
if (r < 0) {
return 0;
}
char *word;
if (!word) {
r = -ENOMEM;
goto finish;
}
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.");
}
r = 0;
return r;
}
static int server_parse_config_file(Server *s) {
FILE *f;
const char *fn;
int r;
assert(s);
fn = "/etc/systemd/journald.conf";
if (!f) {
return 0;
return -errno;
}
r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
if (r < 0)
fclose(f);
return r;
}
static int server_init(Server *s) {
int n, r, fd;
assert(s);
zero(*s);
s->compress = 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->user_journals)
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 = open_syslog_socket(s);
if (r < 0)
return r;
r = open_native_socket(s);
if (r < 0)
return r;
r = open_stdout_socket(s);
if (r < 0)
return r;
r = open_dev_kmsg(s);
if (r < 0)
return r;
r = open_kernel_seqnum(s);
if (r < 0)
return r;
r = open_signalfd(s);
if (r < 0)
return r;
if (!s->rate_limit)
return -ENOMEM;
r = system_journal_open(s);
if (r < 0)
return r;
return 0;
}
static 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)
}
int r;
/* if (getppid() != 1) { */
/* log_error("This program should be invoked by init only."); */
/* return EXIT_FAILURE; */
/* } */
if (argc > 1) {
log_error("This program does not take arguments.");
return EXIT_FAILURE;
}
log_open();
umask(0022);
r = server_init(&server);
if (r < 0)
goto finish;
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
for (;;) {
struct epoll_event event;
if (r < 0) {
continue;
log_error("epoll_wait() failed: %m");
r = -errno;
goto finish;
} else if (r == 0)
break;
if (r < 0)
goto finish;
else if (r == 0)
break;
}
sd_notify(false,
"STATUS=Shutting down...");
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}