1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
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.
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.
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/>.
22 #include <sys/epoll.h>
23 #include <sys/socket.h>
25 #include <sys/signalfd.h>
29 #include <sys/ioctl.h>
30 #include <linux/sockios.h>
31 #include <sys/statvfs.h>
35 #include <systemd/sd-journal.h>
36 #include <systemd/sd-messages.h>
37 #include <systemd/sd-daemon.h>
40 #include <systemd/sd-login.h>
45 #include "journal-file.h"
46 #include "socket-util.h"
47 #include "cgroup-util.h"
49 #include "journal-rate-limit.h"
50 #include "journal-internal.h"
51 #include "journal-vacuum.h"
52 #include "journal-authenticate.h"
53 #include "conf-parser.h"
60 #include <acl/libacl.h>
65 #include <selinux/selinux.h>
68 #define USER_JOURNALS_MAX 1024
69 #define STDOUT_STREAMS_MAX 4096
71 #define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
72 #define DEFAULT_RATE_LIMIT_BURST 200
74 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
76 #define N_IOVEC_META_FIELDS 17
77 #define N_IOVEC_KERNEL_FIELDS 64
78 #define N_IOVEC_UDEV_FIELDS 32
80 #define ENTRY_SIZE_MAX (1024*1024*32)
82 typedef enum StdoutStreamState {
83 STDOUT_STREAM_IDENTIFIER,
84 STDOUT_STREAM_UNIT_ID,
85 STDOUT_STREAM_PRIORITY,
86 STDOUT_STREAM_LEVEL_PREFIX,
87 STDOUT_STREAM_FORWARD_TO_SYSLOG,
88 STDOUT_STREAM_FORWARD_TO_KMSG,
89 STDOUT_STREAM_FORWARD_TO_CONSOLE,
95 StdoutStreamState state;
101 security_context_t security_context;
108 bool forward_to_syslog:1;
109 bool forward_to_kmsg:1;
110 bool forward_to_console:1;
112 char buffer[LINE_MAX+1];
115 LIST_FIELDS(StdoutStream, stdout_stream);
118 static const char* const storage_table[] = {
119 [STORAGE_AUTO] = "auto",
120 [STORAGE_VOLATILE] = "volatile",
121 [STORAGE_PERSISTENT] = "persistent",
122 [STORAGE_NONE] = "none"
125 DEFINE_STRING_TABLE_LOOKUP(storage, Storage);
126 DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage, "Failed to parse storage setting");
128 static uint64_t available_space(Server *s) {
133 uint64_t sum = 0, avail = 0, ss_avail = 0;
139 ts = now(CLOCK_MONOTONIC);
141 if (s->cached_available_space_timestamp + RECHECK_AVAILABLE_SPACE_USEC > ts)
142 return s->cached_available_space;
144 r = sd_id128_get_machine(&machine);
148 if (s->system_journal) {
149 f = "/var/log/journal/";
150 m = &s->system_metrics;
152 f = "/run/log/journal/";
153 m = &s->runtime_metrics;
158 p = strappend(f, sd_id128_to_string(machine, ids));
168 if (fstatvfs(dirfd(d), &ss) < 0)
173 struct dirent buf, *de;
175 r = readdir_r(d, &buf, &de);
182 if (!endswith(de->d_name, ".journal") &&
183 !endswith(de->d_name, ".journal~"))
186 if (fstatat(dirfd(d), de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0)
189 if (!S_ISREG(st.st_mode))
192 sum += (uint64_t) st.st_blocks * 512UL;
195 avail = sum >= m->max_use ? 0 : m->max_use - sum;
197 ss_avail = ss.f_bsize * ss.f_bavail;
199 ss_avail = ss_avail < m->keep_free ? 0 : ss_avail - m->keep_free;
201 if (ss_avail < avail)
204 s->cached_available_space = avail;
205 s->cached_available_space_timestamp = ts;
213 static void server_read_file_gid(Server *s) {
214 const char *adm = "adm";
219 if (s->file_gid_valid)
222 r = get_group_creds(&adm, &s->file_gid);
224 log_warning("Failed to resolve 'adm' group: %s", strerror(-r));
226 /* if we couldn't read the gid, then it will be 0, but that's
227 * fine and we shouldn't try to resolve the group again, so
228 * let's just pretend it worked right-away. */
229 s->file_gid_valid = true;
232 static void server_fix_perms(Server *s, JournalFile *f, uid_t uid) {
237 acl_permset_t permset;
242 server_read_file_gid(s);
244 r = fchmod_and_fchown(f->fd, 0640, 0, s->file_gid);
246 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f->path, strerror(-r));
252 acl = acl_get_fd(f->fd);
254 log_warning("Failed to read ACL on %s, ignoring: %m", f->path);
258 r = acl_find_uid(acl, uid, &entry);
261 if (acl_create_entry(&acl, &entry) < 0 ||
262 acl_set_tag_type(entry, ACL_USER) < 0 ||
263 acl_set_qualifier(entry, &uid) < 0) {
264 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
269 if (acl_get_permset(entry, &permset) < 0 ||
270 acl_add_perm(permset, ACL_READ) < 0 ||
271 acl_calc_mask(&acl) < 0) {
272 log_warning("Failed to patch ACL on %s, ignoring: %m", f->path);
276 if (acl_set_fd(f->fd, acl) < 0)
277 log_warning("Failed to set ACL on %s, ignoring: %m", f->path);
284 static JournalFile* find_journal(Server *s, uid_t uid) {
292 /* We split up user logs only on /var, not on /run. If the
293 * runtime file is open, we write to it exclusively, in order
294 * to guarantee proper order as soon as we flush /run to
295 * /var and close the runtime file. */
297 if (s->runtime_journal)
298 return s->runtime_journal;
301 return s->system_journal;
303 r = sd_id128_get_machine(&machine);
305 return s->system_journal;
307 f = hashmap_get(s->user_journals, UINT32_TO_PTR(uid));
311 if (asprintf(&p, "/var/log/journal/" SD_ID128_FORMAT_STR "/user-%lu.journal",
312 SD_ID128_FORMAT_VAL(machine), (unsigned long) uid) < 0)
313 return s->system_journal;
315 while (hashmap_size(s->user_journals) >= USER_JOURNALS_MAX) {
316 /* Too many open? Then let's close one */
317 f = hashmap_steal_first(s->user_journals);
319 journal_file_close(f);
322 r = journal_file_open_reliably(p, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, s->system_journal, &f);
326 return s->system_journal;
328 server_fix_perms(s, f, uid);
330 r = hashmap_put(s->user_journals, UINT32_TO_PTR(uid), f);
332 journal_file_close(f);
333 return s->system_journal;
339 static void server_rotate(Server *s) {
345 log_info("Rotating...");
347 if (s->runtime_journal) {
348 r = journal_file_rotate(&s->runtime_journal, s->compress, false);
350 if (s->runtime_journal)
351 log_error("Failed to rotate %s: %s", s->runtime_journal->path, strerror(-r));
353 log_error("Failed to create new runtime journal: %s", strerror(-r));
355 server_fix_perms(s, s->runtime_journal, 0);
358 if (s->system_journal) {
359 r = journal_file_rotate(&s->system_journal, s->compress, s->seal);
361 if (s->system_journal)
362 log_error("Failed to rotate %s: %s", s->system_journal->path, strerror(-r));
364 log_error("Failed to create new system journal: %s", strerror(-r));
367 server_fix_perms(s, s->system_journal, 0);
370 HASHMAP_FOREACH_KEY(f, k, s->user_journals, i) {
371 r = journal_file_rotate(&f, s->compress, s->seal);
374 log_error("Failed to rotate %s: %s", f->path, strerror(-r));
376 log_error("Failed to create user journal: %s", strerror(-r));
378 hashmap_replace(s->user_journals, k, f);
379 server_fix_perms(s, s->system_journal, PTR_TO_UINT32(k));
384 static void server_vacuum(Server *s) {
390 log_info("Vacuuming...");
392 r = sd_id128_get_machine(&machine);
394 log_error("Failed to get machine ID: %s", strerror(-r));
398 sd_id128_to_string(machine, ids);
400 if (s->system_journal) {
401 if (asprintf(&p, "/var/log/journal/%s", ids) < 0) {
406 r = journal_directory_vacuum(p, s->system_metrics.max_use, s->system_metrics.keep_free);
407 if (r < 0 && r != -ENOENT)
408 log_error("Failed to vacuum %s: %s", p, strerror(-r));
412 if (s->runtime_journal) {
413 if (asprintf(&p, "/run/log/journal/%s", ids) < 0) {
418 r = journal_directory_vacuum(p, s->runtime_metrics.max_use, s->runtime_metrics.keep_free);
419 if (r < 0 && r != -ENOENT)
420 log_error("Failed to vacuum %s: %s", p, strerror(-r));
424 s->cached_available_space_timestamp = 0;
427 static char *shortened_cgroup_path(pid_t pid) {
429 char *process_path, *init_path, *path;
433 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, pid, &process_path);
437 r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 1, &init_path);
443 if (endswith(init_path, "/system"))
444 init_path[strlen(init_path) - 7] = 0;
445 else if (streq(init_path, "/"))
448 if (startswith(process_path, init_path)) {
451 p = strdup(process_path + strlen(init_path));
469 static void write_to_journal(Server *s, uid_t uid, struct iovec *iovec, unsigned n) {
471 bool vacuumed = false;
478 f = find_journal(s, uid);
482 if (journal_file_rotate_suggested(f)) {
483 log_info("Journal header limits reached or header out-of-date, rotating.");
488 f = find_journal(s, uid);
494 r = journal_file_append_entry(f, NULL, iovec, n, &s->seqnum, NULL, NULL);
499 (r != -E2BIG && /* hit limit */
500 r != -EFBIG && /* hit fs limit */
501 r != -EDQUOT && /* quota hit */
502 r != -ENOSPC && /* disk full */
503 r != -EBADMSG && /* corrupted */
504 r != -ENODATA && /* truncated */
505 r != -EHOSTDOWN && /* other machine */
506 r != -EPROTONOSUPPORT && /* unsupported feature */
507 r != -EBUSY && /* unclean shutdown */
508 r != -ESHUTDOWN /* already archived */)) {
509 log_error("Failed to write entry, ignoring: %s", strerror(-r));
513 if (r == -E2BIG || r == -EFBIG || r == EDQUOT || r == ENOSPC)
514 log_info("Allocation limit reached, rotating.");
515 else if (r == -EHOSTDOWN)
516 log_info("Journal file from other machine, rotating.");
517 else if (r == -EBUSY)
518 log_info("Unlcean shutdown, rotating.");
520 log_warning("Journal file corrupted, rotating.");
526 f = find_journal(s, uid);
530 log_info("Retrying write.");
534 static void dispatch_message_real(
536 struct iovec *iovec, unsigned n, unsigned m,
539 const char *label, size_t label_len,
540 const char *unit_id) {
542 char *pid = NULL, *uid = NULL, *gid = NULL,
543 *source_time = NULL, *boot_id = NULL, *machine_id = NULL,
544 *comm = NULL, *cmdline = NULL, *hostname = NULL,
545 *audit_session = NULL, *audit_loginuid = NULL,
546 *exe = NULL, *cgroup = NULL, *session = NULL,
547 *owner_uid = NULL, *unit = NULL, *selinux_context = NULL;
553 uid_t loginuid = 0, realuid = 0;
558 assert(n + N_IOVEC_META_FIELDS <= m);
566 realuid = ucred->uid;
568 if (asprintf(&pid, "_PID=%lu", (unsigned long) ucred->pid) >= 0)
569 IOVEC_SET_STRING(iovec[n++], pid);
571 if (asprintf(&uid, "_UID=%lu", (unsigned long) ucred->uid) >= 0)
572 IOVEC_SET_STRING(iovec[n++], uid);
574 if (asprintf(&gid, "_GID=%lu", (unsigned long) ucred->gid) >= 0)
575 IOVEC_SET_STRING(iovec[n++], gid);
577 r = get_process_comm(ucred->pid, &t);
579 comm = strappend("_COMM=", t);
583 IOVEC_SET_STRING(iovec[n++], comm);
586 r = get_process_exe(ucred->pid, &t);
588 exe = strappend("_EXE=", t);
592 IOVEC_SET_STRING(iovec[n++], exe);
595 r = get_process_cmdline(ucred->pid, LINE_MAX, false, &t);
597 cmdline = strappend("_CMDLINE=", t);
601 IOVEC_SET_STRING(iovec[n++], cmdline);
604 r = audit_session_from_pid(ucred->pid, &audit);
606 if (asprintf(&audit_session, "_AUDIT_SESSION=%lu", (unsigned long) audit) >= 0)
607 IOVEC_SET_STRING(iovec[n++], audit_session);
609 r = audit_loginuid_from_pid(ucred->pid, &loginuid);
611 if (asprintf(&audit_loginuid, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid) >= 0)
612 IOVEC_SET_STRING(iovec[n++], audit_loginuid);
614 t = shortened_cgroup_path(ucred->pid);
616 cgroup = strappend("_SYSTEMD_CGROUP=", t);
620 IOVEC_SET_STRING(iovec[n++], cgroup);
624 if (sd_pid_get_session(ucred->pid, &t) >= 0) {
625 session = strappend("_SYSTEMD_SESSION=", t);
629 IOVEC_SET_STRING(iovec[n++], session);
632 if (sd_pid_get_owner_uid(ucred->uid, &owner) >= 0)
633 if (asprintf(&owner_uid, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner) >= 0)
634 IOVEC_SET_STRING(iovec[n++], owner_uid);
637 if (cg_pid_get_unit(ucred->pid, &t) >= 0) {
638 unit = strappend("_SYSTEMD_UNIT=", t);
641 unit = strappend("_SYSTEMD_UNIT=", unit_id);
644 IOVEC_SET_STRING(iovec[n++], unit);
648 selinux_context = malloc(sizeof("_SELINUX_CONTEXT=") + label_len);
649 if (selinux_context) {
650 memcpy(selinux_context, "_SELINUX_CONTEXT=", sizeof("_SELINUX_CONTEXT=")-1);
651 memcpy(selinux_context+sizeof("_SELINUX_CONTEXT=")-1, label, label_len);
652 selinux_context[sizeof("_SELINUX_CONTEXT=")-1+label_len] = 0;
653 IOVEC_SET_STRING(iovec[n++], selinux_context);
656 security_context_t con;
658 if (getpidcon(ucred->pid, &con) >= 0) {
659 selinux_context = strappend("_SELINUX_CONTEXT=", con);
661 IOVEC_SET_STRING(iovec[n++], selinux_context);
670 if (asprintf(&source_time, "_SOURCE_REALTIME_TIMESTAMP=%llu",
671 (unsigned long long) timeval_load(tv)) >= 0)
672 IOVEC_SET_STRING(iovec[n++], source_time);
675 /* Note that strictly speaking storing the boot id here is
676 * redundant since the entry includes this in-line
677 * anyway. However, we need this indexed, too. */
678 r = sd_id128_get_boot(&id);
680 if (asprintf(&boot_id, "_BOOT_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
681 IOVEC_SET_STRING(iovec[n++], boot_id);
683 r = sd_id128_get_machine(&id);
685 if (asprintf(&machine_id, "_MACHINE_ID=%s", sd_id128_to_string(id, idbuf)) >= 0)
686 IOVEC_SET_STRING(iovec[n++], machine_id);
688 t = gethostname_malloc();
690 hostname = strappend("_HOSTNAME=", t);
693 IOVEC_SET_STRING(iovec[n++], hostname);
698 write_to_journal(s, realuid == 0 ? 0 : loginuid, iovec, n);
711 free(audit_loginuid);
716 free(selinux_context);
719 static void driver_message(Server *s, sd_id128_t message_id, const char *format, ...) {
720 char mid[11 + 32 + 1];
721 char buffer[16 + LINE_MAX + 1];
722 struct iovec iovec[N_IOVEC_META_FIELDS + 4];
730 IOVEC_SET_STRING(iovec[n++], "PRIORITY=6");
731 IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=driver");
733 memcpy(buffer, "MESSAGE=", 8);
734 va_start(ap, format);
735 vsnprintf(buffer + 8, sizeof(buffer) - 8, format, ap);
737 char_array_0(buffer);
738 IOVEC_SET_STRING(iovec[n++], buffer);
740 snprintf(mid, sizeof(mid), "MESSAGE_ID=" SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(message_id));
742 IOVEC_SET_STRING(iovec[n++], mid);
745 ucred.pid = getpid();
746 ucred.uid = getuid();
747 ucred.gid = getgid();
749 dispatch_message_real(s, iovec, n, ELEMENTSOF(iovec), &ucred, NULL, NULL, 0, NULL);
752 static void dispatch_message(Server *s,
753 struct iovec *iovec, unsigned n, unsigned m,
756 const char *label, size_t label_len,
760 char *path = NULL, *c;
763 assert(iovec || n == 0);
768 if (LOG_PRI(priority) > s->max_level_store)
774 path = shortened_cgroup_path(ucred->pid);
778 /* example: /user/lennart/3/foobar
779 * /system/dbus.service/foobar
781 * So let's cut of everything past the third /, since that is
782 * wher user directories start */
784 c = strchr(path, '/');
786 c = strchr(c+1, '/');
788 c = strchr(c+1, '/');
794 rl = journal_rate_limit_test(s->rate_limit, path, priority & LOG_PRIMASK, available_space(s));
801 /* Write a suppression message if we suppressed something */
803 driver_message(s, SD_MESSAGE_JOURNAL_DROPPED, "Suppressed %u messages from %s", rl - 1, path);
808 dispatch_message_real(s, iovec, n, m, ucred, tv, label, label_len, unit_id);
811 static void forward_syslog_iovec(Server *s, const struct iovec *iovec, unsigned n_iovec, struct ucred *ucred, struct timeval *tv) {
812 struct msghdr msghdr;
813 struct cmsghdr *cmsg;
815 struct cmsghdr cmsghdr;
816 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
818 union sockaddr_union sa;
825 msghdr.msg_iov = (struct iovec*) iovec;
826 msghdr.msg_iovlen = n_iovec;
829 sa.un.sun_family = AF_UNIX;
830 strncpy(sa.un.sun_path, "/run/systemd/journal/syslog", sizeof(sa.un.sun_path));
831 msghdr.msg_name = &sa;
832 msghdr.msg_namelen = offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path);
836 msghdr.msg_control = &control;
837 msghdr.msg_controllen = sizeof(control);
839 cmsg = CMSG_FIRSTHDR(&msghdr);
840 cmsg->cmsg_level = SOL_SOCKET;
841 cmsg->cmsg_type = SCM_CREDENTIALS;
842 cmsg->cmsg_len = CMSG_LEN(sizeof(struct ucred));
843 memcpy(CMSG_DATA(cmsg), ucred, sizeof(struct ucred));
844 msghdr.msg_controllen = cmsg->cmsg_len;
847 /* Forward the syslog message we received via /dev/log to
848 * /run/systemd/syslog. Unfortunately we currently can't set
849 * the SO_TIMESTAMP auxiliary data, and hence we don't. */
851 if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
854 /* The socket is full? I guess the syslog implementation is
855 * too slow, and we shouldn't wait for that... */
859 if (ucred && errno == ESRCH) {
862 /* Hmm, presumably the sender process vanished
863 * by now, so let's fix it as good as we
868 memcpy(CMSG_DATA(cmsg), &u, sizeof(struct ucred));
870 if (sendmsg(s->syslog_fd, &msghdr, MSG_NOSIGNAL) >= 0)
878 log_debug("Failed to forward syslog message: %m");
881 static void forward_syslog_raw(Server *s, int priority, const char *buffer, struct ucred *ucred, struct timeval *tv) {
887 if (LOG_PRI(priority) > s->max_level_syslog)
890 IOVEC_SET_STRING(iovec, buffer);
891 forward_syslog_iovec(s, &iovec, 1, ucred, tv);
894 static void forward_syslog(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred, struct timeval *tv) {
895 struct iovec iovec[5];
896 char header_priority[6], header_time[64], header_pid[16];
900 char *ident_buf = NULL;
903 assert(priority >= 0);
904 assert(priority <= 999);
907 if (LOG_PRI(priority) > s->max_level_syslog)
910 /* First: priority field */
911 snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
912 char_array_0(header_priority);
913 IOVEC_SET_STRING(iovec[n++], header_priority);
915 /* Second: timestamp */
916 t = tv ? tv->tv_sec : ((time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC));
920 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
922 IOVEC_SET_STRING(iovec[n++], header_time);
924 /* Third: identifier and PID */
927 get_process_comm(ucred->pid, &ident_buf);
928 identifier = ident_buf;
931 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
932 char_array_0(header_pid);
935 IOVEC_SET_STRING(iovec[n++], identifier);
937 IOVEC_SET_STRING(iovec[n++], header_pid);
938 } else if (identifier) {
939 IOVEC_SET_STRING(iovec[n++], identifier);
940 IOVEC_SET_STRING(iovec[n++], ": ");
943 /* Fourth: message */
944 IOVEC_SET_STRING(iovec[n++], message);
946 forward_syslog_iovec(s, iovec, n, ucred, tv);
951 static int fixup_priority(int priority) {
953 if ((priority & LOG_FACMASK) == 0)
954 return (priority & LOG_PRIMASK) | LOG_USER;
959 static void forward_kmsg(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
960 struct iovec iovec[5];
961 char header_priority[6], header_pid[16];
963 char *ident_buf = NULL;
966 assert(priority >= 0);
967 assert(priority <= 999);
970 if (_unlikely_(LOG_PRI(priority) > s->max_level_kmsg))
973 if (_unlikely_(s->dev_kmsg_fd < 0))
976 /* Never allow messages with kernel facility to be written to
977 * kmsg, regardless where the data comes from. */
978 priority = fixup_priority(priority);
980 /* First: priority field */
981 snprintf(header_priority, sizeof(header_priority), "<%i>", priority);
982 char_array_0(header_priority);
983 IOVEC_SET_STRING(iovec[n++], header_priority);
985 /* Second: identifier and PID */
988 get_process_comm(ucred->pid, &ident_buf);
989 identifier = ident_buf;
992 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
993 char_array_0(header_pid);
996 IOVEC_SET_STRING(iovec[n++], identifier);
998 IOVEC_SET_STRING(iovec[n++], header_pid);
999 } else if (identifier) {
1000 IOVEC_SET_STRING(iovec[n++], identifier);
1001 IOVEC_SET_STRING(iovec[n++], ": ");
1004 /* Fourth: message */
1005 IOVEC_SET_STRING(iovec[n++], message);
1006 IOVEC_SET_STRING(iovec[n++], "\n");
1008 if (writev(s->dev_kmsg_fd, iovec, n) < 0)
1009 log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno));
1014 static void forward_console(Server *s, int priority, const char *identifier, const char *message, struct ucred *ucred) {
1015 struct iovec iovec[4];
1016 char header_pid[16];
1018 char *ident_buf = NULL;
1024 if (LOG_PRI(priority) > s->max_level_console)
1027 /* First: identifier and PID */
1030 get_process_comm(ucred->pid, &ident_buf);
1031 identifier = ident_buf;
1034 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
1035 char_array_0(header_pid);
1038 IOVEC_SET_STRING(iovec[n++], identifier);
1040 IOVEC_SET_STRING(iovec[n++], header_pid);
1041 } else if (identifier) {
1042 IOVEC_SET_STRING(iovec[n++], identifier);
1043 IOVEC_SET_STRING(iovec[n++], ": ");
1046 /* Third: message */
1047 IOVEC_SET_STRING(iovec[n++], message);
1048 IOVEC_SET_STRING(iovec[n++], "\n");
1050 tty = s->tty_path ? s->tty_path : "/dev/console";
1052 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
1054 log_debug("Failed to open %s for logging: %s", tty, strerror(errno));
1058 if (writev(fd, iovec, n) < 0)
1059 log_debug("Failed to write to %s for logging: %s", tty, strerror(errno));
1061 close_nointr_nofail(fd);
1067 static void read_identifier(const char **buf, char **identifier, char **pid) {
1078 p += strspn(p, WHITESPACE);
1079 l = strcspn(p, WHITESPACE);
1088 if (p[l-1] == ']') {
1094 t = strndup(p+k+1, l-k-2);
1114 *buf += strspn(*buf, WHITESPACE);
1117 static void process_syslog_message(Server *s, const char *buf, struct ucred *ucred, struct timeval *tv, const char *label, size_t label_len) {
1118 char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *syslog_pid = NULL;
1119 struct iovec iovec[N_IOVEC_META_FIELDS + 6];
1121 int priority = LOG_USER | LOG_INFO;
1122 char *identifier = NULL, *pid = NULL;
1129 parse_syslog_priority((char**) &buf, &priority);
1131 if (s->forward_to_syslog)
1132 forward_syslog_raw(s, priority, orig, ucred, tv);
1134 skip_syslog_date((char**) &buf);
1135 read_identifier(&buf, &identifier, &pid);
1137 if (s->forward_to_kmsg)
1138 forward_kmsg(s, priority, identifier, buf, ucred);
1140 if (s->forward_to_console)
1141 forward_console(s, priority, identifier, buf, ucred);
1143 IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=syslog");
1145 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1146 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1148 if (priority & LOG_FACMASK)
1149 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1150 IOVEC_SET_STRING(iovec[n++], syslog_facility);
1153 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
1154 if (syslog_identifier)
1155 IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1159 syslog_pid = strappend("SYSLOG_PID=", pid);
1161 IOVEC_SET_STRING(iovec[n++], syslog_pid);
1164 message = strappend("MESSAGE=", buf);
1166 IOVEC_SET_STRING(iovec[n++], message);
1168 dispatch_message(s, iovec, n, ELEMENTSOF(iovec), ucred, tv, label, label_len, NULL, priority);
1173 free(syslog_priority);
1174 free(syslog_facility);
1175 free(syslog_identifier);
1179 static bool valid_user_field(const char *p, size_t l) {
1182 /* We kinda enforce POSIX syntax recommendations for
1183 environment variables here, but make a couple of additional
1186 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
1188 /* No empty field names */
1192 /* Don't allow names longer than 64 chars */
1196 /* Variables starting with an underscore are protected */
1200 /* Don't allow digits as first character */
1201 if (p[0] >= '0' && p[0] <= '9')
1204 /* Only allow A-Z0-9 and '_' */
1205 for (a = p; a < p + l; a++)
1206 if (!((*a >= 'A' && *a <= 'Z') ||
1207 (*a >= '0' && *a <= '9') ||
1214 static void process_native_message(
1216 const void *buffer, size_t buffer_size,
1217 struct ucred *ucred,
1219 const char *label, size_t label_len) {
1221 struct iovec *iovec = NULL;
1222 unsigned n = 0, m = 0, j, tn = (unsigned) -1;
1225 int priority = LOG_INFO;
1226 char *identifier = NULL, *message = NULL;
1229 assert(buffer || buffer_size == 0);
1232 remaining = buffer_size;
1234 while (remaining > 0) {
1237 e = memchr(p, '\n', remaining);
1240 /* Trailing noise, let's ignore it, and flush what we collected */
1241 log_debug("Received message with trailing noise, ignoring.");
1246 /* Entry separator */
1247 dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
1249 priority = LOG_INFO;
1256 if (*p == '.' || *p == '#') {
1257 /* Ignore control commands for now, and
1259 remaining -= (e - p) + 1;
1264 /* A property follows */
1266 if (n+N_IOVEC_META_FIELDS >= m) {
1270 u = MAX((n+N_IOVEC_META_FIELDS+1) * 2U, 4U);
1271 c = realloc(iovec, u * sizeof(struct iovec));
1281 q = memchr(p, '=', e - p);
1283 if (valid_user_field(p, q - p)) {
1288 /* If the field name starts with an
1289 * underscore, skip the variable,
1290 * since that indidates a trusted
1292 iovec[n].iov_base = (char*) p;
1293 iovec[n].iov_len = l;
1296 /* We need to determine the priority
1297 * of this entry for the rate limiting
1300 memcmp(p, "PRIORITY=", 9) == 0 &&
1301 p[9] >= '0' && p[9] <= '9')
1302 priority = (priority & LOG_FACMASK) | (p[9] - '0');
1305 memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1306 p[16] >= '0' && p[16] <= '9')
1307 priority = (priority & LOG_PRIMASK) | ((p[16] - '0') << 3);
1310 memcmp(p, "SYSLOG_FACILITY=", 16) == 0 &&
1311 p[16] >= '0' && p[16] <= '9' &&
1312 p[17] >= '0' && p[17] <= '9')
1313 priority = (priority & LOG_PRIMASK) | (((p[16] - '0')*10 + (p[17] - '0')) << 3);
1316 memcmp(p, "SYSLOG_IDENTIFIER=", 18) == 0) {
1319 t = strndup(p + 18, l - 18);
1324 } else if (l >= 8 &&
1325 memcmp(p, "MESSAGE=", 8) == 0) {
1328 t = strndup(p + 8, l - 8);
1336 remaining -= (e - p) + 1;
1344 if (remaining < e - p + 1 + sizeof(uint64_t) + 1) {
1345 log_debug("Failed to parse message, ignoring.");
1349 memcpy(&l_le, e + 1, sizeof(uint64_t));
1352 if (remaining < e - p + 1 + sizeof(uint64_t) + l + 1 ||
1353 e[1+sizeof(uint64_t)+l] != '\n') {
1354 log_debug("Failed to parse message, ignoring.");
1358 k = malloc((e - p) + 1 + l);
1364 memcpy(k, p, e - p);
1366 memcpy(k + (e - p) + 1, e + 1 + sizeof(uint64_t), l);
1368 if (valid_user_field(p, e - p)) {
1369 iovec[n].iov_base = k;
1370 iovec[n].iov_len = (e - p) + 1 + l;
1375 remaining -= (e - p) + 1 + sizeof(uint64_t) + l + 1;
1376 p = e + 1 + sizeof(uint64_t) + l + 1;
1384 IOVEC_SET_STRING(iovec[tn], "_TRANSPORT=journal");
1387 if (s->forward_to_syslog)
1388 forward_syslog(s, priority, identifier, message, ucred, tv);
1390 if (s->forward_to_kmsg)
1391 forward_kmsg(s, priority, identifier, message, ucred);
1393 if (s->forward_to_console)
1394 forward_console(s, priority, identifier, message, ucred);
1397 dispatch_message(s, iovec, n, m, ucred, tv, label, label_len, NULL, priority);
1400 for (j = 0; j < n; j++) {
1404 if (iovec[j].iov_base < buffer ||
1405 (const uint8_t*) iovec[j].iov_base >= (const uint8_t*) buffer + buffer_size)
1406 free(iovec[j].iov_base);
1414 static void process_native_file(
1417 struct ucred *ucred,
1419 const char *label, size_t label_len) {
1428 /* Data is in the passed file, since it didn't fit in a
1429 * datagram. We can't map the file here, since clients might
1430 * then truncate it and trigger a SIGBUS for us. So let's
1431 * stupidly read it */
1433 if (fstat(fd, &st) < 0) {
1434 log_error("Failed to stat passed file, ignoring: %m");
1438 if (!S_ISREG(st.st_mode)) {
1439 log_error("File passed is not regular. Ignoring.");
1443 if (st.st_size <= 0)
1446 if (st.st_size > ENTRY_SIZE_MAX) {
1447 log_error("File passed too large. Ignoring.");
1451 p = malloc(st.st_size);
1457 n = pread(fd, p, st.st_size, 0);
1459 log_error("Failed to read file, ignoring: %s", strerror(-n));
1461 process_native_message(s, p, n, ucred, tv, label, label_len);
1466 static int stdout_stream_log(StdoutStream *s, const char *p) {
1467 struct iovec iovec[N_IOVEC_META_FIELDS + 5];
1468 char *message = NULL, *syslog_priority = NULL, *syslog_facility = NULL, *syslog_identifier = NULL;
1472 size_t label_len = 0;
1480 priority = s->priority;
1482 if (s->level_prefix)
1483 parse_syslog_priority((char**) &p, &priority);
1485 if (s->forward_to_syslog || s->server->forward_to_syslog)
1486 forward_syslog(s->server, fixup_priority(priority), s->identifier, p, &s->ucred, NULL);
1488 if (s->forward_to_kmsg || s->server->forward_to_kmsg)
1489 forward_kmsg(s->server, priority, s->identifier, p, &s->ucred);
1491 if (s->forward_to_console || s->server->forward_to_console)
1492 forward_console(s->server, priority, s->identifier, p, &s->ucred);
1494 IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=stdout");
1496 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1497 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1499 if (priority & LOG_FACMASK)
1500 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
1501 IOVEC_SET_STRING(iovec[n++], syslog_facility);
1503 if (s->identifier) {
1504 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", s->identifier);
1505 if (syslog_identifier)
1506 IOVEC_SET_STRING(iovec[n++], syslog_identifier);
1509 message = strappend("MESSAGE=", p);
1511 IOVEC_SET_STRING(iovec[n++], message);
1514 if (s->security_context) {
1515 label = (char*) s->security_context;
1516 label_len = strlen((char*) s->security_context);
1520 dispatch_message(s->server, iovec, n, ELEMENTSOF(iovec), &s->ucred, NULL, label, label_len, s->unit_id, priority);
1523 free(syslog_priority);
1524 free(syslog_facility);
1525 free(syslog_identifier);
1530 static int stdout_stream_line(StdoutStream *s, char *p) {
1540 case STDOUT_STREAM_IDENTIFIER:
1542 s->identifier = NULL;
1544 s->identifier = strdup(p);
1549 s->state = STDOUT_STREAM_UNIT_ID;
1552 case STDOUT_STREAM_UNIT_ID:
1553 if (s->ucred.uid == 0) {
1557 s->unit_id = strdup(p);
1563 s->state = STDOUT_STREAM_PRIORITY;
1566 case STDOUT_STREAM_PRIORITY:
1567 r = safe_atoi(p, &s->priority);
1568 if (r < 0 || s->priority <= 0 || s->priority >= 999) {
1569 log_warning("Failed to parse log priority line.");
1573 s->state = STDOUT_STREAM_LEVEL_PREFIX;
1576 case STDOUT_STREAM_LEVEL_PREFIX:
1577 r = parse_boolean(p);
1579 log_warning("Failed to parse level prefix line.");
1583 s->level_prefix = !!r;
1584 s->state = STDOUT_STREAM_FORWARD_TO_SYSLOG;
1587 case STDOUT_STREAM_FORWARD_TO_SYSLOG:
1588 r = parse_boolean(p);
1590 log_warning("Failed to parse forward to syslog line.");
1594 s->forward_to_syslog = !!r;
1595 s->state = STDOUT_STREAM_FORWARD_TO_KMSG;
1598 case STDOUT_STREAM_FORWARD_TO_KMSG:
1599 r = parse_boolean(p);
1601 log_warning("Failed to parse copy to kmsg line.");
1605 s->forward_to_kmsg = !!r;
1606 s->state = STDOUT_STREAM_FORWARD_TO_CONSOLE;
1609 case STDOUT_STREAM_FORWARD_TO_CONSOLE:
1610 r = parse_boolean(p);
1612 log_warning("Failed to parse copy to console line.");
1616 s->forward_to_console = !!r;
1617 s->state = STDOUT_STREAM_RUNNING;
1620 case STDOUT_STREAM_RUNNING:
1621 return stdout_stream_log(s, p);
1624 assert_not_reached("Unknown stream state");
1627 static int stdout_stream_scan(StdoutStream *s, bool force_flush) {
1635 remaining = s->length;
1640 end = memchr(p, '\n', remaining);
1643 else if (remaining >= sizeof(s->buffer) - 1) {
1644 end = p + sizeof(s->buffer) - 1;
1651 r = stdout_stream_line(s, p);
1659 if (force_flush && remaining > 0) {
1661 r = stdout_stream_line(s, p);
1669 if (p > s->buffer) {
1670 memmove(s->buffer, p, remaining);
1671 s->length = remaining;
1677 static int stdout_stream_process(StdoutStream *s) {
1683 l = read(s->fd, s->buffer+s->length, sizeof(s->buffer)-1-s->length);
1686 if (errno == EAGAIN)
1689 log_warning("Failed to read from stream: %m");
1694 r = stdout_stream_scan(s, true);
1702 r = stdout_stream_scan(s, false);
1710 static void stdout_stream_free(StdoutStream *s) {
1714 assert(s->server->n_stdout_streams > 0);
1715 s->server->n_stdout_streams --;
1716 LIST_REMOVE(StdoutStream, stdout_stream, s->server->stdout_streams, s);
1721 epoll_ctl(s->server->epoll_fd, EPOLL_CTL_DEL, s->fd, NULL);
1723 close_nointr_nofail(s->fd);
1727 if (s->security_context)
1728 freecon(s->security_context);
1731 free(s->identifier);
1735 static int stdout_stream_new(Server *s) {
1736 StdoutStream *stream;
1739 struct epoll_event ev;
1743 fd = accept4(s->stdout_fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC);
1745 if (errno == EAGAIN)
1748 log_error("Failed to accept stdout connection: %m");
1752 if (s->n_stdout_streams >= STDOUT_STREAMS_MAX) {
1753 log_warning("Too many stdout streams, refusing connection.");
1754 close_nointr_nofail(fd);
1758 stream = new0(StdoutStream, 1);
1760 close_nointr_nofail(fd);
1766 len = sizeof(stream->ucred);
1767 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &stream->ucred, &len) < 0) {
1768 log_error("Failed to determine peer credentials: %m");
1774 if (getpeercon(fd, &stream->security_context) < 0 && errno != ENOPROTOOPT)
1775 log_error("Failed to determine peer security context: %m");
1778 if (shutdown(fd, SHUT_WR) < 0) {
1779 log_error("Failed to shutdown writing side of socket: %m");
1785 ev.data.ptr = stream;
1786 ev.events = EPOLLIN;
1787 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, fd, &ev) < 0) {
1788 log_error("Failed to add stream to event loop: %m");
1794 LIST_PREPEND(StdoutStream, stdout_stream, s->stdout_streams, stream);
1795 s->n_stdout_streams ++;
1800 stdout_stream_free(stream);
1804 static bool is_us(const char *pid) {
1809 if (parse_pid(pid, &t) < 0)
1812 return t == getpid();
1815 static void dev_kmsg_record(Server *s, char *p, size_t l) {
1816 struct iovec iovec[N_IOVEC_META_FIELDS + 7 + N_IOVEC_KERNEL_FIELDS + 2 + N_IOVEC_UDEV_FIELDS];
1817 char *message = NULL, *syslog_priority = NULL, *syslog_pid = NULL, *syslog_facility = NULL, *syslog_identifier = NULL, *source_time = NULL;
1819 unsigned n = 0, z = 0, j;
1821 char *identifier = NULL, *pid = NULL, *e, *f, *k;
1824 char *kernel_device = NULL;
1832 e = memchr(p, ',', l);
1837 r = safe_atoi(p, &priority);
1838 if (r < 0 || priority < 0 || priority > 999)
1841 if (s->forward_to_kmsg && (priority & LOG_FACMASK) != LOG_KERN)
1846 e = memchr(p, ',', l);
1851 r = safe_atou64(p, &serial);
1855 if (s->kernel_seqnum) {
1856 /* We already read this one? */
1857 if (serial < *s->kernel_seqnum)
1860 /* Did we lose any? */
1861 if (serial > *s->kernel_seqnum)
1862 driver_message(s, SD_MESSAGE_JOURNAL_MISSED, "Missed %llu kernel messages", (unsigned long long) serial - *s->kernel_seqnum - 1);
1864 /* Make sure we never read this one again. Note that
1865 * we always store the next message serial we expect
1866 * here, simply because this makes handling the first
1867 * message with serial 0 easy. */
1868 *s->kernel_seqnum = serial + 1;
1873 f = memchr(p, ';', l);
1876 /* Kernel 3.6 has the flags field, kernel 3.5 lacks that */
1877 e = memchr(p, ',', l);
1882 r = parse_usec(p, &usec);
1888 e = memchr(p, '\n', l);
1897 for (j = 0; l > 0 && j < N_IOVEC_KERNEL_FIELDS; j++) {
1899 /* Meta data fields attached */
1906 e = memchr(k, '\n', l);
1912 m = cunescape_length_with_prefix(k, e - k, "_KERNEL_");
1916 if (startswith(m, "_KERNEL_DEVICE="))
1917 kernel_device = m + 15;
1919 IOVEC_SET_STRING(iovec[n++], m);
1926 if (kernel_device) {
1927 struct udev_device *ud;
1929 ud = udev_device_new_from_device_id(s->udev, kernel_device);
1932 struct udev_list_entry *ll;
1935 g = udev_device_get_devnode(ud);
1937 b = strappend("_UDEV_DEVNODE=", g);
1939 IOVEC_SET_STRING(iovec[n++], b);
1944 g = udev_device_get_sysname(ud);
1946 b = strappend("_UDEV_SYSNAME=", g);
1948 IOVEC_SET_STRING(iovec[n++], b);
1954 ll = udev_device_get_devlinks_list_entry(ud);
1955 udev_list_entry_foreach(ll, ll) {
1957 if (j > N_IOVEC_UDEV_FIELDS)
1960 g = udev_list_entry_get_name(ll);
1961 b = strappend("_UDEV_DEVLINK=", g);
1963 IOVEC_SET_STRING(iovec[n++], b);
1970 udev_device_unref(ud);
1974 if (asprintf(&source_time, "_SOURCE_MONOTONIC_TIMESTAMP=%llu",
1975 (unsigned long long) usec) >= 0)
1976 IOVEC_SET_STRING(iovec[n++], source_time);
1978 IOVEC_SET_STRING(iovec[n++], "_TRANSPORT=kernel");
1980 if (asprintf(&syslog_priority, "PRIORITY=%i", priority & LOG_PRIMASK) >= 0)
1981 IOVEC_SET_STRING(iovec[n++], syslog_priority);
1983 if ((priority & LOG_FACMASK) == LOG_KERN)
1984 IOVEC_SET_STRING(iovec[n++], "SYSLOG_IDENTIFIER=kernel");
1986 read_identifier((const char**) &p, &identifier, &pid);
1988 /* Avoid any messages we generated ourselves via
1989 * log_info() and friends. */
1990 if (pid && is_us(pid))
1994 syslog_identifier = strappend("SYSLOG_IDENTIFIER=", identifier);
1995 if (syslog_identifier)
1996 IOVEC_SET_STRING(iovec[n++], syslog_identifier);
2000 syslog_pid = strappend("SYSLOG_PID=", pid);
2002 IOVEC_SET_STRING(iovec[n++], syslog_pid);
2005 if (asprintf(&syslog_facility, "SYSLOG_FACILITY=%i", LOG_FAC(priority)) >= 0)
2006 IOVEC_SET_STRING(iovec[n++], syslog_facility);
2009 message = cunescape_length_with_prefix(p, pl, "MESSAGE=");
2011 IOVEC_SET_STRING(iovec[n++], message);
2013 dispatch_message(s, iovec, n, ELEMENTSOF(iovec), NULL, NULL, NULL, 0, NULL, priority);
2016 for (j = 0; j < z; j++)
2017 free(iovec[j].iov_base);
2020 free(syslog_priority);
2021 free(syslog_identifier);
2023 free(syslog_facility);
2029 static int system_journal_open(Server *s) {
2035 r = sd_id128_get_machine(&machine);
2039 sd_id128_to_string(machine, ids);
2041 if (!s->system_journal &&
2042 (s->storage == STORAGE_PERSISTENT || s->storage == STORAGE_AUTO) &&
2043 access("/run/systemd/journal/flushed", F_OK) >= 0) {
2045 /* If in auto mode: first try to create the machine
2046 * path, but not the prefix.
2048 * If in persistent mode: create /var/log/journal and
2049 * the machine path */
2051 if (s->storage == STORAGE_PERSISTENT)
2052 (void) mkdir("/var/log/journal/", 0755);
2054 fn = strappend("/var/log/journal/", ids);
2058 (void) mkdir(fn, 0755);
2061 fn = strjoin("/var/log/journal/", ids, "/system.journal", NULL);
2065 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, s->seal, &s->system_metrics, s->mmap, NULL, &s->system_journal);
2069 server_fix_perms(s, s->system_journal, 0);
2072 if (r != -ENOENT && r != -EROFS)
2073 log_warning("Failed to open system journal: %s", strerror(-r));
2079 if (!s->runtime_journal &&
2080 (s->storage != STORAGE_NONE)) {
2082 fn = strjoin("/run/log/journal/", ids, "/system.journal", NULL);
2086 if (s->system_journal) {
2088 /* Try to open the runtime journal, but only
2089 * if it already exists, so that we can flush
2090 * it into the system journal */
2092 r = journal_file_open(fn, O_RDWR, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
2097 log_warning("Failed to open runtime journal: %s", strerror(-r));
2104 /* OK, we really need the runtime journal, so create
2105 * it if necessary. */
2107 (void) mkdir_parents(fn, 0755);
2108 r = journal_file_open_reliably(fn, O_RDWR|O_CREAT, 0640, s->compress, false, &s->runtime_metrics, s->mmap, NULL, &s->runtime_journal);
2112 log_error("Failed to open runtime journal: %s", strerror(-r));
2117 if (s->runtime_journal)
2118 server_fix_perms(s, s->runtime_journal, 0);
2124 static int server_flush_to_var(Server *s) {
2132 if (s->storage != STORAGE_AUTO &&
2133 s->storage != STORAGE_PERSISTENT)
2136 if (!s->runtime_journal)
2139 system_journal_open(s);
2141 if (!s->system_journal)
2144 log_info("Flushing to /var...");
2146 r = sd_id128_get_machine(&machine);
2148 log_error("Failed to get machine id: %s", strerror(-r));
2152 r = sd_journal_open(&j, SD_JOURNAL_RUNTIME_ONLY);
2154 log_error("Failed to read runtime journal: %s", strerror(-r));
2158 SD_JOURNAL_FOREACH(j) {
2161 f = j->current_file;
2162 assert(f && f->current_offset > 0);
2164 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
2166 log_error("Can't read entry: %s", strerror(-r));
2170 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
2172 log_info("Allocation limit reached.");
2174 journal_file_post_change(s->system_journal);
2178 r = journal_file_copy_entry(f, s->system_journal, o, f->current_offset, NULL, NULL, NULL);
2182 log_error("Can't write entry: %s", strerror(-r));
2188 journal_file_post_change(s->system_journal);
2190 journal_file_close(s->runtime_journal);
2191 s->runtime_journal = NULL;
2194 rm_rf("/run/log/journal", false, true, false);
2199 static int server_read_dev_kmsg(Server *s) {
2200 char buffer[8192+1]; /* the kernel-side limit per record is 8K currently */
2204 assert(s->dev_kmsg_fd >= 0);
2206 l = read(s->dev_kmsg_fd, buffer, sizeof(buffer) - 1);
2210 /* Old kernels who don't allow reading from /dev/kmsg
2211 * return EINVAL when we try. So handle this cleanly,
2212 * but don' try to ever read from it again. */
2213 if (errno == EINVAL) {
2214 epoll_ctl(s->epoll_fd, EPOLL_CTL_DEL, s->dev_kmsg_fd, NULL);
2218 if (errno == EAGAIN || errno == EINTR || errno == EPIPE)
2221 log_error("Failed to read from kernel: %m");
2225 dev_kmsg_record(s, buffer, l);
2229 static int server_flush_dev_kmsg(Server *s) {
2234 if (s->dev_kmsg_fd < 0)
2237 if (!s->dev_kmsg_readable)
2240 log_info("Flushing /dev/kmsg...");
2243 r = server_read_dev_kmsg(s);
2254 static int process_event(Server *s, struct epoll_event *ev) {
2258 if (ev->data.fd == s->signal_fd) {
2259 struct signalfd_siginfo sfsi;
2262 if (ev->events != EPOLLIN) {
2263 log_info("Got invalid event from epoll.");
2267 n = read(s->signal_fd, &sfsi, sizeof(sfsi));
2268 if (n != sizeof(sfsi)) {
2273 if (errno == EINTR || errno == EAGAIN)
2279 log_info("Received SIG%s", signal_to_string(sfsi.ssi_signo));
2281 if (sfsi.ssi_signo == SIGUSR1) {
2282 touch("/run/systemd/journal/flushed");
2283 server_flush_to_var(s);
2287 if (sfsi.ssi_signo == SIGUSR2) {
2295 } else if (ev->data.fd == s->dev_kmsg_fd) {
2298 if (ev->events != EPOLLIN) {
2299 log_info("Got invalid event from epoll.");
2303 r = server_read_dev_kmsg(s);
2309 } else if (ev->data.fd == s->native_fd ||
2310 ev->data.fd == s->syslog_fd) {
2312 if (ev->events != EPOLLIN) {
2313 log_info("Got invalid event from epoll.");
2318 struct msghdr msghdr;
2320 struct ucred *ucred = NULL;
2321 struct timeval *tv = NULL;
2322 struct cmsghdr *cmsg;
2324 size_t label_len = 0;
2326 struct cmsghdr cmsghdr;
2328 /* We use NAME_MAX space for the
2329 * SELinux label here. The kernel
2330 * currently enforces no limit, but
2331 * according to suggestions from the
2332 * SELinux people this will change and
2333 * it will probably be identical to
2334 * NAME_MAX. For now we use that, but
2335 * this should be updated one day when
2336 * the final limit is known.*/
2337 uint8_t buf[CMSG_SPACE(sizeof(struct ucred)) +
2338 CMSG_SPACE(sizeof(struct timeval)) +
2339 CMSG_SPACE(sizeof(int)) + /* fd */
2340 CMSG_SPACE(NAME_MAX)]; /* selinux label */
2347 if (ioctl(ev->data.fd, SIOCINQ, &v) < 0) {
2348 log_error("SIOCINQ failed: %m");
2352 if (s->buffer_size < (size_t) v) {
2356 l = MAX(LINE_MAX + (size_t) v, s->buffer_size * 2);
2357 b = realloc(s->buffer, l+1);
2360 log_error("Couldn't increase buffer.");
2369 iovec.iov_base = s->buffer;
2370 iovec.iov_len = s->buffer_size;
2374 msghdr.msg_iov = &iovec;
2375 msghdr.msg_iovlen = 1;
2376 msghdr.msg_control = &control;
2377 msghdr.msg_controllen = sizeof(control);
2379 n = recvmsg(ev->data.fd, &msghdr, MSG_DONTWAIT|MSG_CMSG_CLOEXEC);
2382 if (errno == EINTR || errno == EAGAIN)
2385 log_error("recvmsg() failed: %m");
2389 for (cmsg = CMSG_FIRSTHDR(&msghdr); cmsg; cmsg = CMSG_NXTHDR(&msghdr, cmsg)) {
2391 if (cmsg->cmsg_level == SOL_SOCKET &&
2392 cmsg->cmsg_type == SCM_CREDENTIALS &&
2393 cmsg->cmsg_len == CMSG_LEN(sizeof(struct ucred)))
2394 ucred = (struct ucred*) CMSG_DATA(cmsg);
2395 else if (cmsg->cmsg_level == SOL_SOCKET &&
2396 cmsg->cmsg_type == SCM_SECURITY) {
2397 label = (char*) CMSG_DATA(cmsg);
2398 label_len = cmsg->cmsg_len - CMSG_LEN(0);
2399 } else if (cmsg->cmsg_level == SOL_SOCKET &&
2400 cmsg->cmsg_type == SO_TIMESTAMP &&
2401 cmsg->cmsg_len == CMSG_LEN(sizeof(struct timeval)))
2402 tv = (struct timeval*) CMSG_DATA(cmsg);
2403 else if (cmsg->cmsg_level == SOL_SOCKET &&
2404 cmsg->cmsg_type == SCM_RIGHTS) {
2405 fds = (int*) CMSG_DATA(cmsg);
2406 n_fds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
2410 if (ev->data.fd == s->syslog_fd) {
2413 if (n > 0 && n_fds == 0) {
2414 e = memchr(s->buffer, '\n', n);
2420 process_syslog_message(s, strstrip(s->buffer), ucred, tv, label, label_len);
2421 } else if (n_fds > 0)
2422 log_warning("Got file descriptors via syslog socket. Ignoring.");
2425 if (n > 0 && n_fds == 0)
2426 process_native_message(s, s->buffer, n, ucred, tv, label, label_len);
2427 else if (n == 0 && n_fds == 1)
2428 process_native_file(s, fds[0], ucred, tv, label, label_len);
2430 log_warning("Got too many file descriptors via native socket. Ignoring.");
2433 close_many(fds, n_fds);
2438 } else if (ev->data.fd == s->stdout_fd) {
2440 if (ev->events != EPOLLIN) {
2441 log_info("Got invalid event from epoll.");
2445 stdout_stream_new(s);
2449 StdoutStream *stream;
2451 if ((ev->events|EPOLLIN|EPOLLHUP) != (EPOLLIN|EPOLLHUP)) {
2452 log_info("Got invalid event from epoll.");
2456 /* If it is none of the well-known fds, it must be an
2457 * stdout stream fd. Note that this is a bit ugly here
2458 * (since we rely that none of the well-known fds
2459 * could be interpreted as pointer), but nonetheless
2460 * safe, since the well-known fds would never get an
2461 * fd > 4096, i.e. beyond the first memory page */
2463 stream = ev->data.ptr;
2465 if (stdout_stream_process(stream) <= 0)
2466 stdout_stream_free(stream);
2471 log_error("Unknown event.");
2475 static int open_syslog_socket(Server *s) {
2476 union sockaddr_union sa;
2478 struct epoll_event ev;
2482 if (s->syslog_fd < 0) {
2484 s->syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2485 if (s->syslog_fd < 0) {
2486 log_error("socket() failed: %m");
2491 sa.un.sun_family = AF_UNIX;
2492 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
2494 unlink(sa.un.sun_path);
2496 r = bind(s->syslog_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2498 log_error("bind() failed: %m");
2502 chmod(sa.un.sun_path, 0666);
2504 fd_nonblock(s->syslog_fd, 1);
2507 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
2509 log_error("SO_PASSCRED failed: %m");
2515 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
2517 log_warning("SO_PASSSEC failed: %m");
2521 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
2523 log_error("SO_TIMESTAMP failed: %m");
2528 ev.events = EPOLLIN;
2529 ev.data.fd = s->syslog_fd;
2530 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->syslog_fd, &ev) < 0) {
2531 log_error("Failed to add syslog server fd to epoll object: %m");
2538 static int open_native_socket(Server*s) {
2539 union sockaddr_union sa;
2541 struct epoll_event ev;
2545 if (s->native_fd < 0) {
2547 s->native_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2548 if (s->native_fd < 0) {
2549 log_error("socket() failed: %m");
2554 sa.un.sun_family = AF_UNIX;
2555 strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
2557 unlink(sa.un.sun_path);
2559 r = bind(s->native_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2561 log_error("bind() failed: %m");
2565 chmod(sa.un.sun_path, 0666);
2567 fd_nonblock(s->native_fd, 1);
2570 r = setsockopt(s->native_fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one));
2572 log_error("SO_PASSCRED failed: %m");
2578 r = setsockopt(s->syslog_fd, SOL_SOCKET, SO_PASSSEC, &one, sizeof(one));
2580 log_warning("SO_PASSSEC failed: %m");
2584 r = setsockopt(s->native_fd, SOL_SOCKET, SO_TIMESTAMP, &one, sizeof(one));
2586 log_error("SO_TIMESTAMP failed: %m");
2591 ev.events = EPOLLIN;
2592 ev.data.fd = s->native_fd;
2593 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->native_fd, &ev) < 0) {
2594 log_error("Failed to add native server fd to epoll object: %m");
2601 static int open_stdout_socket(Server *s) {
2602 union sockaddr_union sa;
2604 struct epoll_event ev;
2608 if (s->stdout_fd < 0) {
2610 s->stdout_fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
2611 if (s->stdout_fd < 0) {
2612 log_error("socket() failed: %m");
2617 sa.un.sun_family = AF_UNIX;
2618 strncpy(sa.un.sun_path, "/run/systemd/journal/stdout", sizeof(sa.un.sun_path));
2620 unlink(sa.un.sun_path);
2622 r = bind(s->stdout_fd, &sa.sa, offsetof(union sockaddr_union, un.sun_path) + strlen(sa.un.sun_path));
2624 log_error("bind() failed: %m");
2628 chmod(sa.un.sun_path, 0666);
2630 if (listen(s->stdout_fd, SOMAXCONN) < 0) {
2631 log_error("liste() failed: %m");
2635 fd_nonblock(s->stdout_fd, 1);
2638 ev.events = EPOLLIN;
2639 ev.data.fd = s->stdout_fd;
2640 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->stdout_fd, &ev) < 0) {
2641 log_error("Failed to add stdout server fd to epoll object: %m");
2648 static int open_dev_kmsg(Server *s) {
2649 struct epoll_event ev;
2653 s->dev_kmsg_fd = open("/dev/kmsg", O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
2654 if (s->dev_kmsg_fd < 0) {
2655 log_warning("Failed to open /dev/kmsg, ignoring: %m");
2660 ev.events = EPOLLIN;
2661 ev.data.fd = s->dev_kmsg_fd;
2662 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->dev_kmsg_fd, &ev) < 0) {
2664 /* This will fail with EPERM on older kernels where
2665 * /dev/kmsg is not readable. */
2669 log_error("Failed to add /dev/kmsg fd to epoll object: %m");
2673 s->dev_kmsg_readable = true;
2678 static int open_kernel_seqnum(Server *s) {
2684 /* We store the seqnum we last read in an mmaped file. That
2685 * way we can just use it like a variable, but it is
2686 * persistant and automatically flushed at reboot. */
2688 fd = open("/run/systemd/journal/kernel-seqnum", O_RDWR|O_CREAT|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW, 0644);
2690 log_error("Failed to open /run/systemd/journal/kernel-seqnum, ignoring: %m");
2694 if (posix_fallocate(fd, 0, sizeof(uint64_t)) < 0) {
2695 log_error("Failed to allocate sequential number file, ignoring: %m");
2696 close_nointr_nofail(fd);
2700 p = mmap(NULL, sizeof(uint64_t), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
2701 if (p == MAP_FAILED) {
2702 log_error("Failed to map sequential number file, ignoring: %m");
2703 close_nointr_nofail(fd);
2707 close_nointr_nofail(fd);
2708 s->kernel_seqnum = p;
2713 static int open_signalfd(Server *s) {
2715 struct epoll_event ev;
2719 assert_se(sigemptyset(&mask) == 0);
2720 sigset_add_many(&mask, SIGINT, SIGTERM, SIGUSR1, SIGUSR2, -1);
2721 assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0);
2723 s->signal_fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
2724 if (s->signal_fd < 0) {
2725 log_error("signalfd(): %m");
2730 ev.events = EPOLLIN;
2731 ev.data.fd = s->signal_fd;
2733 if (epoll_ctl(s->epoll_fd, EPOLL_CTL_ADD, s->signal_fd, &ev) < 0) {
2734 log_error("epoll_ctl(): %m");
2741 static int server_parse_proc_cmdline(Server *s) {
2742 char *line, *w, *state;
2746 if (detect_container(NULL) > 0)
2749 r = read_one_line_file("/proc/cmdline", &line);
2751 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
2755 FOREACH_WORD_QUOTED(w, l, line, state) {
2758 word = strndup(w, l);
2764 if (startswith(word, "systemd.journald.forward_to_syslog=")) {
2765 r = parse_boolean(word + 35);
2767 log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word + 35);
2769 s->forward_to_syslog = r;
2770 } else if (startswith(word, "systemd.journald.forward_to_kmsg=")) {
2771 r = parse_boolean(word + 33);
2773 log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word + 33);
2775 s->forward_to_kmsg = r;
2776 } else if (startswith(word, "systemd.journald.forward_to_console=")) {
2777 r = parse_boolean(word + 36);
2779 log_warning("Failed to parse forward to console switch %s. Ignoring.", word + 36);
2781 s->forward_to_console = r;
2782 } else if (startswith(word, "systemd.journald"))
2783 log_warning("Invalid systemd.journald parameter. Ignoring.");
2795 static int server_parse_config_file(Server *s) {
2802 fn = "/etc/systemd/journald.conf";
2803 f = fopen(fn, "re");
2805 if (errno == ENOENT)
2808 log_warning("Failed to open configuration file %s: %m", fn);
2812 r = config_parse(fn, f, "Journal\0", config_item_perf_lookup, (void*) journald_gperf_lookup, false, s);
2814 log_warning("Failed to parse configuration file: %s", strerror(-r));
2821 static int server_init(Server *s) {
2827 s->syslog_fd = s->native_fd = s->stdout_fd = s->signal_fd = s->epoll_fd = s->dev_kmsg_fd = -1;
2831 s->rate_limit_interval = DEFAULT_RATE_LIMIT_INTERVAL;
2832 s->rate_limit_burst = DEFAULT_RATE_LIMIT_BURST;
2834 s->forward_to_syslog = true;
2836 s->max_level_store = LOG_DEBUG;
2837 s->max_level_syslog = LOG_DEBUG;
2838 s->max_level_kmsg = LOG_NOTICE;
2839 s->max_level_console = LOG_INFO;
2841 memset(&s->system_metrics, 0xFF, sizeof(s->system_metrics));
2842 memset(&s->runtime_metrics, 0xFF, sizeof(s->runtime_metrics));
2844 server_parse_config_file(s);
2845 server_parse_proc_cmdline(s);
2847 mkdir_p("/run/systemd/journal", 0755);
2849 s->user_journals = hashmap_new(trivial_hash_func, trivial_compare_func);
2850 if (!s->user_journals)
2853 s->mmap = mmap_cache_new();
2857 s->epoll_fd = epoll_create1(EPOLL_CLOEXEC);
2858 if (s->epoll_fd < 0) {
2859 log_error("Failed to create epoll object: %m");
2863 n = sd_listen_fds(true);
2865 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n));
2869 for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) {
2871 if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/run/systemd/journal/socket", 0) > 0) {
2873 if (s->native_fd >= 0) {
2874 log_error("Too many native sockets passed.");
2880 } else if (sd_is_socket_unix(fd, SOCK_STREAM, 1, "/run/systemd/journal/stdout", 0) > 0) {
2882 if (s->stdout_fd >= 0) {
2883 log_error("Too many stdout sockets passed.");
2889 } else if (sd_is_socket_unix(fd, SOCK_DGRAM, -1, "/dev/log", 0) > 0) {
2891 if (s->syslog_fd >= 0) {
2892 log_error("Too many /dev/log sockets passed.");
2899 log_error("Unknown socket passed.");
2904 r = open_syslog_socket(s);
2908 r = open_native_socket(s);
2912 r = open_stdout_socket(s);
2916 r = open_dev_kmsg(s);
2920 r = open_kernel_seqnum(s);
2924 r = open_signalfd(s);
2928 s->udev = udev_new();
2932 s->rate_limit = journal_rate_limit_new(s->rate_limit_interval, s->rate_limit_burst);
2936 r = system_journal_open(s);
2943 static void server_done(Server *s) {
2947 while (s->stdout_streams)
2948 stdout_stream_free(s->stdout_streams);
2950 if (s->system_journal)
2951 journal_file_close(s->system_journal);
2953 if (s->runtime_journal)
2954 journal_file_close(s->runtime_journal);
2956 while ((f = hashmap_steal_first(s->user_journals)))
2957 journal_file_close(f);
2959 hashmap_free(s->user_journals);
2961 if (s->epoll_fd >= 0)
2962 close_nointr_nofail(s->epoll_fd);
2964 if (s->signal_fd >= 0)
2965 close_nointr_nofail(s->signal_fd);
2967 if (s->syslog_fd >= 0)
2968 close_nointr_nofail(s->syslog_fd);
2970 if (s->native_fd >= 0)
2971 close_nointr_nofail(s->native_fd);
2973 if (s->stdout_fd >= 0)
2974 close_nointr_nofail(s->stdout_fd);
2976 if (s->dev_kmsg_fd >= 0)
2977 close_nointr_nofail(s->dev_kmsg_fd);
2980 journal_rate_limit_free(s->rate_limit);
2982 if (s->kernel_seqnum)
2983 munmap(s->kernel_seqnum, sizeof(uint64_t));
2989 mmap_cache_unref(s->mmap);
2992 udev_unref(s->udev);
2995 int main(int argc, char *argv[]) {
2999 /* if (getppid() != 1) { */
3000 /* log_error("This program should be invoked by init only."); */
3001 /* return EXIT_FAILURE; */
3005 log_error("This program does not take arguments.");
3006 return EXIT_FAILURE;
3009 log_set_target(LOG_TARGET_SAFE);
3010 log_set_facility(LOG_SYSLOG);
3011 log_set_max_level(LOG_DEBUG);
3012 log_parse_environment();
3017 r = server_init(&server);
3021 server_vacuum(&server);
3022 server_flush_to_var(&server);
3023 server_flush_dev_kmsg(&server);
3025 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
3026 driver_message(&server, SD_MESSAGE_JOURNAL_START, "Journal started");
3030 "STATUS=Processing requests...");
3033 struct epoll_event event;
3039 if (server.system_journal &&
3040 journal_file_next_evolve_usec(server.system_journal, &u)) {
3043 n = now(CLOCK_MONOTONIC);
3048 t = (int) ((u - n + USEC_PER_MSEC - 1) / USEC_PER_MSEC);
3053 r = epoll_wait(server.epoll_fd, &event, 1, t);
3059 log_error("epoll_wait() failed: %m");
3065 r = process_event(&server, &event);
3073 if (server.system_journal)
3074 journal_file_maybe_append_tag(server.system_journal, 0);
3078 log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
3079 driver_message(&server, SD_MESSAGE_JOURNAL_STOP, "Journal stopped");
3083 "STATUS=Shutting down...");
3085 server_done(&server);
3087 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;