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"
37 #include "formats-util.h"
38 #include "process-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;
44 static int log_facility = LOG_DAEMON;
46 static int console_fd = STDERR_FILENO;
47 static int syslog_fd = -1;
48 static int kmsg_fd = -1;
49 static int journal_fd = -1;
51 static bool syslog_is_stream = false;
53 static bool show_color = false;
54 static bool show_location = false;
56 static bool upgrade_syslog_to_journal = false;
58 /* Akin to glibc's __abort_msg; which is private and we hence cannot
60 static char *log_abort_msg = NULL;
62 void log_close_console(void) {
69 safe_close(console_fd);
75 static int log_open_console(void) {
81 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
85 console_fd = STDERR_FILENO;
90 void log_close_kmsg(void) {
91 kmsg_fd = safe_close(kmsg_fd);
94 static int log_open_kmsg(void) {
99 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
106 void log_close_syslog(void) {
107 syslog_fd = safe_close(syslog_fd);
110 static int create_log_socket(int type) {
114 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
118 fd_inc_sndbuf(fd, SNDBUF_SIZE);
120 /* We need a blocking fd here since we'd otherwise lose
121 messages way too early. However, let's not hang forever in the
122 unlikely case of a deadlock. */
124 timeval_store(&tv, 10 * USEC_PER_MSEC);
126 timeval_store(&tv, 10 * USEC_PER_SEC);
127 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
132 static int log_open_syslog(void) {
134 static const union sockaddr_union sa = {
135 .un.sun_family = AF_UNIX,
136 .un.sun_path = "/dev/log",
144 syslog_fd = create_log_socket(SOCK_DGRAM);
150 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
151 safe_close(syslog_fd);
153 /* Some legacy syslog systems still use stream
154 * sockets. They really shouldn't. But what can we
156 syslog_fd = create_log_socket(SOCK_STREAM);
162 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
167 syslog_is_stream = true;
169 syslog_is_stream = false;
178 void log_close_journal(void) {
179 journal_fd = safe_close(journal_fd);
182 static int log_open_journal(void) {
184 static const union sockaddr_union sa = {
185 .un.sun_family = AF_UNIX,
186 .un.sun_path = "/run/systemd/journal/socket",
194 journal_fd = create_log_socket(SOCK_DGRAM);
195 if (journal_fd < 0) {
200 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
215 /* If we don't use the console we close it here, to not get
216 * killed by SAK. If we don't use syslog we close it here so
217 * that we are not confused by somebody deleting the socket in
218 * the fs. If we don't use /dev/kmsg we still keep it open,
219 * because there is no reason to close it. */
221 if (log_target == LOG_TARGET_NULL) {
228 if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
230 isatty(STDERR_FILENO) <= 0) {
232 if (log_target == LOG_TARGET_AUTO ||
233 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
234 log_target == LOG_TARGET_JOURNAL) {
235 r = log_open_journal();
243 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
244 log_target == LOG_TARGET_SYSLOG) {
245 r = log_open_syslog();
253 if (log_target == LOG_TARGET_AUTO ||
254 log_target == LOG_TARGET_SAFE ||
255 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
256 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
257 log_target == LOG_TARGET_KMSG) {
271 return log_open_console();
274 void log_set_target(LogTarget target) {
276 assert(target < _LOG_TARGET_MAX);
278 if (upgrade_syslog_to_journal) {
279 if (target == LOG_TARGET_SYSLOG)
280 target = LOG_TARGET_JOURNAL;
281 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
282 target = LOG_TARGET_JOURNAL_OR_KMSG;
288 void log_close(void) {
295 void log_forget_fds(void) {
296 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
299 void log_set_max_level(int level) {
300 assert((level & LOG_PRIMASK) == level);
302 log_max_level = level;
305 void log_set_facility(int facility) {
306 log_facility = facility;
309 static int write_to_console(
315 const char *object_field,
317 const char *buffer) {
319 char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2];
320 struct iovec iovec[6] = {};
327 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
328 sprintf(prefix, "<%i>", level);
329 IOVEC_SET_STRING(iovec[n++], prefix);
332 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
335 snprintf(location, sizeof(location), "(%s:%i) ", file, line);
336 IOVEC_SET_STRING(iovec[n++], location);
340 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
341 IOVEC_SET_STRING(iovec[n++], buffer);
343 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
344 IOVEC_SET_STRING(iovec[n++], "\n");
346 if (writev(console_fd, iovec, n) < 0) {
348 if (errno == EIO && getpid() == 1) {
350 /* If somebody tried to kick us from our
351 * console tty (via vhangup() or suchlike),
352 * try to reconnect */
360 if (writev(console_fd, iovec, n) < 0)
369 static int write_to_syslog(
375 const char *object_field,
377 const char *buffer) {
379 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
381 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
382 struct iovec iovec[5] = {};
383 struct msghdr msghdr = {
385 .msg_iovlen = ELEMENTSOF(iovec),
393 xsprintf(header_priority, "<%i>", level);
395 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
400 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
403 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
405 IOVEC_SET_STRING(iovec[0], header_priority);
406 IOVEC_SET_STRING(iovec[1], header_time);
407 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
408 IOVEC_SET_STRING(iovec[3], header_pid);
409 IOVEC_SET_STRING(iovec[4], buffer);
411 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
412 if (syslog_is_stream)
418 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
422 if (!syslog_is_stream ||
423 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
426 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
432 static int write_to_kmsg(
438 const char *object_field,
440 const char *buffer) {
442 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
443 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
444 struct iovec iovec[5] = {};
449 xsprintf(header_priority, "<%i>", level);
450 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
452 IOVEC_SET_STRING(iovec[0], header_priority);
453 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
454 IOVEC_SET_STRING(iovec[2], header_pid);
455 IOVEC_SET_STRING(iovec[3], buffer);
456 IOVEC_SET_STRING(iovec[4], "\n");
458 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
464 static int log_do_header(
469 const char *file, int line, const char *func,
470 const char *object_field, const char *object) {
472 snprintf(header, size,
474 "SYSLOG_FACILITY=%i\n"
480 "SYSLOG_IDENTIFIER=%s\n",
483 isempty(file) ? "" : "CODE_FILE=",
484 isempty(file) ? "" : file,
485 isempty(file) ? "" : "\n",
486 line ? "CODE_LINE=" : "",
487 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
489 isempty(func) ? "" : "CODE_FUNCTION=",
490 isempty(func) ? "" : func,
491 isempty(func) ? "" : "\n",
492 error ? "ERRNO=" : "",
493 error ? 1 : 0, error,
495 isempty(object) ? "" : object_field,
496 isempty(object) ? "" : object,
497 isempty(object) ? "" : "\n",
498 program_invocation_short_name);
503 static int write_to_journal(
509 const char *object_field,
511 const char *buffer) {
513 char header[LINE_MAX];
514 struct iovec iovec[4] = {};
515 struct msghdr mh = {};
520 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object);
522 IOVEC_SET_STRING(iovec[0], header);
523 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
524 IOVEC_SET_STRING(iovec[2], buffer);
525 IOVEC_SET_STRING(iovec[3], "\n");
528 mh.msg_iovlen = ELEMENTSOF(iovec);
530 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
536 static int log_dispatch(
542 const char *object_field,
548 if (log_target == LOG_TARGET_NULL)
551 /* Patch in LOG_DAEMON facility if necessary */
552 if ((level & LOG_FACMASK) == 0)
553 level = log_facility | LOG_PRI(level);
562 buffer += strspn(buffer, NEWLINE);
567 if ((e = strpbrk(buffer, NEWLINE)))
570 if (log_target == LOG_TARGET_AUTO ||
571 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
572 log_target == LOG_TARGET_JOURNAL) {
574 k = write_to_journal(level, error, file, line, func, object_field, object, buffer);
582 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
583 log_target == LOG_TARGET_SYSLOG) {
585 k = write_to_syslog(level, error, file, line, func, object_field, object, buffer);
594 (log_target == LOG_TARGET_AUTO ||
595 log_target == LOG_TARGET_SAFE ||
596 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
597 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
598 log_target == LOG_TARGET_KMSG)) {
600 k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
608 (void) write_to_console(level, error, file, line, func, object_field, object, buffer);
616 int log_dump_internal(
626 /* This modifies the buffer... */
631 if (_likely_(LOG_PRI(level) > log_max_level))
634 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
647 char buffer[LINE_MAX];
652 if (_likely_(LOG_PRI(level) > log_max_level))
655 /* Make sure that %m maps to the specified error */
659 vsnprintf(buffer, sizeof(buffer), format, ap);
661 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
670 const char *format, ...) {
675 va_start(ap, format);
676 r = log_internalv(level, error, file, line, func, format, ap);
682 int log_object_internalv(
688 const char *object_field,
694 char buffer[LINE_MAX];
699 if (_likely_(LOG_PRI(level) > log_max_level))
702 /* Make sure that %m maps to the specified error */
706 vsnprintf(buffer, sizeof(buffer), format, ap);
708 return log_dispatch(level, error, file, line, func, object_field, object, buffer);
711 int log_object_internal(
717 const char *object_field,
719 const char *format, ...) {
724 va_start(ap, format);
725 r = log_object_internalv(level, error, file, line, func, object_field, object, format, ap);
731 static void log_assert(
737 const char *format) {
739 static char buffer[LINE_MAX];
741 if (_likely_(LOG_PRI(level) > log_max_level))
744 DISABLE_WARNING_FORMAT_NONLITERAL;
745 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
748 log_abort_msg = buffer;
750 log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
753 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
754 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
758 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
759 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
763 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
765 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
768 int log_oom_internal(const char *file, int line, const char *func) {
769 log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory.");
773 int log_struct_internal(
779 const char *format, ...) {
789 if (_likely_(LOG_PRI(level) > log_max_level))
792 if (log_target == LOG_TARGET_NULL)
795 if ((level & LOG_FACMASK) == 0)
796 level = log_facility | LOG_PRI(level);
798 if ((log_target == LOG_TARGET_AUTO ||
799 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
800 log_target == LOG_TARGET_JOURNAL) &&
802 char header[LINE_MAX];
803 struct iovec iovec[17] = {};
808 static const char nl = '\n';
809 bool fallback = false;
811 /* If the journal is available do structured logging */
812 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
813 IOVEC_SET_STRING(iovec[n++], header);
815 va_start(ap, format);
816 while (format && n + 1 < ELEMENTSOF(iovec)) {
820 /* We need to copy the va_list structure,
821 * since vasprintf() leaves it afterwards at
822 * an undefined location */
828 if (vasprintf(&m, format, aq) < 0) {
835 /* Now, jump enough ahead, so that we point to
836 * the next format string */
837 VA_FORMAT_ADVANCE(format, ap);
839 IOVEC_SET_STRING(iovec[n++], m);
841 iovec[n].iov_base = (char*) &nl;
842 iovec[n].iov_len = 1;
845 format = va_arg(ap, char *);
850 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
854 for (i = 1; i < n; i += 2)
855 free(iovec[i].iov_base);
861 /* Fallback if journal logging is not available or didn't work. */
863 va_start(ap, format);
871 vsnprintf(buf, sizeof(buf), format, aq);
874 if (startswith(buf, "MESSAGE=")) {
879 VA_FORMAT_ADVANCE(format, ap);
881 format = va_arg(ap, char *);
888 return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
891 int log_set_target_from_string(const char *e) {
894 t = log_target_from_string(e);
902 int log_set_max_level_from_string(const char *e) {
905 t = log_level_from_string(e);
909 log_set_max_level(t);
913 static int parse_proc_cmdline_item(const char *key, const char *value) {
916 * The systemd.log_xyz= settings are parsed by all tools, and
919 * However, "quiet" is only parsed by PID 1, and only turns of
920 * status output to /dev/console, but does not alter the log
924 if (streq(key, "debug") && !value)
925 log_set_max_level(LOG_DEBUG);
927 else if (streq(key, "systemd.log_target") && value) {
929 if (log_set_target_from_string(value) < 0)
930 log_warning("Failed to parse log target '%s'. Ignoring.", value);
932 } else if (streq(key, "systemd.log_level") && value) {
934 if (log_set_max_level_from_string(value) < 0)
935 log_warning("Failed to parse log level '%s'. Ignoring.", value);
937 } else if (streq(key, "systemd.log_color") && value) {
939 if (log_show_color_from_string(value) < 0)
940 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
942 } else if (streq(key, "systemd.log_location") && value) {
944 if (log_show_location_from_string(value) < 0)
945 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
951 void log_parse_environment(void) {
954 if (get_ctty_devnr(0, NULL) < 0)
955 /* Only try to read the command line in daemons.
956 We assume that anything that has a controlling
957 tty is user stuff. */
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;