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"
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 if (ev->data.fd == s->syslog_fd) {
289 char buf[LINE_MAX+1];
290 struct msghdr msghdr;
292 struct ucred *ucred = NULL;
293 struct timeval *tv = NULL;
294 struct cmsghdr *cmsg;
296 struct cmsghdr cmsghdr;
297 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
298 CMSG_SPACE(sizeof(struct timeval))];
304 iovec.iov_base = buf;
305 iovec.iov_len = sizeof(buf)-1;
309 msghdr.msg_iov = &iovec;
310 msghdr.msg_iovlen = 1;
311 msghdr.msg_control = &control;
312 msghdr.msg_controllen = sizeof(control);
314 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
317 if (errno == EINTR || errno == EAGAIN)
320 log_error("recvmsg() failed: %m");
324 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
326 if (cmsg->cmsg_level == SOL_SOCKET &&
327 cmsg->cmsg_type == SCM_CREDENTIALS &&
328 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
329 ucred = (struct ucred*) CMSG_DATA(cmsg);
330 else if (cmsg->cmsg_level == SOL_SOCKET &&
331 cmsg->cmsg_type == SO_TIMESTAMP &&
332 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
333 tv = (struct timeval*) CMSG_DATA(cmsg);
336 e = memchr(buf, '\n', n);
342 process_message(s, strstrip(buf), ucred, tv);
348 log_error("Unknown event.");
352 static int server_init(Server *s) {
354 struct epoll_event ev;
360 s->syslog_fd = s->signal_fd = -1;
362 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
363 if (s->epoll_fd < 0) {
364 log_error("Failed to create epoll object: %m");
368 n = sd_listen_fds(true);
370 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
375 log_error("Too many file descriptors passed.");
380 s->syslog_fd = SD_LISTEN_FDS_START;
382 union sockaddr_union sa;
384 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
385 if (s->syslog_fd < 0) {
386 log_error("socket() failed: %m");
391 sa.un.sun_family = AF_UNIX;
392 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
394 unlink(sa.un.sun_path);
396 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
398 log_error("bind() failed: %m");
402 chmod(sa.un.sun_path, 0666);
406 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
408 log_error("SO_PASSCRED failed: %m");
413 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
415 log_error("SO_TIMESTAMP failed: %m");
421 ev.data.fd = s->syslog_fd;
422 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
423 log_error("Failed to add server fd to epoll object: %m");
427 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
428 if (!s->user_journals) {
429 log_error("Out of memory.");
433 r = journal_file_open("/var/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->system_journal);
435 fix_perms(s->system_journal, 0);
436 else if (r == -ENOENT) {
437 mkdir_p("/run/log/journal", 0755);
439 r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->runtime_journal);
441 fix_perms(s->runtime_journal, 0);
444 if (r < 0 && r != -ENOENT) {
445 log_error("Failed to open journal: %s", strerror(-r));
449 assert_se(sigemptyset(&mask) == 0);
450 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
451 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
453 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
454 if (s->signal_fd < 0) {
455 log_error("signalfd(): %m");
461 ev.data.fd = s->signal_fd;
463 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
464 log_error("epoll_ctl(): %m");
471 static void server_done(Server *s) {
475 if (s->system_journal)
476 journal_file_close(s->system_journal);
478 if (s->runtime_journal)
479 journal_file_close(s->runtime_journal);
481 while ((f = hashmap_steal_first(s->user_journals)))
482 journal_file_close(f);
484 hashmap_free(s->user_journals);
486 if (s->epoll_fd >= 0)
487 close_nointr_nofail(s->epoll_fd);
489 if (s->signal_fd >= 0)
490 close_nointr_nofail(s->signal_fd);
492 if (s->syslog_fd >= 0)
493 close_nointr_nofail(s->syslog_fd);
496 int main(int argc, char *argv[]) {
500 /* if (getppid() != 1) { */
501 /* log_error("This program should be invoked by init only."); */
502 /* return EXIT_FAILURE; */
506 log_error("This program does not take arguments.");
510 log_set_target(LOG_TARGET_CONSOLE);
511 log_parse_environment();
516 r = server_init(&server);
520 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
524 "STATUS=Processing messages...");
527 struct epoll_event event;
529 r = epoll_wait(server.epoll_fd, &event, 1, -1);
535 log_error("epoll_wait() failed: %m");
541 r = process_event(&server, &event);
550 "STATUS=Shutting down...");
552 server_done(&server);
554 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;