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