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