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