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 IOVEC_SET_STRING(iovec[n++], location);
338 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
339 IOVEC_SET_STRING(iovec[n++], buffer);
341 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
342 IOVEC_SET_STRING(iovec[n++], "\n");
344 if (writev(console_fd, iovec, n) < 0) {
346 if (errno == EIO && getpid() == 1) {
348 /* If somebody tried to kick us from our
349 * console tty (via vhangup() or suchlike),
350 * try to reconnect */
358 if (writev(console_fd, iovec, n) < 0)
367 static int write_to_syslog(
373 const char *object_field,
375 const char *buffer) {
377 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
379 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
380 struct iovec iovec[5] = {};
381 struct msghdr msghdr = {
383 .msg_iovlen = ELEMENTSOF(iovec),
391 xsprintf(header_priority, "<%i>", level);
393 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
398 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
401 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
403 IOVEC_SET_STRING(iovec[0], header_priority);
404 IOVEC_SET_STRING(iovec[1], header_time);
405 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
406 IOVEC_SET_STRING(iovec[3], header_pid);
407 IOVEC_SET_STRING(iovec[4], buffer);
409 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
410 if (syslog_is_stream)
416 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
420 if (!syslog_is_stream ||
421 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
424 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
430 static int write_to_kmsg(
436 const char *object_field,
438 const char *buffer) {
440 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
441 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
442 struct iovec iovec[5] = {};
447 xsprintf(header_priority, "<%i>", level);
448 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
450 IOVEC_SET_STRING(iovec[0], header_priority);
451 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
452 IOVEC_SET_STRING(iovec[2], header_pid);
453 IOVEC_SET_STRING(iovec[3], buffer);
454 IOVEC_SET_STRING(iovec[4], "\n");
456 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
462 static int log_do_header(
467 const char *file, int line, const char *func,
468 const char *object_field, const char *object) {
470 snprintf(header, size,
472 "SYSLOG_FACILITY=%i\n"
478 "SYSLOG_IDENTIFIER=%s\n",
481 isempty(file) ? "" : "CODE_FILE=",
482 isempty(file) ? "" : file,
483 isempty(file) ? "" : "\n",
484 line ? "CODE_LINE=" : "",
485 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
487 isempty(func) ? "" : "CODE_FUNCTION=",
488 isempty(func) ? "" : func,
489 isempty(func) ? "" : "\n",
490 error ? "ERRNO=" : "",
491 error ? 1 : 0, error,
493 isempty(object) ? "" : object_field,
494 isempty(object) ? "" : object,
495 isempty(object) ? "" : "\n",
496 program_invocation_short_name);
501 static int write_to_journal(
507 const char *object_field,
509 const char *buffer) {
511 char header[LINE_MAX];
512 struct iovec iovec[4] = {};
513 struct msghdr mh = {};
518 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object);
520 IOVEC_SET_STRING(iovec[0], header);
521 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
522 IOVEC_SET_STRING(iovec[2], buffer);
523 IOVEC_SET_STRING(iovec[3], "\n");
526 mh.msg_iovlen = ELEMENTSOF(iovec);
528 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
534 static int log_dispatch(
540 const char *object_field,
546 if (log_target == LOG_TARGET_NULL)
549 /* Patch in LOG_DAEMON facility if necessary */
550 if ((level & LOG_FACMASK) == 0)
551 level = log_facility | LOG_PRI(level);
560 buffer += strspn(buffer, NEWLINE);
565 if ((e = strpbrk(buffer, NEWLINE)))
568 if (log_target == LOG_TARGET_AUTO ||
569 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
570 log_target == LOG_TARGET_JOURNAL) {
572 k = write_to_journal(level, error, file, line, func, object_field, object, buffer);
580 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
581 log_target == LOG_TARGET_SYSLOG) {
583 k = write_to_syslog(level, error, file, line, func, object_field, object, buffer);
592 (log_target == LOG_TARGET_AUTO ||
593 log_target == LOG_TARGET_SAFE ||
594 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
595 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
596 log_target == LOG_TARGET_KMSG)) {
598 k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
606 (void) write_to_console(level, error, file, line, func, object_field, object, buffer);
614 int log_dump_internal(
624 /* This modifies the buffer... */
629 if (_likely_(LOG_PRI(level) > log_max_level))
632 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
645 char buffer[LINE_MAX];
650 if (_likely_(LOG_PRI(level) > log_max_level))
653 /* Make sure that %m maps to the specified error */
657 vsnprintf(buffer, sizeof(buffer), format, ap);
659 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
668 const char *format, ...) {
673 va_start(ap, format);
674 r = log_internalv(level, error, file, line, func, format, ap);
680 int log_object_internalv(
686 const char *object_field,
692 char buffer[LINE_MAX];
697 if (_likely_(LOG_PRI(level) > log_max_level))
700 /* Make sure that %m maps to the specified error */
704 vsnprintf(buffer, sizeof(buffer), format, ap);
706 return log_dispatch(level, error, file, line, func, object_field, object, buffer);
709 int log_object_internal(
715 const char *object_field,
717 const char *format, ...) {
722 va_start(ap, format);
723 r = log_object_internalv(level, error, file, line, func, object_field, object, format, ap);
729 static void log_assert(
735 const char *format) {
737 static char buffer[LINE_MAX];
739 if (_likely_(LOG_PRI(level) > log_max_level))
742 DISABLE_WARNING_FORMAT_NONLITERAL;
743 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
746 log_abort_msg = buffer;
748 log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
751 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
752 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
756 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
757 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
761 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
763 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
766 int log_oom_internal(const char *file, int line, const char *func) {
767 log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory.");
771 int log_struct_internal(
777 const char *format, ...) {
787 if (_likely_(LOG_PRI(level) > log_max_level))
790 if (log_target == LOG_TARGET_NULL)
793 if ((level & LOG_FACMASK) == 0)
794 level = log_facility | LOG_PRI(level);
796 if ((log_target == LOG_TARGET_AUTO ||
797 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
798 log_target == LOG_TARGET_JOURNAL) &&
800 char header[LINE_MAX];
801 struct iovec iovec[17] = {};
806 static const char nl = '\n';
807 bool fallback = false;
809 /* If the journal is available do structured logging */
810 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
811 IOVEC_SET_STRING(iovec[n++], header);
813 va_start(ap, format);
814 while (format && n + 1 < ELEMENTSOF(iovec)) {
818 /* We need to copy the va_list structure,
819 * since vasprintf() leaves it afterwards at
820 * an undefined location */
826 if (vasprintf(&m, format, aq) < 0) {
833 /* Now, jump enough ahead, so that we point to
834 * the next format string */
835 VA_FORMAT_ADVANCE(format, ap);
837 IOVEC_SET_STRING(iovec[n++], m);
839 iovec[n].iov_base = (char*) &nl;
840 iovec[n].iov_len = 1;
843 format = va_arg(ap, char *);
848 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
852 for (i = 1; i < n; i += 2)
853 free(iovec[i].iov_base);
859 /* Fallback if journal logging is not available or didn't work. */
861 va_start(ap, format);
869 vsnprintf(buf, sizeof(buf), format, aq);
872 if (startswith(buf, "MESSAGE=")) {
877 VA_FORMAT_ADVANCE(format, ap);
879 format = va_arg(ap, char *);
886 return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
889 int log_set_target_from_string(const char *e) {
892 t = log_target_from_string(e);
900 int log_set_max_level_from_string(const char *e) {
903 t = log_level_from_string(e);
907 log_set_max_level(t);
911 static int parse_proc_cmdline_item(const char *key, const char *value) {
914 * The systemd.log_xyz= settings are parsed by all tools, and
917 * However, "quiet" is only parsed by PID 1, and only turns of
918 * status output to /dev/console, but does not alter the log
922 if (streq(key, "debug") && !value)
923 log_set_max_level(LOG_DEBUG);
925 else if (streq(key, "systemd.log_target") && value) {
927 if (log_set_target_from_string(value) < 0)
928 log_warning("Failed to parse log target '%s'. Ignoring.", value);
930 } else if (streq(key, "systemd.log_level") && value) {
932 if (log_set_max_level_from_string(value) < 0)
933 log_warning("Failed to parse log level '%s'. Ignoring.", value);
935 } else if (streq(key, "systemd.log_color") && value) {
937 if (log_show_color_from_string(value) < 0)
938 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
940 } else if (streq(key, "systemd.log_location") && value) {
942 if (log_show_location_from_string(value) < 0)
943 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
949 void log_parse_environment(void) {
952 if (get_ctty_devnr(0, NULL) < 0)
953 /* Only try to read the command line in daemons.
954 We assume that anything that has a controlling
955 tty is user stuff. */
956 (void) parse_proc_cmdline(parse_proc_cmdline_item);
958 e = secure_getenv("SYSTEMD_LOG_TARGET");
959 if (e && log_set_target_from_string(e) < 0)
960 log_warning("Failed to parse log target '%s'. Ignoring.", e);
962 e = secure_getenv("SYSTEMD_LOG_LEVEL");
963 if (e && log_set_max_level_from_string(e) < 0)
964 log_warning("Failed to parse log level '%s'. Ignoring.", e);
966 e = secure_getenv("SYSTEMD_LOG_COLOR");
967 if (e && log_show_color_from_string(e) < 0)
968 log_warning("Failed to parse bool '%s'. Ignoring.", e);
970 e = secure_getenv("SYSTEMD_LOG_LOCATION");
971 if (e && log_show_location_from_string(e) < 0)
972 log_warning("Failed to parse bool '%s'. Ignoring.", e);
975 LogTarget log_get_target(void) {
979 int log_get_max_level(void) {
980 return log_max_level;
983 void log_show_color(bool b) {
987 bool log_get_show_color(void) {
991 void log_show_location(bool b) {
995 bool log_get_show_location(void) {
996 return show_location;
999 int log_show_color_from_string(const char *e) {
1002 t = parse_boolean(e);
1010 int log_show_location_from_string(const char *e) {
1013 t = parse_boolean(e);
1017 log_show_location(t);
1021 bool log_on_console(void) {
1022 if (log_target == LOG_TARGET_CONSOLE ||
1023 log_target == LOG_TARGET_CONSOLE_PREFIXED)
1026 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1029 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1030 [LOG_TARGET_CONSOLE] = "console",
1031 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1032 [LOG_TARGET_KMSG] = "kmsg",
1033 [LOG_TARGET_JOURNAL] = "journal",
1034 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1035 [LOG_TARGET_SYSLOG] = "syslog",
1036 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1037 [LOG_TARGET_AUTO] = "auto",
1038 [LOG_TARGET_SAFE] = "safe",
1039 [LOG_TARGET_NULL] = "null"
1042 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1044 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1045 if (si->ssi_pid > 0) {
1046 _cleanup_free_ char *p = NULL;
1048 get_process_comm(si->ssi_pid, &p);
1051 "Received SIG%s from PID %"PRIu32" (%s).",
1052 signal_to_string(si->ssi_signo),
1053 si->ssi_pid, strna(p));
1057 signal_to_string(si->ssi_signo));
1061 void log_set_upgrade_syslog_to_journal(bool b) {
1062 upgrade_syslog_to_journal = b;