chiark / gitweb /
util: rename join() to strjoin()
[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 Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser 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 <stddef.h>
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
31 #include <sys/statvfs.h>
32
33 #include <systemd/sd-journal.h>
34 #include <systemd/sd-messages.h>
35 #include <systemd/sd-daemon.h>
36
37 #ifdef HAVE_LOGIND
38 #include <systemd/sd-login.h>
39 #endif
40
41 #include "mkdir.h"
42 #include "hashmap.h"
43 #include "journal-file.h"
44 #include "socket-util.h"
45 #include "cgroup-util.h"
46 #include "list.h"
47 #include "journal-rate-limit.h"
48 #include "journal-internal.h"
49 #include "conf-parser.h"
50 #include "journald.h"
51 #include "virt.h"
52 #include "missing.h"
53
54 #ifdef HAVE_ACL
55 #include <sys/acl.h>
56 #include <acl/libacl.h>
57 #include "acl-util.h"
58 #endif
59
60 #ifdef HAVE_SELINUX
61 #include <selinux/selinux.h>
62 #endif
63
64 #define USER_JOURNALS_MAX 1024
65 #define STDOUT_STREAMS_MAX 4096
66
67 #define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
68 #define DEFAULT_RATE_LIMIT_BURST 200
69
70 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
71
72 #define N_IOVEC_META_FIELDS 17
73
74 #define ENTRY_SIZE_MAX (1024*1024*32)
75
76 typedef enum StdoutStreamState {
77         STDOUT_STREAM_IDENTIFIER,
78         STDOUT_STREAM_UNIT_ID,
79         STDOUT_STREAM_PRIORITY,
80         STDOUT_STREAM_LEVEL_PREFIX,
81         STDOUT_STREAM_FORWARD_TO_SYSLOG,
82         STDOUT_STREAM_FORWARD_TO_KMSG,
83         STDOUT_STREAM_FORWARD_TO_CONSOLE,
84         STDOUT_STREAM_RUNNING
85 } StdoutStreamState;
86
87 struct StdoutStream {
88         Server *server;
89         StdoutStreamState state;
90
91         int fd;
92
93         struct ucred ucred;
94 #ifdef HAVE_SELINUX
95         security_context_t security_context;
96 #endif
97
98         char *identifier;
99         char *unit_id;
100         int priority;
101         bool level_prefix:1;
102         bool forward_to_syslog:1;
103         bool forward_to_kmsg:1;
104         bool forward_to_console:1;
105
106         char buffer[LINE_MAX+1];
107         size_t length;
108
109         LIST_FIELDS(StdoutStream, stdout_stream);
110 };
111
112 static const char* const storage_table[] = {
113         [STORAGE_AUTO] = "auto",
114         [STORAGE_VOLATILE] = "volatile",
115         [STORAGE_PERSISTENT] = "persistent",
116         [STORAGE_NONE] = "none"
117 };
118
119 DEFINE_STRING_TABLE_LOOKUP(storage, Storage);
120 DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
121
122 static uint64_t available_space(Server *s) {
123         char ids[33], *p;
124         const char *f;
125         sd_id128_t machine;
126         struct statvfs ss;
127         uint64_t sum = 0, avail = 0, ss_avail = 0;
128         int r;
129         DIR *d;
130         usec_t ts;
131         JournalMetrics *m;
132
133         ts = now(CLOCK_MONOTONIC);
134
135         if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
136                 return s->cached_available_space;
137
138         r = sd_id128_get_machine(&machine);
139         if (r < 0)
140                 return 0;
141
142         if (s->system_journal) {
143                 f = "/var/log/journal/";
144                 m = &s->system_metrics;
145         } else {
146                 f = "/run/log/journal/";
147                 m = &s->runtime_metrics;
148         }
149
150         assert(m);
151
152         p = strappend(f, sd_id128_to_string(machine, ids));
153         if (!p)
154                 return 0;
155
156         d = opendir(p);
157         free(p);
158
159         if (!d)
160                 return 0;
161
162         if (fstatvfs(dirfd(d), &ss) < 0)
163                 goto finish;
164
165         for (;;) {
166                 struct stat st;
167                 struct dirent buf, *de;
168
169                 r = readdir_r(d, &buf, &de);
170                 if (r != 0)
171                         break;
172
173                 if (!de)
174                         break;
175
176                 if (!endswith(de->d_name, ".journal") &&
177                     !endswith(de->d_name, ".journal~"))
178                         continue;
179
180                 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
181                         continue;
182
183                 if (!S_ISREG(st.st_mode))
184                         continue;
185
186                 sum += (uint64_t) st.st_blocks * 512UL;
187         }
188
189         avail = sum >= m->max_use ? 0 : m->max_use - sum;
190
191         ss_avail = ss.f_bsize * ss.f_bavail;
192
193         ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
194
195         if (ss_avail < avail)
196                 avail = ss_avail;
197
198         s->cached_available_space = avail;
199         s->cached_available_space_timestamp = ts;
200
201 finish:
202         closedir(d);
203
204         return avail;
205 }
206
207 static void server_read_file_gid(Server *s) {
208         const char *adm = "adm";
209         int r;
210
211         assert(s);
212
213         if (s->file_gid_valid)
214                 return;
215
216         r = get_group_creds(&adm, &s->file_gid);
217         if (r < 0)
218                 log_warning("Failed to resolve 'adm' group: %s", strerror(-r));
219
220         /* if we couldn't read the gid, then it will be 0, but that's
221          * fine and we shouldn't try to resolve the group again, so
222          * let's just pretend it worked right-away. */
223         s->file_gid_valid = true;
224 }
225
226 static void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
227         int r;
228 #ifdef HAVE_ACL
229         acl_t acl;
230         acl_entry_t entry;
231         acl_permset_t permset;
232 #endif
233
234         assert(f);
235
236         server_read_file_gid(s);
237
238         r = fchmod_and_fchown(f->fd, 0640, 0, s->file_gid);
239         if (r < 0)
240                 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
241
242 #ifdef HAVE_ACL
243         if (uid <= 0)
244                 return;
245
246         acl = acl_get_fd(f->fd);
247         if (!acl) {
248                 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
249                 return;
250         }
251
252         r = acl_find_uid(acl, uid, &entry);
253         if (r <= 0) {
254
255                 if (acl_create_entry(&acl, &entry) < 0 ||
256                     acl_set_tag_type(entry, ACL_USER) < 0 ||
257                     acl_set_qualifier(entry, &uid) < 0) {
258                         log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
259                         goto finish;
260                 }
261         }
262
263         if (acl_get_permset(entry, &permset) < 0 ||
264             acl_add_perm(permset, ACL_READ) < 0 ||
265             acl_calc_mask(&acl) < 0) {
266                 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
267                 goto finish;
268         }
269
270         if (acl_set_fd(f->fd, acl) < 0)
271                 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
272
273 finish:
274         acl_free(acl);
275 #endif
276 }
277
278 static JournalFile* find_journal(Server *s, uid_t uid) {
279         char *p;
280         int r;
281         JournalFile *f;
282         char ids[33];
283         sd_id128_t machine;
284
285         assert(s);
286
287         /* We split up user logs only on /var, not on /run. If the
288          * runtime file is open, we write to it exclusively, in order
289          * to guarantee proper order as soon as we flush /run to
290          * /var and close the runtime file. */
291
292         if (s->runtime_journal)
293                 return s->runtime_journal;
294
295         if (uid <= 0)
296                 return s->system_journal;
297
298         r = sd_id128_get_machine(&machine);
299         if (r < 0)
300                 return s->system_journal;
301
302         f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
303         if (f)
304                 return f;
305
306         if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
307                 return s->system_journal;
308
309         while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
310                 /* Too many open? Then let's close one */
311                 f = hashmap_steal_first(s->user_journals);
312                 assert(f);
313                 journal_file_close(f);
314         }
315
316         r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
317         free(p);
318
319         if (r < 0)
320                 return s->system_journal;
321
322         server_fix_perms(s, f, uid);
323
324         r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
325         if (r < 0) {
326                 journal_file_close(f);
327                 return s->system_journal;
328         }
329
330         return f;
331 }
332
333 static void server_rotate(Server *s) {
334         JournalFile *f;
335         void *k;
336         Iterator i;
337         int r;
338
339         log_info("Rotating...");
340
341         if (s->runtime_journal) {
342                 r = journal_file_rotate(&s->runtime_journal);
343                 if (r < 0)
344                         if (s->runtime_journal)
345                                 log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
346                         else
347                                 log_error("Failed to create new runtime journal: %s", strerror(-r));
348                 else
349                         server_fix_perms(s, s->runtime_journal, 0);
350         }
351
352         if (s->system_journal) {
353                 r = journal_file_rotate(&s->system_journal);
354                 if (r < 0)
355                         if (s->system_journal)
356                                 log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
357                         else
358                                 log_error("Failed to create new system journal: %s", strerror(-r));
359
360                 else
361                         server_fix_perms(s, s->system_journal, 0);
362         }
363
364         HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
365                 r = journal_file_rotate(&f);
366                 if (r < 0)
367                         if (f->path)
368                                 log_error("Failed to rotate %s: %s", f->path, strerror(-r));
369                         else
370                                 log_error("Failed to create user journal: %s", strerror(-r));
371                 else {
372                         hashmap_replace(s->user_journals, k, f);
373                         server_fix_perms(s, s->system_journal, PTR_TO_UINT32(k));
374                 }
375         }
376 }
377
378 static void server_vacuum(Server *s) {
379         char *p;
380         char ids[33];
381         sd_id128_t machine;
382         int r;
383
384         log_info("Vacuuming...");
385
386         r = sd_id128_get_machine(&machine);
387         if (r < 0) {
388                 log_error("Failed to get machine ID: %s", strerror(-r));
389                 return;
390         }
391
392         sd_id128_to_string(machine, ids);
393
394         if (s->system_journal) {
395                 if (asprintf(&p, "/var/log/journal/%s", ids) < 0) {
396                         log_error("Out of memory.");
397                         return;
398                 }
399
400                 r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free);
401                 if (r < 0 && r != -ENOENT)
402                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
403                 free(p);
404         }
405
406         if (s->runtime_journal) {
407                 if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
408                         log_error("Out of memory.");
409                         return;
410                 }
411
412                 r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free);
413                 if (r < 0 && r != -ENOENT)
414                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
415                 free(p);
416         }
417
418         s->cached_available_space_timestamp = 0;
419 }
420
421 static char *shortened_cgroup_path(pid_t pid) {
422         int r;
423         char *process_path, *init_path, *path;
424
425         assert(pid > 0);
426
427         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
428         if (r < 0)
429                 return NULL;
430
431         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
432         if (r < 0) {
433                 free(process_path);
434                 return NULL;
435         }
436
437         if (endswith(init_path, "/system"))
438                 init_path[strlen(init_path) - 7] = 0;
439         else if (streq(init_path, "/"))
440                 init_path[0] = 0;
441
442         if (startswith(process_path, init_path)) {
443                 char *p;
444
445                 p = strdup(process_path + strlen(init_path));
446                 if (!p) {
447                         free(process_path);
448                         free(init_path);
449                         return NULL;
450                 }
451                 path = p;
452         } else {
453                 path = process_path;
454                 process_path = NULL;
455         }
456
457         free(process_path);
458         free(init_path);
459
460         return path;
461 }
462
463 static void dispatch_message_real(
464                 Server *s,
465                 struct iovec *iovec, unsigned n, unsigned m,
466                 struct ucred *ucred,
467                 struct timeval *tv,
468                 const char *label, size_t label_len,
469                 const char *unit_id) {
470
471         char *pid = NULL, *uid = NULL, *gid = NULL,
472                 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
473                 *comm = NULL, *cmdline = NULL, *hostname = NULL,
474                 *audit_session = NULL, *audit_loginuid = NULL,
475                 *exe = NULL, *cgroup = NULL, *session = NULL,
476                 *owner_uid = NULL, *unit = NULL, *selinux_context = NULL;
477
478         char idbuf[33];
479         sd_id128_t id;
480         int r;
481         char *t;
482         uid_t loginuid = 0, realuid = 0;
483         JournalFile *f;
484         bool vacuumed = false;
485
486         assert(s);
487         assert(iovec);
488         assert(n > 0);
489         assert(n + N_IOVEC_META_FIELDS <= m);
490
491         if (ucred) {
492                 uint32_t audit;
493 #ifdef HAVE_LOGIND
494                 uid_t owner;
495 #endif
496
497                 realuid = ucred->uid;
498
499                 if (asprintf(&pid, "_PID=%lu", (unsigned long) ucred->pid) >= 0)
500                         IOVEC_SET_STRING(iovec[n++], pid);
501
502                 if (asprintf(&uid, "_UID=%lu", (unsigned long) ucred->uid) >= 0)
503                         IOVEC_SET_STRING(iovec[n++], uid);
504
505                 if (asprintf(&gid, "_GID=%lu", (unsigned long) ucred->gid) >= 0)
506                         IOVEC_SET_STRING(iovec[n++], gid);
507
508                 r = get_process_comm(ucred->pid, &t);
509                 if (r >= 0) {
510                         comm = strappend("_COMM=", t);
511                         free(t);
512
513                         if (comm)
514                                 IOVEC_SET_STRING(iovec[n++], comm);
515                 }
516
517                 r = get_process_exe(ucred->pid, &t);
518                 if (r >= 0) {
519                         exe = strappend("_EXE=", t);
520                         free(t);
521
522                         if (exe)
523                                 IOVEC_SET_STRING(iovec[n++], exe);
524                 }
525
526                 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
527                 if (r >= 0) {
528                         cmdline = strappend("_CMDLINE=", t);
529                         free(t);
530
531                         if (cmdline)
532                                 IOVEC_SET_STRING(iovec[n++], cmdline);
533                 }
534
535                 r = audit_session_from_pid(ucred->pid, &audit);
536                 if (r >= 0)
537                         if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit) >= 0)
538                                 IOVEC_SET_STRING(iovec[n++], audit_session);
539
540                 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
541                 if (r >= 0)
542                         if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
543                                 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
544
545                 t = shortened_cgroup_path(ucred->pid);
546                 if (t) {
547                         cgroup = strappend("_SYSTEMD_CGROUP=", t);
548                         free(t);
549
550                         if (cgroup)
551                                 IOVEC_SET_STRING(iovec[n++], cgroup);
552                 }
553
554 #ifdef HAVE_LOGIND
555                 if (sd_pid_get_session(ucred->pid, &t) >= 0) {
556                         session = strappend("_SYSTEMD_SESSION=", t);
557                         free(t);
558
559                         if (session)
560                                 IOVEC_SET_STRING(iovec[n++], session);
561                 }
562
563                 if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
564                         if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
565                                 IOVEC_SET_STRING(iovec[n++], owner_uid);
566 #endif
567
568                 if (cg_pid_get_unit(ucred->pid, &t) >= 0) {
569                         unit = strappend("_SYSTEMD_UNIT=", t);
570                         free(t);
571                 } else if (unit_id)
572                         unit = strappend("_SYSTEMD_UNIT=", unit_id);
573
574                 if (unit)
575                         IOVEC_SET_STRING(iovec[n++], unit);
576
577 #ifdef HAVE_SELINUX
578                 if (label) {
579                         selinux_context = malloc(sizeof("_SELINUX_CONTEXT=") + label_len);
580                         if (selinux_context) {
581                                 memcpy(selinux_context, "_SELINUX_CONTEXT=", sizeof("_SELINUX_CONTEXT=")-1);
582                                 memcpy(selinux_context+sizeof("_SELINUX_CONTEXT=")-1, label, label_len);
583                                 selinux_context[sizeof("_SELINUX_CONTEXT=")-1+label_len] = 0;
584                                 IOVEC_SET_STRING(iovec[n++], selinux_context);
585                         }
586                 } else {
587                         security_context_t con;
588
589                         if (getpidcon(ucred->pid, &con) >= 0) {
590                                 selinux_context = strappend("_SELINUX_CONTEXT=", con);
591                                 if (selinux_context)
592                                         IOVEC_SET_STRING(iovec[n++], selinux_context);
593
594                                 freecon(con);
595                         }
596                 }
597 #endif
598         }
599
600         if (tv) {
601                 if (asprintf(&source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu",
602                              (unsigned long long) timeval_load(tv)) >= 0)
603                         IOVEC_SET_STRING(iovec[n++], source_time);
604         }
605
606         /* Note that strictly speaking storing the boot id here is
607          * redundant since the entry includes this in-line
608          * anyway. However, we need this indexed, too. */
609         r = sd_id128_get_boot(&id);
610         if (r >= 0)
611                 if (asprintf(&boot_id, "_BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
612                         IOVEC_SET_STRING(iovec[n++], boot_id);
613
614         r = sd_id128_get_machine(&id);
615         if (r >= 0)
616                 if (asprintf(&machine_id, "_MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
617                         IOVEC_SET_STRING(iovec[n++], machine_id);
618
619         t = gethostname_malloc();
620         if (t) {
621                 hostname = strappend("_HOSTNAME=", t);
622                 free(t);
623                 if (hostname)
624                         IOVEC_SET_STRING(iovec[n++], hostname);
625         }
626
627         assert(n <= m);
628
629 retry:
630         f = find_journal(s, realuid == 0 ? 0 : loginuid);
631         if (f) {
632                 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
633
634                 if ((r == -E2BIG || /* hit limit */
635                      r == -EFBIG || /* hit fs limit */
636                      r == -EDQUOT || /* quota hit */
637                      r == -ENOSPC || /* disk full */
638                      r == -EBADMSG || /* corrupted */
639                      r == -ENODATA || /* truncated */
640                      r == -EHOSTDOWN || /* other machine */
641                      r == -EPROTONOSUPPORT) && /* unsupported feature */
642                     !vacuumed) {
643
644                         if (r == -E2BIG)
645                                 log_info("Allocation limit reached, rotating.");
646                         else
647                                 log_warning("Journal file corrupted, rotating.");
648
649                         server_rotate(s);
650                         server_vacuum(s);
651                         vacuumed = true;
652
653                         log_info("Retrying write.");
654                         goto retry;
655                 }
656
657                 if (r < 0)
658                         log_error("Failed to write entry, ignoring: %s", strerror(-r));
659         }
660
661         free(pid);
662         free(uid);
663         free(gid);
664         free(comm);
665         free(exe);
666         free(cmdline);
667         free(source_time);
668         free(boot_id);
669         free(machine_id);
670         free(hostname);
671         free(audit_session);
672         free(audit_loginuid);
673         free(cgroup);
674         free(session);
675         free(owner_uid);
676         free(unit);
677         free(selinux_context);
678 }
679
680 static void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
681         char mid[11 + 32 + 1];
682         char buffer[16 + LINE_MAX + 1];
683         struct iovec iovec[N_IOVEC_META_FIELDS + 4];
684         int n = 0;
685         va_list ap;
686         struct ucred ucred;
687
688         assert(s);
689         assert(format);
690
691         IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
692         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
693
694         memcpy(buffer, "MESSAGE=", 8);
695         va_start(ap, format);
696         vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
697         va_end(ap);
698         char_array_0(buffer);
699         IOVEC_SET_STRING(iovec[n++], buffer);
700
701         snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
702         char_array_0(mid);
703         IOVEC_SET_STRING(iovec[n++], mid);
704
705         zero(ucred);
706         ucred.pid = getpid();
707         ucred.uid = getuid();
708         ucred.gid = getgid();
709
710         dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL);
711 }
712
713 static void dispatch_message(Server *s,
714                              struct iovec *iovec, unsigned n, unsigned m,
715                              struct ucred *ucred,
716                              struct timeval *tv,
717                              const char *label, size_t label_len,
718                              const char *unit_id,
719                              int priority) {
720         int rl;
721         char *path = NULL, *c;
722
723         assert(s);
724         assert(iovec || n == 0);
725
726         if (n == 0)
727                 return;
728
729         if (LOG_PRI(priority) > s->max_level_store)
730                 return;
731
732         if (!ucred)
733                 goto finish;
734
735         path = shortened_cgroup_path(ucred->pid);
736         if (!path)
737                 goto finish;
738
739         /* example: /user/lennart/3/foobar
740          *          /system/dbus.service/foobar
741          *
742          * So let's cut of everything past the third /, since that is
743          * wher user directories start */
744
745         c = strchr(path, '/');
746         if (c) {
747                 c = strchr(c+1, '/');
748                 if (c) {
749                         c = strchr(c+1, '/');
750                         if (c)
751                                 *c = 0;
752                 }
753         }
754
755         rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available_space(s));
756
757         if (rl == 0) {
758                 free(path);
759                 return;
760         }
761
762         /* Write a suppression message if we suppressed something */
763         if (rl > 1)
764                 driver_message(s, SD_MESSAGE_JOURNAL_DROPPED, "Suppressed %u messages from %s", rl - 1, path);
765
766         free(path);
767
768 finish:
769         dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
770 }
771
772 static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
773         struct msghdr msghdr;
774         struct cmsghdr *cmsg;
775         union {
776                 struct cmsghdr cmsghdr;
777                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
778         } control;
779         union sockaddr_union sa;
780
781         assert(s);
782         assert(iovec);
783         assert(n_iovec > 0);
784
785         zero(msghdr);
786         msghdr.msg_iov = (struct iovec*) iovec;
787         msghdr.msg_iovlen = n_iovec;
788
789         zero(sa);
790         sa.un.sun_family = AF_UNIX;
791         strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
792         msghdr.msg_name = &sa;
793         msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
794
795         if (ucred) {
796                 zero(control);
797                 msghdr.msg_control = &control;
798                 msghdr.msg_controllen = sizeof(control);
799
800                 cmsg = CMSG_FIRSTHDR(&msghdr);
801                 cmsg->cmsg_level = SOL_SOCKET;
802                 cmsg->cmsg_type = SCM_CREDENTIALS;
803                 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
804                 memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
805                 msghdr.msg_controllen = cmsg->cmsg_len;
806         }
807
808         /* Forward the syslog message we received via /dev/log to
809          * /run/systemd/syslog. Unfortunately we currently can't set
810          * the SO_TIMESTAMP auxiliary data, and hence we don't. */
811
812         if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
813                 return;
814
815         /* The socket is full? I guess the syslog implementation is
816          * too slow, and we shouldn't wait for that... */
817         if (errno == EAGAIN)
818                 return;
819
820         if (ucred && errno == ESRCH) {
821                 struct ucred u;
822
823                 /* Hmm, presumably the sender process vanished
824                  * by now, so let's fix it as good as we
825                  * can, and retry */
826
827                 u = *ucred;
828                 u.pid = getpid();
829                 memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
830
831                 if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
832                         return;
833
834                 if (errno == EAGAIN)
835                         return;
836         }
837
838         log_debug("Failed to forward syslog message: %m");
839 }
840
841 static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
842         struct iovec iovec;
843
844         assert(s);
845         assert(buffer);
846
847         if (LOG_PRI(priority) > s->max_level_syslog)
848                 return;
849
850         IOVEC_SET_STRING(iovec, buffer);
851         forward_syslog_iovec(s, &iovec, 1, ucred, tv);
852 }
853
854 static void forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
855         struct iovec iovec[5];
856         char header_priority[6], header_time[64], header_pid[16];
857         int n = 0;
858         time_t t;
859         struct tm *tm;
860         char *ident_buf = NULL;
861
862         assert(s);
863         assert(priority >= 0);
864         assert(priority <= 999);
865         assert(message);
866
867         if (LOG_PRI(priority) > s->max_level_syslog)
868                 return;
869
870         /* First: priority field */
871         snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
872         char_array_0(header_priority);
873         IOVEC_SET_STRING(iovec[n++], header_priority);
874
875         /* Second: timestamp */
876         t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
877         tm = localtime(&t);
878         if (!tm)
879                 return;
880         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
881                 return;
882         IOVEC_SET_STRING(iovec[n++], header_time);
883
884         /* Third: identifier and PID */
885         if (ucred) {
886                 if (!identifier) {
887                         get_process_comm(ucred->pid, &ident_buf);
888                         identifier = ident_buf;
889                 }
890
891                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
892                 char_array_0(header_pid);
893
894                 if (identifier)
895                         IOVEC_SET_STRING(iovec[n++], identifier);
896
897                 IOVEC_SET_STRING(iovec[n++], header_pid);
898         } else if (identifier) {
899                 IOVEC_SET_STRING(iovec[n++], identifier);
900                 IOVEC_SET_STRING(iovec[n++], ": ");
901         }
902
903         /* Fourth: message */
904         IOVEC_SET_STRING(iovec[n++], message);
905
906         forward_syslog_iovec(s, iovec, n, ucred, tv);
907
908         free(ident_buf);
909 }
910
911 static int fixup_priority(int priority) {
912
913         if ((priority & LOG_FACMASK) == 0)
914                 return (priority & LOG_PRIMASK) | LOG_USER;
915
916         return priority;
917 }
918
919 static void forward_kmsg(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
920         struct iovec iovec[5];
921         char header_priority[6], header_pid[16];
922         int n = 0;
923         char *ident_buf = NULL;
924         int fd;
925
926         assert(s);
927         assert(priority >= 0);
928         assert(priority <= 999);
929         assert(message);
930
931         if (LOG_PRI(priority) > s->max_level_kmsg)
932                 return;
933
934         /* Never allow messages with kernel facility to be written to
935          * kmsg, regardless where the data comes from. */
936         priority = fixup_priority(priority);
937
938         /* First: priority field */
939         snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
940         char_array_0(header_priority);
941         IOVEC_SET_STRING(iovec[n++], header_priority);
942
943         /* Second: identifier and PID */
944         if (ucred) {
945                 if (!identifier) {
946                         get_process_comm(ucred->pid, &ident_buf);
947                         identifier = ident_buf;
948                 }
949
950                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
951                 char_array_0(header_pid);
952
953                 if (identifier)
954                         IOVEC_SET_STRING(iovec[n++], identifier);
955
956                 IOVEC_SET_STRING(iovec[n++], header_pid);
957         } else if (identifier) {
958                 IOVEC_SET_STRING(iovec[n++], identifier);
959                 IOVEC_SET_STRING(iovec[n++], ": ");
960         }
961
962         /* Fourth: message */
963         IOVEC_SET_STRING(iovec[n++], message);
964         IOVEC_SET_STRING(iovec[n++], "\n");
965
966         fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
967         if (fd < 0) {
968                 log_debug("Failed to open /dev/kmsg for logging: %s", strerror(errno));
969                 goto finish;
970         }
971
972         if (writev(fd, iovec, n) < 0)
973                 log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno));
974
975         close_nointr_nofail(fd);
976
977 finish:
978         free(ident_buf);
979 }
980
981 static void forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
982         struct iovec iovec[4];
983         char header_pid[16];
984         int n = 0, fd;
985         char *ident_buf = NULL;
986         const char *tty;
987
988         assert(s);
989         assert(message);
990
991         if (LOG_PRI(priority) > s->max_level_console)
992                 return;
993
994         /* First: identifier and PID */
995         if (ucred) {
996                 if (!identifier) {
997                         get_process_comm(ucred->pid, &ident_buf);
998                         identifier = ident_buf;
999                 }
1000
1001                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
1002                 char_array_0(header_pid);
1003
1004                 if (identifier)
1005                         IOVEC_SET_STRING(iovec[n++], identifier);
1006
1007                 IOVEC_SET_STRING(iovec[n++], header_pid);
1008         } else if (identifier) {
1009                 IOVEC_SET_STRING(iovec[n++], identifier);
1010                 IOVEC_SET_STRING(iovec[n++], ": ");
1011         }
1012
1013         /* Third: message */
1014         IOVEC_SET_STRING(iovec[n++], message);
1015         IOVEC_SET_STRING(iovec[n++], "\n");
1016
1017         tty = s->tty_path ? s->tty_path : "/dev/console";
1018
1019         fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
1020         if (fd < 0) {
1021                 log_debug("Failed to open %s for logging: %s", tty, strerror(errno));
1022                 goto finish;
1023         }
1024
1025         if (writev(fd, iovec, n) < 0)
1026                 log_debug("Failed to write to %s for logging: %s", tty, strerror(errno));
1027
1028         close_nointr_nofail(fd);
1029
1030 finish:
1031         free(ident_buf);
1032 }
1033
1034 static void read_identifier(const char **buf, char **identifier, char **pid) {
1035         const char *p;
1036         char *t;
1037         size_t l, e;
1038
1039         assert(buf);
1040         assert(identifier);
1041         assert(pid);
1042
1043         p = *buf;
1044
1045         p += strspn(p, WHITESPACE);
1046         l = strcspn(p, WHITESPACE);
1047
1048         if (l <= 0 ||
1049             p[l-1] != ':')
1050                 return;
1051
1052         e = l;
1053         l--;
1054
1055         if (p[l-1] == ']') {
1056                 size_t k = l-1;
1057
1058                 for (;;) {
1059
1060                         if (p[k] == '[') {
1061                                 t = strndup(p+k+1, l-k-2);
1062                                 if (t)
1063                                         *pid = t;
1064
1065                                 l = k;
1066                                 break;
1067                         }
1068
1069                         if (k == 0)
1070                                 break;
1071
1072                         k--;
1073                 }
1074         }
1075
1076         t = strndup(p, l);
1077         if (t)
1078                 *identifier = t;
1079
1080         *buf = p + e;
1081         *buf += strspn(*buf, WHITESPACE);
1082 }
1083
1084 static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len) {
1085         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
1086         struct iovec iovec[N_IOVEC_META_FIELDS + 6];
1087         unsigned n = 0;
1088         int priority = LOG_USER | LOG_INFO;
1089         char *identifier = NULL, *pid = NULL;
1090         const char *orig;
1091
1092         assert(s);
1093         assert(buf);
1094
1095         orig = buf;
1096         parse_syslog_priority((char**) &buf, &priority);
1097
1098         if (s->forward_to_syslog)
1099                 forward_syslog_raw(s, priority, orig, ucred, tv);
1100
1101         skip_syslog_date((char**) &buf);
1102         read_identifier(&buf, &identifier, &pid);
1103
1104         if (s->forward_to_kmsg)
1105                 forward_kmsg(s, priority, identifier, buf, ucred);
1106
1107         if (s->forward_to_console)
1108                 forward_console(s, priority, identifier, buf, ucred);
1109
1110         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
1111
1112         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1113                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1114
1115         if (priority & LOG_FACMASK)
1116                 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1117                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
1118
1119         if (identifier) {
1120                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
1121                 if (syslog_identifier)
1122                         IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1123         }
1124
1125         if (pid) {
1126                 syslog_pid = strappend("SYSLOG_PID=", pid);
1127                 if (syslog_pid)
1128                         IOVEC_SET_STRING(iovec[n++], syslog_pid);
1129         }
1130
1131         message = strappend("MESSAGE=", buf);
1132         if (message)
1133                 IOVEC_SET_STRING(iovec[n++], message);
1134
1135         dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority);
1136
1137         free(message);
1138         free(identifier);
1139         free(pid);
1140         free(syslog_priority);
1141         free(syslog_facility);
1142         free(syslog_identifier);
1143 }
1144
1145 static bool valid_user_field(const char *p, size_t l) {
1146         const char *a;
1147
1148         /* We kinda enforce POSIX syntax recommendations for
1149            environment variables here, but make a couple of additional
1150            requirements.
1151
1152            http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
1153
1154         /* No empty field names */
1155         if (l <= 0)
1156                 return false;
1157
1158         /* Don't allow names longer than 64 chars */
1159         if (l > 64)
1160                 return false;
1161
1162         /* Variables starting with an underscore are protected */
1163         if (p[0] == '_')
1164                 return false;
1165
1166         /* Don't allow digits as first character */
1167         if (p[0] >= '0' && p[0] <= '9')
1168                 return false;
1169
1170         /* Only allow A-Z0-9 and '_' */
1171         for (a = p; a < p + l; a++)
1172                 if (!((*a >= 'A' && *a <= 'Z') ||
1173                       (*a >= '0' && *a <= '9') ||
1174                       *a == '_'))
1175                         return false;
1176
1177         return true;
1178 }
1179
1180 static void process_native_message(
1181                 Server *s,
1182                 const void *buffer, size_t buffer_size,
1183                 struct ucred *ucred,
1184                 struct timeval *tv,
1185                 const char *label, size_t label_len) {
1186
1187         struct iovec *iovec = NULL;
1188         unsigned n = 0, m = 0, j, tn = (unsigned) -1;
1189         const char *p;
1190         size_t remaining;
1191         int priority = LOG_INFO;
1192         char *identifier = NULL, *message = NULL;
1193
1194         assert(s);
1195         assert(buffer || buffer_size == 0);
1196
1197         p = buffer;
1198         remaining = buffer_size;
1199
1200         while (remaining > 0) {
1201                 const char *e, *q;
1202
1203                 e = memchr(p, '\n', remaining);
1204
1205                 if (!e) {
1206                         /* Trailing noise, let's ignore it, and flush what we collected */
1207                         log_debug("Received message with trailing noise, ignoring.");
1208                         break;
1209                 }
1210
1211                 if (e == p) {
1212                         /* Entry separator */
1213                         dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
1214                         n = 0;
1215                         priority = LOG_INFO;
1216
1217                         p++;
1218                         remaining--;
1219                         continue;
1220                 }
1221
1222                 if (*p == '.' || *p == '#') {
1223                         /* Ignore control commands for now, and
1224                          * comments too. */
1225                         remaining -= (e - p) + 1;
1226                         p = e + 1;
1227                         continue;
1228                 }
1229
1230                 /* A property follows */
1231
1232                 if (n+N_IOVEC_META_FIELDS >= m) {
1233                         struct iovec *c;
1234                         unsigned u;
1235
1236                         u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U);
1237                         c = realloc(iovec, u * sizeof(struct iovec));
1238                         if (!c) {
1239                                 log_error("Out of memory");
1240                                 break;
1241                         }
1242
1243                         iovec = c;
1244                         m = u;
1245                 }
1246
1247                 q = memchr(p, '=', e - p);
1248                 if (q) {
1249                         if (valid_user_field(p, q - p)) {
1250                                 size_t l;
1251
1252                                 l = e - p;
1253
1254                                 /* If the field name starts with an
1255                                  * underscore, skip the variable,
1256                                  * since that indidates a trusted
1257                                  * field */
1258                                 iovec[n].iov_base = (char*) p;
1259                                 iovec[n].iov_len = l;
1260                                 n++;
1261
1262                                 /* We need to determine the priority
1263                                  * of this entry for the rate limiting
1264                                  * logic */
1265                                 if (l == 10 &&
1266                                     memcmp(p, "PRIORITY=", 9) == 0 &&
1267                                     p[9] >= '0' && p[9] <= '9')
1268                                         priority = (priority & LOG_FACMASK) | (p[9] - '0');
1269
1270                                 else if (l == 17 &&
1271                                          memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1272                                          p[16] >= '0' && p[16] <= '9')
1273                                         priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
1274
1275                                 else if (l == 18 &&
1276                                          memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1277                                          p[16] >= '0' && p[16] <= '9' &&
1278                                          p[17] >= '0' && p[17] <= '9')
1279                                         priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
1280
1281                                 else if (l >= 19 &&
1282                                          memcmp(p, "SYSLOG_IDENTIFIER=", 18) == 0) {
1283                                         char *t;
1284
1285                                         t = strndup(p + 18, l - 18);
1286                                         if (t) {
1287                                                 free(identifier);
1288                                                 identifier = t;
1289                                         }
1290                                 } else if (l >= 8 &&
1291                                            memcmp(p, "MESSAGE=", 8) == 0) {
1292                                         char *t;
1293
1294                                         t = strndup(p + 8, l - 8);
1295                                         if (t) {
1296                                                 free(message);
1297                                                 message = t;
1298                                         }
1299                                 }
1300                         }
1301
1302                         remaining -= (e - p) + 1;
1303                         p = e + 1;
1304                         continue;
1305                 } else {
1306                         le64_t l_le;
1307                         uint64_t l;
1308                         char *k;
1309
1310                         if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
1311                                 log_debug("Failed to parse message, ignoring.");
1312                                 break;
1313                         }
1314
1315                         memcpy(&l_le, e + 1, sizeof(uint64_t));
1316                         l = le64toh(l_le);
1317
1318                         if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
1319                             e[1+sizeof(uint64_t)+l] != '\n') {
1320                                 log_debug("Failed to parse message, ignoring.");
1321                                 break;
1322                         }
1323
1324                         k = malloc((e - p) + 1 + l);
1325                         if (!k) {
1326                                 log_error("Out of memory");
1327                                 break;
1328                         }
1329
1330                         memcpy(k, p, e - p);
1331                         k[e - p] = '=';
1332                         memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
1333
1334                         if (valid_user_field(p, e - p)) {
1335                                 iovec[n].iov_base = k;
1336                                 iovec[n].iov_len = (e - p) + 1 + l;
1337                                 n++;
1338                         } else
1339                                 free(k);
1340
1341                         remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
1342                         p = e + 1 + sizeof(uint64_t) + l + 1;
1343                 }
1344         }
1345
1346         if (n <= 0)
1347                 goto finish;
1348
1349         tn = n++;
1350         IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
1351
1352         if (message) {
1353                 if (s->forward_to_syslog)
1354                         forward_syslog(s, priority, identifier, message, ucred, tv);
1355
1356                 if (s->forward_to_kmsg)
1357                         forward_kmsg(s, priority, identifier, message, ucred);
1358
1359                 if (s->forward_to_console)
1360                         forward_console(s, priority, identifier, message, ucred);
1361         }
1362
1363         dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
1364
1365 finish:
1366         for (j = 0; j < n; j++)  {
1367                 if (j == tn)
1368                         continue;
1369
1370                 if (iovec[j].iov_base < buffer ||
1371                     (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
1372                         free(iovec[j].iov_base);
1373         }
1374
1375         free(iovec);
1376         free(identifier);
1377         free(message);
1378 }
1379
1380 static void process_native_file(
1381                 Server *s,
1382                 int fd,
1383                 struct ucred *ucred,
1384                 struct timeval *tv,
1385                 const char *label, size_t label_len) {
1386
1387         struct stat st;
1388         void *p;
1389         ssize_t n;
1390
1391         assert(s);
1392         assert(fd >= 0);
1393
1394         /* Data is in the passed file, since it didn't fit in a
1395          * datagram. We can't map the file here, since clients might
1396          * then truncate it and trigger a SIGBUS for us. So let's
1397          * stupidly read it */
1398
1399         if (fstat(fd, &st) < 0) {
1400                 log_error("Failed to stat passed file, ignoring: %m");
1401                 return;
1402         }
1403
1404         if (!S_ISREG(st.st_mode)) {
1405                 log_error("File passed is not regular. Ignoring.");
1406                 return;
1407         }
1408
1409         if (st.st_size <= 0)
1410                 return;
1411
1412         if (st.st_size > ENTRY_SIZE_MAX) {
1413                 log_error("File passed too large. Ignoring.");
1414                 return;
1415         }
1416
1417         p = malloc(st.st_size);
1418         if (!p) {
1419                 log_error("Out of memory");
1420                 return;
1421         }
1422
1423         n = pread(fd, p, st.st_size, 0);
1424         if (n < 0)
1425                 log_error("Failed to read file, ignoring: %s", strerror(-n));
1426         else if (n > 0)
1427                 process_native_message(s, p, n, ucred, tv, label, label_len);
1428
1429         free(p);
1430 }
1431
1432 static int stdout_stream_log(StdoutStream *s, const char *p) {
1433         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
1434         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
1435         unsigned n = 0;
1436         int priority;
1437         char *label = NULL;
1438         size_t label_len = 0;
1439
1440         assert(s);
1441         assert(p);
1442
1443         if (isempty(p))
1444                 return 0;
1445
1446         priority = s->priority;
1447
1448         if (s->level_prefix)
1449                 parse_syslog_priority((char**) &p, &priority);
1450
1451         if (s->forward_to_syslog || s->server->forward_to_syslog)
1452                 forward_syslog(s->server, fixup_priority(priority), s->identifier, p, &s->ucred, NULL);
1453
1454         if (s->forward_to_kmsg || s->server->forward_to_kmsg)
1455                 forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
1456
1457         if (s->forward_to_console || s->server->forward_to_console)
1458                 forward_console(s->server, priority, s->identifier, p, &s->ucred);
1459
1460         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
1461
1462         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1463                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1464
1465         if (priority & LOG_FACMASK)
1466                 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1467                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
1468
1469         if (s->identifier) {
1470                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
1471                 if (syslog_identifier)
1472                         IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1473         }
1474
1475         message = strappend("MESSAGE=", p);
1476         if (message)
1477                 IOVEC_SET_STRING(iovec[n++], message);
1478
1479 #ifdef HAVE_SELINUX
1480         if (s->security_context) {
1481                 label = (char*) s->security_context;
1482                 label_len = strlen((char*) s->security_context);
1483         }
1484 #endif
1485
1486         dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
1487
1488         free(message);
1489         free(syslog_priority);
1490         free(syslog_facility);
1491         free(syslog_identifier);
1492
1493         return 0;
1494 }
1495
1496 static int stdout_stream_line(StdoutStream *s, char *p) {
1497         int r;
1498
1499         assert(s);
1500         assert(p);
1501
1502         p = strstrip(p);
1503
1504         switch (s->state) {
1505
1506         case STDOUT_STREAM_IDENTIFIER:
1507                 if (isempty(p))
1508                         s->identifier = NULL;
1509                 else  {
1510                         s->identifier = strdup(p);
1511                         if (!s->identifier) {
1512                                 log_error("Out of memory");
1513                                 return -ENOMEM;
1514                         }
1515                 }
1516
1517                 s->state = STDOUT_STREAM_UNIT_ID;
1518                 return 0;
1519
1520         case STDOUT_STREAM_UNIT_ID:
1521                 if (s->ucred.uid == 0) {
1522                         if (isempty(p))
1523                                 s->unit_id = NULL;
1524                         else  {
1525                                 s->unit_id = strdup(p);
1526                                 if (!s->unit_id) {
1527                                         log_error("Out of memory");
1528                                         return -ENOMEM;
1529                                 }
1530                         }
1531                 }
1532
1533                 s->state = STDOUT_STREAM_PRIORITY;
1534                 return 0;
1535
1536         case STDOUT_STREAM_PRIORITY:
1537                 r = safe_atoi(p, &s->priority);
1538                 if (r < 0 || s->priority <= 0 || s->priority >= 999) {
1539                         log_warning("Failed to parse log priority line.");
1540                         return -EINVAL;
1541                 }
1542
1543                 s->state = STDOUT_STREAM_LEVEL_PREFIX;
1544                 return 0;
1545
1546         case STDOUT_STREAM_LEVEL_PREFIX:
1547                 r = parse_boolean(p);
1548                 if (r < 0) {
1549                         log_warning("Failed to parse level prefix line.");
1550                         return -EINVAL;
1551                 }
1552
1553                 s->level_prefix = !!r;
1554                 s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
1555                 return 0;
1556
1557         case STDOUT_STREAM_FORWARD_TO_SYSLOG:
1558                 r = parse_boolean(p);
1559                 if (r < 0) {
1560                         log_warning("Failed to parse forward to syslog line.");
1561                         return -EINVAL;
1562                 }
1563
1564                 s->forward_to_syslog = !!r;
1565                 s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
1566                 return 0;
1567
1568         case STDOUT_STREAM_FORWARD_TO_KMSG:
1569                 r = parse_boolean(p);
1570                 if (r < 0) {
1571                         log_warning("Failed to parse copy to kmsg line.");
1572                         return -EINVAL;
1573                 }
1574
1575                 s->forward_to_kmsg = !!r;
1576                 s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
1577                 return 0;
1578
1579         case STDOUT_STREAM_FORWARD_TO_CONSOLE:
1580                 r = parse_boolean(p);
1581                 if (r < 0) {
1582                         log_warning("Failed to parse copy to console line.");
1583                         return -EINVAL;
1584                 }
1585
1586                 s->forward_to_console = !!r;
1587                 s->state = STDOUT_STREAM_RUNNING;
1588                 return 0;
1589
1590         case STDOUT_STREAM_RUNNING:
1591                 return stdout_stream_log(s, p);
1592         }
1593
1594         assert_not_reached("Unknown stream state");
1595 }
1596
1597 static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
1598         char *p;
1599         size_t remaining;
1600         int r;
1601
1602         assert(s);
1603
1604         p = s->buffer;
1605         remaining = s->length;
1606         for (;;) {
1607                 char *end;
1608                 size_t skip;
1609
1610                 end = memchr(p, '\n', remaining);
1611                 if (end)
1612                         skip = end - p + 1;
1613                 else if (remaining >= sizeof(s->buffer) - 1) {
1614                         end = p + sizeof(s->buffer) - 1;
1615                         skip = remaining;
1616                 } else
1617                         break;
1618
1619                 *end = 0;
1620
1621                 r = stdout_stream_line(s, p);
1622                 if (r < 0)
1623                         return r;
1624
1625                 remaining -= skip;
1626                 p += skip;
1627         }
1628
1629         if (force_flush && remaining > 0) {
1630                 p[remaining] = 0;
1631                 r = stdout_stream_line(s, p);
1632                 if (r < 0)
1633                         return r;
1634
1635                 p += remaining;
1636                 remaining = 0;
1637         }
1638
1639         if (p > s->buffer) {
1640                 memmove(s->buffer, p, remaining);
1641                 s->length = remaining;
1642         }
1643
1644         return 0;
1645 }
1646
1647 static int stdout_stream_process(StdoutStream *s) {
1648         ssize_t l;
1649         int r;
1650
1651         assert(s);
1652
1653         l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
1654         if (l < 0) {
1655
1656                 if (errno == EAGAIN)
1657                         return 0;
1658
1659                 log_warning("Failed to read from stream: %m");
1660                 return -errno;
1661         }
1662
1663         if (l == 0) {
1664                 r = stdout_stream_scan(s, true);
1665                 if (r < 0)
1666                         return r;
1667
1668                 return 0;
1669         }
1670
1671         s->length += l;
1672         r = stdout_stream_scan(s, false);
1673         if (r < 0)
1674                 return r;
1675
1676         return 1;
1677
1678 }
1679
1680 static void stdout_stream_free(StdoutStream *s) {
1681         assert(s);
1682
1683         if (s->server) {
1684                 assert(s->server->n_stdout_streams > 0);
1685                 s->server->n_stdout_streams --;
1686                 LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
1687         }
1688
1689         if (s->fd >= 0) {
1690                 if (s->server)
1691                         epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
1692
1693                 close_nointr_nofail(s->fd);
1694         }
1695
1696 #ifdef HAVE_SELINUX
1697         if (s->security_context)
1698                 freecon(s->security_context);
1699 #endif
1700
1701         free(s->identifier);
1702         free(s);
1703 }
1704
1705 static int stdout_stream_new(Server *s) {
1706         StdoutStream *stream;
1707         int fd, r;
1708         socklen_t len;
1709         struct epoll_event ev;
1710
1711         assert(s);
1712
1713         fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1714         if (fd < 0) {
1715                 if (errno == EAGAIN)
1716                         return 0;
1717
1718                 log_error("Failed to accept stdout connection: %m");
1719                 return -errno;
1720         }
1721
1722         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
1723                 log_warning("Too many stdout streams, refusing connection.");
1724                 close_nointr_nofail(fd);
1725                 return 0;
1726         }
1727
1728         stream = new0(StdoutStream, 1);
1729         if (!stream) {
1730                 log_error("Out of memory.");
1731                 close_nointr_nofail(fd);
1732                 return -ENOMEM;
1733         }
1734
1735         stream->fd = fd;
1736
1737         len = sizeof(stream->ucred);
1738         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
1739                 log_error("Failed to determine peer credentials: %m");
1740                 r = -errno;
1741                 goto fail;
1742         }
1743
1744 #ifdef HAVE_SELINUX
1745         if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
1746                 log_error("Failed to determine peer security context: %m");
1747 #endif
1748
1749         if (shutdown(fd, SHUT_WR) < 0) {
1750                 log_error("Failed to shutdown writing side of socket: %m");
1751                 r = -errno;
1752                 goto fail;
1753         }
1754
1755         zero(ev);
1756         ev.data.ptr = stream;
1757         ev.events = EPOLLIN;
1758         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
1759                 log_error("Failed to add stream to event loop: %m");
1760                 r = -errno;
1761                 goto fail;
1762         }
1763
1764         stream->server = s;
1765         LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
1766         s->n_stdout_streams ++;
1767
1768         return 0;
1769
1770 fail:
1771         stdout_stream_free(stream);
1772         return r;
1773 }
1774
1775 static int parse_kernel_timestamp(char **_p, usec_t *t) {
1776         usec_t r;
1777         int k, i;
1778         char *p;
1779
1780         assert(_p);
1781         assert(*_p);
1782         assert(t);
1783
1784         p = *_p;
1785
1786         if (strlen(p) < 14 || p[0] != '[' || p[13] != ']' || p[6] != '.')
1787                 return 0;
1788
1789         r = 0;
1790
1791         for (i = 1; i <= 5; i++) {
1792                 r *= 10;
1793
1794                 if (p[i] == ' ')
1795                         continue;
1796
1797                 k = undecchar(p[i]);
1798                 if (k < 0)
1799                         return 0;
1800
1801                 r += k;
1802         }
1803
1804         for (i = 7; i <= 12; i++) {
1805                 r *= 10;
1806
1807                 k = undecchar(p[i]);
1808                 if (k < 0)
1809                         return 0;
1810
1811                 r += k;
1812         }
1813
1814         *t = r;
1815         *_p += 14;
1816         *_p += strspn(*_p, WHITESPACE);
1817
1818         return 1;
1819 }
1820
1821 static bool is_us(const char *pid) {
1822         pid_t t;
1823
1824         assert(pid);
1825
1826         if (parse_pid(pid, &t) < 0)
1827                 return false;
1828
1829         return t == getpid();
1830 }
1831
1832 static void proc_kmsg_line(Server *s, const char *p) {
1833         struct iovec iovec[N_IOVEC_META_FIELDS + 7];
1834         char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL;
1835         int priority = LOG_KERN | LOG_INFO;
1836         unsigned n = 0;
1837         usec_t usec;
1838         char *identifier = NULL, *pid = NULL;
1839
1840         assert(s);
1841         assert(p);
1842
1843         if (isempty(p))
1844                 return;
1845
1846         parse_syslog_priority((char **) &p, &priority);
1847
1848         if (s->forward_to_kmsg && (priority & LOG_FACMASK) != LOG_KERN)
1849                 return;
1850
1851         if (parse_kernel_timestamp((char **) &p, &usec) > 0) {
1852                 if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu",
1853                              (unsigned long long) usec) >= 0)
1854                         IOVEC_SET_STRING(iovec[n++], source_time);
1855         }
1856
1857         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=kernel");
1858
1859         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1860                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1861
1862         if ((priority & LOG_FACMASK) == LOG_KERN) {
1863
1864                 if (s->forward_to_syslog)
1865                         forward_syslog(s, priority, "kernel", p, NULL, NULL);
1866
1867                 IOVEC_SET_STRING(iovec[n++], "SYSLOG_IDENTIFIER=kernel");
1868         } else {
1869                 read_identifier(&p, &identifier, &pid);
1870
1871                 /* Avoid any messages we generated ourselves via
1872                  * log_info() and friends. */
1873                 if (pid && is_us(pid))
1874                         goto finish;
1875
1876                 if (s->forward_to_syslog)
1877                         forward_syslog(s, priority, identifier, p, NULL, NULL);
1878
1879                 if (identifier) {
1880                         syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
1881                         if (syslog_identifier)
1882                                 IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1883                 }
1884
1885                 if (pid) {
1886                         syslog_pid = strappend("SYSLOG_PID=", pid);
1887                         if (syslog_pid)
1888                                 IOVEC_SET_STRING(iovec[n++], syslog_pid);
1889                 }
1890
1891                 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1892                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
1893         }
1894
1895         message = strappend("MESSAGE=", p);
1896         if (message)
1897                 IOVEC_SET_STRING(iovec[n++], message);
1898
1899         dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, NULL, 0, NULL, priority);
1900
1901 finish:
1902         free(message);
1903         free(syslog_priority);
1904         free(syslog_identifier);
1905         free(syslog_pid);
1906         free(syslog_facility);
1907         free(source_time);
1908         free(identifier);
1909         free(pid);
1910 }
1911
1912 static void proc_kmsg_scan(Server *s) {
1913         char *p;
1914         size_t remaining;
1915
1916         assert(s);
1917
1918         p = s->proc_kmsg_buffer;
1919         remaining = s->proc_kmsg_length;
1920         for (;;) {
1921                 char *end;
1922                 size_t skip;
1923
1924                 end = memchr(p, '\n', remaining);
1925                 if (end)
1926                         skip = end - p + 1;
1927                 else if (remaining >= sizeof(s->proc_kmsg_buffer) - 1) {
1928                         end = p + sizeof(s->proc_kmsg_buffer) - 1;
1929                         skip = remaining;
1930                 } else
1931                         break;
1932
1933                 *end = 0;
1934
1935                 proc_kmsg_line(s, p);
1936
1937                 remaining -= skip;
1938                 p += skip;
1939         }
1940
1941         if (p > s->proc_kmsg_buffer) {
1942                 memmove(s->proc_kmsg_buffer, p, remaining);
1943                 s->proc_kmsg_length = remaining;
1944         }
1945 }
1946
1947 static int system_journal_open(Server *s) {
1948         int r;
1949         char *fn;
1950         sd_id128_t machine;
1951         char ids[33];
1952
1953         r = sd_id128_get_machine(&machine);
1954         if (r < 0)
1955                 return r;
1956
1957         sd_id128_to_string(machine, ids);
1958
1959         if (!s->system_journal &&
1960             (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
1961             access("/run/systemd/journal/flushed", F_OK) >= 0) {
1962
1963                 /* If in auto mode: first try to create the machine
1964                  * path, but not the prefix.
1965                  *
1966                  * If in persistent mode: create /var/log/journal and
1967                  * the machine path */
1968
1969                 if (s->storage == STORAGE_PERSISTENT)
1970                         (void) mkdir("/var/log/journal/", 0755);
1971
1972                 fn = strappend("/var/log/journal/", ids);
1973                 if (!fn)
1974                         return -ENOMEM;
1975
1976                 (void) mkdir(fn, 0755);
1977                 free(fn);
1978
1979                 fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
1980                 if (!fn)
1981                         return -ENOMEM;
1982
1983                 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
1984                 free(fn);
1985
1986                 if (r >= 0) {
1987                         journal_default_metrics(&s->system_metrics, s->system_journal->fd);
1988
1989                         s->system_journal->metrics = s->system_metrics;
1990                         s->system_journal->compress = s->compress;
1991
1992                         server_fix_perms(s, s->system_journal, 0);
1993                 } else if (r < 0) {
1994
1995                         if (r != -ENOENT && r != -EROFS)
1996                                 log_warning("Failed to open system journal: %s", strerror(-r));
1997
1998                         r = 0;
1999                 }
2000         }
2001
2002         if (!s->runtime_journal &&
2003             (s->storage != STORAGE_NONE)) {
2004
2005                 fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
2006                 if (!fn)
2007                         return -ENOMEM;
2008
2009                 if (s->system_journal) {
2010
2011                         /* Try to open the runtime journal, but only
2012                          * if it already exists, so that we can flush
2013                          * it into the system journal */
2014
2015                         r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
2016                         free(fn);
2017
2018                         if (r < 0) {
2019                                 if (r != -ENOENT)
2020                                         log_warning("Failed to open runtime journal: %s", strerror(-r));
2021
2022                                 r = 0;
2023                         }
2024
2025                 } else {
2026
2027                         /* OK, we really need the runtime journal, so create
2028                          * it if necessary. */
2029
2030                         (void) mkdir_parents(fn, 0755);
2031                         r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
2032                         free(fn);
2033
2034                         if (r < 0) {
2035                                 log_error("Failed to open runtime journal: %s", strerror(-r));
2036                                 return r;
2037                         }
2038                 }
2039
2040                 if (s->runtime_journal) {
2041                         journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
2042
2043                         s->runtime_journal->metrics = s->runtime_metrics;
2044                         s->runtime_journal->compress = s->compress;
2045
2046                         server_fix_perms(s, s->runtime_journal, 0);
2047                 }
2048         }
2049
2050         return r;
2051 }
2052
2053 static int server_flush_to_var(Server *s) {
2054         Object *o = NULL;
2055         int r;
2056         sd_id128_t machine;
2057         sd_journal *j;
2058
2059         assert(s);
2060
2061         if (s->storage != STORAGE_AUTO &&
2062             s->storage != STORAGE_PERSISTENT)
2063                 return 0;
2064
2065         if (!s->runtime_journal)
2066                 return 0;
2067
2068         system_journal_open(s);
2069
2070         if (!s->system_journal)
2071                 return 0;
2072
2073         log_info("Flushing to /var...");
2074
2075         r = sd_id128_get_machine(&machine);
2076         if (r < 0) {
2077                 log_error("Failed to get machine id: %s", strerror(-r));
2078                 return r;
2079         }
2080
2081         r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
2082         if (r < 0) {
2083                 log_error("Failed to read runtime journal: %s", strerror(-r));
2084                 return r;
2085         }
2086
2087         SD_JOURNAL_FOREACH(j) {
2088                 JournalFile *f;
2089
2090                 f = j->current_file;
2091                 assert(f && f->current_offset > 0);
2092
2093                 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2094                 if (r < 0) {
2095                         log_error("Can't read entry: %s", strerror(-r));
2096                         goto finish;
2097                 }
2098
2099                 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
2100                 if (r == -E2BIG) {
2101                         log_info("Allocation limit reached.");
2102
2103                         journal_file_post_change(s->system_journal);
2104                         server_rotate(s);
2105                         server_vacuum(s);
2106
2107                         r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
2108                 }
2109
2110                 if (r < 0) {
2111                         log_error("Can't write entry: %s", strerror(-r));
2112                         goto finish;
2113                 }
2114         }
2115
2116 finish:
2117         journal_file_post_change(s->system_journal);
2118
2119         journal_file_close(s->runtime_journal);
2120         s->runtime_journal = NULL;
2121
2122         if (r >= 0) {
2123                 char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
2124                 sd_id128_to_string(machine, path + 17);
2125                 rm_rf(path, false, true, false);
2126         }
2127
2128         return r;
2129 }
2130
2131 static int server_read_proc_kmsg(Server *s) {
2132         ssize_t l;
2133         assert(s);
2134         assert(s->proc_kmsg_fd >= 0);
2135
2136         l = read(s->proc_kmsg_fd, s->proc_kmsg_buffer + s->proc_kmsg_length, sizeof(s->proc_kmsg_buffer) - 1 - s->proc_kmsg_length);
2137         if (l == 0) /* the kernel is stupid and in some race
2138                      * conditions returns 0 in the middle of the
2139                      * stream. */
2140                 return 0;
2141         if (l < 0) {
2142
2143                 if (errno == EAGAIN || errno == EINTR)
2144                         return 0;
2145
2146                 log_error("Failed to read from kernel: %m");
2147                 return -errno;
2148         }
2149
2150         s->proc_kmsg_length += l;
2151
2152         proc_kmsg_scan(s);
2153         return 1;
2154 }
2155
2156 static int server_flush_proc_kmsg(Server *s) {
2157         int r;
2158
2159         assert(s);
2160
2161         if (s->proc_kmsg_fd < 0)
2162                 return 0;
2163
2164         log_info("Flushing /proc/kmsg...");
2165
2166         for (;;) {
2167                 r = server_read_proc_kmsg(s);
2168                 if (r < 0)
2169                         return r;
2170
2171                 if (r == 0)
2172                         break;
2173         }
2174
2175         return 0;
2176 }
2177
2178 static int process_event(Server *s, struct epoll_event *ev) {
2179         assert(s);
2180         assert(ev);
2181
2182         if (ev->data.fd == s->signal_fd) {
2183                 struct signalfd_siginfo sfsi;
2184                 ssize_t n;
2185
2186                 if (ev->events != EPOLLIN) {
2187                         log_info("Got invalid event from epoll.");
2188                         return -EIO;
2189                 }
2190
2191                 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
2192                 if (n != sizeof(sfsi)) {
2193
2194                         if (n >= 0)
2195                                 return -EIO;
2196
2197                         if (errno == EINTR || errno == EAGAIN)
2198                                 return 1;
2199
2200                         return -errno;
2201                 }
2202
2203                 if (sfsi.ssi_signo == SIGUSR1) {
2204                         touch("/run/systemd/journal/flushed");
2205                         server_flush_to_var(s);
2206                         return 1;
2207                 }
2208
2209                 if (sfsi.ssi_signo == SIGUSR2) {
2210                         server_rotate(s);
2211                         server_vacuum(s);
2212                         return 1;
2213                 }
2214
2215                 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
2216                 return 0;
2217
2218         } else if (ev->data.fd == s->proc_kmsg_fd) {
2219                 int r;
2220
2221                 if (ev->events != EPOLLIN) {
2222                         log_info("Got invalid event from epoll.");
2223                         return -EIO;
2224                 }
2225
2226                 r = server_read_proc_kmsg(s);
2227                 if (r < 0)
2228                         return r;
2229
2230                 return 1;
2231
2232         } else if (ev->data.fd == s->native_fd ||
2233                    ev->data.fd == s->syslog_fd) {
2234
2235                 if (ev->events != EPOLLIN) {
2236                         log_info("Got invalid event from epoll.");
2237                         return -EIO;
2238                 }
2239
2240                 for (;;) {
2241                         struct msghdr msghdr;
2242                         struct iovec iovec;
2243                         struct ucred *ucred = NULL;
2244                         struct timeval *tv = NULL;
2245                         struct cmsghdr *cmsg;
2246                         char *label = NULL;
2247                         size_t label_len = 0;
2248                         union {
2249                                 struct cmsghdr cmsghdr;
2250
2251                                 /* We use NAME_MAX space for the
2252                                  * SELinux label here. The kernel
2253                                  * currently enforces no limit, but
2254                                  * according to suggestions from the
2255                                  * SELinux people this will change and
2256                                  * it will probably be identical to
2257                                  * NAME_MAX. For now we use that, but
2258                                  * this should be updated one day when
2259                                  * the final limit is known.*/
2260                                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
2261                                             CMSG_SPACE(sizeof(struct timeval)) +
2262                                             CMSG_SPACE(sizeof(int)) + /* fd */
2263                                             CMSG_SPACE(NAME_MAX)]; /* selinux label */
2264                         } control;
2265                         ssize_t n;
2266                         int v;
2267                         int *fds = NULL;
2268                         unsigned n_fds = 0;
2269
2270                         if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
2271                                 log_error("SIOCINQ failed: %m");
2272                                 return -errno;
2273                         }
2274
2275                         if (s->buffer_size < (size_t) v) {
2276                                 void *b;
2277                                 size_t l;
2278
2279                                 l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
2280                                 b = realloc(s->buffer, l+1);
2281
2282                                 if (!b) {
2283                                         log_error("Couldn't increase buffer.");
2284                                         return -ENOMEM;
2285                                 }
2286
2287                                 s->buffer_size = l;
2288                                 s->buffer = b;
2289                         }
2290
2291                         zero(iovec);
2292                         iovec.iov_base = s->buffer;
2293                         iovec.iov_len = s->buffer_size;
2294
2295                         zero(control);
2296                         zero(msghdr);
2297                         msghdr.msg_iov = &iovec;
2298                         msghdr.msg_iovlen = 1;
2299                         msghdr.msg_control = &control;
2300                         msghdr.msg_controllen = sizeof(control);
2301
2302                         n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2303                         if (n < 0) {
2304
2305                                 if (errno == EINTR || errno == EAGAIN)
2306                                         return 1;
2307
2308                                 log_error("recvmsg() failed: %m");
2309                                 return -errno;
2310                         }
2311
2312                         for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
2313
2314                                 if (cmsg->cmsg_level == SOL_SOCKET &&
2315                                     cmsg->cmsg_type == SCM_CREDENTIALS &&
2316                                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
2317                                         ucred = (struct ucred*) CMSG_DATA(cmsg);
2318                                 else if (cmsg->cmsg_level == SOL_SOCKET &&
2319                                          cmsg->cmsg_type == SCM_SECURITY) {
2320                                         label = (char*) CMSG_DATA(cmsg);
2321                                         label_len = cmsg->cmsg_len - CMSG_LEN(0);
2322                                 } else if (cmsg->cmsg_level == SOL_SOCKET &&
2323                                          cmsg->cmsg_type == SO_TIMESTAMP &&
2324                                          cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
2325                                         tv = (struct timeval*) CMSG_DATA(cmsg);
2326                                 else if (cmsg->cmsg_level == SOL_SOCKET &&
2327                                          cmsg->cmsg_type == SCM_RIGHTS) {
2328                                         fds = (int*) CMSG_DATA(cmsg);
2329                                         n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
2330                                 }
2331                         }
2332
2333                         if (ev->data.fd == s->syslog_fd) {
2334                                 char *e;
2335
2336                                 if (n > 0 && n_fds == 0) {
2337                                         e = memchr(s->buffer, '\n', n);
2338                                         if (e)
2339                                                 *e = 0;
2340                                         else
2341                                                 s->buffer[n] = 0;
2342
2343                                         process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
2344                                 } else if (n_fds > 0)
2345                                         log_warning("Got file descriptors via syslog socket. Ignoring.");
2346
2347                         } else {
2348                                 if (n > 0 && n_fds == 0)
2349                                         process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
2350                                 else if (n == 0 && n_fds == 1)
2351                                         process_native_file(s, fds[0], ucred, tv, label, label_len);
2352                                 else if (n_fds > 0)
2353                                         log_warning("Got too many file descriptors via native socket. Ignoring.");
2354                         }
2355
2356                         close_many(fds, n_fds);
2357                 }
2358
2359                 return 1;
2360
2361         } else if (ev->data.fd == s->stdout_fd) {
2362
2363                 if (ev->events != EPOLLIN) {
2364                         log_info("Got invalid event from epoll.");
2365                         return -EIO;
2366                 }
2367
2368                 stdout_stream_new(s);
2369                 return 1;
2370
2371         } else {
2372                 StdoutStream *stream;
2373
2374                 if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
2375                         log_info("Got invalid event from epoll.");
2376                         return -EIO;
2377                 }
2378
2379                 /* If it is none of the well-known fds, it must be an
2380                  * stdout stream fd. Note that this is a bit ugly here
2381                  * (since we rely that none of the well-known fds
2382                  * could be interpreted as pointer), but nonetheless
2383                  * safe, since the well-known fds would never get an
2384                  * fd > 4096, i.e. beyond the first memory page */
2385
2386                 stream = ev->data.ptr;
2387
2388                 if (stdout_stream_process(stream) <= 0)
2389                         stdout_stream_free(stream);
2390
2391                 return 1;
2392         }
2393
2394         log_error("Unknown event.");
2395         return 0;
2396 }
2397
2398 static int open_syslog_socket(Server *s) {
2399         union sockaddr_union sa;
2400         int one, r;
2401         struct epoll_event ev;
2402
2403         assert(s);
2404
2405         if (s->syslog_fd < 0) {
2406
2407                 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2408                 if (s->syslog_fd < 0) {
2409                         log_error("socket() failed: %m");
2410                         return -errno;
2411                 }
2412
2413                 zero(sa);
2414                 sa.un.sun_family = AF_UNIX;
2415                 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
2416
2417                 unlink(sa.un.sun_path);
2418
2419                 r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2420                 if (r < 0) {
2421                         log_error("bind() failed: %m");
2422                         return -errno;
2423                 }
2424
2425                 chmod(sa.un.sun_path, 0666);
2426         } else
2427                 fd_nonblock(s->syslog_fd, 1);
2428
2429         one = 1;
2430         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
2431         if (r < 0) {
2432                 log_error("SO_PASSCRED failed: %m");
2433                 return -errno;
2434         }
2435
2436 #ifdef HAVE_SELINUX
2437         one = 1;
2438         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
2439         if (r < 0)
2440                 log_warning("SO_PASSSEC failed: %m");
2441 #endif
2442
2443         one = 1;
2444         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
2445         if (r < 0) {
2446                 log_error("SO_TIMESTAMP failed: %m");
2447                 return -errno;
2448         }
2449
2450         zero(ev);
2451         ev.events = EPOLLIN;
2452         ev.data.fd = s->syslog_fd;
2453         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
2454                 log_error("Failed to add syslog server fd to epoll object: %m");
2455                 return -errno;
2456         }
2457
2458         return 0;
2459 }
2460
2461 static int open_native_socket(Server*s) {
2462         union sockaddr_union sa;
2463         int one, r;
2464         struct epoll_event ev;
2465
2466         assert(s);
2467
2468         if (s->native_fd < 0) {
2469
2470                 s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2471                 if (s->native_fd < 0) {
2472                         log_error("socket() failed: %m");
2473                         return -errno;
2474                 }
2475
2476                 zero(sa);
2477                 sa.un.sun_family = AF_UNIX;
2478                 strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
2479
2480                 unlink(sa.un.sun_path);
2481
2482                 r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2483                 if (r < 0) {
2484                         log_error("bind() failed: %m");
2485                         return -errno;
2486                 }
2487
2488                 chmod(sa.un.sun_path, 0666);
2489         } else
2490                 fd_nonblock(s->native_fd, 1);
2491
2492         one = 1;
2493         r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
2494         if (r < 0) {
2495                 log_error("SO_PASSCRED failed: %m");
2496                 return -errno;
2497         }
2498
2499 #ifdef HAVE_SELINUX
2500         one = 1;
2501         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
2502         if (r < 0)
2503                 log_warning("SO_PASSSEC failed: %m");
2504 #endif
2505
2506         one = 1;
2507         r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
2508         if (r < 0) {
2509                 log_error("SO_TIMESTAMP failed: %m");
2510                 return -errno;
2511         }
2512
2513         zero(ev);
2514         ev.events = EPOLLIN;
2515         ev.data.fd = s->native_fd;
2516         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) {
2517                 log_error("Failed to add native server fd to epoll object: %m");
2518                 return -errno;
2519         }
2520
2521         return 0;
2522 }
2523
2524 static int open_stdout_socket(Server *s) {
2525         union sockaddr_union sa;
2526         int r;
2527         struct epoll_event ev;
2528
2529         assert(s);
2530
2531         if (s->stdout_fd < 0) {
2532
2533                 s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2534                 if (s->stdout_fd < 0) {
2535                         log_error("socket() failed: %m");
2536                         return -errno;
2537                 }
2538
2539                 zero(sa);
2540                 sa.un.sun_family = AF_UNIX;
2541                 strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
2542
2543                 unlink(sa.un.sun_path);
2544
2545                 r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2546                 if (r < 0) {
2547                         log_error("bind() failed: %m");
2548                         return -errno;
2549                 }
2550
2551                 chmod(sa.un.sun_path, 0666);
2552
2553                 if (listen(s->stdout_fd, SOMAXCONN) < 0) {
2554                         log_error("liste() failed: %m");
2555                         return -errno;
2556                 }
2557         } else
2558                 fd_nonblock(s->stdout_fd, 1);
2559
2560         zero(ev);
2561         ev.events = EPOLLIN;
2562         ev.data.fd = s->stdout_fd;
2563         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
2564                 log_error("Failed to add stdout server fd to epoll object: %m");
2565                 return -errno;
2566         }
2567
2568         return 0;
2569 }
2570
2571 static int open_proc_kmsg(Server *s) {
2572         struct epoll_event ev;
2573
2574         assert(s);
2575
2576         if (!s->import_proc_kmsg)
2577                 return 0;
2578
2579         s->proc_kmsg_fd = open("/proc/kmsg", O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
2580         if (s->proc_kmsg_fd < 0) {
2581                 log_warning("Failed to open /proc/kmsg, ignoring: %m");
2582                 return 0;
2583         }
2584
2585         zero(ev);
2586         ev.events = EPOLLIN;
2587         ev.data.fd = s->proc_kmsg_fd;
2588         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->proc_kmsg_fd, &ev) < 0) {
2589                 log_error("Failed to add /proc/kmsg fd to epoll object: %m");
2590                 return -errno;
2591         }
2592
2593         return 0;
2594 }
2595
2596 static int open_signalfd(Server *s) {
2597         sigset_t mask;
2598         struct epoll_event ev;
2599
2600         assert(s);
2601
2602         assert_se(sigemptyset(&mask) == 0);
2603         sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1);
2604         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
2605
2606         s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
2607         if (s->signal_fd < 0) {
2608                 log_error("signalfd(): %m");
2609                 return -errno;
2610         }
2611
2612         zero(ev);
2613         ev.events = EPOLLIN;
2614         ev.data.fd = s->signal_fd;
2615
2616         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
2617                 log_error("epoll_ctl(): %m");
2618                 return -errno;
2619         }
2620
2621         return 0;
2622 }
2623
2624 static int server_parse_proc_cmdline(Server *s) {
2625         char *line, *w, *state;
2626         int r;
2627         size_t l;
2628
2629         if (detect_container(NULL) > 0)
2630                 return 0;
2631
2632         r = read_one_line_file("/proc/cmdline", &line);
2633         if (r < 0) {
2634                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
2635                 return 0;
2636         }
2637
2638         FOREACH_WORD_QUOTED(w, l, line, state) {
2639                 char *word;
2640
2641                 word = strndup(w, l);
2642                 if (!word) {
2643                         r = -ENOMEM;
2644                         goto finish;
2645                 }
2646
2647                 if (startswith(word, "systemd.journald.forward_to_syslog=")) {
2648                         r = parse_boolean(word + 35);
2649                         if (r < 0)
2650                                 log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
2651                         else
2652                                 s->forward_to_syslog = r;
2653                 } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
2654                         r = parse_boolean(word + 33);
2655                         if (r < 0)
2656                                 log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
2657                         else
2658                                 s->forward_to_kmsg = r;
2659                 } else if (startswith(word, "systemd.journald.forward_to_console=")) {
2660                         r = parse_boolean(word + 36);
2661                         if (r < 0)
2662                                 log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
2663                         else
2664                                 s->forward_to_console = r;
2665                 } else if (startswith(word, "systemd.journald"))
2666                         log_warning("Invalid systemd.journald parameter. Ignoring.");
2667
2668                 free(word);
2669         }
2670
2671         r = 0;
2672
2673 finish:
2674         free(line);
2675         return r;
2676 }
2677
2678 static int server_parse_config_file(Server *s) {
2679         FILE *f;
2680         const char *fn;
2681         int r;
2682
2683         assert(s);
2684
2685         fn = "/etc/systemd/journald.conf";
2686         f = fopen(fn, "re");
2687         if (!f) {
2688                 if (errno == ENOENT)
2689                         return 0;
2690
2691                 log_warning("Failed to open configuration file %s: %m", fn);
2692                 return -errno;
2693         }
2694
2695         r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
2696         if (r < 0)
2697                 log_warning("Failed to parse configuration file: %s", strerror(-r));
2698
2699         fclose(f);
2700
2701         return r;
2702 }
2703
2704 static int server_init(Server *s) {
2705         int n, r, fd;
2706
2707         assert(s);
2708
2709         zero(*s);
2710         s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = s->proc_kmsg_fd = -1;
2711         s->compress = true;
2712
2713         s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
2714         s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
2715
2716         s->forward_to_syslog = true;
2717
2718         s->max_level_store = LOG_DEBUG;
2719         s->max_level_syslog = LOG_DEBUG;
2720         s->max_level_kmsg = LOG_NOTICE;
2721         s->max_level_console = LOG_INFO;
2722
2723         memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
2724         memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
2725
2726         server_parse_config_file(s);
2727         server_parse_proc_cmdline(s);
2728
2729         s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
2730         if (!s->user_journals) {
2731                 log_error("Out of memory.");
2732                 return -ENOMEM;
2733         }
2734
2735         s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2736         if (s->epoll_fd < 0) {
2737                 log_error("Failed to create epoll object: %m");
2738                 return -errno;
2739         }
2740
2741         n = sd_listen_fds(true);
2742         if (n < 0) {
2743                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
2744                 return n;
2745         }
2746
2747         for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
2748
2749                 if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
2750
2751                         if (s->native_fd >= 0) {
2752                                 log_error("Too many native sockets passed.");
2753                                 return -EINVAL;
2754                         }
2755
2756                         s->native_fd = fd;
2757
2758                 } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
2759
2760                         if (s->stdout_fd >= 0) {
2761                                 log_error("Too many stdout sockets passed.");
2762                                 return -EINVAL;
2763                         }
2764
2765                         s->stdout_fd = fd;
2766
2767                 } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
2768
2769                         if (s->syslog_fd >= 0) {
2770                                 log_error("Too many /dev/log sockets passed.");
2771                                 return -EINVAL;
2772                         }
2773
2774                         s->syslog_fd = fd;
2775
2776                 } else {
2777                         log_error("Unknown socket passed.");
2778                         return -EINVAL;
2779                 }
2780         }
2781
2782         r = open_syslog_socket(s);
2783         if (r < 0)
2784                 return r;
2785
2786         r = open_native_socket(s);
2787         if (r < 0)
2788                 return r;
2789
2790         r = open_stdout_socket(s);
2791         if (r < 0)
2792                 return r;
2793
2794         r = open_proc_kmsg(s);
2795         if (r < 0)
2796                 return r;
2797
2798         r = open_signalfd(s);
2799         if (r < 0)
2800                 return r;
2801
2802         s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
2803         if (!s->rate_limit)
2804                 return -ENOMEM;
2805
2806         r = system_journal_open(s);
2807         if (r < 0)
2808                 return r;
2809
2810         return 0;
2811 }
2812
2813 static void server_done(Server *s) {
2814         JournalFile *f;
2815         assert(s);
2816
2817         while (s->stdout_streams)
2818                 stdout_stream_free(s->stdout_streams);
2819
2820         if (s->system_journal)
2821                 journal_file_close(s->system_journal);
2822
2823         if (s->runtime_journal)
2824                 journal_file_close(s->runtime_journal);
2825
2826         while ((f = hashmap_steal_first(s->user_journals)))
2827                 journal_file_close(f);
2828
2829         hashmap_free(s->user_journals);
2830
2831         if (s->epoll_fd >= 0)
2832                 close_nointr_nofail(s->epoll_fd);
2833
2834         if (s->signal_fd >= 0)
2835                 close_nointr_nofail(s->signal_fd);
2836
2837         if (s->syslog_fd >= 0)
2838                 close_nointr_nofail(s->syslog_fd);
2839
2840         if (s->native_fd >= 0)
2841                 close_nointr_nofail(s->native_fd);
2842
2843         if (s->stdout_fd >= 0)
2844                 close_nointr_nofail(s->stdout_fd);
2845
2846         if (s->proc_kmsg_fd >= 0)
2847                 close_nointr_nofail(s->proc_kmsg_fd);
2848
2849         if (s->rate_limit)
2850                 journal_rate_limit_free(s->rate_limit);
2851
2852         free(s->buffer);
2853         free(s->tty_path);
2854 }
2855
2856 int main(int argc, char *argv[]) {
2857         Server server;
2858         int r;
2859
2860         /* if (getppid() != 1) { */
2861         /*         log_error("This program should be invoked by init only."); */
2862         /*         return EXIT_FAILURE; */
2863         /* } */
2864
2865         if (argc > 1) {
2866                 log_error("This program does not take arguments.");
2867                 return EXIT_FAILURE;
2868         }
2869
2870         log_set_target(LOG_TARGET_SAFE);
2871         log_set_facility(LOG_SYSLOG);
2872         log_parse_environment();
2873         log_open();
2874
2875         umask(0022);
2876
2877         r = server_init(&server);
2878         if (r < 0)
2879                 goto finish;
2880
2881         server_vacuum(&server);
2882         server_flush_to_var(&server);
2883         server_flush_proc_kmsg(&server);
2884
2885         log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
2886         driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
2887
2888         sd_notify(false,
2889                   "READY=1\n"
2890                   "STATUS=Processing requests...");
2891
2892         for (;;) {
2893                 struct epoll_event event;
2894
2895                 r = epoll_wait(server.epoll_fd, &event, 1, -1);
2896                 if (r < 0) {
2897
2898                         if (errno == EINTR)
2899                                 continue;
2900
2901                         log_error("epoll_wait() failed: %m");
2902                         r = -errno;
2903                         goto finish;
2904                 } else if (r == 0)
2905                         break;
2906
2907                 r = process_event(&server, &event);
2908                 if (r < 0)
2909                         goto finish;
2910                 else if (r == 0)
2911                         break;
2912         }
2913
2914         log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
2915         driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
2916
2917 finish:
2918         sd_notify(false,
2919                   "STATUS=Shutting down...");
2920
2921         server_done(&server);
2922
2923         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2924 }