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-file.h"
33 #include "sd-daemon.h"
34 #include "socket-util.h"
36 #include "cgroup-util.h"
38 typedef struct Server {
43 JournalFile *runtime_journal;
44 JournalFile *system_journal;
45 Hashmap *user_journals;
48 static void fix_perms(JournalFile *f, uid_t uid) {
51 acl_permset_t permset;
56 r = fchmod_and_fchown(f->fd, 0640, 0, 0);
58 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
63 acl = acl_get_fd(f->fd);
65 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
69 r = acl_find_uid(acl, uid, &entry);
72 if (acl_create_entry(&acl, &entry) < 0 ||
73 acl_set_tag_type(entry, ACL_USER) < 0 ||
74 acl_set_qualifier(entry, &uid) < 0) {
75 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
80 if (acl_get_permset(entry, &permset) < 0 ||
81 acl_add_perm(permset, ACL_READ) < 0 ||
82 acl_calc_mask(&acl) < 0) {
83 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
87 if (acl_set_fd(f->fd, acl) < 0)
88 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
94 static JournalFile* find_journal(Server *s, uid_t uid) {
101 /* We split up user logs only on /var, not on /run */
102 if (!s->system_journal)
103 return s->runtime_journal;
106 return s->system_journal;
108 f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
112 if (asprintf(&p, "/var/log/journal/%lu.journal", (unsigned long) uid) < 0)
113 return s->system_journal;
115 r = journal_file_open(p, O_RDWR|O_CREAT, 0640, &f);
119 return s->system_journal;
123 r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
125 journal_file_close(f);
126 return s->system_journal;
132 static void process_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
133 char *message = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
134 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
135 *comm = NULL, *cmdline = NULL, *hostname = NULL,
136 *audit_session = NULL, *audit_loginuid = NULL,
137 *syslog_priority = NULL, *syslog_facility = NULL,
138 *exe = NULL, *cgroup = NULL;
139 struct iovec iovec[16];
145 int priority = LOG_USER | LOG_INFO;
149 parse_syslog_priority((char**) &buf, &priority);
150 skip_syslog_date((char**) &buf);
152 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
153 IOVEC_SET_STRING(iovec[n++], syslog_priority);
155 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
156 IOVEC_SET_STRING(iovec[n++], syslog_facility);
158 message = strappend("MESSAGE=", buf);
160 IOVEC_SET_STRING(iovec[n++], message);
166 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
167 IOVEC_SET_STRING(iovec[n++], pid);
169 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
170 IOVEC_SET_STRING(iovec[n++], uid);
172 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
173 IOVEC_SET_STRING(iovec[n++], gid);
175 r = get_process_comm(ucred->pid, &t);
177 comm = strappend("COMM=", t);
179 IOVEC_SET_STRING(iovec[n++], comm);
183 r = get_process_exe(ucred->pid, &t);
185 exe = strappend("EXE=", t);
187 IOVEC_SET_STRING(iovec[n++], exe);
191 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
193 cmdline = strappend("CMDLINE=", t);
195 IOVEC_SET_STRING(iovec[n++], cmdline);
199 r = audit_session_from_pid(ucred->pid, &session);
201 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
202 IOVEC_SET_STRING(iovec[n++], audit_session);
204 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
206 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
207 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
209 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, ucred->pid, &path);
211 cgroup = strappend("SYSTEMD_CGROUP=", path);
213 IOVEC_SET_STRING(iovec[n++], cgroup);
219 if (asprintf(&source_time, "SOURCE_REALTIME_TIMESTAMP=%llu",
220 (unsigned long long) timeval_load(tv)) >= 0)
221 IOVEC_SET_STRING(iovec[n++], source_time);
224 /* Note that strictly speaking storing the boot id here is
225 * redundant since the entry includes this in-line
226 * anyway. However, we need this indexed, too. */
227 r = sd_id128_get_boot(&id);
229 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
230 IOVEC_SET_STRING(iovec[n++], boot_id);
232 r = sd_id128_get_machine(&id);
234 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
235 IOVEC_SET_STRING(iovec[n++], machine_id);
237 t = gethostname_malloc();
239 hostname = strappend("HOSTNAME=", t);
241 IOVEC_SET_STRING(iovec[n++], hostname);
245 f = find_journal(s, loginuid);
247 log_warning("Dropping message, as we can't find a place to store the data.");
249 r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
252 log_error("Failed to write entry, ignoring: %s", strerror(-r));
267 free(audit_loginuid);
268 free(syslog_facility);
269 free(syslog_priority);
273 static int process_event(Server *s, struct epoll_event *ev) {
276 if (ev->events != EPOLLIN) {
277 log_info("Got invalid event from epoll.");
281 if (ev->data.fd == s->signal_fd) {
282 struct signalfd_siginfo sfsi;
285 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
286 if (n != sizeof(sfsi)) {
291 if (errno == EINTR || errno == EAGAIN)
297 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
302 if (ev->data.fd == s->syslog_fd) {
304 char buf[LINE_MAX+1];
305 struct msghdr msghdr;
307 struct ucred *ucred = NULL;
308 struct timeval *tv = NULL;
309 struct cmsghdr *cmsg;
311 struct cmsghdr cmsghdr;
312 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
313 CMSG_SPACE(sizeof(struct timeval))];
319 iovec.iov_base = buf;
320 iovec.iov_len = sizeof(buf)-1;
324 msghdr.msg_iov = &iovec;
325 msghdr.msg_iovlen = 1;
326 msghdr.msg_control = &control;
327 msghdr.msg_controllen = sizeof(control);
329 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
332 if (errno == EINTR || errno == EAGAIN)
335 log_error("recvmsg() failed: %m");
339 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
341 if (cmsg->cmsg_level == SOL_SOCKET &&
342 cmsg->cmsg_type == SCM_CREDENTIALS &&
343 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
344 ucred = (struct ucred*) CMSG_DATA(cmsg);
345 else if (cmsg->cmsg_level == SOL_SOCKET &&
346 cmsg->cmsg_type == SO_TIMESTAMP &&
347 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
348 tv = (struct timeval*) CMSG_DATA(cmsg);
351 e = memchr(buf, '\n', n);
357 process_message(s, strstrip(buf), ucred, tv);
363 log_error("Unknown event.");
367 static int system_journal_open(Server *s) {
373 r = sd_id128_get_machine(&machine);
377 /* First try to create the machine path, but not the prefix */
378 fn = strappend("/var/log/journal/", sd_id128_to_string(machine, ids));
381 (void) mkdir(fn, 0755);
384 /* The create the system journal file */
385 fn = join("/var/log/journal/", ids, "/system.journal", NULL);
389 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, &s->system_journal);
393 fix_perms(s->system_journal, 0);
397 if (r < 0 && r != -ENOENT) {
398 log_error("Failed to open system journal: %s", strerror(-r));
402 /* /var didn't work, so try /run, but this time we
403 * create the prefix too */
404 fn = strappend("/run/log/journal/", ids);
407 (void) mkdir_p(fn, 0755);
410 /* Then create the runtime journal file */
411 fn = join("/run/log/journal/", ids, "/system.journal", NULL);
414 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, &s->runtime_journal);
418 log_error("Failed to open runtime journal: %s", strerror(-r));
422 fix_perms(s->runtime_journal, 0);
426 static int server_init(Server *s) {
428 struct epoll_event ev;
434 s->syslog_fd = s->signal_fd = -1;
436 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
437 if (s->epoll_fd < 0) {
438 log_error("Failed to create epoll object: %m");
442 n = sd_listen_fds(true);
444 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
449 log_error("Too many file descriptors passed.");
454 s->syslog_fd = SD_LISTEN_FDS_START;
456 union sockaddr_union sa;
458 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
459 if (s->syslog_fd < 0) {
460 log_error("socket() failed: %m");
465 sa.un.sun_family = AF_UNIX;
466 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
468 unlink(sa.un.sun_path);
470 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
472 log_error("bind() failed: %m");
476 chmod(sa.un.sun_path, 0666);
480 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
482 log_error("SO_PASSCRED failed: %m");
487 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
489 log_error("SO_TIMESTAMP failed: %m");
495 ev.data.fd = s->syslog_fd;
496 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
497 log_error("Failed to add server fd to epoll object: %m");
501 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
502 if (!s->user_journals) {
503 log_error("Out of memory.");
507 r = system_journal_open(s);
511 assert_se(sigemptyset(&mask) == 0);
512 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
513 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
515 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
516 if (s->signal_fd < 0) {
517 log_error("signalfd(): %m");
523 ev.data.fd = s->signal_fd;
525 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
526 log_error("epoll_ctl(): %m");
533 static void server_done(Server *s) {
537 if (s->system_journal)
538 journal_file_close(s->system_journal);
540 if (s->runtime_journal)
541 journal_file_close(s->runtime_journal);
543 while ((f = hashmap_steal_first(s->user_journals)))
544 journal_file_close(f);
546 hashmap_free(s->user_journals);
548 if (s->epoll_fd >= 0)
549 close_nointr_nofail(s->epoll_fd);
551 if (s->signal_fd >= 0)
552 close_nointr_nofail(s->signal_fd);
554 if (s->syslog_fd >= 0)
555 close_nointr_nofail(s->syslog_fd);
558 int main(int argc, char *argv[]) {
562 /* if (getppid() != 1) { */
563 /* log_error("This program should be invoked by init only."); */
564 /* return EXIT_FAILURE; */
568 log_error("This program does not take arguments.");
572 log_set_target(LOG_TARGET_CONSOLE);
573 log_parse_environment();
578 r = server_init(&server);
582 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
586 "STATUS=Processing messages...");
589 struct epoll_event event;
591 r = epoll_wait(server.epoll_fd, &event, 1, -1);
597 log_error("epoll_wait() failed: %m");
603 r = process_event(&server, &event);
612 "STATUS=Shutting down...");
614 server_done(&server);
616 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;