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