1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
16 #include <sys/signalfd.h>
17 #include <sys/socket.h>
24 #include "sd-messages.h"
26 #include "alloc-util.h"
28 #include "format-util.h"
33 #include "parse-util.h"
34 #include "proc-cmdline.h"
35 #include "process-util.h"
36 #include "signal-util.h"
37 #include "socket-util.h"
38 #include "stdio-util.h"
39 #include "string-table.h"
40 #include "string-util.h"
41 #include "syslog-util.h"
42 #include "terminal-util.h"
43 #include "time-util.h"
47 #define SNDBUF_SIZE (8*1024*1024)
49 static LogTarget log_target = LOG_TARGET_CONSOLE;
50 static int log_max_level[] = {LOG_INFO, LOG_INFO};
51 assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX);
52 static int log_facility = LOG_DAEMON;
54 static int console_fd = STDERR_FILENO;
55 static int syslog_fd = -1;
56 static int kmsg_fd = -1;
57 static int journal_fd = -1;
59 static bool syslog_is_stream = false;
61 static bool show_color = false;
62 static bool show_location = false;
64 #if 0 /// UNNEEDED by elogind
65 static bool upgrade_syslog_to_journal = false;
67 static bool always_reopen_console = false;
68 static bool open_when_needed = false;
69 static bool prohibit_ipc = false;
71 /* Akin to glibc's __abort_msg; which is private and we hence cannot
73 static char *log_abort_msg = NULL;
75 /* An assert to use in logging functions that does not call recursively
76 * into our logging functions (since that might lead to a loop). */
77 #define assert_raw(expr) \
79 if (_unlikely_(!(expr))) { \
80 fputs(#expr "\n", stderr); \
85 static void log_close_console(void) {
86 console_fd = safe_close_above_stdio(console_fd);
89 static int log_open_console(void) {
91 if (!always_reopen_console) {
92 console_fd = STDERR_FILENO;
97 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
101 console_fd = fd_move_above_stdio(console_fd);
107 static void log_close_kmsg(void) {
108 kmsg_fd = safe_close(kmsg_fd);
111 static int log_open_kmsg(void) {
116 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
120 kmsg_fd = fd_move_above_stdio(kmsg_fd);
124 static void log_close_syslog(void) {
125 syslog_fd = safe_close(syslog_fd);
128 static int create_log_socket(int type) {
132 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
136 fd = fd_move_above_stdio(fd);
137 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
139 /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
140 * in the unlikely case of a deadlock. */
141 if (getpid_cached() == 1)
142 timeval_store(&tv, 10 * USEC_PER_MSEC);
144 timeval_store(&tv, 10 * USEC_PER_SEC);
145 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
150 static int log_open_syslog(void) {
152 static const union sockaddr_union sa = {
153 .un.sun_family = AF_UNIX,
154 .un.sun_path = "/dev/log",
162 syslog_fd = create_log_socket(SOCK_DGRAM);
168 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
169 safe_close(syslog_fd);
171 /* Some legacy syslog systems still use stream
172 * sockets. They really shouldn't. But what can we
174 syslog_fd = create_log_socket(SOCK_STREAM);
180 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
185 syslog_is_stream = true;
187 syslog_is_stream = false;
196 static void log_close_journal(void) {
197 #if 0 /// elogind does not support journald
198 journal_fd = safe_close(journal_fd);
202 #if 0 /// UNNEEDED by elogind
203 static int log_open_journal(void) {
205 static const union sockaddr_union sa = {
206 .un.sun_family = AF_UNIX,
207 .un.sun_path = "/run/systemd/journal/socket",
215 journal_fd = create_log_socket(SOCK_DGRAM);
216 if (journal_fd < 0) {
221 if (connect(journal_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
237 /* Do not call from library code. */
239 /* If we don't use the console we close it here, to not get
240 * killed by SAK. If we don't use syslog we close it here so
241 * that we are not confused by somebody deleting the socket in
242 * the fs, and to make sure we don't use it if prohibit_ipc is
243 * set. If we don't use /dev/kmsg we still keep it open,
244 * because there is no reason to close it. */
246 if (log_target == LOG_TARGET_NULL) {
253 if (log_target != LOG_TARGET_AUTO ||
254 getpid_cached() == 1 ||
255 isatty(STDERR_FILENO) <= 0) {
257 #if 0 /// elogind does not support logging to systemd-journald
259 IN_SET(log_target, LOG_TARGET_AUTO,
260 LOG_TARGET_JOURNAL_OR_KMSG,
261 LOG_TARGET_JOURNAL)) {
262 r = log_open_journal();
272 IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
273 LOG_TARGET_SYSLOG)) {
274 r = log_open_syslog();
282 if (IN_SET(log_target, LOG_TARGET_AUTO,
283 LOG_TARGET_JOURNAL_OR_KMSG,
284 LOG_TARGET_SYSLOG_OR_KMSG,
299 return log_open_console();
302 void log_set_target(LogTarget target) {
304 assert(target < _LOG_TARGET_MAX);
306 #if 0 /// elogind does not support logging to systemd-journald
307 if (upgrade_syslog_to_journal) {
308 if (target == LOG_TARGET_SYSLOG)
309 target = LOG_TARGET_JOURNAL;
310 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
311 target = LOG_TARGET_JOURNAL_OR_KMSG;
318 void log_close(void) {
319 /* Do not call from library code. */
327 #if 0 /// UNNEEDED by elogind
328 void log_forget_fds(void) {
329 /* Do not call from library code. */
331 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
335 void log_set_max_level_realm(LogRealm realm, int level) {
336 assert((level & LOG_PRIMASK) == level);
337 assert(realm < ELEMENTSOF(log_max_level));
339 log_max_level[realm] = level;
342 void log_set_facility(int facility) {
343 log_facility = facility;
346 static int write_to_console(
352 const char *buffer) {
354 char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
355 struct iovec iovec[6] = {};
362 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
363 xsprintf(prefix, "<%i>", level);
364 iovec[n++] = IOVEC_MAKE_STRING(prefix);
367 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
370 (void) snprintf(location, sizeof location, "(%s:%i) ", file, line);
371 iovec[n++] = IOVEC_MAKE_STRING(location);
375 iovec[n++] = IOVEC_MAKE_STRING(ANSI_HIGHLIGHT_RED);
376 iovec[n++] = IOVEC_MAKE_STRING(buffer);
378 iovec[n++] = IOVEC_MAKE_STRING(ANSI_NORMAL);
379 iovec[n++] = IOVEC_MAKE_STRING("\n");
381 if (writev(console_fd, iovec, n) < 0) {
383 if (errno == EIO && getpid_cached() == 1) {
385 /* If somebody tried to kick us from our
386 * console tty (via vhangup() or suchlike),
387 * try to reconnect */
395 if (writev(console_fd, iovec, n) < 0)
404 static int write_to_syslog(
410 const char *buffer) {
412 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
414 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
415 struct iovec iovec[5] = {};
416 struct msghdr msghdr = {
418 .msg_iovlen = ELEMENTSOF(iovec),
426 xsprintf(header_priority, "<%i>", level);
428 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
433 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
436 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
438 iovec[0] = IOVEC_MAKE_STRING(header_priority);
439 iovec[1] = IOVEC_MAKE_STRING(header_time);
440 iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name);
441 iovec[3] = IOVEC_MAKE_STRING(header_pid);
442 iovec[4] = IOVEC_MAKE_STRING(buffer);
444 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
445 if (syslog_is_stream)
451 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
455 if (!syslog_is_stream ||
456 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
459 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
465 static int write_to_kmsg(
471 const char *buffer) {
473 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
474 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
475 struct iovec iovec[5] = {};
480 xsprintf(header_priority, "<%i>", level);
481 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
483 iovec[0] = IOVEC_MAKE_STRING(header_priority);
484 iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name);
485 iovec[2] = IOVEC_MAKE_STRING(header_pid);
486 iovec[3] = IOVEC_MAKE_STRING(buffer);
487 iovec[4] = IOVEC_MAKE_STRING("\n");
489 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
495 static int log_do_header(
500 const char *file, int line, const char *func,
501 const char *object_field, const char *object,
502 const char *extra_field, const char *extra) {
505 r = snprintf(header, size,
507 "SYSLOG_FACILITY=%i\n"
508 "%s%.256s%s" /* CODE_FILE */
509 "%s%.*i%s" /* CODE_LINE */
510 "%s%.256s%s" /* CODE_FUNC */
511 "%s%.*i%s" /* ERRNO */
512 "%s%.256s%s" /* object */
513 "%s%.256s%s" /* extra */
514 "SYSLOG_IDENTIFIER=%.256s\n",
517 isempty(file) ? "" : "CODE_FILE=",
518 isempty(file) ? "" : file,
519 isempty(file) ? "" : "\n",
520 line ? "CODE_LINE=" : "",
521 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
523 isempty(func) ? "" : "CODE_FUNC=",
524 isempty(func) ? "" : func,
525 isempty(func) ? "" : "\n",
526 error ? "ERRNO=" : "",
527 error ? 1 : 0, error,
529 isempty(object) ? "" : object_field,
530 isempty(object) ? "" : object,
531 isempty(object) ? "" : "\n",
532 isempty(extra) ? "" : extra_field,
533 isempty(extra) ? "" : extra,
534 isempty(extra) ? "" : "\n",
535 program_invocation_short_name);
536 assert_raw((size_t) r < size);
541 #if 0 /// UNNEEDED by elogind
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)
578 int log_dispatch_internal(
584 const char *object_field,
586 const char *extra_field,
595 if (log_target == LOG_TARGET_NULL)
598 /* Patch in LOG_DAEMON facility if necessary */
599 if ((level & LOG_FACMASK) == 0)
600 level = log_facility | LOG_PRI(level);
602 if (open_when_needed)
609 buffer += strspn(buffer, NEWLINE);
614 if ((e = strpbrk(buffer, NEWLINE)))
617 #if 0 /// elogind does not support logging to systemd-journald
618 if (IN_SET(log_target, LOG_TARGET_AUTO,
619 LOG_TARGET_JOURNAL_OR_KMSG,
620 LOG_TARGET_JOURNAL)) {
622 k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
623 if (k < 0 && k != -EAGAIN)
628 if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
629 LOG_TARGET_SYSLOG)) {
631 k = write_to_syslog(level, error, file, line, func, buffer);
632 if (k < 0 && k != -EAGAIN)
637 IN_SET(log_target, LOG_TARGET_AUTO,
638 LOG_TARGET_SYSLOG_OR_KMSG,
639 LOG_TARGET_JOURNAL_OR_KMSG,
645 k = write_to_kmsg(level, error, file, line, func, buffer);
653 (void) write_to_console(level, error, file, line, func, buffer);
658 if (open_when_needed)
664 int log_dump_internal(
672 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
675 /* This modifies the buffer... */
680 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
683 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
686 int log_internalv_realm(
695 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
696 char buffer[LINE_MAX];
702 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
705 /* Make sure that %m maps to the specified error (or "Success"). */
708 (void) vsnprintf(buffer, sizeof buffer, format, ap);
710 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
713 int log_internal_realm(
719 const char *format, ...) {
724 va_start(ap, format);
725 r = log_internalv_realm(level, error, file, line, func, format, ap);
732 static int log_object_internalv(
738 const char *object_field,
740 const char *extra_field,
751 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
754 /* Make sure that %m maps to the specified error (or "Success"). */
757 /* Prepend the object name before the message */
762 buffer = newa(char, n + 2 + LINE_MAX);
763 b = stpcpy(stpcpy(buffer, object), ": ");
765 b = buffer = newa(char, LINE_MAX);
767 (void) vsnprintf(b, LINE_MAX, format, ap);
769 return log_dispatch_internal(level, error, file, line, func,
770 object_field, object, extra_field, extra, buffer);
773 int log_object_internal(
779 const char *object_field,
781 const char *extra_field,
783 const char *format, ...) {
788 va_start(ap, format);
789 r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
795 static void log_assert(
801 const char *format) {
803 static char buffer[LINE_MAX];
804 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
806 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
809 DISABLE_WARNING_FORMAT_NONLITERAL;
810 (void) snprintf(buffer, sizeof buffer, format, text, file, line, func);
813 log_abort_msg = buffer;
815 log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer);
818 _noreturn_ void log_assert_failed_realm(
825 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
826 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
830 _noreturn_ void log_assert_failed_unreachable_realm(
837 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
838 "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
842 void log_assert_failed_return_realm(
849 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func,
850 "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
853 int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) {
854 return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR),
855 ENOMEM, file, line, func, "Out of memory.");
858 int log_format_iovec(
862 bool newline_separator,
867 static const char nl = '\n';
869 while (format && *n + 1 < iovec_len) {
874 /* We need to copy the va_list structure,
875 * since vasprintf() leaves it afterwards at
876 * an undefined location */
881 r = vasprintf(&m, format, aq);
886 /* Now, jump enough ahead, so that we point to
887 * the next format string */
888 VA_FORMAT_ADVANCE(format, ap);
890 iovec[(*n)++] = IOVEC_MAKE_STRING(m);
892 if (newline_separator) {
893 iovec[*n].iov_base = (char*) &nl;
894 iovec[*n].iov_len = 1;
898 format = va_arg(ap, char *);
903 int log_struct_internal(
909 const char *format, ...) {
911 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
920 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
923 if (log_target == LOG_TARGET_NULL)
926 if ((level & LOG_FACMASK) == 0)
927 level = log_facility | LOG_PRI(level);
929 #if 0 /// elogind does not support logging to systemd-journald
930 if (IN_SET(log_target,
932 LOG_TARGET_JOURNAL_OR_KMSG,
933 LOG_TARGET_JOURNAL)) {
935 if (open_when_needed)
938 if (journal_fd >= 0) {
939 char header[LINE_MAX];
940 struct iovec iovec[17] = {};
946 bool fallback = false;
948 /* If the journal is available do structured logging */
949 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
950 iovec[n++] = IOVEC_MAKE_STRING(header);
952 va_start(ap, format);
953 r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap);
958 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
962 for (i = 1; i < n; i += 2)
963 free(iovec[i].iov_base);
966 if (open_when_needed)
975 /* Fallback if journal logging is not available or didn't work. */
977 va_start(ap, format);
984 (void) vsnprintf(buf, sizeof buf, format, aq);
987 if (startswith(buf, "MESSAGE=")) {
992 VA_FORMAT_ADVANCE(format, ap);
994 format = va_arg(ap, char *);
999 if (open_when_needed)
1005 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
1008 int log_struct_iovec_internal(
1014 const struct iovec input_iovec[],
1015 size_t n_input_iovec) {
1017 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
1025 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
1028 if (log_target == LOG_TARGET_NULL)
1031 if ((level & LOG_FACMASK) == 0)
1032 level = log_facility | LOG_PRI(level);
1034 if (IN_SET(log_target, LOG_TARGET_AUTO,
1035 LOG_TARGET_JOURNAL_OR_KMSG,
1036 LOG_TARGET_JOURNAL) &&
1039 struct iovec iovec[1 + n_input_iovec*2];
1040 char header[LINE_MAX];
1041 struct msghdr mh = {
1043 .msg_iovlen = 1 + n_input_iovec*2,
1046 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1047 iovec[0] = IOVEC_MAKE_STRING(header);
1049 for (i = 0; i < n_input_iovec; i++) {
1050 iovec[1+i*2] = input_iovec[i];
1051 iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n");
1054 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0)
1058 for (i = 0; i < n_input_iovec; i++)
1059 if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE="))
1062 if (_unlikely_(i >= n_input_iovec)) /* Couldn't find MESSAGE=? */
1065 m = strndupa(input_iovec[i].iov_base + STRLEN("MESSAGE="),
1066 input_iovec[i].iov_len - STRLEN("MESSAGE="));
1068 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
1071 int log_set_target_from_string(const char *e) {
1074 t = log_target_from_string(e);
1082 int log_set_max_level_from_string_realm(LogRealm realm, const char *e) {
1085 t = log_level_from_string(e);
1089 log_set_max_level_realm(realm, t);
1093 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
1096 * The systemd.log_xyz= settings are parsed by all tools, and
1099 * However, "quiet" is only parsed by PID 1, and only turns of
1100 * status output to /dev/console, but does not alter the log
1104 if (streq(key, "debug") && !value)
1105 log_set_max_level(LOG_DEBUG);
1107 else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
1109 if (proc_cmdline_value_missing(key, value))
1112 if (log_set_target_from_string(value) < 0)
1113 log_warning("Failed to parse log target '%s'. Ignoring.", value);
1115 } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
1117 if (proc_cmdline_value_missing(key, value))
1120 if (log_set_max_level_from_string(value) < 0)
1121 log_warning("Failed to parse log level '%s'. Ignoring.", value);
1123 } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
1125 if (log_show_color_from_string(value ?: "1") < 0)
1126 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
1128 } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
1130 if (log_show_location_from_string(value ?: "1") < 0)
1131 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
1137 void log_parse_environment_realm(LogRealm realm) {
1138 /* Do not call from library code. */
1142 if (get_ctty_devnr(0, NULL) < 0)
1143 /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is
1145 (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1147 e = getenv("SYSTEMD_LOG_TARGET");
1148 if (e && log_set_target_from_string(e) < 0)
1149 log_warning("Failed to parse log target '%s'. Ignoring.", e);
1151 e = getenv("SYSTEMD_LOG_LEVEL");
1152 if (e && log_set_max_level_from_string_realm(realm, e) < 0)
1153 log_warning("Failed to parse log level '%s'. Ignoring.", e);
1155 e = getenv("SYSTEMD_LOG_COLOR");
1156 if (e && log_show_color_from_string(e) < 0)
1157 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1159 e = getenv("SYSTEMD_LOG_LOCATION");
1160 if (e && log_show_location_from_string(e) < 0)
1161 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1164 LogTarget log_get_target(void) {
1168 int log_get_max_level_realm(LogRealm realm) {
1169 return log_max_level[realm];
1172 void log_show_color(bool b) {
1176 bool log_get_show_color(void) {
1180 void log_show_location(bool b) {
1184 bool log_get_show_location(void) {
1185 return show_location;
1188 int log_show_color_from_string(const char *e) {
1191 t = parse_boolean(e);
1199 int log_show_location_from_string(const char *e) {
1202 t = parse_boolean(e);
1206 log_show_location(t);
1210 bool log_on_console(void) {
1211 if (IN_SET(log_target, LOG_TARGET_CONSOLE,
1212 LOG_TARGET_CONSOLE_PREFIXED))
1215 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1218 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1219 [LOG_TARGET_CONSOLE] = "console",
1220 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1221 [LOG_TARGET_KMSG] = "kmsg",
1222 #if 0 /// elogind does not support logging to systemd-journald
1223 [LOG_TARGET_JOURNAL] = "journal",
1224 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1226 [LOG_TARGET_SYSLOG] = "syslog",
1227 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1228 [LOG_TARGET_AUTO] = "auto",
1229 [LOG_TARGET_NULL] = "null",
1232 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1234 #if 0 /// UNNEEDED by elogind
1235 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1238 if (pid_is_valid(si->ssi_pid)) {
1239 _cleanup_free_ char *p = NULL;
1241 (void) get_process_comm(si->ssi_pid, &p);
1244 "Received SIG%s from PID %"PRIu32" (%s).",
1245 signal_to_string(si->ssi_signo),
1246 si->ssi_pid, strna(p));
1250 signal_to_string(si->ssi_signo));
1254 int log_syntax_internal(
1257 const char *config_file,
1258 unsigned config_line,
1263 const char *format, ...) {
1266 char buffer[LINE_MAX];
1268 const char *unit_fmt = NULL;
1273 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
1276 if (log_target == LOG_TARGET_NULL)
1281 va_start(ap, format);
1282 (void) vsnprintf(buffer, sizeof buffer, format, ap);
1286 unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
1288 return log_struct_internal(
1289 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),
1292 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
1293 "CONFIG_FILE=%s", config_file,
1294 "CONFIG_LINE=%u", config_line,
1295 LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
1300 int log_syntax_invalid_utf8_internal(
1303 const char *config_file,
1304 unsigned config_line,
1308 const char *rvalue) {
1310 _cleanup_free_ char *p = NULL;
1313 p = utf8_escape_invalid(rvalue);
1315 log_syntax_internal(unit, level, config_file, config_line, 0, file, line, func,
1316 "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1321 #if 0 /// UNNEEDED by elogind
1322 void log_set_upgrade_syslog_to_journal(bool b) {
1323 upgrade_syslog_to_journal = b;
1325 /* Make the change effective immediately */
1327 if (log_target == LOG_TARGET_SYSLOG)
1328 log_target = LOG_TARGET_JOURNAL;
1329 else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
1330 log_target = LOG_TARGET_JOURNAL_OR_KMSG;
1334 void log_set_always_reopen_console(bool b) {
1335 always_reopen_console = b;
1339 void log_set_open_when_needed(bool b) {
1340 open_when_needed = b;
1343 void log_set_prohibit_ipc(bool b) {
1347 int log_emergency_level(void) {
1348 /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1349 * then the system of the whole system is obviously affected. */
1351 return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
1354 int log_dup_console(void) {
1357 /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
1358 * whenever we want to continue logging through the original fd, but want to rearrange stderr. */
1360 if (console_fd >= 3)
1363 copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);