1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include <sys/signalfd.h>
11 #include <sys/socket.h>
17 #include "sd-messages.h"
19 #include "alloc-util.h"
20 #include "errno-util.h"
22 #include "format-util.h"
26 #include "parse-util.h"
27 #include "proc-cmdline.h"
28 #include "process-util.h"
29 #include "ratelimit.h"
30 #include "signal-util.h"
31 #include "socket-util.h"
32 #include "stdio-util.h"
33 #include "string-table.h"
34 #include "string-util.h"
35 #include "syslog-util.h"
36 #include "terminal-util.h"
37 #include "time-util.h"
40 #define SNDBUF_SIZE (8*1024*1024)
42 static LogTarget log_target
= LOG_TARGET_CONSOLE
;
43 static int log_max_level
[] = {LOG_INFO
, LOG_INFO
};
44 assert_cc(ELEMENTSOF(log_max_level
) == _LOG_REALM_MAX
);
45 static int log_facility
= LOG_DAEMON
;
47 static int console_fd
= STDERR_FILENO
;
48 static int syslog_fd
= -1;
49 static int kmsg_fd
= -1;
50 static int journal_fd
= -1;
52 static bool syslog_is_stream
= false;
54 static bool show_color
= false;
55 static bool show_location
= false;
57 static bool upgrade_syslog_to_journal
= false;
58 static bool always_reopen_console
= false;
59 static bool open_when_needed
= false;
60 static bool prohibit_ipc
= false;
62 /* Akin to glibc's __abort_msg; which is private and we hence cannot
64 static char *log_abort_msg
= NULL
;
66 /* An assert to use in logging functions that does not call recursively
67 * into our logging functions (since that might lead to a loop). */
68 #define assert_raw(expr) \
70 if (_unlikely_(!(expr))) { \
71 fputs(#expr "\n", stderr); \
76 static void log_close_console(void) {
77 console_fd
= safe_close_above_stdio(console_fd
);
80 static int log_open_console(void) {
82 if (!always_reopen_console
) {
83 console_fd
= STDERR_FILENO
;
90 fd
= open_terminal("/dev/console", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
94 console_fd
= fd_move_above_stdio(fd
);
100 static void log_close_kmsg(void) {
101 kmsg_fd
= safe_close(kmsg_fd
);
104 static int log_open_kmsg(void) {
109 kmsg_fd
= open("/dev/kmsg", O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
113 kmsg_fd
= fd_move_above_stdio(kmsg_fd
);
117 static void log_close_syslog(void) {
118 syslog_fd
= safe_close(syslog_fd
);
121 static int create_log_socket(int type
) {
125 fd
= socket(AF_UNIX
, type
|SOCK_CLOEXEC
, 0);
129 fd
= fd_move_above_stdio(fd
);
130 (void) fd_inc_sndbuf(fd
, SNDBUF_SIZE
);
132 /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
133 * in the unlikely case of a deadlock. */
134 if (getpid_cached() == 1)
135 timeval_store(&tv
, 10 * USEC_PER_MSEC
);
137 timeval_store(&tv
, 10 * USEC_PER_SEC
);
138 (void) setsockopt(fd
, SOL_SOCKET
, SO_SNDTIMEO
, &tv
, sizeof(tv
));
143 static int log_open_syslog(void) {
145 static const union sockaddr_union sa
= {
146 .un
.sun_family
= AF_UNIX
,
147 .un
.sun_path
= "/dev/log",
155 syslog_fd
= create_log_socket(SOCK_DGRAM
);
161 if (connect(syslog_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0) {
162 safe_close(syslog_fd
);
164 /* Some legacy syslog systems still use stream
165 * sockets. They really shouldn't. But what can we
167 syslog_fd
= create_log_socket(SOCK_STREAM
);
173 if (connect(syslog_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0) {
178 syslog_is_stream
= true;
180 syslog_is_stream
= false;
189 static void log_close_journal(void) {
190 journal_fd
= safe_close(journal_fd
);
193 static int log_open_journal(void) {
195 static const union sockaddr_union sa
= {
196 .un
.sun_family
= AF_UNIX
,
197 .un
.sun_path
= "/run/systemd/journal/socket",
205 journal_fd
= create_log_socket(SOCK_DGRAM
);
206 if (journal_fd
< 0) {
211 if (connect(journal_fd
, &sa
.sa
, SOCKADDR_UN_LEN(sa
.un
)) < 0) {
226 /* Do not call from library code. */
228 /* If we don't use the console we close it here, to not get
229 * killed by SAK. If we don't use syslog we close it here so
230 * that we are not confused by somebody deleting the socket in
231 * the fs, and to make sure we don't use it if prohibit_ipc is
232 * set. If we don't use /dev/kmsg we still keep it open,
233 * because there is no reason to close it. */
235 if (log_target
== LOG_TARGET_NULL
) {
242 if (log_target
!= LOG_TARGET_AUTO
||
243 getpid_cached() == 1 ||
244 isatty(STDERR_FILENO
) <= 0) {
247 IN_SET(log_target
, LOG_TARGET_AUTO
,
248 LOG_TARGET_JOURNAL_OR_KMSG
,
249 LOG_TARGET_JOURNAL
)) {
250 r
= log_open_journal();
259 IN_SET(log_target
, LOG_TARGET_SYSLOG_OR_KMSG
,
260 LOG_TARGET_SYSLOG
)) {
261 r
= log_open_syslog();
269 if (IN_SET(log_target
, LOG_TARGET_AUTO
,
270 LOG_TARGET_JOURNAL_OR_KMSG
,
271 LOG_TARGET_SYSLOG_OR_KMSG
,
286 return log_open_console();
289 void log_set_target(LogTarget target
) {
291 assert(target
< _LOG_TARGET_MAX
);
293 if (upgrade_syslog_to_journal
) {
294 if (target
== LOG_TARGET_SYSLOG
)
295 target
= LOG_TARGET_JOURNAL
;
296 else if (target
== LOG_TARGET_SYSLOG_OR_KMSG
)
297 target
= LOG_TARGET_JOURNAL_OR_KMSG
;
303 void log_close(void) {
304 /* Do not call from library code. */
312 void log_forget_fds(void) {
313 /* Do not call from library code. */
315 console_fd
= kmsg_fd
= syslog_fd
= journal_fd
= -1;
318 void log_set_max_level_realm(LogRealm realm
, int level
) {
319 assert((level
& LOG_PRIMASK
) == level
);
320 assert(realm
< ELEMENTSOF(log_max_level
));
322 log_max_level
[realm
] = level
;
325 void log_set_facility(int facility
) {
326 log_facility
= facility
;
329 static int write_to_console(
335 const char *buffer
) {
337 char location
[256], prefix
[1 + DECIMAL_STR_MAX(int) + 2];
338 struct iovec iovec
[6] = {};
339 const char *on
= NULL
, *off
= NULL
;
345 if (log_target
== LOG_TARGET_CONSOLE_PREFIXED
) {
346 xsprintf(prefix
, "<%i>", level
);
347 iovec
[n
++] = IOVEC_MAKE_STRING(prefix
);
351 get_log_colors(LOG_PRI(level
), &on
, &off
, NULL
);
354 const char *lon
= "", *loff
= "";
356 lon
= ANSI_HIGHLIGHT_YELLOW4
;
360 (void) snprintf(location
, sizeof location
, "%s%s:%i%s: ", lon
, file
, line
, loff
);
361 iovec
[n
++] = IOVEC_MAKE_STRING(location
);
365 iovec
[n
++] = IOVEC_MAKE_STRING(on
);
366 iovec
[n
++] = IOVEC_MAKE_STRING(buffer
);
368 iovec
[n
++] = IOVEC_MAKE_STRING(off
);
369 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
371 if (writev(console_fd
, iovec
, n
) < 0) {
373 if (errno
== EIO
&& getpid_cached() == 1) {
375 /* If somebody tried to kick us from our console tty (via vhangup() or suchlike), try
379 (void) log_open_console();
383 if (writev(console_fd
, iovec
, n
) < 0)
392 static int write_to_syslog(
398 const char *buffer
) {
400 char header_priority
[2 + DECIMAL_STR_MAX(int) + 1],
402 header_pid
[4 + DECIMAL_STR_MAX(pid_t
) + 1];
403 struct iovec iovec
[5] = {};
404 struct msghdr msghdr
= {
406 .msg_iovlen
= ELEMENTSOF(iovec
),
414 xsprintf(header_priority
, "<%i>", level
);
416 t
= (time_t) (now(CLOCK_REALTIME
) / USEC_PER_SEC
);
417 if (!localtime_r(&t
, &tm
))
420 if (strftime(header_time
, sizeof(header_time
), "%h %e %T ", &tm
) <= 0)
423 xsprintf(header_pid
, "["PID_FMT
"]: ", getpid_cached());
425 iovec
[0] = IOVEC_MAKE_STRING(header_priority
);
426 iovec
[1] = IOVEC_MAKE_STRING(header_time
);
427 iovec
[2] = IOVEC_MAKE_STRING(program_invocation_short_name
);
428 iovec
[3] = IOVEC_MAKE_STRING(header_pid
);
429 iovec
[4] = IOVEC_MAKE_STRING(buffer
);
431 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
432 if (syslog_is_stream
)
438 n
= sendmsg(syslog_fd
, &msghdr
, MSG_NOSIGNAL
);
442 if (!syslog_is_stream
||
443 (size_t) n
>= IOVEC_TOTAL_SIZE(iovec
, ELEMENTSOF(iovec
)))
446 IOVEC_INCREMENT(iovec
, ELEMENTSOF(iovec
), n
);
452 static int write_to_kmsg(
458 const char *buffer
) {
460 /* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a
461 * safety catch for the case where start indiscriminately logging in a loop. It will not catch cases
462 * where we log excessively, but not in a tight loop.
464 * Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple
467 static thread_local RateLimit ratelimit
= { 5 * USEC_PER_SEC
, 200 };
469 char header_priority
[2 + DECIMAL_STR_MAX(int) + 1],
470 header_pid
[4 + DECIMAL_STR_MAX(pid_t
) + 1];
471 struct iovec iovec
[5] = {};
476 if (!ratelimit_below(&ratelimit
))
479 xsprintf(header_priority
, "<%i>", level
);
480 xsprintf(header_pid
, "["PID_FMT
"]: ", getpid_cached());
482 iovec
[0] = IOVEC_MAKE_STRING(header_priority
);
483 iovec
[1] = IOVEC_MAKE_STRING(program_invocation_short_name
);
484 iovec
[2] = IOVEC_MAKE_STRING(header_pid
);
485 iovec
[3] = IOVEC_MAKE_STRING(buffer
);
486 iovec
[4] = IOVEC_MAKE_STRING("\n");
488 if (writev(kmsg_fd
, iovec
, ELEMENTSOF(iovec
)) < 0)
494 static int log_do_header(
499 const char *file
, int line
, const char *func
,
500 const char *object_field
, const char *object
,
501 const char *extra_field
, const char *extra
) {
504 error
= IS_SYNTHETIC_ERRNO(error
) ? 0 : ERRNO_VALUE(error
);
506 r
= snprintf(header
, size
,
508 "SYSLOG_FACILITY=%i\n"
509 "%s%.256s%s" /* CODE_FILE */
510 "%s%.*i%s" /* CODE_LINE */
511 "%s%.256s%s" /* CODE_FUNC */
512 "%s%.*i%s" /* ERRNO */
513 "%s%.256s%s" /* object */
514 "%s%.256s%s" /* extra */
515 "SYSLOG_IDENTIFIER=%.256s\n",
518 isempty(file
) ? "" : "CODE_FILE=",
519 isempty(file
) ? "" : file
,
520 isempty(file
) ? "" : "\n",
521 line
? "CODE_LINE=" : "",
522 line
? 1 : 0, line
, /* %.0d means no output too, special case for 0 */
524 isempty(func
) ? "" : "CODE_FUNC=",
525 isempty(func
) ? "" : func
,
526 isempty(func
) ? "" : "\n",
527 error
? "ERRNO=" : "",
528 error
? 1 : 0, error
,
530 isempty(object
) ? "" : object_field
,
531 isempty(object
) ? "" : object
,
532 isempty(object
) ? "" : "\n",
533 isempty(extra
) ? "" : extra_field
,
534 isempty(extra
) ? "" : extra
,
535 isempty(extra
) ? "" : "\n",
536 program_invocation_short_name
);
537 assert_raw((size_t) r
< size
);
542 static int write_to_journal(
548 const char *object_field
,
550 const char *extra_field
,
552 const char *buffer
) {
554 char header
[LINE_MAX
];
555 struct iovec iovec
[4] = {};
556 struct msghdr mh
= {};
561 log_do_header(header
, sizeof(header
), level
, error
, file
, line
, func
, object_field
, object
, extra_field
, extra
);
563 iovec
[0] = IOVEC_MAKE_STRING(header
);
564 iovec
[1] = IOVEC_MAKE_STRING("MESSAGE=");
565 iovec
[2] = IOVEC_MAKE_STRING(buffer
);
566 iovec
[3] = IOVEC_MAKE_STRING("\n");
569 mh
.msg_iovlen
= ELEMENTSOF(iovec
);
571 if (sendmsg(journal_fd
, &mh
, MSG_NOSIGNAL
) < 0)
577 int log_dispatch_internal(
583 const char *object_field
,
585 const char *extra_field
,
591 if (log_target
== LOG_TARGET_NULL
)
592 return -ERRNO_VALUE(error
);
594 /* Patch in LOG_DAEMON facility if necessary */
595 if ((level
& LOG_FACMASK
) == 0)
596 level
|= log_facility
;
598 if (open_when_needed
)
605 buffer
+= strspn(buffer
, NEWLINE
);
610 if ((e
= strpbrk(buffer
, NEWLINE
)))
613 if (IN_SET(log_target
, LOG_TARGET_AUTO
,
614 LOG_TARGET_JOURNAL_OR_KMSG
,
615 LOG_TARGET_JOURNAL
)) {
617 k
= write_to_journal(level
, error
, file
, line
, func
, object_field
, object
, extra_field
, extra
, buffer
);
618 if (k
< 0 && k
!= -EAGAIN
)
622 if (IN_SET(log_target
, LOG_TARGET_SYSLOG_OR_KMSG
,
623 LOG_TARGET_SYSLOG
)) {
625 k
= write_to_syslog(level
, error
, file
, line
, func
, buffer
);
626 if (k
< 0 && k
!= -EAGAIN
)
631 IN_SET(log_target
, LOG_TARGET_AUTO
,
632 LOG_TARGET_SYSLOG_OR_KMSG
,
633 LOG_TARGET_JOURNAL_OR_KMSG
,
639 k
= write_to_kmsg(level
, error
, file
, line
, func
, buffer
);
642 (void) log_open_console();
647 (void) write_to_console(level
, error
, file
, line
, func
, buffer
);
652 if (open_when_needed
)
655 return -ERRNO_VALUE(error
);
658 int log_dump_internal(
666 LogRealm realm
= LOG_REALM_REMOVE_LEVEL(level
);
669 /* This modifies the buffer... */
671 if (_likely_(LOG_PRI(level
) > log_max_level
[realm
]))
672 return -ERRNO_VALUE(error
);
674 return log_dispatch_internal(level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
, buffer
);
677 int log_internalv_realm(
686 LogRealm realm
= LOG_REALM_REMOVE_LEVEL(level
);
687 char buffer
[LINE_MAX
];
690 if (_likely_(LOG_PRI(level
) > log_max_level
[realm
]))
691 return -ERRNO_VALUE(error
);
693 /* Make sure that %m maps to the specified error (or "Success"). */
694 errno
= ERRNO_VALUE(error
);
696 (void) vsnprintf(buffer
, sizeof buffer
, format
, ap
);
698 return log_dispatch_internal(level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
, buffer
);
701 int log_internal_realm(
707 const char *format
, ...) {
712 va_start(ap
, format
);
713 r
= log_internalv_realm(level
, error
, file
, line
, func
, format
, ap
);
719 int log_object_internalv(
725 const char *object_field
,
727 const char *extra_field
,
735 if (_likely_(LOG_PRI(level
) > log_max_level
[LOG_REALM_SYSTEMD
]))
736 return -ERRNO_VALUE(error
);
738 /* Make sure that %m maps to the specified error (or "Success"). */
739 errno
= ERRNO_VALUE(error
);
741 /* Prepend the object name before the message */
746 buffer
= newa(char, n
+ 2 + LINE_MAX
);
747 b
= stpcpy(stpcpy(buffer
, object
), ": ");
749 b
= buffer
= newa(char, LINE_MAX
);
751 (void) vsnprintf(b
, LINE_MAX
, format
, ap
);
753 return log_dispatch_internal(level
, error
, file
, line
, func
,
754 object_field
, object
, extra_field
, extra
, buffer
);
757 int log_object_internal(
763 const char *object_field
,
765 const char *extra_field
,
767 const char *format
, ...) {
772 va_start(ap
, format
);
773 r
= log_object_internalv(level
, error
, file
, line
, func
, object_field
, object
, extra_field
, extra
, format
, ap
);
779 static void log_assert(
785 const char *format
) {
787 static char buffer
[LINE_MAX
];
788 LogRealm realm
= LOG_REALM_REMOVE_LEVEL(level
);
790 if (_likely_(LOG_PRI(level
) > log_max_level
[realm
]))
793 DISABLE_WARNING_FORMAT_NONLITERAL
;
794 (void) snprintf(buffer
, sizeof buffer
, format
, text
, file
, line
, func
);
797 log_abort_msg
= buffer
;
799 log_dispatch_internal(level
, 0, file
, line
, func
, NULL
, NULL
, NULL
, NULL
, buffer
);
802 _noreturn_
void log_assert_failed_realm(
809 log_assert(LOG_REALM_PLUS_LEVEL(realm
, LOG_CRIT
), text
, file
, line
, func
,
810 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
814 _noreturn_
void log_assert_failed_unreachable_realm(
821 log_assert(LOG_REALM_PLUS_LEVEL(realm
, LOG_CRIT
), text
, file
, line
, func
,
822 "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
826 void log_assert_failed_return_realm(
833 log_assert(LOG_REALM_PLUS_LEVEL(realm
, LOG_DEBUG
), text
, file
, line
, func
,
834 "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
837 int log_oom_internal(LogRealm realm
, const char *file
, int line
, const char *func
) {
838 return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm
, LOG_ERR
),
839 ENOMEM
, file
, line
, func
, "Out of memory.");
842 int log_format_iovec(
846 bool newline_separator
,
851 static const char nl
= '\n';
853 while (format
&& *n
+ 1 < iovec_len
) {
858 /* We need to copy the va_list structure,
859 * since vasprintf() leaves it afterwards at
860 * an undefined location */
862 errno
= ERRNO_VALUE(error
);
865 r
= vasprintf(&m
, format
, aq
);
870 /* Now, jump enough ahead, so that we point to
871 * the next format string */
872 VA_FORMAT_ADVANCE(format
, ap
);
874 iovec
[(*n
)++] = IOVEC_MAKE_STRING(m
);
876 if (newline_separator
) {
877 iovec
[*n
] = IOVEC_MAKE((char *)&nl
, 1);
881 format
= va_arg(ap
, char *);
886 int log_struct_internal(
892 const char *format
, ...) {
894 LogRealm realm
= LOG_REALM_REMOVE_LEVEL(level
);
900 if (_likely_(LOG_PRI(level
) > log_max_level
[realm
]) ||
901 log_target
== LOG_TARGET_NULL
)
902 return -ERRNO_VALUE(error
);
904 if ((level
& LOG_FACMASK
) == 0)
905 level
|= log_facility
;
907 if (IN_SET(log_target
,
909 LOG_TARGET_JOURNAL_OR_KMSG
,
910 LOG_TARGET_JOURNAL
)) {
912 if (open_when_needed
)
915 if (journal_fd
>= 0) {
916 char header
[LINE_MAX
];
917 struct iovec iovec
[17] = {};
923 bool fallback
= false;
925 /* If the journal is available do structured logging.
926 * Do not report the errno if it is synthetic. */
927 log_do_header(header
, sizeof(header
), level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
);
928 iovec
[n
++] = IOVEC_MAKE_STRING(header
);
930 va_start(ap
, format
);
931 r
= log_format_iovec(iovec
, ELEMENTSOF(iovec
), &n
, true, error
, format
, ap
);
936 (void) sendmsg(journal_fd
, &mh
, MSG_NOSIGNAL
);
940 for (i
= 1; i
< n
; i
+= 2)
941 free(iovec
[i
].iov_base
);
944 if (open_when_needed
)
947 return -ERRNO_VALUE(error
);
952 /* Fallback if journal logging is not available or didn't work. */
954 va_start(ap
, format
);
958 errno
= ERRNO_VALUE(error
);
961 (void) vsnprintf(buf
, sizeof buf
, format
, aq
);
964 if (startswith(buf
, "MESSAGE=")) {
969 VA_FORMAT_ADVANCE(format
, ap
);
971 format
= va_arg(ap
, char *);
976 if (open_when_needed
)
979 return -ERRNO_VALUE(error
);
982 return log_dispatch_internal(level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
, buf
+ 8);
985 int log_struct_iovec_internal(
991 const struct iovec input_iovec
[],
992 size_t n_input_iovec
) {
994 LogRealm realm
= LOG_REALM_REMOVE_LEVEL(level
);
999 if (_likely_(LOG_PRI(level
) > log_max_level
[realm
]) ||
1000 log_target
== LOG_TARGET_NULL
)
1001 return -ERRNO_VALUE(error
);
1003 if ((level
& LOG_FACMASK
) == 0)
1004 level
|= log_facility
;
1006 if (IN_SET(log_target
, LOG_TARGET_AUTO
,
1007 LOG_TARGET_JOURNAL_OR_KMSG
,
1008 LOG_TARGET_JOURNAL
) &&
1011 struct iovec iovec
[1 + n_input_iovec
*2];
1012 char header
[LINE_MAX
];
1013 struct msghdr mh
= {
1015 .msg_iovlen
= 1 + n_input_iovec
*2,
1018 log_do_header(header
, sizeof(header
), level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
);
1019 iovec
[0] = IOVEC_MAKE_STRING(header
);
1021 for (i
= 0; i
< n_input_iovec
; i
++) {
1022 iovec
[1+i
*2] = input_iovec
[i
];
1023 iovec
[1+i
*2+1] = IOVEC_MAKE_STRING("\n");
1026 if (sendmsg(journal_fd
, &mh
, MSG_NOSIGNAL
) >= 0)
1027 return -ERRNO_VALUE(error
);
1030 for (i
= 0; i
< n_input_iovec
; i
++)
1031 if (memory_startswith(input_iovec
[i
].iov_base
, input_iovec
[i
].iov_len
, "MESSAGE="))
1034 if (_unlikely_(i
>= n_input_iovec
)) /* Couldn't find MESSAGE=? */
1035 return -ERRNO_VALUE(error
);
1037 m
= strndupa(input_iovec
[i
].iov_base
+ STRLEN("MESSAGE="),
1038 input_iovec
[i
].iov_len
- STRLEN("MESSAGE="));
1040 return log_dispatch_internal(level
, error
, file
, line
, func
, NULL
, NULL
, NULL
, NULL
, m
);
1043 int log_set_target_from_string(const char *e
) {
1046 t
= log_target_from_string(e
);
1054 int log_set_max_level_from_string_realm(LogRealm realm
, const char *e
) {
1057 t
= log_level_from_string(e
);
1061 log_set_max_level_realm(realm
, t
);
1065 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
1068 * The systemd.log_xyz= settings are parsed by all tools, and
1071 * However, "quiet" is only parsed by PID 1, and only turns of
1072 * status output to /dev/console, but does not alter the log
1076 if (streq(key
, "debug") && !value
)
1077 log_set_max_level(LOG_DEBUG
);
1079 else if (proc_cmdline_key_streq(key
, "systemd.log_target")) {
1081 if (proc_cmdline_value_missing(key
, value
))
1084 if (log_set_target_from_string(value
) < 0)
1085 log_warning("Failed to parse log target '%s'. Ignoring.", value
);
1087 } else if (proc_cmdline_key_streq(key
, "systemd.log_level")) {
1089 if (proc_cmdline_value_missing(key
, value
))
1092 if (log_set_max_level_from_string(value
) < 0)
1093 log_warning("Failed to parse log level '%s'. Ignoring.", value
);
1095 } else if (proc_cmdline_key_streq(key
, "systemd.log_color")) {
1097 if (log_show_color_from_string(value
?: "1") < 0)
1098 log_warning("Failed to parse log color setting '%s'. Ignoring.", value
);
1100 } else if (proc_cmdline_key_streq(key
, "systemd.log_location")) {
1102 if (log_show_location_from_string(value
?: "1") < 0)
1103 log_warning("Failed to parse log location setting '%s'. Ignoring.", value
);
1109 void log_parse_environment_realm(LogRealm realm
) {
1110 /* Do not call from library code. */
1114 if (getpid_cached() == 1 || get_ctty_devnr(0, NULL
) < 0)
1115 /* Only try to read the command line in daemons. We assume that anything that has a
1116 * controlling tty is user stuff. For PID1 we do a special check in case it hasn't
1117 * closed the console yet. */
1118 (void) proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, PROC_CMDLINE_STRIP_RD_PREFIX
);
1120 e
= getenv("SYSTEMD_LOG_TARGET");
1121 if (e
&& log_set_target_from_string(e
) < 0)
1122 log_warning("Failed to parse log target '%s'. Ignoring.", e
);
1124 e
= getenv("SYSTEMD_LOG_LEVEL");
1125 if (e
&& log_set_max_level_from_string_realm(realm
, e
) < 0)
1126 log_warning("Failed to parse log level '%s'. Ignoring.", e
);
1128 e
= getenv("SYSTEMD_LOG_COLOR");
1129 if (e
&& log_show_color_from_string(e
) < 0)
1130 log_warning("Failed to parse log color '%s'. Ignoring.", e
);
1132 e
= getenv("SYSTEMD_LOG_LOCATION");
1133 if (e
&& log_show_location_from_string(e
) < 0)
1134 log_warning("Failed to parse log location '%s'. Ignoring.", e
);
1137 LogTarget
log_get_target(void) {
1141 int log_get_max_level_realm(LogRealm realm
) {
1142 return log_max_level
[realm
];
1145 void log_show_color(bool b
) {
1149 bool log_get_show_color(void) {
1153 void log_show_location(bool b
) {
1157 bool log_get_show_location(void) {
1158 return show_location
;
1161 int log_show_color_from_string(const char *e
) {
1164 t
= parse_boolean(e
);
1172 int log_show_location_from_string(const char *e
) {
1175 t
= parse_boolean(e
);
1179 log_show_location(t
);
1183 bool log_on_console(void) {
1184 if (IN_SET(log_target
, LOG_TARGET_CONSOLE
,
1185 LOG_TARGET_CONSOLE_PREFIXED
))
1188 return syslog_fd
< 0 && kmsg_fd
< 0 && journal_fd
< 0;
1191 static const char *const log_target_table
[_LOG_TARGET_MAX
] = {
1192 [LOG_TARGET_CONSOLE
] = "console",
1193 [LOG_TARGET_CONSOLE_PREFIXED
] = "console-prefixed",
1194 [LOG_TARGET_KMSG
] = "kmsg",
1195 [LOG_TARGET_JOURNAL
] = "journal",
1196 [LOG_TARGET_JOURNAL_OR_KMSG
] = "journal-or-kmsg",
1197 [LOG_TARGET_SYSLOG
] = "syslog",
1198 [LOG_TARGET_SYSLOG_OR_KMSG
] = "syslog-or-kmsg",
1199 [LOG_TARGET_AUTO
] = "auto",
1200 [LOG_TARGET_NULL
] = "null",
1203 DEFINE_STRING_TABLE_LOOKUP(log_target
, LogTarget
);
1205 void log_received_signal(int level
, const struct signalfd_siginfo
*si
) {
1208 if (pid_is_valid(si
->ssi_pid
)) {
1209 _cleanup_free_
char *p
= NULL
;
1211 (void) get_process_comm(si
->ssi_pid
, &p
);
1214 "Received SIG%s from PID %"PRIu32
" (%s).",
1215 signal_to_string(si
->ssi_signo
),
1216 si
->ssi_pid
, strna(p
));
1220 signal_to_string(si
->ssi_signo
));
1223 int log_syntax_internal(
1226 const char *config_file
,
1227 unsigned config_line
,
1232 const char *format
, ...) {
1235 char buffer
[LINE_MAX
];
1237 const char *unit_fmt
= NULL
;
1239 if (_likely_(LOG_PRI(level
) > log_max_level
[LOG_REALM_SYSTEMD
]) ||
1240 log_target
== LOG_TARGET_NULL
)
1241 return -ERRNO_VALUE(error
);
1243 errno
= ERRNO_VALUE(error
);
1245 va_start(ap
, format
);
1246 (void) vsnprintf(buffer
, sizeof buffer
, format
, ap
);
1250 unit_fmt
= getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
1253 if (config_line
> 0)
1254 return log_struct_internal(
1255 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
1258 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
1259 "CONFIG_FILE=%s", config_file
,
1260 "CONFIG_LINE=%u", config_line
,
1261 LOG_MESSAGE("%s:%u: %s", config_file
, config_line
, buffer
),
1265 return log_struct_internal(
1266 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
1269 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
1270 "CONFIG_FILE=%s", config_file
,
1271 LOG_MESSAGE("%s: %s", config_file
, buffer
),
1275 return log_struct_internal(
1276 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
1279 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
1280 LOG_MESSAGE("%s: %s", unit
, buffer
),
1284 return log_struct_internal(
1285 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD
, level
),
1288 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR
,
1289 LOG_MESSAGE("%s", buffer
),
1293 int log_syntax_invalid_utf8_internal(
1296 const char *config_file
,
1297 unsigned config_line
,
1301 const char *rvalue
) {
1303 _cleanup_free_
char *p
= NULL
;
1306 p
= utf8_escape_invalid(rvalue
);
1308 log_syntax_internal(unit
, level
, config_file
, config_line
, 0, file
, line
, func
,
1309 "String is not UTF-8 clean, ignoring assignment: %s", strna(p
));
1314 void log_set_upgrade_syslog_to_journal(bool b
) {
1315 upgrade_syslog_to_journal
= b
;
1317 /* Make the change effective immediately */
1319 if (log_target
== LOG_TARGET_SYSLOG
)
1320 log_target
= LOG_TARGET_JOURNAL
;
1321 else if (log_target
== LOG_TARGET_SYSLOG_OR_KMSG
)
1322 log_target
= LOG_TARGET_JOURNAL_OR_KMSG
;
1326 void log_set_always_reopen_console(bool b
) {
1327 always_reopen_console
= b
;
1330 void log_set_open_when_needed(bool b
) {
1331 open_when_needed
= b
;
1334 void log_set_prohibit_ipc(bool b
) {
1338 int log_emergency_level(void) {
1339 /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1340 * then the system of the whole system is obviously affected. */
1342 return getpid_cached() == 1 ? LOG_EMERG
: LOG_ERR
;
1345 int log_dup_console(void) {
1348 /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
1349 * whenever we want to continue logging through the original fd, but want to rearrange stderr. */
1351 if (console_fd
>= 3)
1354 copy
= fcntl(console_fd
, F_DUPFD_CLOEXEC
, 3);
1362 void log_setup_service(void) {
1363 /* Sets up logging the way it is most appropriate for running a program as a service. Note that using this
1364 * doesn't make the binary unsuitable for invocation on the command line, as log output will still go to the
1365 * terminal if invoked interactively. */
1367 log_set_target(LOG_TARGET_AUTO
);
1368 log_parse_environment();