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