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 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.
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.
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/>.
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>
34 #include <systemd/sd-journal.h>
35 #include <systemd/sd-login.h>
36 #include <systemd/sd-messages.h>
37 #include <systemd/sd-daemon.h>
40 #include "journal-file.h"
41 #include "socket-util.h"
42 #include "cgroup-util.h"
44 #include "journal-rate-limit.h"
45 #include "journal-internal.h"
46 #include "conf-parser.h"
53 #include <acl/libacl.h>
58 #include <selinux/selinux.h>
61 #define USER_JOURNALS_MAX 1024
62 #define STDOUT_STREAMS_MAX 4096
64 #define DEFAULT_RATE_LIMIT_INTERVAL (10*USEC_PER_SEC)
65 #define DEFAULT_RATE_LIMIT_BURST 200
67 #define RECHECK_AVAILABLE_SPACE_USEC (30*USEC_PER_SEC)
69 #define RECHECK_VAR_AVAILABLE_USEC (30*USEC_PER_SEC)
71 #define N_IOVEC_META_FIELDS 17
73 #define ENTRY_SIZE_MAX (1024*1024*32)
75 typedef enum StdoutStreamState
{
76 STDOUT_STREAM_IDENTIFIER
,
77 STDOUT_STREAM_PRIORITY
,
78 STDOUT_STREAM_LEVEL_PREFIX
,
79 STDOUT_STREAM_FORWARD_TO_SYSLOG
,
80 STDOUT_STREAM_FORWARD_TO_KMSG
,
81 STDOUT_STREAM_FORWARD_TO_CONSOLE
,
87 StdoutStreamState state
;
93 security_context_t security_context
;
99 bool forward_to_syslog
:1;
100 bool forward_to_kmsg
:1;
101 bool forward_to_console
:1;
103 char buffer
[LINE_MAX
+1];
106 LIST_FIELDS(StdoutStream
, stdout_stream
);
109 static int server_flush_to_var(Server
*s
);
111 static uint64_t available_space(Server
*s
) {
116 uint64_t sum
= 0, avail
= 0, ss_avail
= 0;
122 ts
= now(CLOCK_MONOTONIC
);
124 if (s
->cached_available_space_timestamp
+ RECHECK_AVAILABLE_SPACE_USEC
> ts
)
125 return s
->cached_available_space
;
127 r
= sd_id128_get_machine(&machine
);
131 if (s
->system_journal
) {
132 f
= "/var/log/journal/";
133 m
= &s
->system_metrics
;
135 f
= "/run/log/journal/";
136 m
= &s
->runtime_metrics
;
141 p
= strappend(f
, sd_id128_to_string(machine
, ids
));
151 if (fstatvfs(dirfd(d
), &ss
) < 0)
156 struct dirent buf
, *de
;
159 k
= readdir_r(d
, &buf
, &de
);
168 if (!endswith(de
->d_name
, ".journal") &&
169 !endswith(de
->d_name
, ".journal~"))
172 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_SYMLINK_NOFOLLOW
) < 0)
175 if (!S_ISREG(st
.st_mode
))
178 sum
+= (uint64_t) st
.st_blocks
* 512UL;
181 avail
= sum
>= m
->max_use
? 0 : m
->max_use
- sum
;
183 ss_avail
= ss
.f_bsize
* ss
.f_bavail
;
185 ss_avail
= ss_avail
< m
->keep_free
? 0 : ss_avail
- m
->keep_free
;
187 if (ss_avail
< avail
)
190 s
->cached_available_space
= avail
;
191 s
->cached_available_space_timestamp
= ts
;
199 static void server_read_file_gid(Server
*s
) {
200 const char *adm
= "adm";
205 if (s
->file_gid_valid
)
208 r
= get_group_creds(&adm
, &s
->file_gid
);
210 log_warning("Failed to resolve 'adm' group: %s", strerror(-r
));
212 /* if we couldn't read the gid, then it will be 0, but that's
213 * fine and we shouldn't try to resolve the group again, so
214 * let's just pretend it worked right-away. */
215 s
->file_gid_valid
= true;
218 static void server_fix_perms(Server
*s
, JournalFile
*f
, uid_t uid
) {
223 acl_permset_t permset
;
228 server_read_file_gid(s
);
230 r
= fchmod_and_fchown(f
->fd
, 0640, 0, s
->file_gid
);
232 log_warning("Failed to fix access mode/rights on %s, ignoring: %s", f
->path
, strerror(-r
));
238 acl
= acl_get_fd(f
->fd
);
240 log_warning("Failed to read ACL on %s, ignoring: %m", f
->path
);
244 r
= acl_find_uid(acl
, uid
, &entry
);
247 if (acl_create_entry(&acl
, &entry
) < 0 ||
248 acl_set_tag_type(entry
, ACL_USER
) < 0 ||
249 acl_set_qualifier(entry
, &uid
) < 0) {
250 log_warning("Failed to patch ACL on %s, ignoring: %m", f
->path
);
255 if (acl_get_permset(entry
, &permset
) < 0 ||
256 acl_add_perm(permset
, ACL_READ
) < 0 ||
257 acl_calc_mask(&acl
) < 0) {
258 log_warning("Failed to patch ACL on %s, ignoring: %m", f
->path
);
262 if (acl_set_fd(f
->fd
, acl
) < 0)
263 log_warning("Failed to set ACL on %s, ignoring: %m", f
->path
);
270 static JournalFile
* find_journal(Server
*s
, uid_t uid
) {
279 /* We split up user logs only on /var, not on /run. If the
280 * runtime file is open, we write to it exclusively, in order
281 * to guarantee proper order as soon as we flush /run to
282 * /var and close the runtime file. */
284 if (s
->runtime_journal
)
285 return s
->runtime_journal
;
288 return s
->system_journal
;
290 r
= sd_id128_get_machine(&machine
);
292 return s
->system_journal
;
294 f
= hashmap_get(s
->user_journals
, UINT32_TO_PTR(uid
));
298 if (asprintf(&p
, "/var/log/journal/%s/user-%lu.journal", sd_id128_to_string(machine
, ids
), (unsigned long) uid
) < 0)
299 return s
->system_journal
;
301 while (hashmap_size(s
->user_journals
) >= USER_JOURNALS_MAX
) {
302 /* Too many open? Then let's close one */
303 f
= hashmap_steal_first(s
->user_journals
);
305 journal_file_close(f
);
308 r
= journal_file_open_reliably(p
, O_RDWR
|O_CREAT
, 0640, s
->system_journal
, &f
);
312 return s
->system_journal
;
314 server_fix_perms(s
, f
, uid
);
316 r
= hashmap_put(s
->user_journals
, UINT32_TO_PTR(uid
), f
);
318 journal_file_close(f
);
319 return s
->system_journal
;
325 static void server_rotate(Server
*s
) {
331 log_info("Rotating...");
333 if (s
->runtime_journal
) {
334 r
= journal_file_rotate(&s
->runtime_journal
);
336 log_error("Failed to rotate %s: %s", s
->runtime_journal
->path
, strerror(-r
));
338 server_fix_perms(s
, s
->runtime_journal
, 0);
341 if (s
->system_journal
) {
342 r
= journal_file_rotate(&s
->system_journal
);
344 log_error("Failed to rotate %s: %s", s
->system_journal
->path
, strerror(-r
));
346 server_fix_perms(s
, s
->system_journal
, 0);
349 HASHMAP_FOREACH_KEY(f
, k
, s
->user_journals
, i
) {
350 r
= journal_file_rotate(&f
);
352 log_error("Failed to rotate %s: %s", f
->path
, strerror(-r
));
354 hashmap_replace(s
->user_journals
, k
, f
);
355 server_fix_perms(s
, s
->system_journal
, PTR_TO_UINT32(k
));
360 static void server_vacuum(Server
*s
) {
366 log_info("Vacuuming...");
368 r
= sd_id128_get_machine(&machine
);
370 log_error("Failed to get machine ID: %s", strerror(-r
));
374 sd_id128_to_string(machine
, ids
);
376 if (s
->system_journal
) {
377 if (asprintf(&p
, "/var/log/journal/%s", ids
) < 0) {
378 log_error("Out of memory.");
382 r
= journal_directory_vacuum(p
, s
->system_metrics
.max_use
, s
->system_metrics
.keep_free
);
383 if (r
< 0 && r
!= -ENOENT
)
384 log_error("Failed to vacuum %s: %s", p
, strerror(-r
));
389 if (s
->runtime_journal
) {
390 if (asprintf(&p
, "/run/log/journal/%s", ids
) < 0) {
391 log_error("Out of memory.");
395 r
= journal_directory_vacuum(p
, s
->runtime_metrics
.max_use
, s
->runtime_metrics
.keep_free
);
396 if (r
< 0 && r
!= -ENOENT
)
397 log_error("Failed to vacuum %s: %s", p
, strerror(-r
));
401 s
->cached_available_space_timestamp
= 0;
404 static char *shortened_cgroup_path(pid_t pid
) {
406 char *process_path
, *init_path
, *path
;
410 r
= cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER
, pid
, &process_path
);
414 r
= cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER
, 1, &init_path
);
420 if (endswith(init_path
, "/system"))
421 init_path
[strlen(init_path
) - 7] = 0;
422 else if (streq(init_path
, "/"))
425 if (startswith(process_path
, init_path
)) {
428 p
= strdup(process_path
+ strlen(init_path
));
446 static void dispatch_message_real(
448 struct iovec
*iovec
, unsigned n
, unsigned m
,
451 const char *label
, size_t label_len
) {
453 char *pid
= NULL
, *uid
= NULL
, *gid
= NULL
,
454 *source_time
= NULL
, *boot_id
= NULL
, *machine_id
= NULL
,
455 *comm
= NULL
, *cmdline
= NULL
, *hostname
= NULL
,
456 *audit_session
= NULL
, *audit_loginuid
= NULL
,
457 *exe
= NULL
, *cgroup
= NULL
, *session
= NULL
,
458 *owner_uid
= NULL
, *unit
= NULL
, *selinux_context
= NULL
;
464 uid_t loginuid
= 0, realuid
= 0;
466 bool vacuumed
= false;
471 assert(n
+ N_IOVEC_META_FIELDS
<= m
);
477 realuid
= ucred
->uid
;
479 if (asprintf(&pid
, "_PID=%lu", (unsigned long) ucred
->pid
) >= 0)
480 IOVEC_SET_STRING(iovec
[n
++], pid
);
482 if (asprintf(&uid
, "_UID=%lu", (unsigned long) ucred
->uid
) >= 0)
483 IOVEC_SET_STRING(iovec
[n
++], uid
);
485 if (asprintf(&gid
, "_GID=%lu", (unsigned long) ucred
->gid
) >= 0)
486 IOVEC_SET_STRING(iovec
[n
++], gid
);
488 r
= get_process_comm(ucred
->pid
, &t
);
490 comm
= strappend("_COMM=", t
);
494 IOVEC_SET_STRING(iovec
[n
++], comm
);
497 r
= get_process_exe(ucred
->pid
, &t
);
499 exe
= strappend("_EXE=", t
);
503 IOVEC_SET_STRING(iovec
[n
++], exe
);
506 r
= get_process_cmdline(ucred
->pid
, LINE_MAX
, false, &t
);
508 cmdline
= strappend("_CMDLINE=", t
);
512 IOVEC_SET_STRING(iovec
[n
++], cmdline
);
515 r
= audit_session_from_pid(ucred
->pid
, &audit
);
517 if (asprintf(&audit_session
, "_AUDIT_SESSION=%lu", (unsigned long) audit
) >= 0)
518 IOVEC_SET_STRING(iovec
[n
++], audit_session
);
520 r
= audit_loginuid_from_pid(ucred
->pid
, &loginuid
);
522 if (asprintf(&audit_loginuid
, "_AUDIT_LOGINUID=%lu", (unsigned long) loginuid
) >= 0)
523 IOVEC_SET_STRING(iovec
[n
++], audit_loginuid
);
525 t
= shortened_cgroup_path(ucred
->pid
);
527 cgroup
= strappend("_SYSTEMD_CGROUP=", t
);
531 IOVEC_SET_STRING(iovec
[n
++], cgroup
);
534 if (sd_pid_get_session(ucred
->pid
, &t
) >= 0) {
535 session
= strappend("_SYSTEMD_SESSION=", t
);
539 IOVEC_SET_STRING(iovec
[n
++], session
);
542 if (sd_pid_get_unit(ucred
->pid
, &t
) >= 0) {
543 unit
= strappend("_SYSTEMD_UNIT=", t
);
547 IOVEC_SET_STRING(iovec
[n
++], unit
);
550 if (sd_pid_get_owner_uid(ucred
->uid
, &owner
) >= 0)
551 if (asprintf(&owner_uid
, "_SYSTEMD_OWNER_UID=%lu", (unsigned long) owner
) >= 0)
552 IOVEC_SET_STRING(iovec
[n
++], owner_uid
);
556 selinux_context
= malloc(sizeof("_SELINUX_CONTEXT=") + label_len
);
557 if (selinux_context
) {
558 memcpy(selinux_context
, "_SELINUX_CONTEXT=", sizeof("_SELINUX_CONTEXT=")-1);
559 memcpy(selinux_context
+sizeof("_SELINUX_CONTEXT=")-1, label
, label_len
);
560 selinux_context
[sizeof("_SELINUX_CONTEXT=")-1+label_len
] = 0;
561 IOVEC_SET_STRING(iovec
[n
++], selinux_context
);
564 security_context_t con
;
566 if (getpidcon(ucred
->pid
, &con
) >= 0) {
567 selinux_context
= strappend("_SELINUX_CONTEXT=", con
);
569 IOVEC_SET_STRING(iovec
[n
++], selinux_context
);
578 if (asprintf(&source_time
, "_SOURCE_REALTIME_TIMESTAMP=%llu",
579 (unsigned long long) timeval_load(tv
)) >= 0)
580 IOVEC_SET_STRING(iovec
[n
++], source_time
);
583 /* Note that strictly speaking storing the boot id here is
584 * redundant since the entry includes this in-line
585 * anyway. However, we need this indexed, too. */
586 r
= sd_id128_get_boot(&id
);
588 if (asprintf(&boot_id
, "_BOOT_ID=%s", sd_id128_to_string(id
, idbuf
)) >= 0)
589 IOVEC_SET_STRING(iovec
[n
++], boot_id
);
591 r
= sd_id128_get_machine(&id
);
593 if (asprintf(&machine_id
, "_MACHINE_ID=%s", sd_id128_to_string(id
, idbuf
)) >= 0)
594 IOVEC_SET_STRING(iovec
[n
++], machine_id
);
596 t
= gethostname_malloc();
598 hostname
= strappend("_HOSTNAME=", t
);
601 IOVEC_SET_STRING(iovec
[n
++], hostname
);
606 server_flush_to_var(s
);
609 f
= find_journal(s
, realuid
== 0 ? 0 : loginuid
);
611 log_warning("Dropping message, as we can't find a place to store the data.");
613 r
= journal_file_append_entry(f
, NULL
, iovec
, n
, &s
->seqnum
, NULL
, NULL
);
615 if ((r
== -EBADMSG
|| r
== -E2BIG
) && !vacuumed
) {
618 log_info("Allocation limit reached, rotating.");
620 log_warning("Journal file corrupted, rotating.");
626 log_info("Retrying write.");
631 log_error("Failed to write entry, ignoring: %s", strerror(-r
));
645 free(audit_loginuid
);
650 free(selinux_context
);
653 static void driver_message(Server
*s
, sd_id128_t message_id
, const char *format
, ...) {
654 char mid
[11 + 32 + 1];
655 char buffer
[16 + LINE_MAX
+ 1];
656 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 4];
664 IOVEC_SET_STRING(iovec
[n
++], "PRIORITY=5");
665 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=driver");
667 memcpy(buffer
, "MESSAGE=", 8);
668 va_start(ap
, format
);
669 vsnprintf(buffer
+ 8, sizeof(buffer
) - 8, format
, ap
);
671 char_array_0(buffer
);
672 IOVEC_SET_STRING(iovec
[n
++], buffer
);
674 snprintf(mid
, sizeof(mid
), "MESSAGE_ID=" SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(message_id
));
676 IOVEC_SET_STRING(iovec
[n
++], mid
);
679 ucred
.pid
= getpid();
680 ucred
.uid
= getuid();
681 ucred
.gid
= getgid();
683 dispatch_message_real(s
, iovec
, n
, ELEMENTSOF(iovec
), &ucred
, NULL
, NULL
, 0);
686 static void dispatch_message(Server
*s
,
687 struct iovec
*iovec
, unsigned n
, unsigned m
,
690 const char *label
, size_t label_len
,
693 char *path
= NULL
, *c
;
696 assert(iovec
|| n
== 0);
704 path
= shortened_cgroup_path(ucred
->pid
);
708 /* example: /user/lennart/3/foobar
709 * /system/dbus.service/foobar
711 * So let's cut of everything past the third /, since that is
712 * wher user directories start */
714 c
= strchr(path
, '/');
716 c
= strchr(c
+1, '/');
718 c
= strchr(c
+1, '/');
724 rl
= journal_rate_limit_test(s
->rate_limit
, path
, priority
& LOG_PRIMASK
, available_space(s
));
731 /* Write a suppression message if we suppressed something */
733 driver_message(s
, SD_MESSAGE_JOURNAL_DROPPED
, "Suppressed %u messages from %s", rl
- 1, path
);
738 dispatch_message_real(s
, iovec
, n
, m
, ucred
, tv
, label
, label_len
);
741 static void forward_syslog_iovec(Server
*s
, const struct iovec
*iovec
, unsigned n_iovec
, struct ucred
*ucred
, struct timeval
*tv
) {
742 struct msghdr msghdr
;
743 struct cmsghdr
*cmsg
;
745 struct cmsghdr cmsghdr
;
746 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
))];
748 union sockaddr_union sa
;
755 msghdr
.msg_iov
= (struct iovec
*) iovec
;
756 msghdr
.msg_iovlen
= n_iovec
;
759 sa
.un
.sun_family
= AF_UNIX
;
760 strncpy(sa
.un
.sun_path
, "/run/systemd/journal/syslog", sizeof(sa
.un
.sun_path
));
761 msghdr
.msg_name
= &sa
;
762 msghdr
.msg_namelen
= offsetof(union sockaddr_union
, un
.sun_path
) + strlen(sa
.un
.sun_path
);
766 msghdr
.msg_control
= &control
;
767 msghdr
.msg_controllen
= sizeof(control
);
769 cmsg
= CMSG_FIRSTHDR(&msghdr
);
770 cmsg
->cmsg_level
= SOL_SOCKET
;
771 cmsg
->cmsg_type
= SCM_CREDENTIALS
;
772 cmsg
->cmsg_len
= CMSG_LEN(sizeof(struct ucred
));
773 memcpy(CMSG_DATA(cmsg
), ucred
, sizeof(struct ucred
));
774 msghdr
.msg_controllen
= cmsg
->cmsg_len
;
777 /* Forward the syslog message we received via /dev/log to
778 * /run/systemd/syslog. Unfortunately we currently can't set
779 * the SO_TIMESTAMP auxiliary data, and hence we don't. */
781 if (sendmsg(s
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) >= 0)
784 /* The socket is full? I guess the syslog implementation is
785 * too slow, and we shouldn't wait for that... */
789 if (ucred
&& errno
== ESRCH
) {
792 /* Hmm, presumably the sender process vanished
793 * by now, so let's fix it as good as we
798 memcpy(CMSG_DATA(cmsg
), &u
, sizeof(struct ucred
));
800 if (sendmsg(s
->syslog_fd
, &msghdr
, MSG_NOSIGNAL
) >= 0)
807 log_debug("Failed to forward syslog message: %m");
810 static void forward_syslog_raw(Server
*s
, const char *buffer
, struct ucred
*ucred
, struct timeval
*tv
) {
816 IOVEC_SET_STRING(iovec
, buffer
);
817 forward_syslog_iovec(s
, &iovec
, 1, ucred
, tv
);
820 static void forward_syslog(Server
*s
, int priority
, const char *identifier
, const char *message
, struct ucred
*ucred
, struct timeval
*tv
) {
821 struct iovec iovec
[5];
822 char header_priority
[6], header_time
[64], header_pid
[16];
826 char *ident_buf
= NULL
;
829 assert(priority
>= 0);
830 assert(priority
<= 999);
833 /* First: priority field */
834 snprintf(header_priority
, sizeof(header_priority
), "<%i>", priority
);
835 char_array_0(header_priority
);
836 IOVEC_SET_STRING(iovec
[n
++], header_priority
);
838 /* Second: timestamp */
839 t
= tv
? tv
->tv_sec
: ((time_t) (now(CLOCK_REALTIME
) / USEC_PER_SEC
));
843 if (strftime(header_time
, sizeof(header_time
), "%h %e %T ", tm
) <= 0)
845 IOVEC_SET_STRING(iovec
[n
++], header_time
);
847 /* Third: identifier and PID */
850 get_process_comm(ucred
->pid
, &ident_buf
);
851 identifier
= ident_buf
;
854 snprintf(header_pid
, sizeof(header_pid
), "[%lu]: ", (unsigned long) ucred
->pid
);
855 char_array_0(header_pid
);
858 IOVEC_SET_STRING(iovec
[n
++], identifier
);
860 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
861 } else if (identifier
) {
862 IOVEC_SET_STRING(iovec
[n
++], identifier
);
863 IOVEC_SET_STRING(iovec
[n
++], ": ");
866 /* Fourth: message */
867 IOVEC_SET_STRING(iovec
[n
++], message
);
869 forward_syslog_iovec(s
, iovec
, n
, ucred
, tv
);
874 static int fixup_priority(int priority
) {
876 if ((priority
& LOG_FACMASK
) == 0)
877 return (priority
& LOG_PRIMASK
) | LOG_USER
;
882 static void forward_kmsg(Server
*s
, int priority
, const char *identifier
, const char *message
, struct ucred
*ucred
) {
883 struct iovec iovec
[5];
884 char header_priority
[6], header_pid
[16];
886 char *ident_buf
= NULL
;
890 assert(priority
>= 0);
891 assert(priority
<= 999);
894 /* Never allow messages with kernel facility to be written to
895 * kmsg, regardless where the data comes from. */
896 priority
= fixup_priority(priority
);
898 /* First: priority field */
899 snprintf(header_priority
, sizeof(header_priority
), "<%i>", priority
);
900 char_array_0(header_priority
);
901 IOVEC_SET_STRING(iovec
[n
++], header_priority
);
903 /* Second: identifier and PID */
906 get_process_comm(ucred
->pid
, &ident_buf
);
907 identifier
= ident_buf
;
910 snprintf(header_pid
, sizeof(header_pid
), "[%lu]: ", (unsigned long) ucred
->pid
);
911 char_array_0(header_pid
);
914 IOVEC_SET_STRING(iovec
[n
++], identifier
);
916 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
917 } else if (identifier
) {
918 IOVEC_SET_STRING(iovec
[n
++], identifier
);
919 IOVEC_SET_STRING(iovec
[n
++], ": ");
922 /* Fourth: message */
923 IOVEC_SET_STRING(iovec
[n
++], message
);
924 IOVEC_SET_STRING(iovec
[n
++], "\n");
926 fd
= open("/dev/kmsg", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
928 log_debug("Failed to open /dev/kmsg for logging: %s", strerror(errno
));
932 if (writev(fd
, iovec
, n
) < 0)
933 log_debug("Failed to write to /dev/kmsg for logging: %s", strerror(errno
));
935 close_nointr_nofail(fd
);
941 static void forward_console(Server
*s
, const char *identifier
, const char *message
, struct ucred
*ucred
) {
942 struct iovec iovec
[4];
945 char *ident_buf
= NULL
;
950 /* First: identifier and PID */
953 get_process_comm(ucred
->pid
, &ident_buf
);
954 identifier
= ident_buf
;
957 snprintf(header_pid
, sizeof(header_pid
), "[%lu]: ", (unsigned long) ucred
->pid
);
958 char_array_0(header_pid
);
961 IOVEC_SET_STRING(iovec
[n
++], identifier
);
963 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
964 } else if (identifier
) {
965 IOVEC_SET_STRING(iovec
[n
++], identifier
);
966 IOVEC_SET_STRING(iovec
[n
++], ": ");
970 IOVEC_SET_STRING(iovec
[n
++], message
);
971 IOVEC_SET_STRING(iovec
[n
++], "\n");
973 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
975 log_debug("Failed to open /dev/console for logging: %s", strerror(errno
));
979 if (writev(fd
, iovec
, n
) < 0)
980 log_debug("Failed to write to /dev/console for logging: %s", strerror(errno
));
982 close_nointr_nofail(fd
);
988 static void read_identifier(const char **buf
, char **identifier
, char **pid
) {
999 p
+= strspn(p
, WHITESPACE
);
1000 l
= strcspn(p
, WHITESPACE
);
1009 if (p
[l
-1] == ']') {
1015 t
= strndup(p
+k
+1, l
-k
-2);
1035 *buf
+= strspn(*buf
, WHITESPACE
);
1038 static void process_syslog_message(Server
*s
, const char *buf
, struct ucred
*ucred
, struct timeval
*tv
, const char *label
, size_t label_len
) {
1039 char *message
= NULL
, *syslog_priority
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *syslog_pid
= NULL
;
1040 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 6];
1042 int priority
= LOG_USER
| LOG_INFO
;
1043 char *identifier
= NULL
, *pid
= NULL
;
1048 if (s
->forward_to_syslog
)
1049 forward_syslog_raw(s
, buf
, ucred
, tv
);
1051 parse_syslog_priority((char**) &buf
, &priority
);
1052 skip_syslog_date((char**) &buf
);
1053 read_identifier(&buf
, &identifier
, &pid
);
1055 if (s
->forward_to_kmsg
)
1056 forward_kmsg(s
, priority
, identifier
, buf
, ucred
);
1058 if (s
->forward_to_console
)
1059 forward_console(s
, identifier
, buf
, ucred
);
1061 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=syslog");
1063 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
1064 IOVEC_SET_STRING(iovec
[n
++], syslog_priority
);
1066 if (priority
& LOG_FACMASK
)
1067 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
1068 IOVEC_SET_STRING(iovec
[n
++], syslog_facility
);
1071 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
1072 if (syslog_identifier
)
1073 IOVEC_SET_STRING(iovec
[n
++], syslog_identifier
);
1077 syslog_pid
= strappend("SYSLOG_PID=", pid
);
1079 IOVEC_SET_STRING(iovec
[n
++], syslog_pid
);
1082 message
= strappend("MESSAGE=", buf
);
1084 IOVEC_SET_STRING(iovec
[n
++], message
);
1086 dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), ucred
, tv
, label
, label_len
, priority
);
1091 free(syslog_priority
);
1092 free(syslog_facility
);
1093 free(syslog_identifier
);
1096 static bool valid_user_field(const char *p
, size_t l
) {
1099 /* We kinda enforce POSIX syntax recommendations for
1100 environment variables here, but make a couple of additional
1103 http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap08.html */
1105 /* No empty field names */
1109 /* Don't allow names longer than 64 chars */
1113 /* Variables starting with an underscore are protected */
1117 /* Don't allow digits as first character */
1118 if (p
[0] >= '0' && p
[0] <= '9')
1121 /* Only allow A-Z0-9 and '_' */
1122 for (a
= p
; a
< p
+ l
; a
++)
1123 if (!((*a
>= 'A' && *a
<= 'Z') ||
1124 (*a
>= '0' && *a
<= '9') ||
1131 static void process_native_message(
1133 const void *buffer
, size_t buffer_size
,
1134 struct ucred
*ucred
,
1136 const char *label
, size_t label_len
) {
1138 struct iovec
*iovec
= NULL
;
1139 unsigned n
= 0, m
= 0, j
, tn
= (unsigned) -1;
1142 int priority
= LOG_INFO
;
1143 char *identifier
= NULL
, *message
= NULL
;
1146 assert(buffer
|| n
== 0);
1149 remaining
= buffer_size
;
1151 while (remaining
> 0) {
1154 e
= memchr(p
, '\n', remaining
);
1157 /* Trailing noise, let's ignore it, and flush what we collected */
1158 log_debug("Received message with trailing noise, ignoring.");
1163 /* Entry separator */
1164 dispatch_message(s
, iovec
, n
, m
, ucred
, tv
, label
, label_len
, priority
);
1166 priority
= LOG_INFO
;
1173 if (*p
== '.' || *p
== '#') {
1174 /* Ignore control commands for now, and
1176 remaining
-= (e
- p
) + 1;
1181 /* A property follows */
1183 if (n
+N_IOVEC_META_FIELDS
>= m
) {
1187 u
= MAX((n
+N_IOVEC_META_FIELDS
+1) * 2U, 4U);
1188 c
= realloc(iovec
, u
* sizeof(struct iovec
));
1190 log_error("Out of memory");
1198 q
= memchr(p
, '=', e
- p
);
1200 if (valid_user_field(p
, q
- p
)) {
1205 /* If the field name starts with an
1206 * underscore, skip the variable,
1207 * since that indidates a trusted
1209 iovec
[n
].iov_base
= (char*) p
;
1210 iovec
[n
].iov_len
= l
;
1213 /* We need to determine the priority
1214 * of this entry for the rate limiting
1217 memcmp(p
, "PRIORITY=", 9) == 0 &&
1218 p
[9] >= '0' && p
[9] <= '9')
1219 priority
= (priority
& LOG_FACMASK
) | (p
[9] - '0');
1222 memcmp(p
, "SYSLOG_FACILITY=", 16) == 0 &&
1223 p
[16] >= '0' && p
[16] <= '9')
1224 priority
= (priority
& LOG_PRIMASK
) | ((p
[16] - '0') << 3);
1227 memcmp(p
, "SYSLOG_FACILITY=", 16) == 0 &&
1228 p
[16] >= '0' && p
[16] <= '9' &&
1229 p
[17] >= '0' && p
[17] <= '9')
1230 priority
= (priority
& LOG_PRIMASK
) | (((p
[16] - '0')*10 + (p
[17] - '0')) << 3);
1233 memcmp(p
, "SYSLOG_IDENTIFIER=", 11) == 0) {
1236 t
= strndup(p
+ 11, l
- 11);
1241 } else if (l
>= 8 &&
1242 memcmp(p
, "MESSAGE=", 8) == 0) {
1245 t
= strndup(p
+ 8, l
- 8);
1253 remaining
-= (e
- p
) + 1;
1260 if (remaining
< e
- p
+ 1 + sizeof(uint64_t) + 1) {
1261 log_debug("Failed to parse message, ignoring.");
1265 memcpy(&l
, e
+ 1, sizeof(uint64_t));
1268 if (remaining
< e
- p
+ 1 + sizeof(uint64_t) + l
+ 1 ||
1269 e
[1+sizeof(uint64_t)+l
] != '\n') {
1270 log_debug("Failed to parse message, ignoring.");
1274 k
= malloc((e
- p
) + 1 + l
);
1276 log_error("Out of memory");
1280 memcpy(k
, p
, e
- p
);
1282 memcpy(k
+ (e
- p
) + 1, e
+ 1 + sizeof(uint64_t), l
);
1284 if (valid_user_field(p
, e
- p
)) {
1285 iovec
[n
].iov_base
= k
;
1286 iovec
[n
].iov_len
= (e
- p
) + 1 + l
;
1291 remaining
-= (e
- p
) + 1 + sizeof(uint64_t) + l
+ 1;
1292 p
= e
+ 1 + sizeof(uint64_t) + l
+ 1;
1300 IOVEC_SET_STRING(iovec
[tn
], "_TRANSPORT=journal");
1303 if (s
->forward_to_syslog
)
1304 forward_syslog(s
, priority
, identifier
, message
, ucred
, tv
);
1306 if (s
->forward_to_kmsg
)
1307 forward_kmsg(s
, priority
, identifier
, message
, ucred
);
1309 if (s
->forward_to_console
)
1310 forward_console(s
, identifier
, message
, ucred
);
1313 dispatch_message(s
, iovec
, n
, m
, ucred
, tv
, label
, label_len
, priority
);
1316 for (j
= 0; j
< n
; j
++) {
1320 if (iovec
[j
].iov_base
< buffer
||
1321 (const uint8_t*) iovec
[j
].iov_base
>= (const uint8_t*) buffer
+ buffer_size
)
1322 free(iovec
[j
].iov_base
);
1330 static void process_native_file(
1333 struct ucred
*ucred
,
1335 const char *label
, size_t label_len
) {
1344 /* Data is in the passed file, since it didn't fit in a
1345 * datagram. We can't map the file here, since clients might
1346 * then truncate it and trigger a SIGBUS for us. So let's
1347 * stupidly read it */
1349 if (fstat(fd
, &st
) < 0) {
1350 log_error("Failed to stat passed file, ignoring: %m");
1354 if (!S_ISREG(st
.st_mode
)) {
1355 log_error("File passed is not regular. Ignoring.");
1359 if (st
.st_size
<= 0)
1362 if (st
.st_size
> ENTRY_SIZE_MAX
) {
1363 log_error("File passed too large. Ignoring.");
1367 p
= malloc(st
.st_size
);
1369 log_error("Out of memory");
1373 n
= pread(fd
, p
, st
.st_size
, 0);
1375 log_error("Failed to read file, ignoring: %s", strerror(-n
));
1377 process_native_message(s
, p
, n
, ucred
, tv
, label
, label_len
);
1382 static int stdout_stream_log(StdoutStream
*s
, const char *p
) {
1383 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 5];
1384 char *message
= NULL
, *syslog_priority
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
;
1388 size_t label_len
= 0;
1396 priority
= s
->priority
;
1398 if (s
->level_prefix
)
1399 parse_syslog_priority((char**) &p
, &priority
);
1401 if (s
->forward_to_syslog
|| s
->server
->forward_to_syslog
)
1402 forward_syslog(s
->server
, fixup_priority(priority
), s
->identifier
, p
, &s
->ucred
, NULL
);
1404 if (s
->forward_to_kmsg
|| s
->server
->forward_to_kmsg
)
1405 forward_kmsg(s
->server
, priority
, s
->identifier
, p
, &s
->ucred
);
1407 if (s
->forward_to_console
|| s
->server
->forward_to_console
)
1408 forward_console(s
->server
, s
->identifier
, p
, &s
->ucred
);
1410 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=stdout");
1412 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
1413 IOVEC_SET_STRING(iovec
[n
++], syslog_priority
);
1415 if (priority
& LOG_FACMASK
)
1416 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
1417 IOVEC_SET_STRING(iovec
[n
++], syslog_facility
);
1419 if (s
->identifier
) {
1420 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", s
->identifier
);
1421 if (syslog_identifier
)
1422 IOVEC_SET_STRING(iovec
[n
++], syslog_identifier
);
1425 message
= strappend("MESSAGE=", p
);
1427 IOVEC_SET_STRING(iovec
[n
++], message
);
1430 if (s
->security_context
) {
1431 label
= (char*) s
->security_context
;
1432 label_len
= strlen((char*) s
->security_context
);
1436 dispatch_message(s
->server
, iovec
, n
, ELEMENTSOF(iovec
), &s
->ucred
, NULL
, label
, label_len
, priority
);
1439 free(syslog_priority
);
1440 free(syslog_facility
);
1441 free(syslog_identifier
);
1446 static int stdout_stream_line(StdoutStream
*s
, char *p
) {
1456 case STDOUT_STREAM_IDENTIFIER
:
1458 s
->identifier
= NULL
;
1460 s
->identifier
= strdup(p
);
1461 if (!s
->identifier
) {
1462 log_error("Out of memory");
1467 s
->state
= STDOUT_STREAM_PRIORITY
;
1470 case STDOUT_STREAM_PRIORITY
:
1471 r
= safe_atoi(p
, &s
->priority
);
1472 if (r
< 0 || s
->priority
<= 0 || s
->priority
>= 999) {
1473 log_warning("Failed to parse log priority line.");
1477 s
->state
= STDOUT_STREAM_LEVEL_PREFIX
;
1480 case STDOUT_STREAM_LEVEL_PREFIX
:
1481 r
= parse_boolean(p
);
1483 log_warning("Failed to parse level prefix line.");
1487 s
->level_prefix
= !!r
;
1488 s
->state
= STDOUT_STREAM_FORWARD_TO_SYSLOG
;
1491 case STDOUT_STREAM_FORWARD_TO_SYSLOG
:
1492 r
= parse_boolean(p
);
1494 log_warning("Failed to parse forward to syslog line.");
1498 s
->forward_to_syslog
= !!r
;
1499 s
->state
= STDOUT_STREAM_FORWARD_TO_KMSG
;
1502 case STDOUT_STREAM_FORWARD_TO_KMSG
:
1503 r
= parse_boolean(p
);
1505 log_warning("Failed to parse copy to kmsg line.");
1509 s
->forward_to_kmsg
= !!r
;
1510 s
->state
= STDOUT_STREAM_FORWARD_TO_CONSOLE
;
1513 case STDOUT_STREAM_FORWARD_TO_CONSOLE
:
1514 r
= parse_boolean(p
);
1516 log_warning("Failed to parse copy to console line.");
1520 s
->forward_to_console
= !!r
;
1521 s
->state
= STDOUT_STREAM_RUNNING
;
1524 case STDOUT_STREAM_RUNNING
:
1525 return stdout_stream_log(s
, p
);
1528 assert_not_reached("Unknown stream state");
1531 static int stdout_stream_scan(StdoutStream
*s
, bool force_flush
) {
1539 remaining
= s
->length
;
1544 end
= memchr(p
, '\n', remaining
);
1547 else if (remaining
>= sizeof(s
->buffer
) - 1) {
1548 end
= p
+ sizeof(s
->buffer
) - 1;
1555 r
= stdout_stream_line(s
, p
);
1563 if (force_flush
&& remaining
> 0) {
1565 r
= stdout_stream_line(s
, p
);
1573 if (p
> s
->buffer
) {
1574 memmove(s
->buffer
, p
, remaining
);
1575 s
->length
= remaining
;
1581 static int stdout_stream_process(StdoutStream
*s
) {
1587 l
= read(s
->fd
, s
->buffer
+s
->length
, sizeof(s
->buffer
)-1-s
->length
);
1590 if (errno
== EAGAIN
)
1593 log_warning("Failed to read from stream: %m");
1598 r
= stdout_stream_scan(s
, true);
1606 r
= stdout_stream_scan(s
, false);
1614 static void stdout_stream_free(StdoutStream
*s
) {
1618 assert(s
->server
->n_stdout_streams
> 0);
1619 s
->server
->n_stdout_streams
--;
1620 LIST_REMOVE(StdoutStream
, stdout_stream
, s
->server
->stdout_streams
, s
);
1625 epoll_ctl(s
->server
->epoll_fd
, EPOLL_CTL_DEL
, s
->fd
, NULL
);
1627 close_nointr_nofail(s
->fd
);
1631 if (s
->security_context
)
1632 freecon(s
->security_context
);
1635 free(s
->identifier
);
1639 static int stdout_stream_new(Server
*s
) {
1640 StdoutStream
*stream
;
1643 struct epoll_event ev
;
1647 fd
= accept4(s
->stdout_fd
, NULL
, NULL
, SOCK_NONBLOCK
|SOCK_CLOEXEC
);
1649 if (errno
== EAGAIN
)
1652 log_error("Failed to accept stdout connection: %m");
1656 if (s
->n_stdout_streams
>= STDOUT_STREAMS_MAX
) {
1657 log_warning("Too many stdout streams, refusing connection.");
1658 close_nointr_nofail(fd
);
1662 stream
= new0(StdoutStream
, 1);
1664 log_error("Out of memory.");
1665 close_nointr_nofail(fd
);
1671 len
= sizeof(stream
->ucred
);
1672 if (getsockopt(fd
, SOL_SOCKET
, SO_PEERCRED
, &stream
->ucred
, &len
) < 0) {
1673 log_error("Failed to determine peer credentials: %m");
1679 if (getpeercon(fd
, &stream
->security_context
) < 0)
1680 log_error("Failed to determine peer security context.");
1683 if (shutdown(fd
, SHUT_WR
) < 0) {
1684 log_error("Failed to shutdown writing side of socket: %m");
1690 ev
.data
.ptr
= stream
;
1691 ev
.events
= EPOLLIN
;
1692 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, fd
, &ev
) < 0) {
1693 log_error("Failed to add stream to event loop: %m");
1699 LIST_PREPEND(StdoutStream
, stdout_stream
, s
->stdout_streams
, stream
);
1700 s
->n_stdout_streams
++;
1705 stdout_stream_free(stream
);
1709 static int parse_kernel_timestamp(char **_p
, usec_t
*t
) {
1720 if (strlen(p
) < 14 || p
[0] != '[' || p
[13] != ']' || p
[6] != '.')
1725 for (i
= 1; i
<= 5; i
++) {
1731 k
= undecchar(p
[i
]);
1738 for (i
= 7; i
<= 12; i
++) {
1741 k
= undecchar(p
[i
]);
1750 *_p
+= strspn(*_p
, WHITESPACE
);
1755 static void proc_kmsg_line(Server
*s
, const char *p
) {
1756 struct iovec iovec
[N_IOVEC_META_FIELDS
+ 7];
1757 char *message
= NULL
, *syslog_priority
= NULL
, *syslog_pid
= NULL
, *syslog_facility
= NULL
, *syslog_identifier
= NULL
, *source_time
= NULL
;
1758 int priority
= LOG_KERN
| LOG_INFO
;
1761 char *identifier
= NULL
, *pid
= NULL
;
1769 parse_syslog_priority((char **) &p
, &priority
);
1771 if (s
->forward_to_kmsg
&& (priority
& LOG_FACMASK
) != LOG_KERN
)
1774 if (parse_kernel_timestamp((char **) &p
, &usec
) > 0) {
1775 if (asprintf(&source_time
, "_SOURCE_MONOTONIC_TIMESTAMP=%llu",
1776 (unsigned long long) usec
) >= 0)
1777 IOVEC_SET_STRING(iovec
[n
++], source_time
);
1780 IOVEC_SET_STRING(iovec
[n
++], "_TRANSPORT=kernel");
1782 if (asprintf(&syslog_priority
, "PRIORITY=%i", priority
& LOG_PRIMASK
) >= 0)
1783 IOVEC_SET_STRING(iovec
[n
++], syslog_priority
);
1785 if ((priority
& LOG_FACMASK
) == LOG_KERN
) {
1787 if (s
->forward_to_syslog
)
1788 forward_syslog(s
, priority
, "kernel", p
, NULL
, NULL
);
1790 IOVEC_SET_STRING(iovec
[n
++], "SYSLOG_IDENTIFIER=kernel");
1792 read_identifier(&p
, &identifier
, &pid
);
1794 if (s
->forward_to_syslog
)
1795 forward_syslog(s
, priority
, identifier
, p
, NULL
, NULL
);
1798 syslog_identifier
= strappend("SYSLOG_IDENTIFIER=", identifier
);
1799 if (syslog_identifier
)
1800 IOVEC_SET_STRING(iovec
[n
++], syslog_identifier
);
1804 syslog_pid
= strappend("SYSLOG_PID=", pid
);
1806 IOVEC_SET_STRING(iovec
[n
++], syslog_pid
);
1809 if (asprintf(&syslog_facility
, "SYSLOG_FACILITY=%i", LOG_FAC(priority
)) >= 0)
1810 IOVEC_SET_STRING(iovec
[n
++], syslog_facility
);
1813 message
= strappend("MESSAGE=", p
);
1815 IOVEC_SET_STRING(iovec
[n
++], message
);
1817 dispatch_message(s
, iovec
, n
, ELEMENTSOF(iovec
), NULL
, NULL
, NULL
, 0, priority
);
1820 free(syslog_priority
);
1821 free(syslog_identifier
);
1823 free(syslog_facility
);
1829 static void proc_kmsg_scan(Server
*s
) {
1835 p
= s
->proc_kmsg_buffer
;
1836 remaining
= s
->proc_kmsg_length
;
1841 end
= memchr(p
, '\n', remaining
);
1844 else if (remaining
>= sizeof(s
->proc_kmsg_buffer
) - 1) {
1845 end
= p
+ sizeof(s
->proc_kmsg_buffer
) - 1;
1852 proc_kmsg_line(s
, p
);
1858 if (p
> s
->proc_kmsg_buffer
) {
1859 memmove(s
->proc_kmsg_buffer
, p
, remaining
);
1860 s
->proc_kmsg_length
= remaining
;
1864 static int system_journal_open(Server
*s
) {
1870 r
= sd_id128_get_machine(&machine
);
1874 sd_id128_to_string(machine
, ids
);
1876 if (!s
->system_journal
) {
1878 /* First try to create the machine path, but not the prefix */
1879 fn
= strappend("/var/log/journal/", ids
);
1882 (void) mkdir(fn
, 0755);
1885 /* The create the system journal file */
1886 fn
= join("/var/log/journal/", ids
, "/system.journal", NULL
);
1890 r
= journal_file_open_reliably(fn
, O_RDWR
|O_CREAT
, 0640, NULL
, &s
->system_journal
);
1894 journal_default_metrics(&s
->system_metrics
, s
->system_journal
->fd
);
1896 s
->system_journal
->metrics
= s
->system_metrics
;
1897 s
->system_journal
->compress
= s
->compress
;
1899 server_fix_perms(s
, s
->system_journal
, 0);
1902 if (r
!= -ENOENT
&& r
!= -EROFS
)
1903 log_warning("Failed to open system journal: %s", strerror(-r
));
1909 if (!s
->runtime_journal
) {
1911 fn
= join("/run/log/journal/", ids
, "/system.journal", NULL
);
1915 if (s
->system_journal
) {
1917 /* Try to open the runtime journal, but only
1918 * if it already exists, so that we can flush
1919 * it into the system journal */
1921 r
= journal_file_open(fn
, O_RDWR
, 0640, NULL
, &s
->runtime_journal
);
1926 log_warning("Failed to open runtime journal: %s", strerror(-r
));
1933 /* OK, we really need the runtime journal, so create
1934 * it if necessary. */
1936 (void) mkdir_parents(fn
, 0755);
1937 r
= journal_file_open_reliably(fn
, O_RDWR
|O_CREAT
, 0640, NULL
, &s
->runtime_journal
);
1941 log_error("Failed to open runtime journal: %s", strerror(-r
));
1946 if (s
->runtime_journal
) {
1947 journal_default_metrics(&s
->runtime_metrics
, s
->runtime_journal
->fd
);
1949 s
->runtime_journal
->metrics
= s
->runtime_metrics
;
1950 s
->runtime_journal
->compress
= s
->compress
;
1952 server_fix_perms(s
, s
->runtime_journal
, 0);
1959 static int server_flush_to_var(Server
*s
) {
1960 char path
[] = "/run/log/journal/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
1969 if (!s
->runtime_journal
)
1972 ts
= now(CLOCK_MONOTONIC
);
1973 if (s
->var_available_timestamp
+ RECHECK_VAR_AVAILABLE_USEC
> ts
)
1976 s
->var_available_timestamp
= ts
;
1978 system_journal_open(s
);
1980 if (!s
->system_journal
)
1983 log_info("Flushing to /var...");
1985 r
= sd_id128_get_machine(&machine
);
1987 log_error("Failed to get machine id: %s", strerror(-r
));
1991 r
= sd_journal_open(&j
, SD_JOURNAL_RUNTIME_ONLY
);
1993 log_error("Failed to read runtime journal: %s", strerror(-r
));
1997 SD_JOURNAL_FOREACH(j
) {
2000 f
= j
->current_file
;
2001 assert(f
&& f
->current_offset
> 0);
2003 r
= journal_file_move_to_object(f
, OBJECT_ENTRY
, f
->current_offset
, &o
);
2005 log_error("Can't read entry: %s", strerror(-r
));
2009 r
= journal_file_copy_entry(f
, s
->system_journal
, o
, f
->current_offset
, NULL
, NULL
, NULL
);
2011 log_info("Allocation limit reached.");
2013 journal_file_post_change(s
->system_journal
);
2017 r
= journal_file_copy_entry(f
, s
->system_journal
, o
, f
->current_offset
, NULL
, NULL
, NULL
);
2021 log_error("Can't write entry: %s", strerror(-r
));
2027 journal_file_post_change(s
->system_journal
);
2029 journal_file_close(s
->runtime_journal
);
2030 s
->runtime_journal
= NULL
;
2033 sd_id128_to_string(machine
, path
+ 17);
2034 rm_rf(path
, false, true, false);
2040 static int server_read_proc_kmsg(Server
*s
) {
2043 assert(s
->proc_kmsg_fd
>= 0);
2045 l
= read(s
->proc_kmsg_fd
, s
->proc_kmsg_buffer
+ s
->proc_kmsg_length
, sizeof(s
->proc_kmsg_buffer
) - 1 - s
->proc_kmsg_length
);
2048 if (errno
== EAGAIN
|| errno
== EINTR
)
2051 log_error("Failed to read from kernel: %m");
2055 s
->proc_kmsg_length
+= l
;
2061 static int server_flush_proc_kmsg(Server
*s
) {
2066 if (s
->proc_kmsg_fd
< 0)
2069 log_info("Flushing /proc/kmsg...");
2072 r
= server_read_proc_kmsg(s
);
2083 static int process_event(Server
*s
, struct epoll_event
*ev
) {
2086 if (ev
->data
.fd
== s
->signal_fd
) {
2087 struct signalfd_siginfo sfsi
;
2090 if (ev
->events
!= EPOLLIN
) {
2091 log_info("Got invalid event from epoll.");
2095 n
= read(s
->signal_fd
, &sfsi
, sizeof(sfsi
));
2096 if (n
!= sizeof(sfsi
)) {
2101 if (errno
== EINTR
|| errno
== EAGAIN
)
2107 if (sfsi
.ssi_signo
== SIGUSR1
) {
2108 server_flush_to_var(s
);
2112 log_debug("Received SIG%s", signal_to_string(sfsi
.ssi_signo
));
2115 } else if (ev
->data
.fd
== s
->proc_kmsg_fd
) {
2118 if (ev
->events
!= EPOLLIN
) {
2119 log_info("Got invalid event from epoll.");
2123 r
= server_read_proc_kmsg(s
);
2129 } else if (ev
->data
.fd
== s
->native_fd
||
2130 ev
->data
.fd
== s
->syslog_fd
) {
2132 if (ev
->events
!= EPOLLIN
) {
2133 log_info("Got invalid event from epoll.");
2138 struct msghdr msghdr
;
2140 struct ucred
*ucred
= NULL
;
2141 struct timeval
*tv
= NULL
;
2142 struct cmsghdr
*cmsg
;
2144 size_t label_len
= 0;
2146 struct cmsghdr cmsghdr
;
2147 uint8_t buf
[CMSG_SPACE(sizeof(struct ucred
)) +
2148 CMSG_SPACE(sizeof(struct timeval
)) +
2149 CMSG_SPACE(sizeof(int)) +
2150 CMSG_SPACE(PAGE_SIZE
)]; /* selinux label */
2157 if (ioctl(ev
->data
.fd
, SIOCINQ
, &v
) < 0) {
2158 log_error("SIOCINQ failed: %m");
2162 if (s
->buffer_size
< (size_t) v
) {
2166 l
= MAX(LINE_MAX
+ (size_t) v
, s
->buffer_size
* 2);
2167 b
= realloc(s
->buffer
, l
+1);
2170 log_error("Couldn't increase buffer.");
2179 iovec
.iov_base
= s
->buffer
;
2180 iovec
.iov_len
= s
->buffer_size
;
2184 msghdr
.msg_iov
= &iovec
;
2185 msghdr
.msg_iovlen
= 1;
2186 msghdr
.msg_control
= &control
;
2187 msghdr
.msg_controllen
= sizeof(control
);
2189 n
= recvmsg(ev
->data
.fd
, &msghdr
, MSG_DONTWAIT
|MSG_CMSG_CLOEXEC
);
2192 if (errno
== EINTR
|| errno
== EAGAIN
)
2195 log_error("recvmsg() failed: %m");
2199 for (cmsg
= CMSG_FIRSTHDR(&msghdr
); cmsg
; cmsg
= CMSG_NXTHDR(&msghdr
, cmsg
)) {
2201 if (cmsg
->cmsg_level
== SOL_SOCKET
&&
2202 cmsg
->cmsg_type
== SCM_CREDENTIALS
&&
2203 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct ucred
)))
2204 ucred
= (struct ucred
*) CMSG_DATA(cmsg
);
2205 else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
2206 cmsg
->cmsg_type
== SCM_SECURITY
) {
2207 label
= (char*) CMSG_DATA(cmsg
);
2208 label_len
= cmsg
->cmsg_len
- CMSG_LEN(0);
2209 } else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
2210 cmsg
->cmsg_type
== SO_TIMESTAMP
&&
2211 cmsg
->cmsg_len
== CMSG_LEN(sizeof(struct timeval
)))
2212 tv
= (struct timeval
*) CMSG_DATA(cmsg
);
2213 else if (cmsg
->cmsg_level
== SOL_SOCKET
&&
2214 cmsg
->cmsg_type
== SCM_RIGHTS
) {
2215 fds
= (int*) CMSG_DATA(cmsg
);
2216 n_fds
= (cmsg
->cmsg_len
- CMSG_LEN(0)) / sizeof(int);
2220 if (ev
->data
.fd
== s
->syslog_fd
) {
2223 if (n
> 0 && n_fds
== 0) {
2224 e
= memchr(s
->buffer
, '\n', n
);
2230 process_syslog_message(s
, strstrip(s
->buffer
), ucred
, tv
, label
, label_len
);
2231 } else if (n_fds
> 0)
2232 log_warning("Got file descriptors via syslog socket. Ignoring.");
2235 if (n
> 0 && n_fds
== 0)
2236 process_native_message(s
, s
->buffer
, n
, ucred
, tv
, label
, label_len
);
2237 else if (n
== 0 && n_fds
== 1)
2238 process_native_file(s
, fds
[0], ucred
, tv
, label
, label_len
);
2240 log_warning("Got too many file descriptors via native socket. Ignoring.");
2243 close_many(fds
, n_fds
);
2248 } else if (ev
->data
.fd
== s
->stdout_fd
) {
2250 if (ev
->events
!= EPOLLIN
) {
2251 log_info("Got invalid event from epoll.");
2255 stdout_stream_new(s
);
2259 StdoutStream
*stream
;
2261 if ((ev
->events
|EPOLLIN
|EPOLLHUP
) != (EPOLLIN
|EPOLLHUP
)) {
2262 log_info("Got invalid event from epoll.");
2266 /* If it is none of the well-known fds, it must be an
2267 * stdout stream fd. Note that this is a bit ugly here
2268 * (since we rely that none of the well-known fds
2269 * could be interpreted as pointer), but nonetheless
2270 * safe, since the well-known fds would never get an
2271 * fd > 4096, i.e. beyond the first memory page */
2273 stream
= ev
->data
.ptr
;
2275 if (stdout_stream_process(stream
) <= 0)
2276 stdout_stream_free(stream
);
2281 log_error("Unknown event.");
2285 static int open_syslog_socket(Server
*s
) {
2286 union sockaddr_union sa
;
2288 struct epoll_event ev
;
2292 if (s
->syslog_fd
< 0) {
2294 s
->syslog_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
2295 if (s
->syslog_fd
< 0) {
2296 log_error("socket() failed: %m");
2301 sa
.un
.sun_family
= AF_UNIX
;
2302 strncpy(sa
.un
.sun_path
, "/dev/log", sizeof(sa
.un
.sun_path
));
2304 unlink(sa
.un
.sun_path
);
2306 r
= bind(s
->syslog_fd
, &sa
.sa
, offsetof(union sockaddr_union
, un
.sun_path
) + strlen(sa
.un
.sun_path
));
2308 log_error("bind() failed: %m");
2312 chmod(sa
.un
.sun_path
, 0666);
2314 fd_nonblock(s
->syslog_fd
, 1);
2317 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
2319 log_error("SO_PASSCRED failed: %m");
2325 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
));
2327 log_warning("SO_PASSSEC failed: %m");
2331 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
));
2333 log_error("SO_TIMESTAMP failed: %m");
2338 ev
.events
= EPOLLIN
;
2339 ev
.data
.fd
= s
->syslog_fd
;
2340 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->syslog_fd
, &ev
) < 0) {
2341 log_error("Failed to add syslog server fd to epoll object: %m");
2348 static int open_native_socket(Server
*s
) {
2349 union sockaddr_union sa
;
2351 struct epoll_event ev
;
2355 if (s
->native_fd
< 0) {
2357 s
->native_fd
= socket(AF_UNIX
, SOCK_DGRAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
2358 if (s
->native_fd
< 0) {
2359 log_error("socket() failed: %m");
2364 sa
.un
.sun_family
= AF_UNIX
;
2365 strncpy(sa
.un
.sun_path
, "/run/systemd/journal/socket", sizeof(sa
.un
.sun_path
));
2367 unlink(sa
.un
.sun_path
);
2369 r
= bind(s
->native_fd
, &sa
.sa
, offsetof(union sockaddr_union
, un
.sun_path
) + strlen(sa
.un
.sun_path
));
2371 log_error("bind() failed: %m");
2375 chmod(sa
.un
.sun_path
, 0666);
2377 fd_nonblock(s
->native_fd
, 1);
2380 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_PASSCRED
, &one
, sizeof(one
));
2382 log_error("SO_PASSCRED failed: %m");
2388 r
= setsockopt(s
->syslog_fd
, SOL_SOCKET
, SO_PASSSEC
, &one
, sizeof(one
));
2390 log_warning("SO_PASSSEC failed: %m");
2394 r
= setsockopt(s
->native_fd
, SOL_SOCKET
, SO_TIMESTAMP
, &one
, sizeof(one
));
2396 log_error("SO_TIMESTAMP failed: %m");
2401 ev
.events
= EPOLLIN
;
2402 ev
.data
.fd
= s
->native_fd
;
2403 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->native_fd
, &ev
) < 0) {
2404 log_error("Failed to add native server fd to epoll object: %m");
2411 static int open_stdout_socket(Server
*s
) {
2412 union sockaddr_union sa
;
2414 struct epoll_event ev
;
2418 if (s
->stdout_fd
< 0) {
2420 s
->stdout_fd
= socket(AF_UNIX
, SOCK_STREAM
|SOCK_CLOEXEC
|SOCK_NONBLOCK
, 0);
2421 if (s
->stdout_fd
< 0) {
2422 log_error("socket() failed: %m");
2427 sa
.un
.sun_family
= AF_UNIX
;
2428 strncpy(sa
.un
.sun_path
, "/run/systemd/journal/stdout", sizeof(sa
.un
.sun_path
));
2430 unlink(sa
.un
.sun_path
);
2432 r
= bind(s
->stdout_fd
, &sa
.sa
, offsetof(union sockaddr_union
, un
.sun_path
) + strlen(sa
.un
.sun_path
));
2434 log_error("bind() failed: %m");
2438 chmod(sa
.un
.sun_path
, 0666);
2440 if (listen(s
->stdout_fd
, SOMAXCONN
) < 0) {
2441 log_error("liste() failed: %m");
2445 fd_nonblock(s
->stdout_fd
, 1);
2448 ev
.events
= EPOLLIN
;
2449 ev
.data
.fd
= s
->stdout_fd
;
2450 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->stdout_fd
, &ev
) < 0) {
2451 log_error("Failed to add stdout server fd to epoll object: %m");
2458 static int open_proc_kmsg(Server
*s
) {
2459 struct epoll_event ev
;
2463 if (!s
->import_proc_kmsg
)
2467 s
->proc_kmsg_fd
= open("/proc/kmsg", O_CLOEXEC
|O_NONBLOCK
|O_NOCTTY
);
2468 if (s
->proc_kmsg_fd
< 0) {
2469 log_warning("Failed to open /proc/kmsg, ignoring: %m");
2474 ev
.events
= EPOLLIN
;
2475 ev
.data
.fd
= s
->proc_kmsg_fd
;
2476 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->proc_kmsg_fd
, &ev
) < 0) {
2477 log_error("Failed to add /proc/kmsg fd to epoll object: %m");
2484 static int open_signalfd(Server
*s
) {
2486 struct epoll_event ev
;
2490 assert_se(sigemptyset(&mask
) == 0);
2491 sigset_add_many(&mask
, SIGINT
, SIGTERM
, SIGUSR1
, -1);
2492 assert_se(sigprocmask(SIG_SETMASK
, &mask
, NULL
) == 0);
2494 s
->signal_fd
= signalfd(-1, &mask
, SFD_NONBLOCK
|SFD_CLOEXEC
);
2495 if (s
->signal_fd
< 0) {
2496 log_error("signalfd(): %m");
2501 ev
.events
= EPOLLIN
;
2502 ev
.data
.fd
= s
->signal_fd
;
2504 if (epoll_ctl(s
->epoll_fd
, EPOLL_CTL_ADD
, s
->signal_fd
, &ev
) < 0) {
2505 log_error("epoll_ctl(): %m");
2512 static int server_parse_proc_cmdline(Server
*s
) {
2513 char *line
, *w
, *state
;
2517 if (detect_container(NULL
) > 0)
2520 r
= read_one_line_file("/proc/cmdline", &line
);
2522 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r
));
2526 FOREACH_WORD_QUOTED(w
, l
, line
, state
) {
2529 word
= strndup(w
, l
);
2535 if (startswith(word
, "systemd_journald.forward_to_syslog=")) {
2536 r
= parse_boolean(word
+ 35);
2538 log_warning("Failed to parse forward to syslog switch %s. Ignoring.", word
+ 35);
2540 s
->forward_to_syslog
= r
;
2541 } else if (startswith(word
, "systemd_journald.forward_to_kmsg=")) {
2542 r
= parse_boolean(word
+ 33);
2544 log_warning("Failed to parse forward to kmsg switch %s. Ignoring.", word
+ 33);
2546 s
->forward_to_kmsg
= r
;
2547 } else if (startswith(word
, "systemd_journald.forward_to_console=")) {
2548 r
= parse_boolean(word
+ 36);
2550 log_warning("Failed to parse forward to console switch %s. Ignoring.", word
+ 36);
2552 s
->forward_to_console
= r
;
2565 static int server_parse_config_file(Server
*s
) {
2572 fn
= "/etc/systemd/systemd-journald.conf";
2573 f
= fopen(fn
, "re");
2575 if (errno
== ENOENT
)
2578 log_warning("Failed to open configuration file %s: %m", fn
);
2582 r
= config_parse(fn
, f
, "Journal\0", config_item_perf_lookup
, (void*) journald_gperf_lookup
, false, s
);
2584 log_warning("Failed to parse configuration file: %s", strerror(-r
));
2591 static int server_init(Server
*s
) {
2597 s
->syslog_fd
= s
->native_fd
= s
->stdout_fd
= s
->signal_fd
= s
->epoll_fd
= s
->proc_kmsg_fd
= -1;
2600 s
->rate_limit_interval
= DEFAULT_RATE_LIMIT_INTERVAL
;
2601 s
->rate_limit_burst
= DEFAULT_RATE_LIMIT_BURST
;
2603 s
->forward_to_syslog
= true;
2604 s
->import_proc_kmsg
= true;
2606 memset(&s
->system_metrics
, 0xFF, sizeof(s
->system_metrics
));
2607 memset(&s
->runtime_metrics
, 0xFF, sizeof(s
->runtime_metrics
));
2609 server_parse_config_file(s
);
2610 server_parse_proc_cmdline(s
);
2612 s
->user_journals
= hashmap_new(trivial_hash_func
, trivial_compare_func
);
2613 if (!s
->user_journals
) {
2614 log_error("Out of memory.");
2618 s
->epoll_fd
= epoll_create1(EPOLL_CLOEXEC
);
2619 if (s
->epoll_fd
< 0) {
2620 log_error("Failed to create epoll object: %m");
2624 n
= sd_listen_fds(true);
2626 log_error("Failed to read listening file descriptors from environment: %s", strerror(-n
));
2630 for (fd
= SD_LISTEN_FDS_START
; fd
< SD_LISTEN_FDS_START
+ n
; fd
++) {
2632 if (sd_is_socket_unix(fd
, SOCK_DGRAM
, -1, "/run/systemd/journal/socket", 0) > 0) {
2634 if (s
->native_fd
>= 0) {
2635 log_error("Too many native sockets passed.");
2641 } else if (sd_is_socket_unix(fd
, SOCK_STREAM
, 1, "/run/systemd/journal/stdout", 0) > 0) {
2643 if (s
->stdout_fd
>= 0) {
2644 log_error("Too many stdout sockets passed.");
2650 } else if (sd_is_socket_unix(fd
, SOCK_DGRAM
, -1, "/dev/log", 0) > 0) {
2652 if (s
->syslog_fd
>= 0) {
2653 log_error("Too many /dev/log sockets passed.");
2660 log_error("Unknown socket passed.");
2665 r
= open_syslog_socket(s
);
2669 r
= open_native_socket(s
);
2673 r
= open_stdout_socket(s
);
2677 r
= open_proc_kmsg(s
);
2681 r
= open_signalfd(s
);
2685 s
->rate_limit
= journal_rate_limit_new(s
->rate_limit_interval
, s
->rate_limit_burst
);
2689 r
= system_journal_open(s
);
2696 static void server_done(Server
*s
) {
2700 while (s
->stdout_streams
)
2701 stdout_stream_free(s
->stdout_streams
);
2703 if (s
->system_journal
)
2704 journal_file_close(s
->system_journal
);
2706 if (s
->runtime_journal
)
2707 journal_file_close(s
->runtime_journal
);
2709 while ((f
= hashmap_steal_first(s
->user_journals
)))
2710 journal_file_close(f
);
2712 hashmap_free(s
->user_journals
);
2714 if (s
->epoll_fd
>= 0)
2715 close_nointr_nofail(s
->epoll_fd
);
2717 if (s
->signal_fd
>= 0)
2718 close_nointr_nofail(s
->signal_fd
);
2720 if (s
->syslog_fd
>= 0)
2721 close_nointr_nofail(s
->syslog_fd
);
2723 if (s
->native_fd
>= 0)
2724 close_nointr_nofail(s
->native_fd
);
2726 if (s
->stdout_fd
>= 0)
2727 close_nointr_nofail(s
->stdout_fd
);
2729 if (s
->proc_kmsg_fd
>= 0)
2730 close_nointr_nofail(s
->proc_kmsg_fd
);
2733 journal_rate_limit_free(s
->rate_limit
);
2738 int main(int argc
, char *argv
[]) {
2742 /* if (getppid() != 1) { */
2743 /* log_error("This program should be invoked by init only."); */
2744 /* return EXIT_FAILURE; */
2748 log_error("This program does not take arguments.");
2749 return EXIT_FAILURE
;
2752 log_set_target(LOG_TARGET_CONSOLE
);
2753 log_parse_environment();
2758 r
= server_init(&server
);
2762 server_vacuum(&server
);
2763 server_flush_to_var(&server
);
2764 server_flush_proc_kmsg(&server
);
2766 log_debug("systemd-journald running as pid %lu", (unsigned long) getpid());
2767 driver_message(&server
, SD_MESSAGE_JOURNAL_START
, "Journal started");
2771 "STATUS=Processing requests...");
2774 struct epoll_event event
;
2776 r
= epoll_wait(server
.epoll_fd
, &event
, 1, -1);
2782 log_error("epoll_wait() failed: %m");
2788 r
= process_event(&server
, &event
);
2795 log_debug("systemd-journald stopped as pid %lu", (unsigned long) getpid());
2796 driver_message(&server
, SD_MESSAGE_JOURNAL_STOP
, "Journal stopped");
2800 "STATUS=Shutting down...");
2802 server_done(&server
);
2804 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;