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