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 /* Akin to glibc's __abort_msg; which is private and we hence cannot
56 static char *log_abort_msg = NULL;
58 void log_close_console(void) {
65 close_nointr_nofail(console_fd);
71 static int log_open_console(void) {
77 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
81 console_fd = STDERR_FILENO;
86 void log_close_kmsg(void) {
91 close_nointr_nofail(kmsg_fd);
95 static int log_open_kmsg(void) {
100 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
107 void log_close_syslog(void) {
112 close_nointr_nofail(syslog_fd);
116 static int create_log_socket(int type) {
120 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
124 fd_inc_sndbuf(fd, SNDBUF_SIZE);
126 /* We need a blocking fd here since we'd otherwise lose
127 messages way too early. However, let's not hang forever in the
128 unlikely case of a deadlock. */
130 timeval_store(&tv, 10 * USEC_PER_MSEC);
132 timeval_store(&tv, 10 * USEC_PER_SEC);
133 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
138 static int log_open_syslog(void) {
140 union sockaddr_union sa = {
141 .un.sun_family = AF_UNIX,
142 .un.sun_path = "/dev/log",
148 syslog_fd = create_log_socket(SOCK_DGRAM);
154 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
155 close_nointr_nofail(syslog_fd);
157 /* Some legacy syslog systems still use stream
158 * sockets. They really shouldn't. But what can we
160 syslog_fd = create_log_socket(SOCK_STREAM);
166 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
171 syslog_is_stream = true;
173 syslog_is_stream = false;
182 void log_close_journal(void) {
187 close_nointr_nofail(journal_fd);
191 static int log_open_journal(void) {
192 union sockaddr_union sa = {
193 .un.sun_family = AF_UNIX,
194 .un.sun_path = "/run/systemd/journal/socket",
201 journal_fd = create_log_socket(SOCK_DGRAM);
202 if (journal_fd < 0) {
207 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
222 /* If we don't use the console we close it here, to not get
223 * killed by SAK. If we don't use syslog we close it here so
224 * that we are not confused by somebody deleting the socket in
225 * the fs. If we don't use /dev/kmsg we still keep it open,
226 * because there is no reason to close it. */
228 if (log_target == LOG_TARGET_NULL) {
235 if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
237 isatty(STDERR_FILENO) <= 0) {
239 if (log_target == LOG_TARGET_AUTO ||
240 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
241 log_target == LOG_TARGET_JOURNAL) {
242 r = log_open_journal();
250 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
251 log_target == LOG_TARGET_SYSLOG) {
252 r = log_open_syslog();
260 if (log_target == LOG_TARGET_AUTO ||
261 log_target == LOG_TARGET_SAFE ||
262 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
263 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
264 log_target == LOG_TARGET_KMSG) {
278 return log_open_console();
281 void log_set_target(LogTarget target) {
283 assert(target < _LOG_TARGET_MAX);
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(
314 const char *object_name,
316 const char *buffer) {
319 struct iovec iovec[5] = {};
326 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
329 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
330 char_array_0(location);
331 IOVEC_SET_STRING(iovec[n++], location);
335 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
336 IOVEC_SET_STRING(iovec[n++], buffer);
338 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
339 IOVEC_SET_STRING(iovec[n++], "\n");
341 if (writev(console_fd, iovec, n) < 0) {
343 if (errno == EIO && getpid() == 1) {
345 /* If somebody tried to kick us from our
346 * console tty (via vhangup() or suchlike),
347 * try to reconnect */
355 if (writev(console_fd, iovec, n) < 0)
364 static int write_to_syslog(
369 const char *object_name,
371 const char *buffer) {
373 char header_priority[16], header_time[64], header_pid[16];
374 struct iovec iovec[5] = {};
375 struct msghdr msghdr = {
377 .msg_iovlen = ELEMENTSOF(iovec),
385 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
386 char_array_0(header_priority);
388 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
393 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
396 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
397 char_array_0(header_pid);
399 IOVEC_SET_STRING(iovec[0], header_priority);
400 IOVEC_SET_STRING(iovec[1], header_time);
401 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
402 IOVEC_SET_STRING(iovec[3], header_pid);
403 IOVEC_SET_STRING(iovec[4], buffer);
405 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
406 if (syslog_is_stream)
412 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
416 if (!syslog_is_stream ||
417 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
420 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
426 static int write_to_kmsg(
431 const char *object_name,
433 const char *buffer) {
435 char header_priority[16], header_pid[16];
436 struct iovec iovec[5] = {};
441 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
442 char_array_0(header_priority);
444 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
445 char_array_0(header_pid);
447 IOVEC_SET_STRING(iovec[0], header_priority);
448 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
449 IOVEC_SET_STRING(iovec[2], header_pid);
450 IOVEC_SET_STRING(iovec[3], buffer);
451 IOVEC_SET_STRING(iovec[4], "\n");
453 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
459 static int log_do_header(char *header, size_t size,
461 const char *file, int line, const char *func,
462 const char *object_name, const char *object) {
463 snprintf(header, size,
465 "SYSLOG_FACILITY=%i\n"
470 "SYSLOG_IDENTIFIER=%s\n",
473 file ? "CODE_FILE=" : "",
474 file ? LINE_MAX : 0, file, /* %.0s means no output */
476 line ? "CODE_LINE=" : "",
477 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
479 func ? "CODE_FUNCTION=" : "",
480 func ? LINE_MAX : 0, func,
482 object ? object_name : "",
483 object ? LINE_MAX : 0, object, /* %.0s means no output */
485 program_invocation_short_name);
486 header[size - 1] = '\0';
490 static int write_to_journal(
495 const char *object_name,
497 const char *buffer) {
499 char header[LINE_MAX];
500 struct iovec iovec[4] = {};
501 struct msghdr mh = {};
506 log_do_header(header, sizeof(header), level,
507 file, line, func, object_name, object);
509 IOVEC_SET_STRING(iovec[0], header);
510 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
511 IOVEC_SET_STRING(iovec[2], buffer);
512 IOVEC_SET_STRING(iovec[3], "\n");
515 mh.msg_iovlen = ELEMENTSOF(iovec);
517 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
523 static int log_dispatch(
528 const char *object_name,
534 if (log_target == LOG_TARGET_NULL)
537 /* Patch in LOG_DAEMON facility if necessary */
538 if ((level & LOG_FACMASK) == 0)
539 level = log_facility | LOG_PRI(level);
545 buffer += strspn(buffer, NEWLINE);
550 if ((e = strpbrk(buffer, NEWLINE)))
553 if (log_target == LOG_TARGET_AUTO ||
554 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
555 log_target == LOG_TARGET_JOURNAL) {
557 k = write_to_journal(level, file, line, func,
558 object_name, object, buffer);
567 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
568 log_target == LOG_TARGET_SYSLOG) {
570 k = write_to_syslog(level, file, line, func,
571 object_name, object, buffer);
581 (log_target == LOG_TARGET_AUTO ||
582 log_target == LOG_TARGET_SAFE ||
583 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
584 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
585 log_target == LOG_TARGET_KMSG)) {
587 k = write_to_kmsg(level, file, line, func,
588 object_name, object, buffer);
597 k = write_to_console(level, file, line, func,
598 object_name, object, buffer);
609 int log_dump_internal(
618 /* This modifies the buffer... */
620 if (_likely_(LOG_PRI(level) > log_max_level))
623 return log_dispatch(level, file, line, func, NULL, NULL, buffer);
635 char buffer[LINE_MAX];
637 if (_likely_(LOG_PRI(level) > log_max_level))
640 vsnprintf(buffer, sizeof(buffer), format, ap);
641 char_array_0(buffer);
643 return log_dispatch(level, file, line, func, NULL, NULL, buffer);
651 const char *format, ...) {
656 va_start(ap, format);
657 r = log_metav(level, file, line, func, format, ap);
663 int log_metav_object(
668 const char *object_name,
674 char buffer[LINE_MAX];
676 if (_likely_(LOG_PRI(level) > log_max_level))
679 vsnprintf(buffer, sizeof(buffer), format, ap);
680 char_array_0(buffer);
682 return log_dispatch(level, file, line, func,
683 object_name, object, buffer);
691 const char *object_name,
693 const char *format, ...) {
698 va_start(ap, format);
699 r = log_metav_object(level, file, line, func,
700 object_name, object, format, ap);
706 #pragma GCC diagnostic push
707 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
708 static void log_assert(int level, const char *text, const char *file, int line, const char *func, const char *format) {
709 static char buffer[LINE_MAX];
711 if (_likely_(LOG_PRI(level) > log_max_level))
714 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
716 char_array_0(buffer);
717 log_abort_msg = buffer;
719 log_dispatch(level, file, line, func, NULL, NULL, buffer);
721 #pragma GCC diagnostic pop
723 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
724 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
728 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
729 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
733 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
735 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
738 int log_oom_internal(const char *file, int line, const char *func) {
739 log_meta(LOG_ERR, file, line, func, "Out of memory.");
743 int log_struct_internal(
748 const char *format, ...) {
754 if (_likely_(LOG_PRI(level) > log_max_level))
757 if (log_target == LOG_TARGET_NULL)
760 if ((level & LOG_FACMASK) == 0)
761 level = log_facility | LOG_PRI(level);
763 if ((log_target == LOG_TARGET_AUTO ||
764 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
765 log_target == LOG_TARGET_JOURNAL) &&
768 char header[LINE_MAX];
769 struct iovec iovec[17] = {};
774 static const char nl = '\n';
776 /* If the journal is available do structured logging */
777 log_do_header(header, sizeof(header), level,
778 file, line, func, NULL, NULL);
779 IOVEC_SET_STRING(iovec[n++], header);
781 va_start(ap, format);
782 while (format && n + 1 < ELEMENTSOF(iovec)) {
786 /* We need to copy the va_list structure,
787 * since vasprintf() leaves it afterwards at
788 * an undefined location */
791 if (vasprintf(&buf, format, aq) < 0) {
798 /* Now, jump enough ahead, so that we point to
799 * the next format string */
800 VA_FORMAT_ADVANCE(format, ap);
802 IOVEC_SET_STRING(iovec[n++], buf);
804 iovec[n].iov_base = (char*) &nl;
805 iovec[n].iov_len = 1;
808 format = va_arg(ap, char *);
813 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
820 for (i = 1; i < n; i += 2)
821 free(iovec[i].iov_base);
827 /* Fallback if journal logging is not available */
829 va_start(ap, format);
834 vsnprintf(buf, sizeof(buf), format, aq);
838 if (startswith(buf, "MESSAGE=")) {
843 VA_FORMAT_ADVANCE(format, ap);
845 format = va_arg(ap, char *);
850 r = log_dispatch(level, file, line, func,
851 NULL, NULL, buf + 8);
859 int log_set_target_from_string(const char *e) {
862 t = log_target_from_string(e);
870 int log_set_max_level_from_string(const char *e) {
873 t = log_level_from_string(e);
877 log_set_max_level(t);
881 void log_parse_environment(void) {
882 _cleanup_free_ char *line = NULL;
886 r = proc_cmdline(&line);
888 log_warning("Failed to read /proc/cmdline. Ignoring: %s", strerror(-r));
893 FOREACH_WORD_QUOTED(w, l, line, state) {
894 if (l == 5 && startswith(w, "debug")) {
895 log_set_max_level(LOG_DEBUG);
901 e = secure_getenv("SYSTEMD_LOG_TARGET");
902 if (e && log_set_target_from_string(e) < 0)
903 log_warning("Failed to parse log target %s. Ignoring.", e);
905 e = secure_getenv("SYSTEMD_LOG_LEVEL");
906 if (e && log_set_max_level_from_string(e) < 0)
907 log_warning("Failed to parse log level %s. Ignoring.", e);
909 e = secure_getenv("SYSTEMD_LOG_COLOR");
910 if (e && log_show_color_from_string(e) < 0)
911 log_warning("Failed to parse bool %s. Ignoring.", e);
913 e = secure_getenv("SYSTEMD_LOG_LOCATION");
914 if (e && log_show_location_from_string(e) < 0)
915 log_warning("Failed to parse bool %s. Ignoring.", e);
918 LogTarget log_get_target(void) {
922 int log_get_max_level(void) {
923 return log_max_level;
926 void log_show_color(bool b) {
930 void log_show_location(bool b) {
934 int log_show_color_from_string(const char *e) {
937 t = parse_boolean(e);
945 int log_show_location_from_string(const char *e) {
948 t = parse_boolean(e);
952 log_show_location(t);
956 bool log_on_console(void) {
957 if (log_target == LOG_TARGET_CONSOLE)
960 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
963 static const char *const log_target_table[] = {
964 [LOG_TARGET_CONSOLE] = "console",
965 [LOG_TARGET_KMSG] = "kmsg",
966 [LOG_TARGET_JOURNAL] = "journal",
967 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
968 [LOG_TARGET_SYSLOG] = "syslog",
969 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
970 [LOG_TARGET_AUTO] = "auto",
971 [LOG_TARGET_SAFE] = "safe",
972 [LOG_TARGET_NULL] = "null"
975 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
977 void log_received_signal(int level, const struct signalfd_siginfo *si) {
978 if (si->ssi_pid > 0) {
979 _cleanup_free_ char *p = NULL;
981 get_process_comm(si->ssi_pid, &p);
984 "Received SIG%s from PID "PID_FMT" (%s).",
985 signal_to_string(si->ssi_signo),
986 si->ssi_pid, strna(p));
990 signal_to_string(si->ssi_signo));