chiark / gitweb /
journal: split user logs into their own journal files
[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-private.h"
33 #include "sd-daemon.h"
34 #include "socket-util.h"
35 #include "acl-util.h"
36
37 typedef struct Server {
38         int syslog_fd;
39         int epoll_fd;
40         int signal_fd;
41
42         JournalFile *runtime_journal;
43         JournalFile *system_journal;
44         Hashmap *user_journals;
45 } Server;
46
47 static 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
89 finish:
90         acl_free(acl);
91 }
92
93 static 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
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,
137                 *exe = NULL;
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;
145         uid_t loginuid = 0;
146         JournalFile *f;
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;
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
214         r = sd_id128_get_boot(&id);
215         if (r >= 0)
216                 if (asprintf(&boot_id, "BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
217                         IOVEC_SET_STRING(iovec[n++], boot_id);
218
219         r = sd_id128_get_machine(&id);
220         if (r >= 0)
221                 if (asprintf(&machine_id, "MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
222                         IOVEC_SET_STRING(iovec[n++], machine_id);
223
224         t = gethostname_malloc();
225         if (t) {
226                 hostname = strappend("HOSTNAME=", t);
227                 if (hostname)
228                         IOVEC_SET_STRING(iovec[n++], hostname);
229                 free(t);
230         }
231
232         f = find_journal(s, loginuid);
233         if (!f)
234                 log_warning("Dropping message, as we can't find a place to store the data.");
235         else {
236                 r = journal_file_append_entry(f, NULL, iovec, n, NULL, NULL);
237
238                 if (r < 0)
239                         log_error("Failed to write entry, ignoring: %s", strerror(-r));
240         }
241
242         free(message);
243         free(pid);
244         free(uid);
245         free(gid);
246         free(comm);
247         free(cmdline);
248         free(source_time);
249         free(boot_id);
250         free(machine_id);
251         free(hostname);
252         free(audit_session);
253         free(audit_loginuid);
254         free(syslog_facility);
255         free(syslog_priority);
256 }
257
258 static int process_event(Server *s, struct epoll_event *ev) {
259         assert(s);
260
261         if (ev->events != EPOLLIN) {
262                 log_info("Got invalid event from epoll.");
263                 return -EIO;
264         }
265
266         if (ev->data.fd == s->signal_fd) {
267                 struct signalfd_siginfo sfsi;
268                 ssize_t n;
269
270                 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
271                 if (n != sizeof(sfsi)) {
272
273                         if (n >= 0)
274                                 return -EIO;
275
276                         if (errno == EINTR || errno == EAGAIN)
277                                 return 0;
278
279                         return -errno;
280                 }
281
282                 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
283                 return 0;
284
285         } else {
286                 for (;;) {
287                         char buf[LINE_MAX+1];
288                         struct msghdr msghdr;
289                         struct iovec iovec;
290                         struct ucred *ucred = NULL;
291                         struct timeval *tv = NULL;
292                         struct cmsghdr *cmsg;
293                         union {
294                                 struct cmsghdr cmsghdr;
295                                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
296                                             CMSG_SPACE(sizeof(struct timeval))];
297                         } control;
298                         ssize_t n;
299                         char *e;
300
301                         zero(iovec);
302                         iovec.iov_base = buf;
303                         iovec.iov_len = sizeof(buf)-1;
304
305                         zero(control);
306                         zero(msghdr);
307                         msghdr.msg_iov = &iovec;
308                         msghdr.msg_iovlen = 1;
309                         msghdr.msg_control = &control;
310                         msghdr.msg_controllen = sizeof(control);
311
312                         n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
313                         if (n < 0) {
314
315                                 if (errno == EINTR || errno == EAGAIN)
316                                         return 1;
317
318                                 log_error("recvmsg() failed: %m");
319                                 return -errno;
320                         }
321
322                         for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
323
324                                 if (cmsg->cmsg_level == SOL_SOCKET &&
325                                     cmsg->cmsg_type == SCM_CREDENTIALS &&
326                                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
327                                         ucred = (struct ucred*) CMSG_DATA(cmsg);
328                                 else if (cmsg->cmsg_level == SOL_SOCKET &&
329                                          cmsg->cmsg_type == SO_TIMESTAMP &&
330                                          cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
331                                         tv = (struct timeval*) CMSG_DATA(cmsg);
332                         }
333
334                         e = memchr(buf, '\n', n);
335                         if (e)
336                                 *e = 0;
337                         else
338                                 buf[n] = 0;
339
340                         process_message(s, strstrip(buf), ucred, tv);
341                 }
342         }
343
344         return 1;
345 }
346
347 static int server_init(Server *s) {
348         int n, one, r;
349         struct epoll_event ev;
350         sigset_t mask;
351
352         assert(s);
353
354         zero(*s);
355         s->syslog_fd = s->signal_fd = -1;
356
357         s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
358         if (s->epoll_fd < 0) {
359                 log_error("Failed to create epoll object: %m");
360                 return -errno;
361         }
362
363         n = sd_listen_fds(true);
364         if (n < 0) {
365                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
366                 return n;
367         }
368
369         if (n > 1) {
370                 log_error("Too many file descriptors passed.");
371                 return -EINVAL;
372         }
373
374         if (n == 1)
375                 s->syslog_fd = SD_LISTEN_FDS_START;
376         else {
377                 union sockaddr_union sa;
378
379                 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
380                 if (s->syslog_fd < 0) {
381                         log_error("socket() failed: %m");
382                         return -errno;
383                 }
384
385                 zero(sa);
386                 sa.un.sun_family = AF_UNIX;
387                 strncpy(sa.un.sun_path, "/run/systemd/syslog", sizeof(sa.un.sun_path));
388
389                 unlink(sa.un.sun_path);
390
391                 r = bind(s->syslog_fd, &sa.sa, sizeof(sa.un));
392                 if (r < 0) {
393                         log_error("bind() failed: %m");
394                         return -errno;
395                 }
396
397                 chmod(sa.un.sun_path, 0666);
398         }
399
400         one = 1;
401         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
402         if (r < 0) {
403                 log_error("SO_PASSCRED failed: %m");
404                 return -errno;
405         }
406
407         one = 1;
408         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
409         if (r < 0) {
410                 log_error("SO_TIMESTAMP failed: %m");
411                 return -errno;
412         }
413
414         zero(ev);
415         ev.events = EPOLLIN;
416         ev.data.fd = s->syslog_fd;
417         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
418                 log_error("Failed to add server fd to epoll object: %m");
419                 return -errno;
420         }
421
422         s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
423         if (!s->user_journals) {
424                 log_error("Out of memory.");
425                 return -ENOMEM;
426         }
427
428         r = journal_file_open("/var/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->system_journal);
429         if (r >= 0)
430                 fix_perms(s->system_journal, 0);
431         else if (r == -ENOENT) {
432                 mkdir_p("/run/log/journal", 0755);
433
434                 r = journal_file_open("/run/log/journal/system.journal", O_RDWR|O_CREAT, 0640, &s->runtime_journal);
435                 if (r >= 0)
436                         fix_perms(s->runtime_journal, 0);
437         }
438
439         if (r < 0 && r != -ENOENT) {
440                 log_error("Failed to open journal: %s", strerror(-r));
441                 return r;
442         }
443
444         assert_se(sigemptyset(&mask) == 0);
445         sigset_add_many(&mask, SIGINT, SIGTERM, -1);
446         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
447
448         s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
449         if (s->signal_fd < 0) {
450                 log_error("signalfd(): %m");
451                 return -errno;
452         }
453
454         zero(ev);
455         ev.events = EPOLLIN;
456         ev.data.fd = s->signal_fd;
457
458         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
459                 log_error("epoll_ctl(): %m");
460                 return -errno;
461         }
462
463         return 0;
464 }
465
466 static void server_done(Server *s) {
467         JournalFile *f;
468         assert(s);
469
470         if (s->system_journal)
471                 journal_file_close(s->system_journal);
472
473         if (s->runtime_journal)
474                 journal_file_close(s->runtime_journal);
475
476         while ((f = hashmap_steal_first(s->user_journals)))
477                 journal_file_close(f);
478
479         hashmap_free(s->user_journals);
480
481         if (s->epoll_fd >= 0)
482                 close_nointr_nofail(s->epoll_fd);
483
484         if (s->signal_fd >= 0)
485                 close_nointr_nofail(s->signal_fd);
486
487         if (s->syslog_fd >= 0)
488                 close_nointr_nofail(s->syslog_fd);
489 }
490
491 int main(int argc, char *argv[]) {
492         Server server;
493         int r;
494
495         /* if (getppid() != 1) { */
496         /*         log_error("This program should be invoked by init only."); */
497         /*         return EXIT_FAILURE; */
498         /* } */
499
500         if (argc > 1) {
501                 log_error("This program does not take arguments.");
502                 return EXIT_FAILURE;
503         }
504
505         log_set_target(LOG_TARGET_CONSOLE);
506         log_parse_environment();
507         log_open();
508
509         umask(0022);
510
511         r = server_init(&server);
512         if (r < 0)
513                 goto finish;
514
515         log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
516
517         sd_notify(false,
518                   "READY=1\n"
519                   "STATUS=Processing messages...");
520
521         for (;;) {
522                 struct epoll_event event;
523
524                 r = epoll_wait(server.epoll_fd, &event, 1, -1);
525                 if (r < 0) {
526
527                         if (errno == EINTR)
528                                 continue;
529
530                         log_error("epoll_wait() failed: %m");
531                         r = -errno;
532                         goto finish;
533                 } else if (r == 0)
534                         break;
535
536                 r = process_event(&server, &event);
537                 if (r < 0)
538                         goto finish;
539                 else if (r == 0)
540                         break;
541         }
542
543 finish:
544         sd_notify(false,
545                   "STATUS=Shutting down...");
546
547         server_done(&server);
548
549         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
550 }