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