journald.c revision f4b4781191e8edfb5690e4447166e3ba7bcb48f5
/*-*- 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 "hashmap.h"
#include "journal-private.h"
#include "sd-daemon.h"
#include "socket-util.h"
#include "acl-util.h"
typedef struct Server {
int syslog_fd;
int epoll_fd;
int signal_fd;
} 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;
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;
if (f)
return f;
return s->system_journal;
free(p);
if (r < 0)
return s->system_journal;
if (r < 0) {
return s->system_journal;
}
return f;
}
unsigned n = 0;
char idbuf[33];
int r;
char *t;
JournalFile *f;
skip_syslog_date((char**) &buf);
if (message)
if (ucred) {
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 (tv) {
(unsigned long long) timeval_load(tv)) >= 0)
}
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);
}
f = find_journal(s, loginuid);
if (!f)
log_warning("Dropping message, as we can't find a place to store the data.");
else {
if (r < 0)
}
}
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;
} else {
for (;;) {
union {
CMSG_SPACE(sizeof(struct timeval))];
} control;
ssize_t n;
char *e;
if (n < 0) {
return 1;
log_error("recvmsg() failed: %m");
return -errno;
}
}
if (e)
*e = 0;
else
buf[n] = 0;
}
}
return 1;
}
static int server_init(Server *s) {
int n, one, r;
struct epoll_event ev;
assert(s);
zero(*s);
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 (n > 1) {
log_error("Too many file descriptors passed.");
return -EINVAL;
}
if (n == 1)
s->syslog_fd = SD_LISTEN_FDS_START;
else {
union sockaddr_union sa;
if (s->syslog_fd < 0) {
log_error("socket() failed: %m");
return -errno;
}
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;
}
log_error("Failed to add server fd to epoll object: %m");
return -errno;
}
if (!s->user_journals) {
log_error("Out of memory.");
return -ENOMEM;
}
if (r >= 0)
fix_perms(s->system_journal, 0);
else if (r == -ENOENT) {
r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->runtime_journal);
if (r >= 0)
fix_perms(s->runtime_journal, 0);
}
if (r < 0 && r != -ENOENT) {
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)
}
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;
}