journald.c revision bc85bfee87e11317fbcd1160c9003860dc6edde9
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU 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 "hashmap.h"
#include "journal-file.h"
#include "sd-daemon.h"
#include "socket-util.h"
#include "acl-util.h"
#include "cgroup-util.h"
#define USER_JOURNALS_MAX 1024
typedef struct Server {
int epoll_fd;
int signal_fd;
int syslog_fd;
int native_fd;
char *buffer;
} Server;
int r;
assert(f);
if (r < 0)
if (uid <= 0)
return;
if (!acl) {
return;
}
if (r <= 0) {
goto finish;
}
}
acl_calc_mask(&acl) < 0) {
goto finish;
}
}
char *p;
int r;
JournalFile *f;
char ids[33];
assert(s);
/* We split up user logs only on /var, not on /run */
if (!s->system_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;
if (r < 0) {
return s->system_journal;
}
return f;
}
static void server_vacuum(Server *s) {
Iterator i;
void *k;
char *p;
char ids[33];
int r;
JournalFile *f;
log_info("Rotating...");
if (s->runtime_journal) {
r = journal_file_rotate(&s->runtime_journal);
if (r < 0)
}
if (s->system_journal) {
r = journal_file_rotate(&s->system_journal);
if (r < 0)
}
HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
r = journal_file_rotate(&f);
if (r < 0)
else
hashmap_replace(s->user_journals, k, f);
}
log_info("Vacuuming...");
r = sd_id128_get_machine(&machine);
if (r < 0) {
return;
}
log_error("Out of memory.");
return;
}
if (r < 0 && r != -ENOENT)
free(p);
log_error("Out of memory.");
return;
}
if (r < 0 && r != -ENOENT)
free(p);
}
static void dispatch_message(Server *s, struct iovec *iovec, unsigned n, unsigned m, struct ucred *ucred, struct timeval *tv) {
char idbuf[33];
int r;
char *t;
JournalFile *f;
bool vacuumed = false;
assert(s);
if (n == 0)
return;
assert(n + 13 <= m);
if (ucred) {
char *path;
if (r >= 0) {
if (comm)
free(t);
}
if (r >= 0) {
if (comm)
free(t);
}
if (r >= 0) {
if (cmdline)
free(t);
}
if (r >= 0)
if (r >= 0)
if (r >= 0) {
if (cgroup)
}
}
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) {
if (hostname)
free(t);
}
assert(n <= m);
if (!f)
log_warning("Dropping message, as we can't find a place to store the data.");
else {
log_info("Allocation limit reached.");
server_vacuum(s);
vacuumed = true;
log_info("Retrying write.");
goto retry;
}
if (r < 0)
}
}
static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
unsigned n = 0;
assert(s);
skip_syslog_date((char**) &buf);
if (message)
}
static void process_native_message(Server *s, const void *buffer, size_t buffer_size, struct ucred *ucred, struct timeval *tv) {
unsigned n = 0, m = 0, j;
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 == '.') {
/* Control command, ignore for now */
remaining -= (e - p) + 1;
p = e + 1;
continue;
}
/* A property follows */
if (n+13 >= m) {
struct iovec *c;
unsigned u;
if (!c) {
log_error("Out of memory");
break;
}
iovec = c;
m = u;
}
q = memchr(p, '=', e - p);
if (q) {
if (p[0] != '_') {
/* If the field name starts with an
* underscore, skip the variable,
* since that indidates a trusted
* field */
n++;
}
remaining -= (e - p) + 1;
p = e + 1;
continue;
} else {
uint64_t l;
char *k;
log_debug("Failed to parse message, ignoring.");
break;
}
l = le64toh(l);
log_debug("Failed to parse message, ignoring.");
break;
}
k = malloc((e - p) + 1 + l);
if (!k) {
log_error("Out of memory");
break;
}
memcpy(k, p, e - p);
k[e - p] = '=';
if (k[0] != '_') {
n++;
} else
free(k);
}
}
for (j = 0; j < n; j++)
}
assert(s);
log_info("Got invalid event from epoll.");
return -EIO;
}
struct signalfd_siginfo sfsi;
ssize_t n;
if (n != sizeof(sfsi)) {
if (n >= 0)
return -EIO;
return 0;
return -errno;
}
return 0;
}
for (;;) {
union {
CMSG_SPACE(sizeof(struct timeval))];
} control;
ssize_t n;
int v;
log_error("SIOCINQ failed: %m");
return -errno;
}
if (v <= 0)
return 1;
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 (e)
*e = 0;
else
s->buffer[n] = 0;
} else
}
return 1;
}
log_error("Unknown event.");
return 0;
}
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;
/* First try to create the machine path, but not the prefix */
if (!fn)
return -ENOMEM;
/* The create the system journal file */
if (!fn)
return -ENOMEM;
if (r >= 0) {
fix_perms(s->system_journal, 0);
return r;
}
if (r < 0 && r != -ENOENT) {
return r;
}
/* /var didn't work, so try /run, but this time we
* create the prefix too */
if (!fn)
return -ENOMEM;
if (r < 0) {
return r;
}
fix_perms(s->runtime_journal, 0);
return r;
}
static int open_syslog_socket(Server *s) {
union sockaddr_union sa;
int one, r;
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;
}
}
one = 1;
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
one = 1;
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
return 0;
}
static int open_native_socket(Server*s) {
union sockaddr_union sa;
int one, r;
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;
}
}
one = 1;
if (r < 0) {
log_error("SO_PASSCRED failed: %m");
return -errno;
}
one = 1;
if (r < 0) {
log_error("SO_TIMESTAMP failed: %m");
return -errno;
}
return 0;
}
static int server_init(Server *s) {
int n, r, fd;
struct epoll_event ev;
assert(s);
zero(*s);
s->max_use = DEFAULT_MAX_USE;
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->syslog_fd >= 0) {
return -EINVAL;
}
if (s->native_fd >= 0) {
log_error("Too many native sockets passed.");
return -EINVAL;
}
} else {
log_error("Unknown socket passed.");
return -EINVAL;
}
}
r = open_syslog_socket(s);
if (r < 0)
return r;
log_error("Failed to add syslog server fd to epoll object: %m");
return -errno;
}
r = open_native_socket(s);
if (r < 0)
return r;
log_error("Failed to add native server fd to epoll object: %m");
return -errno;
}
if (!s->user_journals) {
log_error("Out of memory.");
return -ENOMEM;
}
r = system_journal_open(s);
if (r < 0)
return r;
if (s->signal_fd < 0) {
log_error("signalfd(): %m");
return -errno;
}
log_error("epoll_ctl(): %m");
return -errno;
}
return 0;
}
static void server_done(Server *s) {
JournalFile *f;
assert(s);
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)
}
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 messages...");
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;
}