chiark / gitweb /
journald: shorten the time we wait for a forwarding syslog
[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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 <sys/acl.h>
29 #include <acl/libacl.h>
30 #include <stddef.h>
31 #include <sys/ioctl.h>
32 #include <linux/sockios.h>
33 #include <sys/statvfs.h>
34
35 #include <systemd/sd-journal.h>
36 #include <systemd/sd-login.h>
37 #include <systemd/sd-messages.h>
38 #include <systemd/sd-daemon.h>
39
40 #include "hashmap.h"
41 #include "journal-file.h"
42 #include "socket-util.h"
43 #include "acl-util.h"
44 #include "cgroup-util.h"
45 #include "list.h"
46 #include "journal-rate-limit.h"
47 #include "journal-internal.h"
48 #include "conf-parser.h"
49 #include "journald.h"
50
51 #define USER_JOURNALS_MAX 1024
52 #define STDOUT_STREAMS_MAX 4096
53
54 #define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
55 #define DEFAULT_RATE_LIMIT_BURST 200
56
57 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
58
59 #define RECHECK_VAR_AVAILABLE_USEC (30*USEC_PER_SEC)
60
61 #define SYSLOG_TIMEOUT_USEC (250*USEC_PER_MSEC)
62
63 #define N_IOVEC_META_FIELDS 16
64
65 typedef enum StdoutStreamState {
66         STDOUT_STREAM_IDENTIFIER,
67         STDOUT_STREAM_PRIORITY,
68         STDOUT_STREAM_LEVEL_PREFIX,
69         STDOUT_STREAM_FORWARD_TO_SYSLOG,
70         STDOUT_STREAM_FORWARD_TO_KMSG,
71         STDOUT_STREAM_FORWARD_TO_CONSOLE,
72         STDOUT_STREAM_RUNNING
73 } StdoutStreamState;
74
75 struct StdoutStream {
76         Server *server;
77         StdoutStreamState state;
78
79         int fd;
80
81         struct ucred ucred;
82
83         char *identifier;
84         int priority;
85         bool level_prefix:1;
86         bool forward_to_syslog:1;
87         bool forward_to_kmsg:1;
88         bool forward_to_console:1;
89
90         char buffer[LINE_MAX+1];
91         size_t length;
92
93         LIST_FIELDS(StdoutStream, stdout_stream);
94 };
95
96 static int server_flush_to_var(Server *s);
97
98 static uint64_t available_space(Server *s) {
99         char ids[33], *p;
100         const char *f;
101         sd_id128_t machine;
102         struct statvfs ss;
103         uint64_t sum = 0, avail = 0, ss_avail = 0;
104         int r;
105         DIR *d;
106         usec_t ts;
107         JournalMetrics *m;
108
109         ts = now(CLOCK_MONOTONIC);
110
111         if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
112                 return s->cached_available_space;
113
114         r = sd_id128_get_machine(&machine);
115         if (r < 0)
116                 return 0;
117
118         if (s->system_journal) {
119                 f = "/var/log/journal/";
120                 m = &s->system_metrics;
121         } else {
122                 f = "/run/log/journal/";
123                 m = &s->runtime_metrics;
124         }
125
126         assert(m);
127
128         p = strappend(f, sd_id128_to_string(machine, ids));
129         if (!p)
130                 return 0;
131
132         d = opendir(p);
133         free(p);
134
135         if (!d)
136                 return 0;
137
138         if (fstatvfs(dirfd(d), &ss) < 0)
139                 goto finish;
140
141         for (;;) {
142                 struct stat st;
143                 struct dirent buf, *de;
144                 int k;
145
146                 k = readdir_r(d, &buf, &de);
147                 if (k != 0) {
148                         r = -k;
149                         goto finish;
150                 }
151
152                 if (!de)
153                         break;
154
155                 if (!dirent_is_file_with_suffix(de, ".journal"))
156                         continue;
157
158                 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
159                         continue;
160
161                 sum += (uint64_t) st.st_blocks * (uint64_t) st.st_blksize;
162         }
163
164         avail = sum >= m->max_use ? 0 : m->max_use - sum;
165
166         ss_avail = ss.f_bsize * ss.f_bavail;
167
168         ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
169
170         if (ss_avail < avail)
171                 avail = ss_avail;
172
173         s->cached_available_space = avail;
174         s->cached_available_space_timestamp = ts;
175
176 finish:
177         closedir(d);
178
179         return avail;
180 }
181
182 static void fix_perms(JournalFile *f, uid_t uid) {
183         acl_t acl;
184         acl_entry_t entry;
185         acl_permset_t permset;
186         int r;
187
188         assert(f);
189
190         r = fchmod_and_fchown(f->fd, 0640, 0, 0);
191         if (r < 0)
192                 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
193
194         if (uid <= 0)
195                 return;
196
197         acl = acl_get_fd(f->fd);
198         if (!acl) {
199                 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
200                 return;
201         }
202
203         r = acl_find_uid(acl, uid, &entry);
204         if (r <= 0) {
205
206                 if (acl_create_entry(&acl, &entry) < 0 ||
207                     acl_set_tag_type(entry, ACL_USER) < 0 ||
208                     acl_set_qualifier(entry, &uid) < 0) {
209                         log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
210                         goto finish;
211                 }
212         }
213
214         if (acl_get_permset(entry, &permset) < 0 ||
215             acl_add_perm(permset, ACL_READ) < 0 ||
216             acl_calc_mask(&acl) < 0) {
217                 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
218                 goto finish;
219         }
220
221         if (acl_set_fd(f->fd, acl) < 0)
222                 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
223
224 finish:
225         acl_free(acl);
226 }
227
228 static JournalFile* find_journal(Server *s, uid_t uid) {
229         char *p;
230         int r;
231         JournalFile *f;
232         char ids[33];
233         sd_id128_t machine;
234
235         assert(s);
236
237         /* We split up user logs only on /var, not on /run. If the
238          * runtime file is open, we write to it exclusively, in order
239          * to guarantee proper order as soon as we flush /run to
240          * /var and close the runtime file. */
241
242         if (s->runtime_journal)
243                 return s->runtime_journal;
244
245         if (uid <= 0)
246                 return s->system_journal;
247
248         r = sd_id128_get_machine(&machine);
249         if (r < 0)
250                 return s->system_journal;
251
252         f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
253         if (f)
254                 return f;
255
256         if (asprintf(&p, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine, ids), (unsigned long) uid) < 0)
257                 return s->system_journal;
258
259         while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
260                 /* Too many open? Then let's close one */
261                 f = hashmap_steal_first(s->user_journals);
262                 assert(f);
263                 journal_file_close(f);
264         }
265
266         r = journal_file_open(p, O_RDWR|O_CREAT, 0640, s->system_journal, &f);
267         free(p);
268
269         if (r < 0)
270                 return s->system_journal;
271
272         fix_perms(f, uid);
273         f->metrics = s->system_metrics;
274         f->compress = s->compress;
275
276         r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
277         if (r < 0) {
278                 journal_file_close(f);
279                 return s->system_journal;
280         }
281
282         return f;
283 }
284
285 static void server_rotate(Server *s) {
286         JournalFile *f;
287         void *k;
288         Iterator i;
289         int r;
290
291         log_info("Rotating...");
292
293         if (s->runtime_journal) {
294                 r = journal_file_rotate(&s->runtime_journal);
295                 if (r < 0)
296                         log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
297         }
298
299         if (s->system_journal) {
300                 r = journal_file_rotate(&s->system_journal);
301                 if (r < 0)
302                         log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
303         }
304
305         HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
306                 r = journal_file_rotate(&f);
307                 if (r < 0)
308                         log_error("Failed to rotate %s: %s", f->path, strerror(-r));
309                 else
310                         hashmap_replace(s->user_journals, k, f);
311         }
312 }
313
314 static void server_vacuum(Server *s) {
315         char *p;
316         char ids[33];
317         sd_id128_t machine;
318         int r;
319
320         log_info("Vacuuming...");
321
322         r = sd_id128_get_machine(&machine);
323         if (r < 0) {
324                 log_error("Failed to get machine ID: %s", strerror(-r));
325                 return;
326         }
327
328         sd_id128_to_string(machine, ids);
329
330         if (s->system_journal) {
331                 if (asprintf(&p, "/var/log/journal/%s", ids) < 0) {
332                         log_error("Out of memory.");
333                         return;
334                 }
335
336                 r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free);
337                 if (r < 0 && r != -ENOENT)
338                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
339                 free(p);
340         }
341
342
343         if (s->runtime_journal) {
344                 if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
345                         log_error("Out of memory.");
346                         return;
347                 }
348
349                 r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free);
350                 if (r < 0 && r != -ENOENT)
351                         log_error("Failed to vacuum %s: %s", p, strerror(-r));
352                 free(p);
353         }
354
355         s->cached_available_space_timestamp = 0;
356 }
357
358 static char *shortened_cgroup_path(pid_t pid) {
359         int r;
360         char *process_path, *init_path, *path;
361
362         assert(pid > 0);
363
364         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
365         if (r < 0)
366                 return NULL;
367
368         r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
369         if (r < 0) {
370                 free(process_path);
371                 return NULL;
372         }
373
374         if (endswith(init_path, "/system"))
375                 init_path[strlen(init_path) - 7] = 0;
376         else if (streq(init_path, "/"))
377                 init_path[0] = 0;
378
379         if (startswith(process_path, init_path)) {
380                 char *p;
381
382                 p = strdup(process_path + strlen(init_path));
383                 if (!p) {
384                         free(process_path);
385                         free(init_path);
386                         return NULL;
387                 }
388                 path = p;
389         } else {
390                 path = process_path;
391                 process_path = NULL;
392         }
393
394         free(process_path);
395         free(init_path);
396
397         return path;
398 }
399
400 static void dispatch_message_real(Server *s,
401                              struct iovec *iovec, unsigned n, unsigned m,
402                              struct ucred *ucred,
403                              struct timeval *tv) {
404
405         char *pid = NULL, *uid = NULL, *gid = NULL,
406                 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
407                 *comm = NULL, *cmdline = NULL, *hostname = NULL,
408                 *audit_session = NULL, *audit_loginuid = NULL,
409                 *exe = NULL, *cgroup = NULL, *session = NULL,
410                 *owner_uid = NULL, *unit = NULL;
411
412         char idbuf[33];
413         sd_id128_t id;
414         int r;
415         char *t;
416         uid_t loginuid = 0, realuid = 0;
417         JournalFile *f;
418         bool vacuumed = false;
419
420         assert(s);
421         assert(iovec);
422         assert(n > 0);
423         assert(n + N_IOVEC_META_FIELDS <= m);
424
425         if (ucred) {
426                 uint32_t audit;
427                 uid_t owner;
428
429                 realuid = ucred->uid;
430
431                 if (asprintf(&pid, "_PID=%lu", (unsigned long) ucred->pid) >= 0)
432                         IOVEC_SET_STRING(iovec[n++], pid);
433
434                 if (asprintf(&uid, "_UID=%lu", (unsigned long) ucred->uid) >= 0)
435                         IOVEC_SET_STRING(iovec[n++], uid);
436
437                 if (asprintf(&gid, "_GID=%lu", (unsigned long) ucred->gid) >= 0)
438                         IOVEC_SET_STRING(iovec[n++], gid);
439
440                 r = get_process_comm(ucred->pid, &t);
441                 if (r >= 0) {
442                         comm = strappend("_COMM=", t);
443                         free(t);
444
445                         if (comm)
446                                 IOVEC_SET_STRING(iovec[n++], comm);
447                 }
448
449                 r = get_process_exe(ucred->pid, &t);
450                 if (r >= 0) {
451                         exe = strappend("_EXE=", t);
452                         free(t);
453
454                         if (comm)
455                                 IOVEC_SET_STRING(iovec[n++], exe);
456                 }
457
458                 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
459                 if (r >= 0) {
460                         cmdline = strappend("_CMDLINE=", t);
461                         free(t);
462
463                         if (cmdline)
464                                 IOVEC_SET_STRING(iovec[n++], cmdline);
465                 }
466
467                 r = audit_session_from_pid(ucred->pid, &audit);
468                 if (r >= 0)
469                         if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit) >= 0)
470                                 IOVEC_SET_STRING(iovec[n++], audit_session);
471
472                 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
473                 if (r >= 0)
474                         if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
475                                 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
476
477                 t = shortened_cgroup_path(ucred->pid);
478                 if (t) {
479                         cgroup = strappend("_SYSTEMD_CGROUP=", t);
480                         free(t);
481
482                         if (cgroup)
483                                 IOVEC_SET_STRING(iovec[n++], cgroup);
484                 }
485
486                 if (sd_pid_get_session(ucred->pid, &t) >= 0) {
487                         session = strappend("_SYSTEMD_SESSION=", t);
488                         free(t);
489
490                         if (session)
491                                 IOVEC_SET_STRING(iovec[n++], session);
492                 }
493
494                 if (sd_pid_get_unit(ucred->pid, &t) >= 0) {
495                         unit = strappend("_SYSTEMD_UNIT=", t);
496                         free(t);
497
498                         if (unit)
499                                 IOVEC_SET_STRING(iovec[n++], unit);
500                 }
501
502                 if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
503                         if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
504                                 IOVEC_SET_STRING(iovec[n++], owner_uid);
505         }
506
507         if (tv) {
508                 if (asprintf(&source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu",
509                              (unsigned long long) timeval_load(tv)) >= 0)
510                         IOVEC_SET_STRING(iovec[n++], source_time);
511         }
512
513         /* Note that strictly speaking storing the boot id here is
514          * redundant since the entry includes this in-line
515          * anyway. However, we need this indexed, too. */
516         r = sd_id128_get_boot(&id);
517         if (r >= 0)
518                 if (asprintf(&boot_id, "_BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
519                         IOVEC_SET_STRING(iovec[n++], boot_id);
520
521         r = sd_id128_get_machine(&id);
522         if (r >= 0)
523                 if (asprintf(&machine_id, "_MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
524                         IOVEC_SET_STRING(iovec[n++], machine_id);
525
526         t = gethostname_malloc();
527         if (t) {
528                 hostname = strappend("_HOSTNAME=", t);
529                 free(t);
530                 if (hostname)
531                         IOVEC_SET_STRING(iovec[n++], hostname);
532         }
533
534         assert(n <= m);
535
536         server_flush_to_var(s);
537
538 retry:
539         f = find_journal(s, realuid == 0 ? 0 : loginuid);
540         if (!f)
541                 log_warning("Dropping message, as we can't find a place to store the data.");
542         else {
543                 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
544
545                 if (r == -E2BIG && !vacuumed) {
546                         log_info("Allocation limit reached.");
547
548                         server_rotate(s);
549                         server_vacuum(s);
550                         vacuumed = true;
551
552                         log_info("Retrying write.");
553                         goto retry;
554                 }
555
556                 if (r < 0)
557                         log_error("Failed to write entry, ignoring: %s", strerror(-r));
558         }
559
560         free(pid);
561         free(uid);
562         free(gid);
563         free(comm);
564         free(exe);
565         free(cmdline);
566         free(source_time);
567         free(boot_id);
568         free(machine_id);
569         free(hostname);
570         free(audit_session);
571         free(audit_loginuid);
572         free(cgroup);
573         free(session);
574         free(owner_uid);
575         free(unit);
576 }
577
578 static void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
579         char mid[11 + 32 + 1];
580         char buffer[16 + LINE_MAX + 1];
581         struct iovec iovec[N_IOVEC_META_FIELDS + 4];
582         int n = 0;
583         va_list ap;
584         struct ucred ucred;
585
586         assert(s);
587         assert(format);
588
589         IOVEC_SET_STRING(iovec[n++], "PRIORITY=5");
590         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
591
592         memcpy(buffer, "MESSAGE=", 8);
593         va_start(ap, format);
594         vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
595         va_end(ap);
596         char_array_0(buffer);
597         IOVEC_SET_STRING(iovec[n++], buffer);
598
599         snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
600         char_array_0(mid);
601         IOVEC_SET_STRING(iovec[n++], mid);
602
603         zero(ucred);
604         ucred.pid = getpid();
605         ucred.uid = getuid();
606         ucred.gid = getgid();
607
608         dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL);
609 }
610
611 static void dispatch_message(Server *s,
612                              struct iovec *iovec, unsigned n, unsigned m,
613                              struct ucred *ucred,
614                              struct timeval *tv,
615                              int priority) {
616         int rl;
617         char *path = NULL, *c;
618
619         assert(s);
620         assert(iovec || n == 0);
621
622         if (n == 0)
623                 return;
624
625         if (!ucred)
626                 goto finish;
627
628         path = shortened_cgroup_path(ucred->pid);
629         if (!path)
630                 goto finish;
631
632         /* example: /user/lennart/3/foobar
633          *          /system/dbus.service/foobar
634          *
635          * So let's cut of everything past the third /, since that is
636          * wher user directories start */
637
638         c = strchr(path, '/');
639         if (c) {
640                 c = strchr(c+1, '/');
641                 if (c) {
642                         c = strchr(c+1, '/');
643                         if (c)
644                                 *c = 0;
645                 }
646         }
647
648         rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available_space(s));
649
650         if (rl == 0) {
651                 free(path);
652                 return;
653         }
654
655         /* Write a suppression message if we suppressed something */
656         if (rl > 1)
657                 driver_message(s, SD_MESSAGE_JOURNAL_DROPPED, "Suppressed %u messages from %s", rl - 1, path);
658
659         free(path);
660
661 finish:
662         dispatch_message_real(s, iovec, n, m, ucred, tv);
663 }
664
665 static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
666         struct msghdr msghdr;
667         struct cmsghdr *cmsg;
668         union {
669                 struct cmsghdr cmsghdr;
670                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
671         } control;
672         union sockaddr_union sa;
673
674         assert(s);
675         assert(iovec);
676         assert(n_iovec > 0);
677
678         zero(msghdr);
679         msghdr.msg_iov = (struct iovec*) iovec;
680         msghdr.msg_iovlen = n_iovec;
681
682         zero(sa);
683         sa.un.sun_family = AF_UNIX;
684         strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
685         msghdr.msg_name = &sa;
686         msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
687
688         if (ucred) {
689                 zero(control);
690                 msghdr.msg_control = &control;
691                 msghdr.msg_controllen = sizeof(control);
692
693                 cmsg = CMSG_FIRSTHDR(&msghdr);
694                 cmsg->cmsg_level = SOL_SOCKET;
695                 cmsg->cmsg_type = SCM_CREDENTIALS;
696                 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
697                 memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
698                 msghdr.msg_controllen = cmsg->cmsg_len;
699         }
700
701         /* Forward the syslog message we received via /dev/log to
702          * /run/systemd/syslog. Unfortunately we currently can't set
703          * the SO_TIMESTAMP auxiliary data, and hence we don't. */
704
705         if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
706                 return;
707
708         if (ucred && errno == ESRCH) {
709                 struct ucred u;
710
711                 /* Hmm, presumably the sender process vanished
712                  * by now, so let's fix it as good as we
713                  * can, and retry */
714
715                 u = *ucred;
716                 u.pid = getpid();
717                 memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
718
719                 if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
720                         return;
721         }
722
723         log_debug("Failed to forward syslog message: %m");
724 }
725
726 static void forward_syslog_raw(Server *s, const char *buffer, struct ucred *ucred, struct timeval *tv) {
727         struct iovec iovec;
728
729         assert(s);
730         assert(buffer);
731
732         IOVEC_SET_STRING(iovec, buffer);
733         forward_syslog_iovec(s, &iovec, 1, ucred, tv);
734 }
735
736 static void forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
737         struct iovec iovec[5];
738         char header_priority[6], header_time[64], header_pid[16];
739         int n = 0;
740         time_t t;
741         struct tm *tm;
742         char *ident_buf = NULL;
743
744         assert(s);
745         assert(priority >= 0);
746         assert(priority <= 999);
747         assert(message);
748
749         /* First: priority field */
750         snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
751         char_array_0(header_priority);
752         IOVEC_SET_STRING(iovec[n++], header_priority);
753
754         /* Second: timestamp */
755         t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
756         tm = localtime(&t);
757         if (!tm)
758                 return;
759         if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
760                 return;
761         IOVEC_SET_STRING(iovec[n++], header_time);
762
763         /* Third: identifier and PID */
764         if (ucred) {
765                 if (!identifier) {
766                         get_process_comm(ucred->pid, &ident_buf);
767                         identifier = ident_buf;
768                 }
769
770                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
771                 char_array_0(header_pid);
772
773                 if (identifier)
774                         IOVEC_SET_STRING(iovec[n++], identifier);
775
776                 IOVEC_SET_STRING(iovec[n++], header_pid);
777         } else if (identifier) {
778                 IOVEC_SET_STRING(iovec[n++], identifier);
779                 IOVEC_SET_STRING(iovec[n++], ": ");
780         }
781
782         /* Fourth: message */
783         IOVEC_SET_STRING(iovec[n++], message);
784
785         forward_syslog_iovec(s, iovec, n, ucred, tv);
786
787         free(ident_buf);
788 }
789
790 static int fixup_priority(int priority) {
791
792         if ((priority & LOG_FACMASK) == 0)
793                 return (priority & LOG_PRIMASK) | LOG_USER;
794
795         return priority;
796 }
797
798 static void forward_kmsg(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
799         struct iovec iovec[5];
800         char header_priority[6], header_pid[16];
801         int n = 0;
802         char *ident_buf = NULL;
803         int fd;
804
805         assert(s);
806         assert(priority >= 0);
807         assert(priority <= 999);
808         assert(message);
809
810         /* Never allow messages with kernel facility to be written to
811          * kmsg, regardless where the data comes from. */
812         priority = fixup_priority(priority);
813
814         /* First: priority field */
815         snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
816         char_array_0(header_priority);
817         IOVEC_SET_STRING(iovec[n++], header_priority);
818
819         /* Second: identifier and PID */
820         if (ucred) {
821                 if (!identifier) {
822                         get_process_comm(ucred->pid, &ident_buf);
823                         identifier = ident_buf;
824                 }
825
826                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
827                 char_array_0(header_pid);
828
829                 if (identifier)
830                         IOVEC_SET_STRING(iovec[n++], identifier);
831
832                 IOVEC_SET_STRING(iovec[n++], header_pid);
833         } else if (identifier) {
834                 IOVEC_SET_STRING(iovec[n++], identifier);
835                 IOVEC_SET_STRING(iovec[n++], ": ");
836         }
837
838         /* Fourth: message */
839         IOVEC_SET_STRING(iovec[n++], message);
840         IOVEC_SET_STRING(iovec[n++], "\n");
841
842         fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
843         if (fd < 0) {
844                 log_debug("Failed to open /dev/kmsg for logging: %s", strerror(errno));
845                 goto finish;
846         }
847
848         if (writev(fd, iovec, n) < 0)
849                 log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno));
850
851         close_nointr_nofail(fd);
852
853 finish:
854         free(ident_buf);
855 }
856
857 static void forward_console(Server *s, const char *identifier, const char *message, struct ucred *ucred) {
858         struct iovec iovec[4];
859         char header_pid[16];
860         int n = 0, fd;
861         char *ident_buf = NULL;
862
863         assert(s);
864         assert(message);
865
866         /* First: identifier and PID */
867         if (ucred) {
868                 if (!identifier) {
869                         get_process_comm(ucred->pid, &ident_buf);
870                         identifier = ident_buf;
871                 }
872
873                 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
874                 char_array_0(header_pid);
875
876                 if (identifier)
877                         IOVEC_SET_STRING(iovec[n++], identifier);
878
879                 IOVEC_SET_STRING(iovec[n++], header_pid);
880         } else if (identifier) {
881                 IOVEC_SET_STRING(iovec[n++], identifier);
882                 IOVEC_SET_STRING(iovec[n++], ": ");
883         }
884
885         /* Third: message */
886         IOVEC_SET_STRING(iovec[n++], message);
887         IOVEC_SET_STRING(iovec[n++], "\n");
888
889         fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
890         if (fd < 0) {
891                 log_debug("Failed to open /dev/console for logging: %s", strerror(errno));
892                 goto finish;
893         }
894
895         if (writev(fd, iovec, n) < 0)
896                 log_debug("Failed to write to /dev/console for logging: %s", strerror(errno));
897
898         close_nointr_nofail(fd);
899
900 finish:
901         free(ident_buf);
902 }
903
904 static void read_identifier(const char **buf, char **identifier) {
905         const char *p;
906         char *t;
907         size_t l, e;
908
909         assert(buf);
910         assert(identifier);
911
912         p = *buf;
913
914         p += strspn(p, WHITESPACE);
915         l = strcspn(p, WHITESPACE);
916
917         if (l <= 0 ||
918             p[l-1] != ':')
919                 return;
920
921         e = l;
922         l--;
923
924         if (p[l-1] == ']') {
925                 size_t k = l-1;
926
927                 for (;;) {
928
929                         if (p[k] == '[') {
930                                 l = k;
931                                 break;
932                         }
933
934                         if (k == 0)
935                                 break;
936
937                         k--;
938                 }
939         }
940
941         t = strndup(p, l);
942         if (t)
943                 *identifier = t;
944
945         *buf = p + e;
946         *buf += strspn(*buf, WHITESPACE);
947 }
948
949 static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv) {
950         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
951         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
952         unsigned n = 0;
953         int priority = LOG_USER | LOG_INFO;
954         char *identifier = NULL;
955
956         assert(s);
957         assert(buf);
958
959         if (s->forward_to_syslog)
960                 forward_syslog_raw(s, buf, ucred, tv);
961
962         parse_syslog_priority((char**) &buf, &priority);
963         skip_syslog_date((char**) &buf);
964         read_identifier(&buf, &identifier);
965
966         if (s->forward_to_kmsg)
967                 forward_kmsg(s, priority, identifier, buf, ucred);
968
969         if (s->forward_to_console)
970                 forward_console(s, identifier, buf, ucred);
971
972         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
973
974         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
975                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
976
977         if (priority & LOG_FACMASK)
978                 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
979                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
980
981         if (identifier) {
982                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
983                 if (syslog_identifier)
984                         IOVEC_SET_STRING(iovec[n++], syslog_identifier);
985         }
986
987         message = strappend("MESSAGE=", buf);
988         if (message)
989                 IOVEC_SET_STRING(iovec[n++], message);
990
991         dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, priority);
992
993         free(message);
994         free(identifier);
995         free(syslog_priority);
996         free(syslog_facility);
997         free(syslog_identifier);
998 }
999
1000 static bool valid_user_field(const char *p, size_t l) {
1001         const char *a;
1002
1003         /* We kinda enforce POSIX syntax recommendations for
1004            environment variables here, but make a couple of additional
1005            requirements.
1006
1007            http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
1008
1009         /* No empty field names */
1010         if (l <= 0)
1011                 return false;
1012
1013         /* Don't allow names longer than 64 chars */
1014         if (l > 64)
1015                 return false;
1016
1017         /* Variables starting with an underscore are protected */
1018         if (p[0] == '_')
1019                 return false;
1020
1021         /* Don't allow digits as first character */
1022         if (p[0] >= '0' && p[0] <= '9')
1023                 return false;
1024
1025         /* Only allow A-Z0-9 and '_' */
1026         for (a = p; a < p + l; a++)
1027                 if (!((*a >= 'A' && *a <= 'Z') ||
1028                       (*a >= '0' && *a <= '9') ||
1029                       *a == '_'))
1030                         return false;
1031
1032         return true;
1033 }
1034
1035 static void process_native_message(Server *s, const void *buffer, size_t buffer_size, struct ucred *ucred, struct timeval *tv) {
1036         struct iovec *iovec = NULL;
1037         unsigned n = 0, m = 0, j, tn = (unsigned) -1;
1038         const char *p;
1039         size_t remaining;
1040         int priority = LOG_INFO;
1041         char *identifier = NULL, *message = NULL;
1042
1043         assert(s);
1044         assert(buffer || n == 0);
1045
1046         p = buffer;
1047         remaining = buffer_size;
1048
1049         while (remaining > 0) {
1050                 const char *e, *q;
1051
1052                 e = memchr(p, '\n', remaining);
1053
1054                 if (!e) {
1055                         /* Trailing noise, let's ignore it, and flush what we collected */
1056                         log_debug("Received message with trailing noise, ignoring.");
1057                         break;
1058                 }
1059
1060                 if (e == p) {
1061                         /* Entry separator */
1062                         dispatch_message(s, iovec, n, m, ucred, tv, priority);
1063                         n = 0;
1064                         priority = LOG_INFO;
1065
1066                         p++;
1067                         remaining--;
1068                         continue;
1069                 }
1070
1071                 if (*p == '.' || *p == '#') {
1072                         /* Ignore control commands for now, and
1073                          * comments too. */
1074                         remaining -= (e - p) + 1;
1075                         p = e + 1;
1076                         continue;
1077                 }
1078
1079                 /* A property follows */
1080
1081                 if (n+N_IOVEC_META_FIELDS >= m) {
1082                         struct iovec *c;
1083                         unsigned u;
1084
1085                         u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U);
1086                         c = realloc(iovec, u * sizeof(struct iovec));
1087                         if (!c) {
1088                                 log_error("Out of memory");
1089                                 break;
1090                         }
1091
1092                         iovec = c;
1093                         m = u;
1094                 }
1095
1096                 q = memchr(p, '=', e - p);
1097                 if (q) {
1098                         if (valid_user_field(p, q - p)) {
1099                                 size_t l;
1100
1101                                 l = e - p;
1102
1103                                 /* If the field name starts with an
1104                                  * underscore, skip the variable,
1105                                  * since that indidates a trusted
1106                                  * field */
1107                                 iovec[n].iov_base = (char*) p;
1108                                 iovec[n].iov_len = l;
1109                                 n++;
1110
1111                                 /* We need to determine the priority
1112                                  * of this entry for the rate limiting
1113                                  * logic */
1114                                 if (l == 10 &&
1115                                     memcmp(p, "PRIORITY=", 9) == 0 &&
1116                                     p[9] >= '0' && p[9] <= '9')
1117                                         priority = (priority & LOG_FACMASK) | (p[9] - '0');
1118
1119                                 else if (l == 17 &&
1120                                          memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1121                                          p[16] >= '0' && p[16] <= '9')
1122                                         priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
1123
1124                                 else if (l == 18 &&
1125                                          memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1126                                          p[16] >= '0' && p[16] <= '9' &&
1127                                          p[17] >= '0' && p[17] <= '9')
1128                                         priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
1129
1130                                 else if (l >= 12 &&
1131                                          memcmp(p, "SYSLOG_IDENTIFIER=", 11) == 0) {
1132                                         char *t;
1133
1134                                         t = strndup(p + 11, l - 11);
1135                                         if (t) {
1136                                                 free(identifier);
1137                                                 identifier = t;
1138                                         }
1139                                 } else if (l >= 8 &&
1140                                            memcmp(p, "MESSAGE=", 8) == 0) {
1141                                         char *t;
1142
1143                                         t = strndup(p + 8, l - 8);
1144                                         if (t) {
1145                                                 free(message);
1146                                                 message = t;
1147                                         }
1148                                 }
1149                         }
1150
1151                         remaining -= (e - p) + 1;
1152                         p = e + 1;
1153                         continue;
1154                 } else {
1155                         uint64_t l;
1156                         char *k;
1157
1158                         if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
1159                                 log_debug("Failed to parse message, ignoring.");
1160                                 break;
1161                         }
1162
1163                         memcpy(&l, e + 1, sizeof(uint64_t));
1164                         l = le64toh(l);
1165
1166                         if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
1167                             e[1+sizeof(uint64_t)+l] != '\n') {
1168                                 log_debug("Failed to parse message, ignoring.");
1169                                 break;
1170                         }
1171
1172                         k = malloc((e - p) + 1 + l);
1173                         if (!k) {
1174                                 log_error("Out of memory");
1175                                 break;
1176                         }
1177
1178                         memcpy(k, p, e - p);
1179                         k[e - p] = '=';
1180                         memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
1181
1182                         if (valid_user_field(p, e - p)) {
1183                                 iovec[n].iov_base = k;
1184                                 iovec[n].iov_len = (e - p) + 1 + l;
1185                                 n++;
1186                         } else
1187                                 free(k);
1188
1189                         remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
1190                         p = e + 1 + sizeof(uint64_t) + l + 1;
1191                 }
1192         }
1193
1194         if (n <= 0)
1195                 goto finish;
1196
1197         tn = n++;
1198         IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
1199
1200         if (message) {
1201                 if (s->forward_to_syslog)
1202                         forward_syslog(s, priority, identifier, message, ucred, tv);
1203
1204                 if (s->forward_to_kmsg)
1205                         forward_kmsg(s, priority, identifier, message, ucred);
1206
1207                 if (s->forward_to_console)
1208                         forward_console(s, identifier, message, ucred);
1209         }
1210
1211         dispatch_message(s, iovec, n, m, ucred, tv, priority);
1212
1213 finish:
1214         for (j = 0; j < n; j++)  {
1215                 if (j == tn)
1216                         continue;
1217
1218                 if (iovec[j].iov_base < buffer ||
1219                     (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
1220                         free(iovec[j].iov_base);
1221         }
1222
1223         free(identifier);
1224         free(message);
1225 }
1226
1227 static int stdout_stream_log(StdoutStream *s, const char *p) {
1228         struct iovec iovec[N_IOVEC_META_FIELDS + 5];
1229         char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
1230         unsigned n = 0;
1231         int priority;
1232
1233         assert(s);
1234         assert(p);
1235
1236         priority = s->priority;
1237
1238         if (s->level_prefix)
1239                 parse_syslog_priority((char**) &p, &priority);
1240
1241         if (s->forward_to_syslog || s->server->forward_to_syslog)
1242                 forward_syslog(s->server, fixup_priority(priority), s->identifier, p, &s->ucred, NULL);
1243
1244         if (s->forward_to_kmsg || s->server->forward_to_kmsg)
1245                 forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
1246
1247         if (s->forward_to_console || s->server->forward_to_console)
1248                 forward_console(s->server, s->identifier, p, &s->ucred);
1249
1250         IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
1251
1252         if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1253                 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1254
1255         if (priority & LOG_FACMASK)
1256                 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1257                         IOVEC_SET_STRING(iovec[n++], syslog_facility);
1258
1259         if (s->identifier) {
1260                 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
1261                 if (syslog_identifier)
1262                         IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1263         }
1264
1265         message = strappend("MESSAGE=", p);
1266         if (message)
1267                 IOVEC_SET_STRING(iovec[n++], message);
1268
1269         dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, priority);
1270
1271         free(message);
1272         free(syslog_priority);
1273         free(syslog_facility);
1274         free(syslog_identifier);
1275
1276         return 0;
1277 }
1278
1279 static int stdout_stream_line(StdoutStream *s, char *p) {
1280         int r;
1281
1282         assert(s);
1283         assert(p);
1284
1285         p = strstrip(p);
1286
1287         switch (s->state) {
1288
1289         case STDOUT_STREAM_IDENTIFIER:
1290                 s->identifier = strdup(p);
1291                 if (!s->identifier) {
1292                         log_error("Out of memory");
1293                         return -ENOMEM;
1294                 }
1295
1296                 s->state = STDOUT_STREAM_PRIORITY;
1297                 return 0;
1298
1299         case STDOUT_STREAM_PRIORITY:
1300                 r = safe_atoi(p, &s->priority);
1301                 if (r < 0 || s->priority <= 0 || s->priority >= 999) {
1302                         log_warning("Failed to parse log priority line.");
1303                         return -EINVAL;
1304                 }
1305
1306                 s->state = STDOUT_STREAM_LEVEL_PREFIX;
1307                 return 0;
1308
1309         case STDOUT_STREAM_LEVEL_PREFIX:
1310                 r = parse_boolean(p);
1311                 if (r < 0) {
1312                         log_warning("Failed to parse level prefix line.");
1313                         return -EINVAL;
1314                 }
1315
1316                 s->level_prefix = !!r;
1317                 s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
1318                 return 0;
1319
1320         case STDOUT_STREAM_FORWARD_TO_SYSLOG:
1321                 r = parse_boolean(p);
1322                 if (r < 0) {
1323                         log_warning("Failed to parse forward to syslog line.");
1324                         return -EINVAL;
1325                 }
1326
1327                 s->forward_to_syslog = !!r;
1328                 s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
1329                 return 0;
1330
1331         case STDOUT_STREAM_FORWARD_TO_KMSG:
1332                 r = parse_boolean(p);
1333                 if (r < 0) {
1334                         log_warning("Failed to parse copy to kmsg line.");
1335                         return -EINVAL;
1336                 }
1337
1338                 s->forward_to_kmsg = !!r;
1339                 s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
1340                 return 0;
1341
1342         case STDOUT_STREAM_FORWARD_TO_CONSOLE:
1343                 r = parse_boolean(p);
1344                 if (r < 0) {
1345                         log_warning("Failed to parse copy to console line.");
1346                         return -EINVAL;
1347                 }
1348
1349                 s->forward_to_console = !!r;
1350                 s->state = STDOUT_STREAM_RUNNING;
1351                 return 0;
1352
1353         case STDOUT_STREAM_RUNNING:
1354                 return stdout_stream_log(s, p);
1355         }
1356
1357         assert_not_reached("Unknown stream state");
1358 }
1359
1360 static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
1361         char *p;
1362         size_t remaining;
1363         int r;
1364
1365         assert(s);
1366
1367         p = s->buffer;
1368         remaining = s->length;
1369         for (;;) {
1370                 char *end;
1371                 size_t skip;
1372
1373                 end = memchr(p, '\n', remaining);
1374                 if (end)
1375                         skip = end - p + 1;
1376                 else if (remaining >= sizeof(s->buffer) - 1) {
1377                         end = p + sizeof(s->buffer) - 1;
1378                         skip = sizeof(s->buffer) - 1;
1379                 } else
1380                         break;
1381
1382                 *end = 0;
1383
1384                 r = stdout_stream_line(s, p);
1385                 if (r < 0)
1386                         return r;
1387
1388                 remaining -= skip;
1389                 p += skip;
1390         }
1391
1392         if (force_flush && remaining > 0) {
1393                 p[remaining] = 0;
1394                 r = stdout_stream_line(s, p);
1395                 if (r < 0)
1396                         return r;
1397
1398                 p += remaining;
1399                 remaining = 0;
1400         }
1401
1402         if (p > s->buffer) {
1403                 memmove(s->buffer, p, remaining);
1404                 s->length = remaining;
1405         }
1406
1407         return 0;
1408 }
1409
1410 static int stdout_stream_process(StdoutStream *s) {
1411         ssize_t l;
1412         int r;
1413
1414         assert(s);
1415
1416         l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
1417         if (l < 0) {
1418
1419                 if (errno == EAGAIN)
1420                         return 0;
1421
1422                 log_warning("Failed to read from stream: %m");
1423                 return -errno;
1424         }
1425
1426         if (l == 0) {
1427                 r = stdout_stream_scan(s, true);
1428                 if (r < 0)
1429                         return r;
1430
1431                 return 0;
1432         }
1433
1434         s->length += l;
1435         r = stdout_stream_scan(s, false);
1436         if (r < 0)
1437                 return r;
1438
1439         return 1;
1440
1441 }
1442
1443 static void stdout_stream_free(StdoutStream *s) {
1444         assert(s);
1445
1446         if (s->server) {
1447                 assert(s->server->n_stdout_streams > 0);
1448                 s->server->n_stdout_streams --;
1449                 LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
1450         }
1451
1452         if (s->fd >= 0) {
1453                 if (s->server)
1454                         epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
1455
1456                 close_nointr_nofail(s->fd);
1457         }
1458
1459         free(s->identifier);
1460         free(s);
1461 }
1462
1463 static int stdout_stream_new(Server *s) {
1464         StdoutStream *stream;
1465         int fd, r;
1466         socklen_t len;
1467         struct epoll_event ev;
1468
1469         assert(s);
1470
1471         fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1472         if (fd < 0) {
1473                 if (errno == EAGAIN)
1474                         return 0;
1475
1476                 log_error("Failed to accept stdout connection: %m");
1477                 return -errno;
1478         }
1479
1480         if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
1481                 log_warning("Too many stdout streams, refusing connection.");
1482                 close_nointr_nofail(fd);
1483                 return 0;
1484         }
1485
1486         stream = new0(StdoutStream, 1);
1487         if (!stream) {
1488                 log_error("Out of memory.");
1489                 close_nointr_nofail(fd);
1490                 return -ENOMEM;
1491         }
1492
1493         stream->fd = fd;
1494
1495         len = sizeof(stream->ucred);
1496         if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
1497                 log_error("Failed to determine peer credentials: %m");
1498                 r = -errno;
1499                 goto fail;
1500         }
1501
1502         if (shutdown(fd, SHUT_WR) < 0) {
1503                 log_error("Failed to shutdown writing side of socket: %m");
1504                 r = -errno;
1505                 goto fail;
1506         }
1507
1508         zero(ev);
1509         ev.data.ptr = stream;
1510         ev.events = EPOLLIN;
1511         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
1512                 log_error("Failed to add stream to event loop: %m");
1513                 r = -errno;
1514                 goto fail;
1515         }
1516
1517         stream->server = s;
1518         LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
1519         s->n_stdout_streams ++;
1520
1521         return 0;
1522
1523 fail:
1524         stdout_stream_free(stream);
1525         return r;
1526 }
1527
1528 static int system_journal_open(Server *s) {
1529         int r;
1530         char *fn;
1531         sd_id128_t machine;
1532         char ids[33];
1533
1534         r = sd_id128_get_machine(&machine);
1535         if (r < 0)
1536                 return r;
1537
1538         sd_id128_to_string(machine, ids);
1539
1540         if (!s->system_journal) {
1541
1542                 /* First try to create the machine path, but not the prefix */
1543                 fn = strappend("/var/log/journal/", ids);
1544                 if (!fn)
1545                         return -ENOMEM;
1546                 (void) mkdir(fn, 0755);
1547                 free(fn);
1548
1549                 /* The create the system journal file */
1550                 fn = join("/var/log/journal/", ids, "/system.journal", NULL);
1551                 if (!fn)
1552                         return -ENOMEM;
1553
1554                 r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->system_journal);
1555                 free(fn);
1556
1557                 if (r >= 0) {
1558                         journal_default_metrics(&s->system_metrics, s->system_journal->fd);
1559
1560                         s->system_journal->metrics = s->system_metrics;
1561                         s->system_journal->compress = s->compress;
1562
1563                         fix_perms(s->system_journal, 0);
1564                 } else if (r < 0) {
1565
1566                         if (r != -ENOENT && r != -EROFS)
1567                                 log_warning("Failed to open system journal: %s", strerror(-r));
1568
1569                         r = 0;
1570                 }
1571         }
1572
1573         if (!s->runtime_journal) {
1574
1575                 fn = join("/run/log/journal/", ids, "/system.journal", NULL);
1576                 if (!fn)
1577                         return -ENOMEM;
1578
1579                 if (s->system_journal) {
1580
1581                         /* Try to open the runtime journal, but only
1582                          * if it already exists, so that we can flush
1583                          * it into the system journal */
1584
1585                         r = journal_file_open(fn, O_RDWR, 0640, NULL, &s->runtime_journal);
1586                         free(fn);
1587
1588                         if (r < 0) {
1589                                 if (r != -ENOENT)
1590                                         log_warning("Failed to open runtime journal: %s", strerror(-r));
1591
1592                                 r = 0;
1593                         }
1594
1595                 } else {
1596
1597                         /* OK, we really need the runtime journal, so create
1598                          * it if necessary. */
1599
1600                         (void) mkdir_parents(fn, 0755);
1601                         r = journal_file_open(fn, O_RDWR|O_CREAT, 0640, NULL, &s->runtime_journal);
1602                         free(fn);
1603
1604                         if (r < 0) {
1605                                 log_error("Failed to open runtime journal: %s", strerror(-r));
1606                                 return r;
1607                         }
1608                 }
1609
1610                 if (s->runtime_journal) {
1611                         journal_default_metrics(&s->runtime_metrics, s->runtime_journal->fd);
1612
1613                         s->runtime_journal->metrics = s->runtime_metrics;
1614                         s->runtime_journal->compress = s->compress;
1615
1616                         fix_perms(s->runtime_journal, 0);
1617                 }
1618         }
1619
1620         return r;
1621 }
1622
1623 static int server_flush_to_var(Server *s) {
1624         char path[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1625         Object *o = NULL;
1626         int r;
1627         sd_id128_t machine;
1628         sd_journal *j;
1629         usec_t ts;
1630
1631         assert(s);
1632
1633         if (!s->runtime_journal)
1634                 return 0;
1635
1636         ts = now(CLOCK_MONOTONIC);
1637         if (s->var_available_timestamp + RECHECK_VAR_AVAILABLE_USEC > ts)
1638                 return 0;
1639
1640         s->var_available_timestamp = ts;
1641
1642         system_journal_open(s);
1643
1644         if (!s->system_journal)
1645                 return 0;
1646
1647         r = sd_id128_get_machine(&machine);
1648         if (r < 0) {
1649                 log_error("Failed to get machine id: %s", strerror(-r));
1650                 return r;
1651         }
1652
1653         r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
1654         if (r < 0) {
1655                 log_error("Failed to read runtime journal: %s", strerror(-r));
1656                 return r;
1657         }
1658
1659         SD_JOURNAL_FOREACH(j) {
1660                 JournalFile *f;
1661
1662                 f = j->current_file;
1663                 assert(f && f->current_offset > 0);
1664
1665                 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
1666                 if (r < 0) {
1667                         log_error("Can't read entry: %s", strerror(-r));
1668                         goto finish;
1669                 }
1670
1671                 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
1672                 if (r == -E2BIG) {
1673                         log_info("Allocation limit reached.");
1674
1675                         journal_file_post_change(s->system_journal);
1676                         server_rotate(s);
1677                         server_vacuum(s);
1678
1679                         r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
1680                 }
1681
1682                 if (r < 0) {
1683                         log_error("Can't write entry: %s", strerror(-r));
1684                         goto finish;
1685                 }
1686         }
1687
1688 finish:
1689         journal_file_post_change(s->system_journal);
1690
1691         journal_file_close(s->runtime_journal);
1692         s->runtime_journal = NULL;
1693
1694         if (r >= 0) {
1695                 sd_id128_to_string(machine, path + 17);
1696                 rm_rf(path, false, true, false);
1697         }
1698
1699         return r;
1700 }
1701
1702 static int process_event(Server *s, struct epoll_event *ev) {
1703         assert(s);
1704
1705         if (ev->data.fd == s->signal_fd) {
1706                 struct signalfd_siginfo sfsi;
1707                 ssize_t n;
1708
1709                 if (ev->events != EPOLLIN) {
1710                         log_info("Got invalid event from epoll.");
1711                         return -EIO;
1712                 }
1713
1714                 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
1715                 if (n != sizeof(sfsi)) {
1716
1717                         if (n >= 0)
1718                                 return -EIO;
1719
1720                         if (errno == EINTR || errno == EAGAIN)
1721                                 return 0;
1722
1723                         return -errno;
1724                 }
1725
1726                 if (sfsi.ssi_signo == SIGUSR1) {
1727                         server_flush_to_var(s);
1728                         return 0;
1729                 }
1730
1731                 log_debug("Received SIG%s", signal_to_string(sfsi.ssi_signo));
1732                 return 0;
1733
1734         } else if (ev->data.fd == s->native_fd ||
1735                    ev->data.fd == s->syslog_fd) {
1736
1737                 if (ev->events != EPOLLIN) {
1738                         log_info("Got invalid event from epoll.");
1739                         return -EIO;
1740                 }
1741
1742                 for (;;) {
1743                         struct msghdr msghdr;
1744                         struct iovec iovec;
1745                         struct ucred *ucred = NULL;
1746                         struct timeval *tv = NULL;
1747                         struct cmsghdr *cmsg;
1748                         union {
1749                                 struct cmsghdr cmsghdr;
1750                                 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
1751                                             CMSG_SPACE(sizeof(struct timeval))];
1752                         } control;
1753                         ssize_t n;
1754                         int v;
1755
1756                         if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
1757                                 log_error("SIOCINQ failed: %m");
1758                                 return -errno;
1759                         }
1760
1761                         if (v <= 0)
1762                                 return 1;
1763
1764                         if (s->buffer_size < (size_t) v) {
1765                                 void *b;
1766                                 size_t l;
1767
1768                                 l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
1769                                 b = realloc(s->buffer, l+1);
1770
1771                                 if (!b) {
1772                                         log_error("Couldn't increase buffer.");
1773                                         return -ENOMEM;
1774                                 }
1775
1776                                 s->buffer_size = l;
1777                                 s->buffer = b;
1778                         }
1779
1780                         zero(iovec);
1781                         iovec.iov_base = s->buffer;
1782                         iovec.iov_len = s->buffer_size;
1783
1784                         zero(control);
1785                         zero(msghdr);
1786                         msghdr.msg_iov = &iovec;
1787                         msghdr.msg_iovlen = 1;
1788                         msghdr.msg_control = &control;
1789                         msghdr.msg_controllen = sizeof(control);
1790
1791                         n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT);
1792                         if (n < 0) {
1793
1794                                 if (errno == EINTR || errno == EAGAIN)
1795                                         return 1;
1796
1797                                 log_error("recvmsg() failed: %m");
1798                                 return -errno;
1799                         }
1800
1801                         for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
1802
1803                                 if (cmsg->cmsg_level == SOL_SOCKET &&
1804                                     cmsg->cmsg_type == SCM_CREDENTIALS &&
1805                                     cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
1806                                         ucred = (struct ucred*) CMSG_DATA(cmsg);
1807                                 else if (cmsg->cmsg_level == SOL_SOCKET &&
1808                                          cmsg->cmsg_type == SO_TIMESTAMP &&
1809                                          cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
1810                                         tv = (struct timeval*) CMSG_DATA(cmsg);
1811                         }
1812
1813                         if (ev->data.fd == s->syslog_fd) {
1814                                 char *e;
1815
1816                                 e = memchr(s->buffer, '\n', n);
1817                                 if (e)
1818                                         *e = 0;
1819                                 else
1820                                         s->buffer[n] = 0;
1821
1822                                 process_syslog_message(s, strstrip(s->buffer), ucred, tv);
1823                         } else
1824                                 process_native_message(s, s->buffer, n, ucred, tv);
1825                 }
1826
1827                 return 1;
1828
1829         } else if (ev->data.fd == s->stdout_fd) {
1830
1831                 if (ev->events != EPOLLIN) {
1832                         log_info("Got invalid event from epoll.");
1833                         return -EIO;
1834                 }
1835
1836                 stdout_stream_new(s);
1837                 return 1;
1838
1839         } else {
1840                 StdoutStream *stream;
1841
1842                 if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
1843                         log_info("Got invalid event from epoll.");
1844                         return -EIO;
1845                 }
1846
1847                 /* If it is none of the well-known fds, it must be an
1848                  * stdout stream fd. Note that this is a bit ugly here
1849                  * (since we rely that none of the well-known fds
1850                  * could be interpreted as pointer), but nonetheless
1851                  * safe, since the well-known fds would never get an
1852                  * fd > 4096, i.e. beyond the first memory page */
1853
1854                 stream = ev->data.ptr;
1855
1856                 if (stdout_stream_process(stream) <= 0)
1857                         stdout_stream_free(stream);
1858
1859                 return 1;
1860         }
1861
1862         log_error("Unknown event.");
1863         return 0;
1864 }
1865
1866 static int open_syslog_socket(Server *s) {
1867         union sockaddr_union sa;
1868         int one, r;
1869         struct epoll_event ev;
1870         struct timeval tv;
1871
1872         assert(s);
1873
1874         if (s->syslog_fd < 0) {
1875
1876                 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1877                 if (s->syslog_fd < 0) {
1878                         log_error("socket() failed: %m");
1879                         return -errno;
1880                 }
1881
1882                 zero(sa);
1883                 sa.un.sun_family = AF_UNIX;
1884                 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
1885
1886                 unlink(sa.un.sun_path);
1887
1888                 r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
1889                 if (r < 0) {
1890                         log_error("bind() failed: %m");
1891                         return -errno;
1892                 }
1893
1894                 chmod(sa.un.sun_path, 0666);
1895         }
1896
1897         one = 1;
1898         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
1899         if (r < 0) {
1900                 log_error("SO_PASSCRED failed: %m");
1901                 return -errno;
1902         }
1903
1904         one = 1;
1905         r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
1906         if (r < 0) {
1907                 log_error("SO_TIMESTAMP failed: %m");
1908                 return -errno;
1909         }
1910
1911         /* Since we use the same socket for forwarding this to some
1912          * other syslog implementation, make sure we don't hang
1913          * forever */
1914         timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
1915         if (setsockopt(s->syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
1916                 log_error("SO_SNDTIMEO failed: %m");
1917                 return -errno;
1918         }
1919
1920         zero(ev);
1921         ev.events = EPOLLIN;
1922         ev.data.fd = s->syslog_fd;
1923         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
1924                 log_error("Failed to add syslog server fd to epoll object: %m");
1925                 return -errno;
1926         }
1927
1928         return 0;
1929 }
1930
1931 static int open_native_socket(Server*s) {
1932         union sockaddr_union sa;
1933         int one, r;
1934         struct epoll_event ev;
1935
1936         assert(s);
1937
1938         if (s->native_fd < 0) {
1939
1940                 s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0);
1941                 if (s->native_fd < 0) {
1942                         log_error("socket() failed: %m");
1943                         return -errno;
1944                 }
1945
1946                 zero(sa);
1947                 sa.un.sun_family = AF_UNIX;
1948                 strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
1949
1950                 unlink(sa.un.sun_path);
1951
1952                 r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
1953                 if (r < 0) {
1954                         log_error("bind() failed: %m");
1955                         return -errno;
1956                 }
1957
1958                 chmod(sa.un.sun_path, 0666);
1959         }
1960
1961         one = 1;
1962         r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
1963         if (r < 0) {
1964                 log_error("SO_PASSCRED failed: %m");
1965                 return -errno;
1966         }
1967
1968         one = 1;
1969         r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
1970         if (r < 0) {
1971                 log_error("SO_TIMESTAMP failed: %m");
1972                 return -errno;
1973         }
1974
1975         zero(ev);
1976         ev.events = EPOLLIN;
1977         ev.data.fd = s->native_fd;
1978         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) {
1979                 log_error("Failed to add native server fd to epoll object: %m");
1980                 return -errno;
1981         }
1982
1983         return 0;
1984 }
1985
1986 static int open_stdout_socket(Server *s) {
1987         union sockaddr_union sa;
1988         int r;
1989         struct epoll_event ev;
1990
1991         assert(s);
1992
1993         if (s->stdout_fd < 0) {
1994
1995                 s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0);
1996                 if (s->stdout_fd < 0) {
1997                         log_error("socket() failed: %m");
1998                         return -errno;
1999                 }
2000
2001                 zero(sa);
2002                 sa.un.sun_family = AF_UNIX;
2003                 strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
2004
2005                 unlink(sa.un.sun_path);
2006
2007                 r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2008                 if (r < 0) {
2009                         log_error("bind() failed: %m");
2010                         return -errno;
2011                 }
2012
2013                 chmod(sa.un.sun_path, 0666);
2014
2015                 if (listen(s->stdout_fd, SOMAXCONN) < 0) {
2016                         log_error("liste() failed: %m");
2017                         return -errno;
2018                 }
2019         }
2020
2021         zero(ev);
2022         ev.events = EPOLLIN;
2023         ev.data.fd = s->stdout_fd;
2024         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
2025                 log_error("Failed to add stdout server fd to epoll object: %m");
2026                 return -errno;
2027         }
2028
2029         return 0;
2030 }
2031
2032 static int open_signalfd(Server *s) {
2033         sigset_t mask;
2034         struct epoll_event ev;
2035
2036         assert(s);
2037
2038         assert_se(sigemptyset(&mask) == 0);
2039         sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, -1);
2040         assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
2041
2042         s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
2043         if (s->signal_fd < 0) {
2044                 log_error("signalfd(): %m");
2045                 return -errno;
2046         }
2047
2048         zero(ev);
2049         ev.events = EPOLLIN;
2050         ev.data.fd = s->signal_fd;
2051
2052         if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
2053                 log_error("epoll_ctl(): %m");
2054                 return -errno;
2055         }
2056
2057         return 0;
2058 }
2059
2060 static int server_parse_config_file(Server *s) {
2061         FILE *f;
2062         const char *fn;
2063         int r;
2064
2065         assert(s);
2066
2067         fn = "/etc/systemd/systemd-journald.conf";
2068         f = fopen(fn, "re");
2069         if (!f) {
2070                 if (errno == ENOENT)
2071                         return 0;
2072
2073                 log_warning("Failed to open configuration file %s: %m", fn);
2074                 return -errno;
2075         }
2076
2077         r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
2078         if (r < 0)
2079                 log_warning("Failed to parse configuration file: %s", strerror(-r));
2080
2081         fclose(f);
2082
2083         return r;
2084 }
2085
2086 static int server_init(Server *s) {
2087         int n, r, fd;
2088
2089         assert(s);
2090
2091         zero(*s);
2092         s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = -1;
2093         s->compress = true;
2094
2095         s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
2096         s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
2097
2098         s->forward_to_syslog = true;
2099
2100         memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
2101         memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
2102
2103         server_parse_config_file(s);
2104
2105         s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
2106         if (!s->user_journals) {
2107                 log_error("Out of memory.");
2108                 return -ENOMEM;
2109         }
2110
2111         s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2112         if (s->epoll_fd < 0) {
2113                 log_error("Failed to create epoll object: %m");
2114                 return -errno;
2115         }
2116
2117         n = sd_listen_fds(true);
2118         if (n < 0) {
2119                 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
2120                 return n;
2121         }
2122
2123         for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
2124
2125                 if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
2126
2127                         if (s->native_fd >= 0) {
2128                                 log_error("Too many native sockets passed.");
2129                                 return -EINVAL;
2130                         }
2131
2132                         s->native_fd = fd;
2133
2134                 } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
2135
2136                         if (s->stdout_fd >= 0) {
2137                                 log_error("Too many stdout sockets passed.");
2138                                 return -EINVAL;
2139                         }
2140
2141                         s->stdout_fd = fd;
2142
2143                 } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
2144
2145                         if (s->syslog_fd >= 0) {
2146                                 log_error("Too many /dev/log sockets passed.");
2147                                 return -EINVAL;
2148                         }
2149
2150                         s->syslog_fd = fd;
2151
2152                 } else {
2153                         log_error("Unknown socket passed.");
2154                         return -EINVAL;
2155                 }
2156         }
2157
2158         r = open_syslog_socket(s);
2159         if (r < 0)
2160                 return r;
2161
2162         r = open_native_socket(s);
2163         if (r < 0)
2164                 return r;
2165
2166         r = open_stdout_socket(s);
2167         if (r < 0)
2168                 return r;
2169
2170         r = system_journal_open(s);
2171         if (r < 0)
2172                 return r;
2173
2174         r = open_signalfd(s);
2175         if (r < 0)
2176                 return r;
2177
2178         s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
2179         if (!s->rate_limit)
2180                 return -ENOMEM;
2181
2182         return 0;
2183 }
2184
2185 static void server_done(Server *s) {
2186         JournalFile *f;
2187         assert(s);
2188
2189         while (s->stdout_streams)
2190                 stdout_stream_free(s->stdout_streams);
2191
2192         if (s->system_journal)
2193                 journal_file_close(s->system_journal);
2194
2195         if (s->runtime_journal)
2196                 journal_file_close(s->runtime_journal);
2197
2198         while ((f = hashmap_steal_first(s->user_journals)))
2199                 journal_file_close(f);
2200
2201         hashmap_free(s->user_journals);
2202
2203         if (s->epoll_fd >= 0)
2204                 close_nointr_nofail(s->epoll_fd);
2205
2206         if (s->signal_fd >= 0)
2207                 close_nointr_nofail(s->signal_fd);
2208
2209         if (s->syslog_fd >= 0)
2210                 close_nointr_nofail(s->syslog_fd);
2211
2212         if (s->native_fd >= 0)
2213                 close_nointr_nofail(s->native_fd);
2214
2215         if (s->stdout_fd >= 0)
2216                 close_nointr_nofail(s->stdout_fd);
2217
2218         if (s->rate_limit)
2219                 journal_rate_limit_free(s->rate_limit);
2220
2221         free(s->buffer);
2222 }
2223
2224 int main(int argc, char *argv[]) {
2225         Server server;
2226         int r;
2227
2228         /* if (getppid() != 1) { */
2229         /*         log_error("This program should be invoked by init only."); */
2230         /*         return EXIT_FAILURE; */
2231         /* } */
2232
2233         if (argc > 1) {
2234                 log_error("This program does not take arguments.");
2235                 return EXIT_FAILURE;
2236         }
2237
2238         log_set_target(LOG_TARGET_CONSOLE);
2239         log_parse_environment();
2240         log_open();
2241
2242         umask(0022);
2243
2244         r = server_init(&server);
2245         if (r < 0)
2246                 goto finish;
2247
2248         server_vacuum(&server);
2249         server_flush_to_var(&server);
2250
2251         log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
2252         driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
2253
2254         sd_notify(false,
2255                   "READY=1\n"
2256                   "STATUS=Processing requests...");
2257
2258         for (;;) {
2259                 struct epoll_event event;
2260
2261                 r = epoll_wait(server.epoll_fd, &event, 1, -1);
2262                 if (r < 0) {
2263
2264                         if (errno == EINTR)
2265                                 continue;
2266
2267                         log_error("epoll_wait() failed: %m");
2268                         r = -errno;
2269                         goto finish;
2270                 } else if (r == 0)
2271                         break;
2272
2273                 r = process_event(&server, &event);
2274                 if (r < 0)
2275                         goto finish;
2276                 else if (r == 0)
2277                         break;
2278         }
2279
2280         log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
2281         driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
2282
2283 finish:
2284         sd_notify(false,
2285                   "STATUS=Shutting down...");
2286
2287         server_done(&server);
2288
2289         return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
2290 }