]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald.c
journal: place machin journals in machine specific subdirectories
[thirdparty/systemd.git] / src / journal / journald.c
CommitLineData
87d2c1ff
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2011 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <sys/epoll.h>
23#include <sys/socket.h>
24#include <errno.h>
25#include <sys/signalfd.h>
26#include <unistd.h>
27#include <fcntl.h>
f4b47811
LP
28#include <sys/acl.h>
29#include <acl/libacl.h>
87d2c1ff
LP
30
31#include "hashmap.h"
cec736d2 32#include "journal-file.h"
87d2c1ff
LP
33#include "sd-daemon.h"
34#include "socket-util.h"
f4b47811 35#include "acl-util.h"
87d2c1ff
LP
36
37typedef struct Server {
38 int syslog_fd;
39 int epoll_fd;
40 int signal_fd;
41
f4b47811 42 JournalFile *runtime_journal;
87d2c1ff
LP
43 JournalFile *system_journal;
44 Hashmap *user_journals;
45} Server;
46
f4b47811
LP
47static void fix_perms(JournalFile *f, uid_t uid) {
48 acl_t acl;
49 acl_entry_t entry;
50 acl_permset_t permset;
51 int r;
52
53 assert(f);
54
55 r = fchmod_and_fchown(f->fd, 0640, 0, 0);
56 if (r < 0)
57 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
58
59 if (uid <= 0)
60 return;
61
62 acl = acl_get_fd(f->fd);
63 if (!acl) {
64 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
65 return;
66 }
67
68 r = acl_find_uid(acl, uid, &entry);
69 if (r <= 0) {
70
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);
75 goto finish;
76 }
77 }
78
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);
83 goto finish;
84 }
85
86 if (acl_set_fd(f->fd, acl) < 0)
87 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
88
89finish:
90 acl_free(acl);
91}
92
93static JournalFile* find_journal(Server *s, uid_t uid) {
94 char *p;
95 int r;
96 JournalFile *f;
97
98 assert(s);
99
100 /* We split up user logs only on /var, not on /run */
101 if (!s->system_journal)
102 return s->runtime_journal;
103
104 if (uid <= 0)
105 return s->system_journal;
106
107 f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
108 if (f)
109 return f;
110
111 if (asprintf(&p, "/var/log/journal/%lu.journal", (unsigned long) uid) < 0)
112 return s->system_journal;
113
114 r = journal_file_open(p, O_RDWR|O_CREAT, 0640, &f);
115 free(p);
116
117 if (r < 0)
118 return s->system_journal;
119
120 fix_perms(f, uid);
121
122 r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
123 if (r < 0) {
124 journal_file_close(f);
125 return s->system_journal;
126 }
127
128 return f;
129}
130
87d2c1ff
LP
131static 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,
137 *exe = NULL;
87d2c1ff
LP
138 struct iovec iovec[15];
139 unsigned n = 0;
140 char idbuf[33];
141 sd_id128_t id;
142 int r;
143 char *t;
144 int priority = LOG_USER | LOG_INFO;
f4b47811
LP
145 uid_t loginuid = 0;
146 JournalFile *f;
87d2c1ff
LP
147
148 parse_syslog_priority((char**) &buf, &priority);
149 skip_syslog_date((char**) &buf);
150
151 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
152 IOVEC_SET_STRING(iovec[n++], syslog_priority);
153
154 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
155 IOVEC_SET_STRING(iovec[n++], syslog_facility);
156
157 message = strappend("MESSAGE=", buf);
158 if (message)
159 IOVEC_SET_STRING(iovec[n++], message);
160
161 if (ucred) {
162 uint32_t session;
87d2c1ff
LP
163
164 if (asprintf(&pid, "PID=%lu", (unsigned long) ucred->pid) >= 0)
165 IOVEC_SET_STRING(iovec[n++], pid);
166
167 if (asprintf(&uid, "UID=%lu", (unsigned long) ucred->uid) >= 0)
168 IOVEC_SET_STRING(iovec[n++], uid);
169
170 if (asprintf(&gid, "GID=%lu", (unsigned long) ucred->gid) >= 0)
171 IOVEC_SET_STRING(iovec[n++], gid);
172
173 r = get_process_comm(ucred->pid, &t);
174 if (r >= 0) {
175 comm = strappend("COMM=", t);
176 if (comm)
177 IOVEC_SET_STRING(iovec[n++], comm);
178 free(t);
179 }
180
181 r = get_process_exe(ucred->pid, &t);
182 if (r >= 0) {
183 exe = strappend("EXE=", t);
184 if (comm)
185 IOVEC_SET_STRING(iovec[n++], exe);
186 free(t);
187 }
188
189 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
190 if (r >= 0) {
191 cmdline = strappend("CMDLINE=", t);
192 if (cmdline)
193 IOVEC_SET_STRING(iovec[n++], cmdline);
194 free(t);
195 }
196
197 r = audit_session_from_pid(ucred->pid, &session);
198 if (r >= 0)
199 if (asprintf(&audit_session, "AUDIT_SESSION=%lu", (unsigned long) session) >= 0)
200 IOVEC_SET_STRING(iovec[n++], audit_session);
201
202 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
203 if (r >= 0)
204 if (asprintf(&audit_loginuid, "AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
205 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
206 }
207
208 if (tv) {
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);
212 }
213
ed49ef3f
LP
214 /* Note that strictly speaking storing the boot id here is
215 * redundant since the entry includes this in-line
216 * anyway. However, we need this indexed, too. */
87d2c1ff
LP
217 r = sd_id128_get_boot(&id);
218 if (r >= 0)
219 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
220 IOVEC_SET_STRING(iovec[n++], boot_id);
221
222 r = sd_id128_get_machine(&id);
223 if (r >= 0)
224 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
225 IOVEC_SET_STRING(iovec[n++], machine_id);
226
227 t = gethostname_malloc();
228 if (t) {
229 hostname = strappend("HOSTNAME=", t);
230 if (hostname)
231 IOVEC_SET_STRING(iovec[n++], hostname);
232 free(t);
233 }
234
f4b47811
LP
235 f = find_journal(s, loginuid);
236 if (!f)
237 log_warning("Dropping message, as we can't find a place to store the data.");
238 else {
239 r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
87d2c1ff 240
f4b47811
LP
241 if (r < 0)
242 log_error("Failed to write entry, ignoring: %s", strerror(-r));
243 }
87d2c1ff
LP
244
245 free(message);
246 free(pid);
247 free(uid);
248 free(gid);
249 free(comm);
250 free(cmdline);
251 free(source_time);
252 free(boot_id);
253 free(machine_id);
254 free(hostname);
255 free(audit_session);
256 free(audit_loginuid);
257 free(syslog_facility);
258 free(syslog_priority);
259}
260
261static int process_event(Server *s, struct epoll_event *ev) {
262 assert(s);
263
264 if (ev->events != EPOLLIN) {
265 log_info("Got invalid event from epoll.");
266 return -EIO;
267 }
268
269 if (ev->data.fd == s->signal_fd) {
270 struct signalfd_siginfo sfsi;
271 ssize_t n;
272
273 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
274 if (n != sizeof(sfsi)) {
275
276 if (n >= 0)
277 return -EIO;
278
279 if (errno == EINTR || errno == EAGAIN)
280 return 0;
281
282 return -errno;
283 }
284
285 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
286 return 0;
287
cec736d2
LP
288 }
289
290 if (ev->data.fd == s->syslog_fd) {
87d2c1ff
LP
291 for (;;) {
292 char buf[LINE_MAX+1];
293 struct msghdr msghdr;
294 struct iovec iovec;
295 struct ucred *ucred = NULL;
296 struct timeval *tv = NULL;
297 struct cmsghdr *cmsg;
298 union {
299 struct cmsghdr cmsghdr;
300 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
301 CMSG_SPACE(sizeof(struct timeval))];
302 } control;
303 ssize_t n;
304 char *e;
305
306 zero(iovec);
307 iovec.iov_base = buf;
308 iovec.iov_len = sizeof(buf)-1;
309
310 zero(control);
311 zero(msghdr);
312 msghdr.msg_iov = &iovec;
313 msghdr.msg_iovlen = 1;
314 msghdr.msg_control = &control;
315 msghdr.msg_controllen = sizeof(control);
316
317 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
318 if (n < 0) {
319
320 if (errno == EINTR || errno == EAGAIN)
321 return 1;
322
323 log_error("recvmsg() failed: %m");
324 return -errno;
325 }
326
327 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
328
329 if (cmsg->cmsg_level == SOL_SOCKET &&
330 cmsg->cmsg_type == SCM_CREDENTIALS &&
331 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
332 ucred = (struct ucred*) CMSG_DATA(cmsg);
333 else if (cmsg->cmsg_level == SOL_SOCKET &&
334 cmsg->cmsg_type == SO_TIMESTAMP &&
335 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
336 tv = (struct timeval*) CMSG_DATA(cmsg);
337 }
338
339 e = memchr(buf, '\n', n);
340 if (e)
341 *e = 0;
342 else
343 buf[n] = 0;
344
345 process_message(s, strstrip(buf), ucred, tv);
346 }
cec736d2
LP
347
348 return 1;
87d2c1ff
LP
349 }
350
cec736d2
LP
351 log_error("Unknown event.");
352 return 0;
87d2c1ff
LP
353}
354
ed49ef3f
LP
355static int system_journal_open(Server *s) {
356 int r;
357 char *fn;
358 sd_id128_t machine;
359 char ids[33];
360
361 r = sd_id128_get_machine(&machine);
362 if (r < 0)
363 return r;
364
365 /* First try to create the machine path, but not the prefix */
366 fn = join("/var/log/journal/", sd_id128_to_string(machine, ids), NULL);
367 if (!fn)
368 return -ENOMEM;
369 (void) mkdir(fn, 0755);
370 free(fn);
371
372 /* The create the system journal file */
373 fn = join("/var/log/journal/", ids, "/system.journal", NULL);
374 if (!fn)
375 return -ENOMEM;
376
377 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, &s->system_journal);
378 free(fn);
379
380 if (r >= 0)
381 fix_perms(s->system_journal, 0);
382 else if (r == -ENOENT) {
383
384 /* /var didn't work, so try /run, but this time we
385 * create the prefix too */
386 fn = join("/run/log/journal/", ids, NULL);
387 if (!fn)
388 return -ENOMEM;
389 (void) mkdir_p(fn, 0755);
390 free(fn);
391
392 /* Then create the runtime journal file */
393 fn = join("/run/log/journal/", ids, "/system.journal", NULL);
394 if (!fn)
395 return -ENOMEM;
396 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, &s->runtime_journal);
397 free(fn);
398
399 if (r >= 0)
400 fix_perms(s->runtime_journal, 0);
401 }
402
403 if (r < 0 && r != -ENOENT) {
404 log_error("Failed to open journal: %s", strerror(-r));
405 return r;
406 }
407
408 return 0;
409}
410
87d2c1ff
LP
411static int server_init(Server *s) {
412 int n, one, r;
413 struct epoll_event ev;
414 sigset_t mask;
415
416 assert(s);
417
418 zero(*s);
419 s->syslog_fd = s->signal_fd = -1;
420
421 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
422 if (s->epoll_fd < 0) {
423 log_error("Failed to create epoll object: %m");
424 return -errno;
425 }
426
427 n = sd_listen_fds(true);
428 if (n < 0) {
429 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
430 return n;
431 }
432
433 if (n > 1) {
434 log_error("Too many file descriptors passed.");
435 return -EINVAL;
436 }
437
438 if (n == 1)
439 s->syslog_fd = SD_LISTEN_FDS_START;
440 else {
441 union sockaddr_union sa;
442
443 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
444 if (s->syslog_fd < 0) {
445 log_error("socket() failed: %m");
446 return -errno;
447 }
448
449 zero(sa);
450 sa.un.sun_family = AF_UNIX;
451 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
452
453 unlink(sa.un.sun_path);
454
455 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
456 if (r < 0) {
457 log_error("bind() failed: %m");
458 return -errno;
459 }
460
461 chmod(sa.un.sun_path, 0666);
462 }
463
464 one = 1;
465 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
466 if (r < 0) {
467 log_error("SO_PASSCRED failed: %m");
468 return -errno;
469 }
470
471 one = 1;
472 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
473 if (r < 0) {
474 log_error("SO_TIMESTAMP failed: %m");
475 return -errno;
476 }
477
478 zero(ev);
479 ev.events = EPOLLIN;
480 ev.data.fd = s->syslog_fd;
481 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
482 log_error("Failed to add server fd to epoll object: %m");
483 return -errno;
484 }
485
486 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
487 if (!s->user_journals) {
488 log_error("Out of memory.");
489 return -ENOMEM;
490 }
491
ed49ef3f
LP
492 r = system_journal_open(s);
493 if (r < 0)
87d2c1ff 494 return r;
87d2c1ff
LP
495
496 assert_se(sigemptyset(&mask) == 0);
497 sigset_add_many(&mask, SIGINT, SIGTERM, -1);
498 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
499
500 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
501 if (s->signal_fd < 0) {
502 log_error("signalfd(): %m");
503 return -errno;
504 }
505
506 zero(ev);
507 ev.events = EPOLLIN;
508 ev.data.fd = s->signal_fd;
509
510 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
511 log_error("epoll_ctl(): %m");
512 return -errno;
513 }
514
515 return 0;
516}
517
518static void server_done(Server *s) {
519 JournalFile *f;
520 assert(s);
521
522 if (s->system_journal)
523 journal_file_close(s->system_journal);
524
f4b47811
LP
525 if (s->runtime_journal)
526 journal_file_close(s->runtime_journal);
527
87d2c1ff
LP
528 while ((f = hashmap_steal_first(s->user_journals)))
529 journal_file_close(f);
530
531 hashmap_free(s->user_journals);
532
533 if (s->epoll_fd >= 0)
534 close_nointr_nofail(s->epoll_fd);
535
536 if (s->signal_fd >= 0)
537 close_nointr_nofail(s->signal_fd);
538
539 if (s->syslog_fd >= 0)
540 close_nointr_nofail(s->syslog_fd);
541}
542
543int main(int argc, char *argv[]) {
544 Server server;
545 int r;
546
547 /* if (getppid() != 1) { */
548 /* log_error("This program should be invoked by init only."); */
549 /* return EXIT_FAILURE; */
550 /* } */
551
552 if (argc > 1) {
553 log_error("This program does not take arguments.");
554 return EXIT_FAILURE;
555 }
556
f4b47811 557 log_set_target(LOG_TARGET_CONSOLE);
87d2c1ff
LP
558 log_parse_environment();
559 log_open();
560
561 umask(0022);
562
563 r = server_init(&server);
564 if (r < 0)
565 goto finish;
566
567 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
568
569 sd_notify(false,
570 "READY=1\n"
571 "STATUS=Processing messages...");
572
573 for (;;) {
574 struct epoll_event event;
575
576 r = epoll_wait(server.epoll_fd, &event, 1, -1);
577 if (r < 0) {
578
579 if (errno == EINTR)
580 continue;
581
582 log_error("epoll_wait() failed: %m");
583 r = -errno;
584 goto finish;
585 } else if (r == 0)
586 break;
587
588 r = process_event(&server, &event);
589 if (r < 0)
590 goto finish;
591 else if (r == 0)
592 break;
593 }
594
595finish:
596 sd_notify(false,
597 "STATUS=Shutting down...");
598
599 server_done(&server);
600
601 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
602}