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