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