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