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;
50 static void fix_perms(JournalFile *f, uid_t uid) {
53 acl_permset_t permset;
58 r = fchmod_and_fchown(f->fd, 0640, 0, 0);
60 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
65 acl = acl_get_fd(f->fd);
67 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
71 r = acl_find_uid(acl, uid, &entry);
74 if (acl_create_entry(&acl, &entry) < 0 ||
75 acl_set_tag_type(entry, ACL_USER) < 0 ||
76 acl_set_qualifier(entry, &uid) < 0) {
77 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
82 if (acl_get_permset(entry, &permset) < 0 ||
83 acl_add_perm(permset, ACL_READ) < 0 ||
84 acl_calc_mask(&acl) < 0) {
85 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
89 if (acl_set_fd(f->fd, acl) < 0)
90 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
96 static JournalFile* find_journal(Server *s, uid_t uid) {
105 /* We split up user logs only on /var, not on /run */
106 if (!s->system_journal)
107 return s->runtime_journal;
110 return s->system_journal;
112 r = sd_id128_get_machine(&machine);
114 return s->system_journal;
116 f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
120 if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
121 return s->system_journal;
123 r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
127 return s->system_journal;
131 r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
133 journal_file_close(f);
134 return s->system_journal;
140 static void process_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
141 char *message = NULL, *pid = NULL, *uid = NULL, *gid = NULL,
142 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
143 *comm = NULL, *cmdline = NULL, *hostname = NULL,
144 *audit_session = NULL, *audit_loginuid = NULL,
145 *syslog_priority = NULL, *syslog_facility = NULL,
146 *exe = NULL, *cgroup = NULL;
147 struct iovec iovec[16];
153 int priority = LOG_USER | LOG_INFO;
157 parse_syslog_priority((char**) &buf, &priority);
158 skip_syslog_date((char**) &buf);
160 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
161 IOVEC_SET_STRING(iovec[n++], syslog_priority);
163 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
164 IOVEC_SET_STRING(iovec[n++], syslog_facility);
166 message = strappend("MESSAGE=", buf);
168 IOVEC_SET_STRING(iovec[n++], message);
174 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
175 IOVEC_SET_STRING(iovec[n++], pid);
177 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
178 IOVEC_SET_STRING(iovec[n++], uid);
180 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
181 IOVEC_SET_STRING(iovec[n++], gid);
183 r = get_process_comm(ucred->pid, &t);
185 comm = strappend("COMM=", t);
187 IOVEC_SET_STRING(iovec[n++], comm);
191 r = get_process_exe(ucred->pid, &t);
193 exe = strappend("EXE=", t);
195 IOVEC_SET_STRING(iovec[n++], exe);
199 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
201 cmdline = strappend("CMDLINE=", t);
203 IOVEC_SET_STRING(iovec[n++], cmdline);
207 r = audit_session_from_pid(ucred->pid, &session);
209 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
210 IOVEC_SET_STRING(iovec[n++], audit_session);
212 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
214 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
215 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
217 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, ucred->pid, &path);
219 cgroup = strappend("SYSTEMD_CGROUP=", path);
221 IOVEC_SET_STRING(iovec[n++], cgroup);
227 if (asprintf(&source_time, "SOURCE_REALTIME_TIMESTAMP=%llu",
228 (unsigned long long) timeval_load(tv)) >= 0)
229 IOVEC_SET_STRING(iovec[n++], source_time);
232 /* Note that strictly speaking storing the boot id here is
233 * redundant since the entry includes this in-line
234 * anyway. However, we need this indexed, too. */
235 r = sd_id128_get_boot(&id);
237 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
238 IOVEC_SET_STRING(iovec[n++], boot_id);
240 r = sd_id128_get_machine(&id);
242 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
243 IOVEC_SET_STRING(iovec[n++], machine_id);
245 t = gethostname_malloc();
247 hostname = strappend("HOSTNAME=", t);
249 IOVEC_SET_STRING(iovec[n++], hostname);
253 f = find_journal(s, loginuid);
255 log_warning("Dropping message, as we can't find a place to store the data.");
257 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
260 log_error("Failed to write entry, ignoring: %s", strerror(-r));
275 free(audit_loginuid);
276 free(syslog_facility);
277 free(syslog_priority);
281 static int process_event(Server *s, struct epoll_event *ev) {
284 if (ev->events != EPOLLIN) {
285 log_info("Got invalid event from epoll.");
289 if (ev->data.fd == s->signal_fd) {
290 struct signalfd_siginfo sfsi;
293 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
294 if (n != sizeof(sfsi)) {
299 if (errno == EINTR || errno == EAGAIN)
305 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
310 if (ev->data.fd == s->syslog_fd) {
312 char buf[LINE_MAX+1];
313 struct msghdr msghdr;
315 struct ucred *ucred = NULL;
316 struct timeval *tv = NULL;
317 struct cmsghdr *cmsg;
319 struct cmsghdr cmsghdr;
320 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
321 CMSG_SPACE(sizeof(struct timeval))];
327 iovec.iov_base = buf;
328 iovec.iov_len = sizeof(buf)-1;
332 msghdr.msg_iov = &iovec;
333 msghdr.msg_iovlen = 1;
334 msghdr.msg_control = &control;
335 msghdr.msg_controllen = sizeof(control);
337 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
340 if (errno == EINTR || errno == EAGAIN)
343 log_error("recvmsg() failed: %m");
347 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
349 if (cmsg->cmsg_level == SOL_SOCKET &&
350 cmsg->cmsg_type == SCM_CREDENTIALS &&
351 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
352 ucred = (struct ucred*) CMSG_DATA(cmsg);
353 else if (cmsg->cmsg_level == SOL_SOCKET &&
354 cmsg->cmsg_type == SO_TIMESTAMP &&
355 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
356 tv = (struct timeval*) CMSG_DATA(cmsg);
359 e = memchr(buf, '\n', n);
365 process_message(s, strstrip(buf), ucred, tv);
371 log_error("Unknown event.");
375 static int system_journal_open(Server *s) {
381 r = sd_id128_get_machine(&machine);
385 /* First try to create the machine path, but not the prefix */
386 fn = strappend("/var/log/journal/", sd_id128_to_string(machine, ids));
389 (void) mkdir(fn, 0755);
392 /* The create the system journal file */
393 fn = join("/var/log/journal/", ids, "/system.journal", NULL);
397 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
401 fix_perms(s->system_journal, 0);
405 if (r < 0 && r != -ENOENT) {
406 log_error("Failed to open system journal: %s", strerror(-r));
410 /* /var didn't work, so try /run, but this time we
411 * create the prefix too */
412 fn = join("/run/log/journal/", ids, "/system.journal", NULL);
416 (void) mkdir_parents(fn, 0755);
417 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
421 log_error("Failed to open runtime journal: %s", strerror(-r));
425 fix_perms(s->runtime_journal, 0);
429 static int server_init(Server *s) {
431 struct epoll_event ev;
437 s->syslog_fd = s->signal_fd = -1;
439 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
440 if (s->epoll_fd < 0) {
441 log_error("Failed to create epoll object: %m");
445 n = sd_listen_fds(true);
447 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
452 log_error("Too many file descriptors passed.");
457 s->syslog_fd = SD_LISTEN_FDS_START;
459 union sockaddr_union sa;
461 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
462 if (s->syslog_fd < 0) {
463 log_error("socket() failed: %m");
468 sa.un.sun_family = AF_UNIX;
469 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
471 unlink(sa.un.sun_path);
473 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
475 log_error("bind() failed: %m");
479 chmod(sa.un.sun_path, 0666);
483 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
485 log_error("SO_PASSCRED failed: %m");
490 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
492 log_error("SO_TIMESTAMP failed: %m");
498 ev.data.fd = s->syslog_fd;
499 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
500 log_error("Failed to add server fd to epoll object: %m");
504 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
505 if (!s->user_journals) {
506 log_error("Out of memory.");
510 r = system_journal_open(s);
514 assert_se(sigemptyset(&mask) == 0);
515 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
516 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
518 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
519 if (s->signal_fd < 0) {
520 log_error("signalfd(): %m");
526 ev.data.fd = s->signal_fd;
528 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
529 log_error("epoll_ctl(): %m");
536 static void server_done(Server *s) {
540 if (s->system_journal)
541 journal_file_close(s->system_journal);
543 if (s->runtime_journal)
544 journal_file_close(s->runtime_journal);
546 while ((f = hashmap_steal_first(s->user_journals)))
547 journal_file_close(f);
549 hashmap_free(s->user_journals);
551 if (s->epoll_fd >= 0)
552 close_nointr_nofail(s->epoll_fd);
554 if (s->signal_fd >= 0)
555 close_nointr_nofail(s->signal_fd);
557 if (s->syslog_fd >= 0)
558 close_nointr_nofail(s->syslog_fd);
561 int main(int argc, char *argv[]) {
565 /* if (getppid() != 1) { */
566 /* log_error("This program should be invoked by init only."); */
567 /* return EXIT_FAILURE; */
571 log_error("This program does not take arguments.");
575 log_set_target(LOG_TARGET_CONSOLE);
576 log_parse_environment();
581 r = server_init(&server);
585 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
589 "STATUS=Processing messages...");
592 struct epoll_event event;
594 r = epoll_wait(server.epoll_fd, &event, 1, -1);
600 log_error("epoll_wait() failed: %m");
606 r = process_event(&server, &event);
615 "STATUS=Shutting down...");
617 server_done(&server);
619 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;