1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/epoll.h>
23 #include <sys/socket.h>
25 #include <sys/signalfd.h>
29 #include <acl/libacl.h>
32 #include "journal-private.h"
33 #include "sd-daemon.h"
34 #include "socket-util.h"
37 typedef struct Server {
42 JournalFile *runtime_journal;
43 JournalFile *system_journal;
44 Hashmap *user_journals;
47 static void fix_perms(JournalFile *f, uid_t uid) {
50 acl_permset_t permset;
55 r = fchmod_and_fchown(f->fd, 0640, 0, 0);
57 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
62 acl = acl_get_fd(f->fd);
64 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
68 r = acl_find_uid(acl, uid, &entry);
71 if (acl_create_entry(&acl, &entry) < 0 ||
72 acl_set_tag_type(entry, ACL_USER) < 0 ||
73 acl_set_qualifier(entry, &uid) < 0) {
74 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
79 if (acl_get_permset(entry, &permset) < 0 ||
80 acl_add_perm(permset, ACL_READ) < 0 ||
81 acl_calc_mask(&acl) < 0) {
82 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
86 if (acl_set_fd(f->fd, acl) < 0)
87 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
93 static JournalFile* find_journal(Server *s, uid_t uid) {
100 /* We split up user logs only on /var, not on /run */
101 if (!s->system_journal)
102 return s->runtime_journal;
105 return s->system_journal;
107 f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
111 if (asprintf(&p, "/var/log/journal/%lu.journal", (unsigned long) uid) < 0)
112 return s->system_journal;
114 r = journal_file_open(p, O_RDWR|O_CREAT, 0640, &f);
118 return s->system_journal;
122 r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
124 journal_file_close(f);
125 return s->system_journal;
131 static void process_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
132 char *message = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
133 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
134 *comm = NULL, *cmdline = NULL, *hostname = NULL,
135 *audit_session = NULL, *audit_loginuid = NULL,
136 *syslog_priority = NULL, *syslog_facility = NULL,
138 struct iovec iovec[15];
144 int priority = LOG_USER | LOG_INFO;
148 parse_syslog_priority((char**) &buf, &priority);
149 skip_syslog_date((char**) &buf);
151 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
152 IOVEC_SET_STRING(iovec[n++], syslog_priority);
154 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
155 IOVEC_SET_STRING(iovec[n++], syslog_facility);
157 message = strappend("MESSAGE=", buf);
159 IOVEC_SET_STRING(iovec[n++], message);
164 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
165 IOVEC_SET_STRING(iovec[n++], pid);
167 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
168 IOVEC_SET_STRING(iovec[n++], uid);
170 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
171 IOVEC_SET_STRING(iovec[n++], gid);
173 r = get_process_comm(ucred->pid, &t);
175 comm = strappend("COMM=", t);
177 IOVEC_SET_STRING(iovec[n++], comm);
181 r = get_process_exe(ucred->pid, &t);
183 exe = strappend("EXE=", t);
185 IOVEC_SET_STRING(iovec[n++], exe);
189 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
191 cmdline = strappend("CMDLINE=", t);
193 IOVEC_SET_STRING(iovec[n++], cmdline);
197 r = audit_session_from_pid(ucred->pid, &session);
199 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
200 IOVEC_SET_STRING(iovec[n++], audit_session);
202 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
204 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
205 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
209 if (asprintf(&source_time, "SOURCE_REALTIME_TIMESTAMP=%llu",
210 (unsigned long long) timeval_load(tv)) >= 0)
211 IOVEC_SET_STRING(iovec[n++], source_time);
214 r = sd_id128_get_boot(&id);
216 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
217 IOVEC_SET_STRING(iovec[n++], boot_id);
219 r = sd_id128_get_machine(&id);
221 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
222 IOVEC_SET_STRING(iovec[n++], machine_id);
224 t = gethostname_malloc();
226 hostname = strappend("HOSTNAME=", t);
228 IOVEC_SET_STRING(iovec[n++], hostname);
232 f = find_journal(s, loginuid);
234 log_warning("Dropping message, as we can't find a place to store the data.");
236 r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
239 log_error("Failed to write entry, ignoring: %s", strerror(-r));
253 free(audit_loginuid);
254 free(syslog_facility);
255 free(syslog_priority);
258 static int process_event(Server *s, struct epoll_event *ev) {
261 if (ev->events != EPOLLIN) {
262 log_info("Got invalid event from epoll.");
266 if (ev->data.fd == s->signal_fd) {
267 struct signalfd_siginfo sfsi;
270 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
271 if (n != sizeof(sfsi)) {
276 if (errno == EINTR || errno == EAGAIN)
282 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
287 char buf[LINE_MAX+1];
288 struct msghdr msghdr;
290 struct ucred *ucred = NULL;
291 struct timeval *tv = NULL;
292 struct cmsghdr *cmsg;
294 struct cmsghdr cmsghdr;
295 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
296 CMSG_SPACE(sizeof(struct timeval))];
302 iovec.iov_base = buf;
303 iovec.iov_len = sizeof(buf)-1;
307 msghdr.msg_iov = &iovec;
308 msghdr.msg_iovlen = 1;
309 msghdr.msg_control = &control;
310 msghdr.msg_controllen = sizeof(control);
312 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
315 if (errno == EINTR || errno == EAGAIN)
318 log_error("recvmsg() failed: %m");
322 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
324 if (cmsg->cmsg_level == SOL_SOCKET &&
325 cmsg->cmsg_type == SCM_CREDENTIALS &&
326 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
327 ucred = (struct ucred*) CMSG_DATA(cmsg);
328 else if (cmsg->cmsg_level == SOL_SOCKET &&
329 cmsg->cmsg_type == SO_TIMESTAMP &&
330 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
331 tv = (struct timeval*) CMSG_DATA(cmsg);
334 e = memchr(buf, '\n', n);
340 process_message(s, strstrip(buf), ucred, tv);
347 static int server_init(Server *s) {
349 struct epoll_event ev;
355 s->syslog_fd = s->signal_fd = -1;
357 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
358 if (s->epoll_fd < 0) {
359 log_error("Failed to create epoll object: %m");
363 n = sd_listen_fds(true);
365 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
370 log_error("Too many file descriptors passed.");
375 s->syslog_fd = SD_LISTEN_FDS_START;
377 union sockaddr_union sa;
379 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
380 if (s->syslog_fd < 0) {
381 log_error("socket() failed: %m");
386 sa.un.sun_family = AF_UNIX;
387 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
389 unlink(sa.un.sun_path);
391 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
393 log_error("bind() failed: %m");
397 chmod(sa.un.sun_path, 0666);
401 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
403 log_error("SO_PASSCRED failed: %m");
408 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
410 log_error("SO_TIMESTAMP failed: %m");
416 ev.data.fd = s->syslog_fd;
417 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
418 log_error("Failed to add server fd to epoll object: %m");
422 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
423 if (!s->user_journals) {
424 log_error("Out of memory.");
428 r = journal_file_open("/var/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->system_journal);
430 fix_perms(s->system_journal, 0);
431 else if (r == -ENOENT) {
432 mkdir_p("/run/log/journal", 0755);
434 r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->runtime_journal);
436 fix_perms(s->runtime_journal, 0);
439 if (r < 0 && r != -ENOENT) {
440 log_error("Failed to open journal: %s", strerror(-r));
444 assert_se(sigemptyset(&mask) == 0);
445 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
446 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
448 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
449 if (s->signal_fd < 0) {
450 log_error("signalfd(): %m");
456 ev.data.fd = s->signal_fd;
458 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
459 log_error("epoll_ctl(): %m");
466 static void server_done(Server *s) {
470 if (s->system_journal)
471 journal_file_close(s->system_journal);
473 if (s->runtime_journal)
474 journal_file_close(s->runtime_journal);
476 while ((f = hashmap_steal_first(s->user_journals)))
477 journal_file_close(f);
479 hashmap_free(s->user_journals);
481 if (s->epoll_fd >= 0)
482 close_nointr_nofail(s->epoll_fd);
484 if (s->signal_fd >= 0)
485 close_nointr_nofail(s->signal_fd);
487 if (s->syslog_fd >= 0)
488 close_nointr_nofail(s->syslog_fd);
491 int main(int argc, char *argv[]) {
495 /* if (getppid() != 1) { */
496 /* log_error("This program should be invoked by init only."); */
497 /* return EXIT_FAILURE; */
501 log_error("This program does not take arguments.");
505 log_set_target(LOG_TARGET_CONSOLE);
506 log_parse_environment();
511 r = server_init(&server);
515 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
519 "STATUS=Processing messages...");
522 struct epoll_event event;
524 r = epoll_wait(server.epoll_fd, &event, 1, -1);
530 log_error("epoll_wait() failed: %m");
536 r = process_event(&server, &event);
545 "STATUS=Shutting down...");
547 server_done(&server);
549 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;