1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/socket.h>
36 #include "socket-util.h"
38 #define SNDBUF_SIZE (8*1024*1024)
40 static LogTarget log_target = LOG_TARGET_CONSOLE;
41 static int log_max_level = LOG_INFO;
42 static int log_facility = LOG_DAEMON;
44 static int console_fd = STDERR_FILENO;
45 static int syslog_fd = -1;
46 static int kmsg_fd = -1;
47 static int journal_fd = -1;
49 static bool syslog_is_stream = false;
51 static bool show_color = false;
52 static bool show_location = false;
54 static bool upgrade_syslog_to_journal = false;
56 /* Akin to glibc's __abort_msg; which is private and we hence cannot
58 static char *log_abort_msg = NULL;
60 void log_close_console(void) {
67 safe_close(console_fd);
73 static int log_open_console(void) {
79 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
83 console_fd = STDERR_FILENO;
88 void log_close_kmsg(void) {
89 kmsg_fd = safe_close(kmsg_fd);
92 static int log_open_kmsg(void) {
97 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
104 void log_close_syslog(void) {
105 syslog_fd = safe_close(syslog_fd);
108 static int create_log_socket(int type) {
112 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
116 fd_inc_sndbuf(fd, SNDBUF_SIZE);
118 /* We need a blocking fd here since we'd otherwise lose
119 messages way too early. However, let's not hang forever in the
120 unlikely case of a deadlock. */
122 timeval_store(&tv, 10 * USEC_PER_MSEC);
124 timeval_store(&tv, 10 * USEC_PER_SEC);
125 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
130 static int log_open_syslog(void) {
132 static const union sockaddr_union sa = {
133 .un.sun_family = AF_UNIX,
134 .un.sun_path = "/dev/log",
142 syslog_fd = create_log_socket(SOCK_DGRAM);
148 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
149 safe_close(syslog_fd);
151 /* Some legacy syslog systems still use stream
152 * sockets. They really shouldn't. But what can we
154 syslog_fd = create_log_socket(SOCK_STREAM);
160 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
165 syslog_is_stream = true;
167 syslog_is_stream = false;
176 void log_close_journal(void) {
177 journal_fd = safe_close(journal_fd);
180 static int log_open_journal(void) {
182 static const union sockaddr_union sa = {
183 .un.sun_family = AF_UNIX,
184 .un.sun_path = "/run/systemd/journal/socket",
192 journal_fd = create_log_socket(SOCK_DGRAM);
193 if (journal_fd < 0) {
198 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
213 /* If we don't use the console we close it here, to not get
214 * killed by SAK. If we don't use syslog we close it here so
215 * that we are not confused by somebody deleting the socket in
216 * the fs. If we don't use /dev/kmsg we still keep it open,
217 * because there is no reason to close it. */
219 if (log_target == LOG_TARGET_NULL) {
226 if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
228 isatty(STDERR_FILENO) <= 0) {
230 if (log_target == LOG_TARGET_AUTO ||
231 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
232 log_target == LOG_TARGET_JOURNAL) {
233 r = log_open_journal();
241 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
242 log_target == LOG_TARGET_SYSLOG) {
243 r = log_open_syslog();
251 if (log_target == LOG_TARGET_AUTO ||
252 log_target == LOG_TARGET_SAFE ||
253 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
254 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
255 log_target == LOG_TARGET_KMSG) {
269 return log_open_console();
272 void log_set_target(LogTarget target) {
274 assert(target < _LOG_TARGET_MAX);
276 if (upgrade_syslog_to_journal) {
277 if (target == LOG_TARGET_SYSLOG)
278 target = LOG_TARGET_JOURNAL;
279 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
280 target = LOG_TARGET_JOURNAL_OR_KMSG;
286 void log_close(void) {
293 void log_forget_fds(void) {
294 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
297 void log_set_max_level(int level) {
298 assert((level & LOG_PRIMASK) == level);
300 log_max_level = level;
303 void log_set_facility(int facility) {
304 log_facility = facility;
307 static int write_to_console(
313 const char *object_field,
315 const char *buffer) {
317 char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2];
318 struct iovec iovec[6] = {};
325 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
326 sprintf(prefix, "<%i>", level);
327 IOVEC_SET_STRING(iovec[n++], prefix);
330 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
333 snprintf(location, sizeof(location), "(%s:%i) ", file, line);
334 char_array_0(location);
335 IOVEC_SET_STRING(iovec[n++], location);
339 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
340 IOVEC_SET_STRING(iovec[n++], buffer);
342 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
343 IOVEC_SET_STRING(iovec[n++], "\n");
345 if (writev(console_fd, iovec, n) < 0) {
347 if (errno == EIO && getpid() == 1) {
349 /* If somebody tried to kick us from our
350 * console tty (via vhangup() or suchlike),
351 * try to reconnect */
359 if (writev(console_fd, iovec, n) < 0)
368 static int write_to_syslog(
374 const char *object_field,
376 const char *buffer) {
378 char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_time[64], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
379 struct iovec iovec[5] = {};
380 struct msghdr msghdr = {
382 .msg_iovlen = ELEMENTSOF(iovec),
390 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
391 char_array_0(header_priority);
393 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
398 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
401 snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
402 char_array_0(header_pid);
404 IOVEC_SET_STRING(iovec[0], header_priority);
405 IOVEC_SET_STRING(iovec[1], header_time);
406 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
407 IOVEC_SET_STRING(iovec[3], header_pid);
408 IOVEC_SET_STRING(iovec[4], buffer);
410 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
411 if (syslog_is_stream)
417 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
421 if (!syslog_is_stream ||
422 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
425 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
431 static int write_to_kmsg(
437 const char *object_field,
439 const char *buffer) {
441 char header_priority[1 + DECIMAL_STR_MAX(int) + 2], header_pid[1 + DECIMAL_STR_MAX(pid_t) + 4];
442 struct iovec iovec[5] = {};
447 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
448 char_array_0(header_priority);
450 snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
451 char_array_0(header_pid);
453 IOVEC_SET_STRING(iovec[0], header_priority);
454 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
455 IOVEC_SET_STRING(iovec[2], header_pid);
456 IOVEC_SET_STRING(iovec[3], buffer);
457 IOVEC_SET_STRING(iovec[4], "\n");
459 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
465 static int log_do_header(
470 const char *file, int line, const char *func,
471 const char *object_field, const char *object) {
473 snprintf(header, size,
475 "SYSLOG_FACILITY=%i\n"
481 "SYSLOG_IDENTIFIER=%s\n",
484 isempty(file) ? "" : "CODE_FILE=",
485 isempty(file) ? "" : file,
486 isempty(file) ? "" : "\n",
487 line ? "CODE_LINE=" : "",
488 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
490 isempty(func) ? "" : "CODE_FUNCTION=",
491 isempty(func) ? "" : func,
492 isempty(func) ? "" : "\n",
493 error ? "ERRNO=" : "",
494 error ? 1 : 0, error,
496 isempty(object) ? "" : object_field,
497 isempty(object) ? "" : object,
498 isempty(object) ? "" : "\n",
499 program_invocation_short_name);
500 header[size - 1] = '\0';
505 static int write_to_journal(
511 const char *object_field,
513 const char *buffer) {
515 char header[LINE_MAX];
516 struct iovec iovec[4] = {};
517 struct msghdr mh = {};
522 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object);
524 IOVEC_SET_STRING(iovec[0], header);
525 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
526 IOVEC_SET_STRING(iovec[2], buffer);
527 IOVEC_SET_STRING(iovec[3], "\n");
530 mh.msg_iovlen = ELEMENTSOF(iovec);
532 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
538 static int log_dispatch(
544 const char *object_field,
550 if (log_target == LOG_TARGET_NULL)
553 /* Patch in LOG_DAEMON facility if necessary */
554 if ((level & LOG_FACMASK) == 0)
555 level = log_facility | LOG_PRI(level);
564 buffer += strspn(buffer, NEWLINE);
569 if ((e = strpbrk(buffer, NEWLINE)))
572 if (log_target == LOG_TARGET_AUTO ||
573 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
574 log_target == LOG_TARGET_JOURNAL) {
576 k = write_to_journal(level, error, file, line, func, object_field, object, buffer);
584 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
585 log_target == LOG_TARGET_SYSLOG) {
587 k = write_to_syslog(level, error, file, line, func, object_field, object, buffer);
596 (log_target == LOG_TARGET_AUTO ||
597 log_target == LOG_TARGET_SAFE ||
598 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
599 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
600 log_target == LOG_TARGET_KMSG)) {
602 k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
610 (void) write_to_console(level, error, file, line, func, object_field, object, buffer);
618 int log_dump_internal(
628 /* This modifies the buffer... */
633 if (_likely_(LOG_PRI(level) > log_max_level))
636 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
649 char buffer[LINE_MAX];
654 if (_likely_(LOG_PRI(level) > log_max_level))
657 /* Make sure that %m maps to the specified error */
661 vsnprintf(buffer, sizeof(buffer), format, ap);
662 char_array_0(buffer);
664 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
673 const char *format, ...) {
678 va_start(ap, format);
679 r = log_internalv(level, error, file, line, func, format, ap);
685 int log_object_internalv(
691 const char *object_field,
697 char buffer[LINE_MAX];
702 if (_likely_(LOG_PRI(level) > log_max_level))
705 /* Make sure that %m maps to the specified error */
709 vsnprintf(buffer, sizeof(buffer), format, ap);
710 char_array_0(buffer);
712 return log_dispatch(level, error, file, line, func, object_field, object, buffer);
715 int log_object_internal(
721 const char *object_field,
723 const char *format, ...) {
728 va_start(ap, format);
729 r = log_object_internalv(level, error, file, line, func, object_field, object, format, ap);
735 static void log_assert(
741 const char *format) {
743 static char buffer[LINE_MAX];
745 if (_likely_(LOG_PRI(level) > log_max_level))
748 DISABLE_WARNING_FORMAT_NONLITERAL;
749 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
752 char_array_0(buffer);
753 log_abort_msg = buffer;
755 log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
758 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
759 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
763 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
764 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
768 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
770 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
773 int log_oom_internal(const char *file, int line, const char *func) {
774 log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory.");
778 int log_struct_internal(
784 const char *format, ...) {
794 if (_likely_(LOG_PRI(level) > log_max_level))
797 if (log_target == LOG_TARGET_NULL)
800 if ((level & LOG_FACMASK) == 0)
801 level = log_facility | LOG_PRI(level);
803 if ((log_target == LOG_TARGET_AUTO ||
804 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
805 log_target == LOG_TARGET_JOURNAL) &&
807 char header[LINE_MAX];
808 struct iovec iovec[17] = {};
813 static const char nl = '\n';
814 bool fallback = false;
816 /* If the journal is available do structured logging */
817 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
818 IOVEC_SET_STRING(iovec[n++], header);
820 va_start(ap, format);
821 while (format && n + 1 < ELEMENTSOF(iovec)) {
825 /* We need to copy the va_list structure,
826 * since vasprintf() leaves it afterwards at
827 * an undefined location */
833 if (vasprintf(&m, format, aq) < 0) {
840 /* Now, jump enough ahead, so that we point to
841 * the next format string */
842 VA_FORMAT_ADVANCE(format, ap);
844 IOVEC_SET_STRING(iovec[n++], m);
846 iovec[n].iov_base = (char*) &nl;
847 iovec[n].iov_len = 1;
850 format = va_arg(ap, char *);
855 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
859 for (i = 1; i < n; i += 2)
860 free(iovec[i].iov_base);
866 /* Fallback if journal logging is not available or didn't work. */
868 va_start(ap, format);
876 vsnprintf(buf, sizeof(buf), format, aq);
880 if (startswith(buf, "MESSAGE=")) {
885 VA_FORMAT_ADVANCE(format, ap);
887 format = va_arg(ap, char *);
894 return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
897 int log_set_target_from_string(const char *e) {
900 t = log_target_from_string(e);
908 int log_set_max_level_from_string(const char *e) {
911 t = log_level_from_string(e);
915 log_set_max_level(t);
919 static int parse_proc_cmdline_item(const char *key, const char *value) {
922 * The systemd.log_xyz= settings are parsed by all tools, and
925 * However, "quiet" is only parsed by PID 1!
928 if (streq(key, "debug") && !value)
929 log_set_max_level(LOG_DEBUG);
931 else if (streq(key, "systemd.log_target") && value) {
933 if (log_set_target_from_string(value) < 0)
934 log_warning("Failed to parse log target '%s'. Ignoring.", value);
936 } else if (streq(key, "systemd.log_level") && value) {
938 if (log_set_max_level_from_string(value) < 0)
939 log_warning("Failed to parse log level '%s'. Ignoring.", value);
941 } else if (streq(key, "systemd.log_color") && value) {
943 if (log_show_color_from_string(value) < 0)
944 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
946 } else if (streq(key, "systemd.log_location") && value) {
948 if (log_show_location_from_string(value) < 0)
949 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
955 void log_parse_environment(void) {
958 (void) parse_proc_cmdline(parse_proc_cmdline_item);
960 e = secure_getenv("SYSTEMD_LOG_TARGET");
961 if (e && log_set_target_from_string(e) < 0)
962 log_warning("Failed to parse log target '%s'. Ignoring.", e);
964 e = secure_getenv("SYSTEMD_LOG_LEVEL");
965 if (e && log_set_max_level_from_string(e) < 0)
966 log_warning("Failed to parse log level '%s'. Ignoring.", e);
968 e = secure_getenv("SYSTEMD_LOG_COLOR");
969 if (e && log_show_color_from_string(e) < 0)
970 log_warning("Failed to parse bool '%s'. Ignoring.", e);
972 e = secure_getenv("SYSTEMD_LOG_LOCATION");
973 if (e && log_show_location_from_string(e) < 0)
974 log_warning("Failed to parse bool '%s'. Ignoring.", e);
977 LogTarget log_get_target(void) {
981 int log_get_max_level(void) {
982 return log_max_level;
985 void log_show_color(bool b) {
989 bool log_get_show_color(void) {
993 void log_show_location(bool b) {
997 bool log_get_show_location(void) {
998 return show_location;
1001 int log_show_color_from_string(const char *e) {
1004 t = parse_boolean(e);
1012 int log_show_location_from_string(const char *e) {
1015 t = parse_boolean(e);
1019 log_show_location(t);
1023 bool log_on_console(void) {
1024 if (log_target == LOG_TARGET_CONSOLE ||
1025 log_target == LOG_TARGET_CONSOLE_PREFIXED)
1028 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1031 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1032 [LOG_TARGET_CONSOLE] = "console",
1033 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1034 [LOG_TARGET_KMSG] = "kmsg",
1035 [LOG_TARGET_JOURNAL] = "journal",
1036 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1037 [LOG_TARGET_SYSLOG] = "syslog",
1038 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1039 [LOG_TARGET_AUTO] = "auto",
1040 [LOG_TARGET_SAFE] = "safe",
1041 [LOG_TARGET_NULL] = "null"
1044 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1046 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1047 if (si->ssi_pid > 0) {
1048 _cleanup_free_ char *p = NULL;
1050 get_process_comm(si->ssi_pid, &p);
1053 "Received SIG%s from PID %"PRIu32" (%s).",
1054 signal_to_string(si->ssi_signo),
1055 si->ssi_pid, strna(p));
1059 signal_to_string(si->ssi_signo));
1063 void log_set_upgrade_syslog_to_journal(bool b) {
1064 upgrade_syslog_to_journal = b;