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 safe_close(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) {
87 kmsg_fd = safe_close(kmsg_fd);
90 static int log_open_kmsg(void) {
95 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
102 void log_close_syslog(void) {
103 syslog_fd = safe_close(syslog_fd);
106 static int create_log_socket(int type) {
110 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
114 fd_inc_sndbuf(fd, SNDBUF_SIZE);
116 /* We need a blocking fd here since we'd otherwise lose
117 messages way too early. However, let's not hang forever in the
118 unlikely case of a deadlock. */
120 timeval_store(&tv, 10 * USEC_PER_MSEC);
122 timeval_store(&tv, 10 * USEC_PER_SEC);
123 setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
128 static int log_open_syslog(void) {
130 union sockaddr_union sa = {
131 .un.sun_family = AF_UNIX,
132 .un.sun_path = "/dev/log",
138 syslog_fd = create_log_socket(SOCK_DGRAM);
144 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
145 safe_close(syslog_fd);
147 /* Some legacy syslog systems still use stream
148 * sockets. They really shouldn't. But what can we
150 syslog_fd = create_log_socket(SOCK_STREAM);
156 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
161 syslog_is_stream = true;
163 syslog_is_stream = false;
172 void log_close_journal(void) {
173 journal_fd = safe_close(journal_fd);
176 static int log_open_journal(void) {
177 union sockaddr_union sa = {
178 .un.sun_family = AF_UNIX,
179 .un.sun_path = "/run/systemd/journal/socket",
186 journal_fd = create_log_socket(SOCK_DGRAM);
187 if (journal_fd < 0) {
192 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
207 /* If we don't use the console we close it here, to not get
208 * killed by SAK. If we don't use syslog we close it here so
209 * that we are not confused by somebody deleting the socket in
210 * the fs. If we don't use /dev/kmsg we still keep it open,
211 * because there is no reason to close it. */
213 if (log_target == LOG_TARGET_NULL) {
220 if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
222 isatty(STDERR_FILENO) <= 0) {
224 if (log_target == LOG_TARGET_AUTO ||
225 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
226 log_target == LOG_TARGET_JOURNAL) {
227 r = log_open_journal();
235 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
236 log_target == LOG_TARGET_SYSLOG) {
237 r = log_open_syslog();
245 if (log_target == LOG_TARGET_AUTO ||
246 log_target == LOG_TARGET_SAFE ||
247 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
248 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
249 log_target == LOG_TARGET_KMSG) {
263 return log_open_console();
266 void log_set_target(LogTarget target) {
268 assert(target < _LOG_TARGET_MAX);
273 void log_close(void) {
280 void log_forget_fds(void) {
281 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
284 void log_set_max_level(int level) {
285 assert((level & LOG_PRIMASK) == level);
287 log_max_level = level;
290 void log_set_facility(int facility) {
291 log_facility = facility;
294 static int write_to_console(
299 const char *object_name,
301 const char *buffer) {
304 struct iovec iovec[5] = {};
311 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
314 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
315 char_array_0(location);
316 IOVEC_SET_STRING(iovec[n++], location);
320 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
321 IOVEC_SET_STRING(iovec[n++], buffer);
323 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
324 IOVEC_SET_STRING(iovec[n++], "\n");
326 if (writev(console_fd, iovec, n) < 0) {
328 if (errno == EIO && getpid() == 1) {
330 /* If somebody tried to kick us from our
331 * console tty (via vhangup() or suchlike),
332 * try to reconnect */
340 if (writev(console_fd, iovec, n) < 0)
349 static int write_to_syslog(
354 const char *object_name,
356 const char *buffer) {
358 char header_priority[16], header_time[64], header_pid[16];
359 struct iovec iovec[5] = {};
360 struct msghdr msghdr = {
362 .msg_iovlen = ELEMENTSOF(iovec),
370 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
371 char_array_0(header_priority);
373 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
378 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
381 snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
382 char_array_0(header_pid);
384 IOVEC_SET_STRING(iovec[0], header_priority);
385 IOVEC_SET_STRING(iovec[1], header_time);
386 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
387 IOVEC_SET_STRING(iovec[3], header_pid);
388 IOVEC_SET_STRING(iovec[4], buffer);
390 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
391 if (syslog_is_stream)
397 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
401 if (!syslog_is_stream ||
402 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
405 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
411 static int write_to_kmsg(
416 const char *object_name,
418 const char *buffer) {
420 char header_priority[16], header_pid[16];
421 struct iovec iovec[5] = {};
426 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
427 char_array_0(header_priority);
429 snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", getpid());
430 char_array_0(header_pid);
432 IOVEC_SET_STRING(iovec[0], header_priority);
433 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
434 IOVEC_SET_STRING(iovec[2], header_pid);
435 IOVEC_SET_STRING(iovec[3], buffer);
436 IOVEC_SET_STRING(iovec[4], "\n");
438 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
444 static int log_do_header(char *header, size_t size,
446 const char *file, int line, const char *func,
447 const char *object_name, const char *object) {
448 snprintf(header, size,
450 "SYSLOG_FACILITY=%i\n"
455 "SYSLOG_IDENTIFIER=%s\n",
458 file ? "CODE_FILE=" : "",
459 file ? LINE_MAX : 0, file, /* %.0s means no output */
461 line ? "CODE_LINE=" : "",
462 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
464 func ? "CODE_FUNCTION=" : "",
465 func ? LINE_MAX : 0, func,
467 object ? object_name : "",
468 object ? LINE_MAX : 0, object, /* %.0s means no output */
470 program_invocation_short_name);
471 header[size - 1] = '\0';
475 static int write_to_journal(
480 const char *object_name,
482 const char *buffer) {
484 char header[LINE_MAX];
485 struct iovec iovec[4] = {};
486 struct msghdr mh = {};
491 log_do_header(header, sizeof(header), level,
492 file, line, func, object_name, object);
494 IOVEC_SET_STRING(iovec[0], header);
495 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
496 IOVEC_SET_STRING(iovec[2], buffer);
497 IOVEC_SET_STRING(iovec[3], "\n");
500 mh.msg_iovlen = ELEMENTSOF(iovec);
502 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
508 static int log_dispatch(
513 const char *object_name,
519 if (log_target == LOG_TARGET_NULL)
522 /* Patch in LOG_DAEMON facility if necessary */
523 if ((level & LOG_FACMASK) == 0)
524 level = log_facility | LOG_PRI(level);
530 buffer += strspn(buffer, NEWLINE);
535 if ((e = strpbrk(buffer, NEWLINE)))
538 if (log_target == LOG_TARGET_AUTO ||
539 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
540 log_target == LOG_TARGET_JOURNAL) {
542 k = write_to_journal(level, file, line, func,
543 object_name, object, buffer);
552 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
553 log_target == LOG_TARGET_SYSLOG) {
555 k = write_to_syslog(level, file, line, func,
556 object_name, object, buffer);
566 (log_target == LOG_TARGET_AUTO ||
567 log_target == LOG_TARGET_SAFE ||
568 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
569 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
570 log_target == LOG_TARGET_KMSG)) {
572 k = write_to_kmsg(level, file, line, func,
573 object_name, object, buffer);
582 k = write_to_console(level, file, line, func,
583 object_name, object, buffer);
594 int log_dump_internal(
603 /* This modifies the buffer... */
605 if (_likely_(LOG_PRI(level) > log_max_level))
608 return log_dispatch(level, file, line, func, NULL, NULL, buffer);
620 char buffer[LINE_MAX];
622 if (_likely_(LOG_PRI(level) > log_max_level))
625 vsnprintf(buffer, sizeof(buffer), format, ap);
626 char_array_0(buffer);
628 return log_dispatch(level, file, line, func, NULL, NULL, buffer);
636 const char *format, ...) {
641 va_start(ap, format);
642 r = log_metav(level, file, line, func, format, ap);
648 int log_metav_object(
653 const char *object_name,
659 char buffer[LINE_MAX];
661 if (_likely_(LOG_PRI(level) > log_max_level))
664 vsnprintf(buffer, sizeof(buffer), format, ap);
665 char_array_0(buffer);
667 return log_dispatch(level, file, line, func,
668 object_name, object, buffer);
676 const char *object_name,
678 const char *format, ...) {
683 va_start(ap, format);
684 r = log_metav_object(level, file, line, func,
685 object_name, object, format, ap);
691 static void log_assert(int level, const char *text, const char *file, int line, const char *func, const char *format) {
692 static char buffer[LINE_MAX];
694 if (_likely_(LOG_PRI(level) > log_max_level))
697 DISABLE_WARNING_FORMAT_NONLITERAL;
698 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
701 char_array_0(buffer);
702 log_abort_msg = buffer;
704 log_dispatch(level, file, line, func, NULL, NULL, buffer);
707 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
708 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
712 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
713 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
717 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
719 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
722 int log_oom_internal(const char *file, int line, const char *func) {
723 log_meta(LOG_ERR, file, line, func, "Out of memory.");
727 int log_struct_internal(
732 const char *format, ...) {
738 if (_likely_(LOG_PRI(level) > log_max_level))
741 if (log_target == LOG_TARGET_NULL)
744 if ((level & LOG_FACMASK) == 0)
745 level = log_facility | LOG_PRI(level);
747 if ((log_target == LOG_TARGET_AUTO ||
748 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
749 log_target == LOG_TARGET_JOURNAL) &&
752 char header[LINE_MAX];
753 struct iovec iovec[17] = {};
758 static const char nl = '\n';
760 /* If the journal is available do structured logging */
761 log_do_header(header, sizeof(header), level,
762 file, line, func, NULL, NULL);
763 IOVEC_SET_STRING(iovec[n++], header);
765 va_start(ap, format);
766 while (format && n + 1 < ELEMENTSOF(iovec)) {
770 /* We need to copy the va_list structure,
771 * since vasprintf() leaves it afterwards at
772 * an undefined location */
775 if (vasprintf(&buf, format, aq) < 0) {
782 /* Now, jump enough ahead, so that we point to
783 * the next format string */
784 VA_FORMAT_ADVANCE(format, ap);
786 IOVEC_SET_STRING(iovec[n++], buf);
788 iovec[n].iov_base = (char*) &nl;
789 iovec[n].iov_len = 1;
792 format = va_arg(ap, char *);
797 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
804 for (i = 1; i < n; i += 2)
805 free(iovec[i].iov_base);
811 /* Fallback if journal logging is not available */
813 va_start(ap, format);
818 vsnprintf(buf, sizeof(buf), format, aq);
822 if (startswith(buf, "MESSAGE=")) {
827 VA_FORMAT_ADVANCE(format, ap);
829 format = va_arg(ap, char *);
834 r = log_dispatch(level, file, line, func,
835 NULL, NULL, buf + 8);
843 int log_set_target_from_string(const char *e) {
846 t = log_target_from_string(e);
854 int log_set_max_level_from_string(const char *e) {
857 t = log_level_from_string(e);
861 log_set_max_level(t);
865 void log_parse_environment(void) {
866 _cleanup_free_ char *line = NULL;
870 r = proc_cmdline(&line);
872 log_warning("Failed to read /proc/cmdline. Ignoring: %s", strerror(-r));
877 FOREACH_WORD_QUOTED(w, l, line, state) {
878 if (l == 5 && startswith(w, "debug")) {
879 log_set_max_level(LOG_DEBUG);
881 } else if (l == 5 && startswith(w, "quiet")) {
882 log_set_max_level(LOG_WARNING);
888 e = secure_getenv("SYSTEMD_LOG_TARGET");
889 if (e && log_set_target_from_string(e) < 0)
890 log_warning("Failed to parse log target %s. Ignoring.", e);
892 e = secure_getenv("SYSTEMD_LOG_LEVEL");
893 if (e && log_set_max_level_from_string(e) < 0)
894 log_warning("Failed to parse log level %s. Ignoring.", e);
896 e = secure_getenv("SYSTEMD_LOG_COLOR");
897 if (e && log_show_color_from_string(e) < 0)
898 log_warning("Failed to parse bool %s. Ignoring.", e);
900 e = secure_getenv("SYSTEMD_LOG_LOCATION");
901 if (e && log_show_location_from_string(e) < 0)
902 log_warning("Failed to parse bool %s. Ignoring.", e);
905 LogTarget log_get_target(void) {
909 int log_get_max_level(void) {
910 return log_max_level;
913 void log_show_color(bool b) {
917 bool log_get_show_color(void) {
921 void log_show_location(bool b) {
925 bool log_get_show_location(void) {
926 return show_location;
929 int log_show_color_from_string(const char *e) {
932 t = parse_boolean(e);
940 int log_show_location_from_string(const char *e) {
943 t = parse_boolean(e);
947 log_show_location(t);
951 bool log_on_console(void) {
952 if (log_target == LOG_TARGET_CONSOLE)
955 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
958 static const char *const log_target_table[_LOG_TARGET_MAX] = {
959 [LOG_TARGET_CONSOLE] = "console",
960 [LOG_TARGET_KMSG] = "kmsg",
961 [LOG_TARGET_JOURNAL] = "journal",
962 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
963 [LOG_TARGET_SYSLOG] = "syslog",
964 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
965 [LOG_TARGET_AUTO] = "auto",
966 [LOG_TARGET_SAFE] = "safe",
967 [LOG_TARGET_NULL] = "null"
970 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
972 void log_received_signal(int level, const struct signalfd_siginfo *si) {
973 if (si->ssi_pid > 0) {
974 _cleanup_free_ char *p = NULL;
976 get_process_comm(si->ssi_pid, &p);
979 "Received SIG%s from PID "PID_FMT" (%s).",
980 signal_to_string(si->ssi_signo),
981 si->ssi_pid, strna(p));
985 signal_to_string(si->ssi_signo));