1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include <sys/signalfd.h>
12 #include <sys/socket.h>
19 #include "sd-messages.h"
21 #include "alloc-util.h"
23 #include "format-util.h"
28 #include "parse-util.h"
29 #include "proc-cmdline.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "socket-util.h"
33 #include "stdio-util.h"
34 #include "string-table.h"
35 #include "string-util.h"
36 #include "syslog-util.h"
37 #include "terminal-util.h"
38 #include "time-util.h"
42 #define SNDBUF_SIZE (8*1024*1024)
44 static LogTarget log_target = LOG_TARGET_CONSOLE;
45 static int log_max_level[] = {LOG_INFO, LOG_INFO};
46 assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX);
47 static int log_facility = LOG_DAEMON;
49 static int console_fd = STDERR_FILENO;
50 static int syslog_fd = -1;
51 static int kmsg_fd = -1;
52 static int journal_fd = -1;
54 static bool syslog_is_stream = false;
56 static bool show_color = false;
57 static bool show_location = false;
59 #if 0 /// UNNEEDED by elogind
60 static bool upgrade_syslog_to_journal = false;
62 static bool always_reopen_console = false;
63 static bool open_when_needed = false;
64 static bool prohibit_ipc = false;
66 /* Akin to glibc's __abort_msg; which is private and we hence cannot
68 static char *log_abort_msg = NULL;
70 /* An assert to use in logging functions that does not call recursively
71 * into our logging functions (since that might lead to a loop). */
72 #define assert_raw(expr) \
74 if (_unlikely_(!(expr))) { \
75 fputs(#expr "\n", stderr); \
80 static void log_close_console(void) {
81 console_fd = safe_close_above_stdio(console_fd);
84 static int log_open_console(void) {
86 if (!always_reopen_console) {
87 console_fd = STDERR_FILENO;
92 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
96 console_fd = fd_move_above_stdio(console_fd);
102 static void log_close_kmsg(void) {
103 kmsg_fd = safe_close(kmsg_fd);
106 static int log_open_kmsg(void) {
111 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
115 kmsg_fd = fd_move_above_stdio(kmsg_fd);
119 static void log_close_syslog(void) {
120 syslog_fd = safe_close(syslog_fd);
123 static int create_log_socket(int type) {
127 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
131 fd = fd_move_above_stdio(fd);
132 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
134 /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
135 * in the unlikely case of a deadlock. */
136 if (getpid_cached() == 1)
137 timeval_store(&tv, 10 * USEC_PER_MSEC);
139 timeval_store(&tv, 10 * USEC_PER_SEC);
140 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
145 static int log_open_syslog(void) {
147 static const union sockaddr_union sa = {
148 .un.sun_family = AF_UNIX,
149 .un.sun_path = "/dev/log",
157 syslog_fd = create_log_socket(SOCK_DGRAM);
163 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
164 safe_close(syslog_fd);
166 /* Some legacy syslog systems still use stream
167 * sockets. They really shouldn't. But what can we
169 syslog_fd = create_log_socket(SOCK_STREAM);
175 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
180 syslog_is_stream = true;
182 syslog_is_stream = false;
191 static void log_close_journal(void) {
192 #if 0 /// elogind does not support journald
193 journal_fd = safe_close(journal_fd);
197 #if 0 /// UNNEEDED by elogind
198 static int log_open_journal(void) {
200 static const union sockaddr_union sa = {
201 .un.sun_family = AF_UNIX,
202 .un.sun_path = "/run/systemd/journal/socket",
210 journal_fd = create_log_socket(SOCK_DGRAM);
211 if (journal_fd < 0) {
216 if (connect(journal_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
232 /* Do not call from library code. */
234 /* If we don't use the console we close it here, to not get
235 * killed by SAK. If we don't use syslog we close it here so
236 * that we are not confused by somebody deleting the socket in
237 * the fs, and to make sure we don't use it if prohibit_ipc is
238 * set. If we don't use /dev/kmsg we still keep it open,
239 * because there is no reason to close it. */
241 if (log_target == LOG_TARGET_NULL) {
248 if (log_target != LOG_TARGET_AUTO ||
249 getpid_cached() == 1 ||
250 isatty(STDERR_FILENO) <= 0) {
252 #if 0 /// elogind does not support logging to systemd-journald
254 IN_SET(log_target, LOG_TARGET_AUTO,
255 LOG_TARGET_JOURNAL_OR_KMSG,
256 LOG_TARGET_JOURNAL)) {
257 r = log_open_journal();
267 IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
268 LOG_TARGET_SYSLOG)) {
269 r = log_open_syslog();
277 if (IN_SET(log_target, LOG_TARGET_AUTO,
278 LOG_TARGET_JOURNAL_OR_KMSG,
279 LOG_TARGET_SYSLOG_OR_KMSG,
294 return log_open_console();
297 void log_set_target(LogTarget target) {
299 assert(target < _LOG_TARGET_MAX);
301 #if 0 /// elogind does not support logging to systemd-journald
302 if (upgrade_syslog_to_journal) {
303 if (target == LOG_TARGET_SYSLOG)
304 target = LOG_TARGET_JOURNAL;
305 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
306 target = LOG_TARGET_JOURNAL_OR_KMSG;
313 void log_close(void) {
314 /* Do not call from library code. */
322 #if 0 /// UNNEEDED by elogind
323 void log_forget_fds(void) {
324 /* Do not call from library code. */
326 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
330 void log_set_max_level_realm(LogRealm realm, int level) {
331 assert((level & LOG_PRIMASK) == level);
332 assert(realm < ELEMENTSOF(log_max_level));
334 log_max_level[realm] = level;
337 void log_set_facility(int facility) {
338 log_facility = facility;
341 static int write_to_console(
347 const char *buffer) {
349 char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
350 struct iovec iovec[6] = {};
357 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
358 xsprintf(prefix, "<%i>", level);
359 iovec[n++] = IOVEC_MAKE_STRING(prefix);
362 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
365 (void) snprintf(location, sizeof location, "(%s:%i) ", file, line);
366 iovec[n++] = IOVEC_MAKE_STRING(location);
370 iovec[n++] = IOVEC_MAKE_STRING(ANSI_HIGHLIGHT_RED);
371 iovec[n++] = IOVEC_MAKE_STRING(buffer);
373 iovec[n++] = IOVEC_MAKE_STRING(ANSI_NORMAL);
374 iovec[n++] = IOVEC_MAKE_STRING("\n");
376 if (writev(console_fd, iovec, n) < 0) {
378 if (errno == EIO && getpid_cached() == 1) {
380 /* If somebody tried to kick us from our
381 * console tty (via vhangup() or suchlike),
382 * try to reconnect */
390 if (writev(console_fd, iovec, n) < 0)
399 static int write_to_syslog(
405 const char *buffer) {
407 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
409 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
410 struct iovec iovec[5] = {};
411 struct msghdr msghdr = {
413 .msg_iovlen = ELEMENTSOF(iovec),
421 xsprintf(header_priority, "<%i>", level);
423 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
428 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
431 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
433 iovec[0] = IOVEC_MAKE_STRING(header_priority);
434 iovec[1] = IOVEC_MAKE_STRING(header_time);
435 iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name);
436 iovec[3] = IOVEC_MAKE_STRING(header_pid);
437 iovec[4] = IOVEC_MAKE_STRING(buffer);
439 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
440 if (syslog_is_stream)
446 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
450 if (!syslog_is_stream ||
451 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
454 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
460 static int write_to_kmsg(
466 const char *buffer) {
468 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
469 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
470 struct iovec iovec[5] = {};
475 xsprintf(header_priority, "<%i>", level);
476 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
478 iovec[0] = IOVEC_MAKE_STRING(header_priority);
479 iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name);
480 iovec[2] = IOVEC_MAKE_STRING(header_pid);
481 iovec[3] = IOVEC_MAKE_STRING(buffer);
482 iovec[4] = IOVEC_MAKE_STRING("\n");
484 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
490 static int log_do_header(
495 const char *file, int line, const char *func,
496 const char *object_field, const char *object,
497 const char *extra_field, const char *extra) {
500 r = snprintf(header, size,
502 "SYSLOG_FACILITY=%i\n"
503 "%s%.256s%s" /* CODE_FILE */
504 "%s%.*i%s" /* CODE_LINE */
505 "%s%.256s%s" /* CODE_FUNC */
506 "%s%.*i%s" /* ERRNO */
507 "%s%.256s%s" /* object */
508 "%s%.256s%s" /* extra */
509 "SYSLOG_IDENTIFIER=%.256s\n",
512 isempty(file) ? "" : "CODE_FILE=",
513 isempty(file) ? "" : file,
514 isempty(file) ? "" : "\n",
515 line ? "CODE_LINE=" : "",
516 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
518 isempty(func) ? "" : "CODE_FUNC=",
519 isempty(func) ? "" : func,
520 isempty(func) ? "" : "\n",
521 error ? "ERRNO=" : "",
522 error ? 1 : 0, error,
524 isempty(object) ? "" : object_field,
525 isempty(object) ? "" : object,
526 isempty(object) ? "" : "\n",
527 isempty(extra) ? "" : extra_field,
528 isempty(extra) ? "" : extra,
529 isempty(extra) ? "" : "\n",
530 program_invocation_short_name);
531 assert_raw((size_t) r < size);
536 #if 0 /// UNNEEDED by elogind
537 static int write_to_journal(
543 const char *object_field,
545 const char *extra_field,
547 const char *buffer) {
549 char header[LINE_MAX];
550 struct iovec iovec[4] = {};
551 struct msghdr mh = {};
556 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
558 iovec[0] = IOVEC_MAKE_STRING(header);
559 iovec[1] = IOVEC_MAKE_STRING("MESSAGE=");
560 iovec[2] = IOVEC_MAKE_STRING(buffer);
561 iovec[3] = IOVEC_MAKE_STRING("\n");
564 mh.msg_iovlen = ELEMENTSOF(iovec);
566 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
573 int log_dispatch_internal(
579 const char *object_field,
581 const char *extra_field,
590 if (log_target == LOG_TARGET_NULL)
593 /* Patch in LOG_DAEMON facility if necessary */
594 if ((level & LOG_FACMASK) == 0)
595 level = log_facility | LOG_PRI(level);
597 if (open_when_needed)
604 buffer += strspn(buffer, NEWLINE);
609 if ((e = strpbrk(buffer, NEWLINE)))
612 #if 0 /// elogind does not support logging to systemd-journald
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)
623 if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
624 LOG_TARGET_SYSLOG)) {
626 k = write_to_syslog(level, error, file, line, func, buffer);
627 if (k < 0 && k != -EAGAIN)
632 IN_SET(log_target, LOG_TARGET_AUTO,
633 LOG_TARGET_SYSLOG_OR_KMSG,
634 LOG_TARGET_JOURNAL_OR_KMSG,
640 k = write_to_kmsg(level, error, file, line, func, buffer);
648 (void) write_to_console(level, error, file, line, func, buffer);
653 if (open_when_needed)
659 int log_dump_internal(
667 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
670 /* This modifies the buffer... */
675 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
678 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
681 int log_internalv_realm(
690 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
691 char buffer[LINE_MAX];
697 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
700 /* Make sure that %m maps to the specified error (or "Success"). */
703 (void) vsnprintf(buffer, sizeof buffer, format, ap);
705 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
708 int log_internal_realm(
714 const char *format, ...) {
719 va_start(ap, format);
720 r = log_internalv_realm(level, error, file, line, func, format, ap);
727 static int log_object_internalv(
733 const char *object_field,
735 const char *extra_field,
746 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
749 /* Make sure that %m maps to the specified error (or "Success"). */
752 /* Prepend the object name before the message */
757 buffer = newa(char, n + 2 + LINE_MAX);
758 b = stpcpy(stpcpy(buffer, object), ": ");
760 b = buffer = newa(char, LINE_MAX);
762 (void) vsnprintf(b, LINE_MAX, format, ap);
764 return log_dispatch_internal(level, error, file, line, func,
765 object_field, object, extra_field, extra, buffer);
768 int log_object_internal(
774 const char *object_field,
776 const char *extra_field,
778 const char *format, ...) {
783 va_start(ap, format);
784 r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
790 static void log_assert(
796 const char *format) {
798 static char buffer[LINE_MAX];
799 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
801 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
804 DISABLE_WARNING_FORMAT_NONLITERAL;
805 (void) snprintf(buffer, sizeof buffer, format, text, file, line, func);
808 log_abort_msg = buffer;
810 log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer);
813 _noreturn_ void log_assert_failed_realm(
820 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
821 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
825 _noreturn_ void log_assert_failed_unreachable_realm(
832 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
833 "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
837 void log_assert_failed_return_realm(
844 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func,
845 "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
848 int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) {
849 return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR),
850 ENOMEM, file, line, func, "Out of memory.");
853 int log_format_iovec(
857 bool newline_separator,
862 static const char nl = '\n';
864 while (format && *n + 1 < iovec_len) {
869 /* We need to copy the va_list structure,
870 * since vasprintf() leaves it afterwards at
871 * an undefined location */
876 r = vasprintf(&m, format, aq);
881 /* Now, jump enough ahead, so that we point to
882 * the next format string */
883 VA_FORMAT_ADVANCE(format, ap);
885 iovec[(*n)++] = IOVEC_MAKE_STRING(m);
887 if (newline_separator) {
888 iovec[*n].iov_base = (char*) &nl;
889 iovec[*n].iov_len = 1;
893 format = va_arg(ap, char *);
898 int log_struct_internal(
904 const char *format, ...) {
906 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
915 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
918 if (log_target == LOG_TARGET_NULL)
921 if ((level & LOG_FACMASK) == 0)
922 level = log_facility | LOG_PRI(level);
924 #if 0 /// elogind does not support logging to systemd-journald
925 if (IN_SET(log_target,
927 LOG_TARGET_JOURNAL_OR_KMSG,
928 LOG_TARGET_JOURNAL)) {
930 if (open_when_needed)
933 if (journal_fd >= 0) {
934 char header[LINE_MAX];
935 struct iovec iovec[17] = {};
941 bool fallback = false;
943 /* If the journal is available do structured logging */
944 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
945 iovec[n++] = IOVEC_MAKE_STRING(header);
947 va_start(ap, format);
948 r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap);
953 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
957 for (i = 1; i < n; i += 2)
958 free(iovec[i].iov_base);
961 if (open_when_needed)
970 /* Fallback if journal logging is not available or didn't work. */
972 va_start(ap, format);
979 (void) vsnprintf(buf, sizeof buf, format, aq);
982 if (startswith(buf, "MESSAGE=")) {
987 VA_FORMAT_ADVANCE(format, ap);
989 format = va_arg(ap, char *);
994 if (open_when_needed)
1000 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
1003 int log_struct_iovec_internal(
1009 const struct iovec input_iovec[],
1010 size_t n_input_iovec) {
1012 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
1020 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
1023 if (log_target == LOG_TARGET_NULL)
1026 if ((level & LOG_FACMASK) == 0)
1027 level = log_facility | LOG_PRI(level);
1029 if (IN_SET(log_target, LOG_TARGET_AUTO,
1030 LOG_TARGET_JOURNAL_OR_KMSG,
1031 LOG_TARGET_JOURNAL) &&
1034 struct iovec iovec[1 + n_input_iovec*2];
1035 char header[LINE_MAX];
1036 struct msghdr mh = {
1038 .msg_iovlen = 1 + n_input_iovec*2,
1041 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1042 iovec[0] = IOVEC_MAKE_STRING(header);
1044 for (i = 0; i < n_input_iovec; i++) {
1045 iovec[1+i*2] = input_iovec[i];
1046 iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n");
1049 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0)
1053 for (i = 0; i < n_input_iovec; i++)
1054 if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE="))
1057 if (_unlikely_(i >= n_input_iovec)) /* Couldn't find MESSAGE=? */
1060 m = strndupa(input_iovec[i].iov_base + STRLEN("MESSAGE="),
1061 input_iovec[i].iov_len - STRLEN("MESSAGE="));
1063 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
1066 int log_set_target_from_string(const char *e) {
1069 t = log_target_from_string(e);
1077 int log_set_max_level_from_string_realm(LogRealm realm, const char *e) {
1080 t = log_level_from_string(e);
1084 log_set_max_level_realm(realm, t);
1088 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
1091 * The systemd.log_xyz= settings are parsed by all tools, and
1094 * However, "quiet" is only parsed by PID 1, and only turns of
1095 * status output to /dev/console, but does not alter the log
1099 if (streq(key, "debug") && !value)
1100 log_set_max_level(LOG_DEBUG);
1102 else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
1104 if (proc_cmdline_value_missing(key, value))
1107 if (log_set_target_from_string(value) < 0)
1108 log_warning("Failed to parse log target '%s'. Ignoring.", value);
1110 } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
1112 if (proc_cmdline_value_missing(key, value))
1115 if (log_set_max_level_from_string(value) < 0)
1116 log_warning("Failed to parse log level '%s'. Ignoring.", value);
1118 } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
1120 if (log_show_color_from_string(value ?: "1") < 0)
1121 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
1123 } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
1125 if (log_show_location_from_string(value ?: "1") < 0)
1126 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
1132 void log_parse_environment_realm(LogRealm realm) {
1133 /* Do not call from library code. */
1137 if (get_ctty_devnr(0, NULL) < 0)
1138 /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is
1140 (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1142 e = getenv("SYSTEMD_LOG_TARGET");
1143 if (e && log_set_target_from_string(e) < 0)
1144 log_warning("Failed to parse log target '%s'. Ignoring.", e);
1146 e = getenv("SYSTEMD_LOG_LEVEL");
1147 if (e && log_set_max_level_from_string_realm(realm, e) < 0)
1148 log_warning("Failed to parse log level '%s'. Ignoring.", e);
1150 e = getenv("SYSTEMD_LOG_COLOR");
1151 if (e && log_show_color_from_string(e) < 0)
1152 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1154 e = getenv("SYSTEMD_LOG_LOCATION");
1155 if (e && log_show_location_from_string(e) < 0)
1156 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1159 LogTarget log_get_target(void) {
1163 int log_get_max_level_realm(LogRealm realm) {
1164 return log_max_level[realm];
1167 void log_show_color(bool b) {
1171 bool log_get_show_color(void) {
1175 void log_show_location(bool b) {
1179 bool log_get_show_location(void) {
1180 return show_location;
1183 int log_show_color_from_string(const char *e) {
1186 t = parse_boolean(e);
1194 int log_show_location_from_string(const char *e) {
1197 t = parse_boolean(e);
1201 log_show_location(t);
1205 bool log_on_console(void) {
1206 if (IN_SET(log_target, LOG_TARGET_CONSOLE,
1207 LOG_TARGET_CONSOLE_PREFIXED))
1210 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1213 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1214 [LOG_TARGET_CONSOLE] = "console",
1215 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1216 [LOG_TARGET_KMSG] = "kmsg",
1217 #if 0 /// elogind does not support logging to systemd-journald
1218 [LOG_TARGET_JOURNAL] = "journal",
1219 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1221 [LOG_TARGET_SYSLOG] = "syslog",
1222 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1223 [LOG_TARGET_AUTO] = "auto",
1224 [LOG_TARGET_NULL] = "null",
1227 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1229 #if 0 /// UNNEEDED by elogind
1230 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1233 if (pid_is_valid(si->ssi_pid)) {
1234 _cleanup_free_ char *p = NULL;
1236 (void) get_process_comm(si->ssi_pid, &p);
1239 "Received SIG%s from PID %"PRIu32" (%s).",
1240 signal_to_string(si->ssi_signo),
1241 si->ssi_pid, strna(p));
1245 signal_to_string(si->ssi_signo));
1249 int log_syntax_internal(
1252 const char *config_file,
1253 unsigned config_line,
1258 const char *format, ...) {
1261 char buffer[LINE_MAX];
1263 const char *unit_fmt = NULL;
1268 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
1271 if (log_target == LOG_TARGET_NULL)
1276 va_start(ap, format);
1277 (void) vsnprintf(buffer, sizeof buffer, format, ap);
1281 unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
1283 return log_struct_internal(
1284 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),
1287 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
1288 "CONFIG_FILE=%s", config_file,
1289 "CONFIG_LINE=%u", config_line,
1290 LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
1295 int log_syntax_invalid_utf8_internal(
1298 const char *config_file,
1299 unsigned config_line,
1303 const char *rvalue) {
1305 _cleanup_free_ char *p = NULL;
1308 p = utf8_escape_invalid(rvalue);
1310 log_syntax_internal(unit, level, config_file, config_line, 0, file, line, func,
1311 "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1316 #if 0 /// UNNEEDED by elogind
1317 void log_set_upgrade_syslog_to_journal(bool b) {
1318 upgrade_syslog_to_journal = b;
1320 /* Make the change effective immediately */
1322 if (log_target == LOG_TARGET_SYSLOG)
1323 log_target = LOG_TARGET_JOURNAL;
1324 else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
1325 log_target = LOG_TARGET_JOURNAL_OR_KMSG;
1329 void log_set_always_reopen_console(bool b) {
1330 always_reopen_console = b;
1334 void log_set_open_when_needed(bool b) {
1335 open_when_needed = b;
1338 void log_set_prohibit_ipc(bool b) {
1342 int log_emergency_level(void) {
1343 /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1344 * then the system of the whole system is obviously affected. */
1346 return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
1349 int log_dup_console(void) {
1352 /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
1353 * whenever we want to continue logging through the original fd, but want to rearrange stderr. */
1355 if (console_fd >= 3)
1358 copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);