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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include <sys/socket.h>
34 #include "socket-util.h"
36 #define SNDBUF_SIZE (8*1024*1024)
38 static LogTarget log_target = LOG_TARGET_CONSOLE;
39 static int log_max_level = LOG_INFO;
41 static int console_fd = STDERR_FILENO;
42 static int syslog_fd = -1;
43 static int kmsg_fd = -1;
44 static int journal_fd = -1;
46 static bool syslog_is_stream = false;
48 static bool show_color = false;
49 static bool show_location = false;
51 /* Akin to glibc's __abort_msg; which is private and we hence cannot
53 static char *log_abort_msg = NULL;
55 void log_close_console(void) {
62 close_nointr_nofail(console_fd);
68 static int log_open_console(void) {
75 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
77 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
81 log_debug("Successfully opened /dev/console for logging.");
83 console_fd = STDERR_FILENO;
88 void log_close_kmsg(void) {
93 close_nointr_nofail(kmsg_fd);
97 static int log_open_kmsg(void) {
102 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
104 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
108 log_debug("Successfully opened /dev/kmsg for logging.");
113 void log_close_syslog(void) {
118 close_nointr_nofail(syslog_fd);
122 static int create_log_socket(int type) {
125 /* All output to the syslog/journal fds we do asynchronously,
126 * and if the buffers are full we just drop the messages */
128 fd = socket(AF_UNIX, type|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
132 fd_inc_sndbuf(fd, SNDBUF_SIZE);
137 static int log_open_syslog(void) {
138 union sockaddr_union sa;
145 sa.un.sun_family = AF_UNIX;
146 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
148 syslog_fd = create_log_socket(SOCK_DGRAM);
154 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 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, sizeof(sa)) < 0) {
171 syslog_is_stream = true;
173 syslog_is_stream = false;
175 log_debug("Successfully opened syslog for logging.");
181 log_debug("Failed to open syslog for logging: %s", strerror(-r));
185 void log_close_journal(void) {
190 close_nointr_nofail(journal_fd);
194 static int log_open_journal(void) {
195 union sockaddr_union sa;
201 journal_fd = create_log_socket(SOCK_DGRAM);
202 if (journal_fd < 0) {
208 sa.un.sun_family = AF_UNIX;
209 strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
211 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
216 log_debug("Successfully opened journal for logging.");
222 log_debug("Failed to open journal for logging: %s", strerror(-r));
229 /* If we don't use the console we close it here, to not get
230 * killed by SAK. If we don't use syslog we close it here so
231 * that we are not confused by somebody deleting the socket in
232 * the fs. If we don't use /dev/kmsg we still keep it open,
233 * because there is no reason to close it. */
235 if (log_target == LOG_TARGET_NULL) {
242 if (log_target != LOG_TARGET_AUTO ||
244 isatty(STDERR_FILENO) <= 0) {
246 if (log_target == LOG_TARGET_AUTO ||
247 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
248 log_target == LOG_TARGET_JOURNAL) {
249 r = log_open_journal();
257 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
258 log_target == LOG_TARGET_SYSLOG) {
259 r = log_open_syslog();
267 if (log_target == LOG_TARGET_AUTO ||
268 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
269 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
270 log_target == LOG_TARGET_KMSG) {
284 /* Get the real /dev/console if we are PID=1, hence reopen */
286 return log_open_console();
289 void log_set_target(LogTarget target) {
291 assert(target < _LOG_TARGET_MAX);
296 void log_close(void) {
303 void log_forget_fds(void) {
304 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
307 void log_set_max_level(int level) {
308 assert((level & LOG_PRIMASK) == level);
310 log_max_level = level;
313 static int write_to_console(
318 const char *buffer) {
321 struct iovec iovec[5];
328 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
333 snprintf(location, sizeof(location), "(%s:%u) ", 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)
351 static int write_to_syslog(
356 const char *buffer) {
358 char header_priority[16], header_time[64], header_pid[16];
359 struct iovec iovec[5];
360 struct msghdr msghdr;
367 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
368 char_array_0(header_priority);
370 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
371 if (!(tm = localtime(&t)))
374 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
377 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
378 char_array_0(header_pid);
381 IOVEC_SET_STRING(iovec[0], header_priority);
382 IOVEC_SET_STRING(iovec[1], header_time);
383 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
384 IOVEC_SET_STRING(iovec[3], header_pid);
385 IOVEC_SET_STRING(iovec[4], buffer);
387 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
388 if (syslog_is_stream)
392 msghdr.msg_iov = iovec;
393 msghdr.msg_iovlen = ELEMENTSOF(iovec);
398 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
402 if (!syslog_is_stream ||
403 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
406 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
412 static int write_to_kmsg(
417 const char *buffer) {
419 char header_priority[16], header_pid[16];
420 struct iovec iovec[5];
425 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
426 char_array_0(header_priority);
428 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
429 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 write_to_journal(
449 const char *buffer) {
451 char header[LINE_MAX];
452 struct iovec iovec[3];
458 snprintf(header, sizeof(header),
469 char_array_0(header);
472 IOVEC_SET_STRING(iovec[0], header);
473 IOVEC_SET_STRING(iovec[1], buffer);
474 IOVEC_SET_STRING(iovec[2], "\n");
478 mh.msg_iovlen = ELEMENTSOF(iovec);
480 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
486 static int log_dispatch(
495 if (log_target == LOG_TARGET_NULL)
498 /* Patch in LOG_DAEMON facility if necessary */
499 if ((level & LOG_FACMASK) == 0)
500 level = LOG_DAEMON | LOG_PRI(level);
506 buffer += strspn(buffer, NEWLINE);
511 if ((e = strpbrk(buffer, NEWLINE)))
514 if (log_target == LOG_TARGET_AUTO ||
515 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
516 log_target == LOG_TARGET_JOURNAL) {
518 k = write_to_journal(level, file, line, func, buffer);
527 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
528 log_target == LOG_TARGET_SYSLOG) {
530 k = write_to_syslog(level, file, line, func, buffer);
540 (log_target == LOG_TARGET_AUTO ||
541 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
542 log_target == LOG_TARGET_KMSG)) {
544 k = write_to_kmsg(level, file, line, func, buffer);
553 k = write_to_console(level, file, line, func, buffer);
564 int log_dump_internal(
573 /* This modifies the buffer... */
575 if (_likely_(LOG_PRI(level) > log_max_level))
579 r = log_dispatch(level, file, line, func, buffer);
590 const char *format, ...) {
592 char buffer[LINE_MAX];
596 if (_likely_(LOG_PRI(level) > log_max_level))
601 va_start(ap, format);
602 vsnprintf(buffer, sizeof(buffer), format, ap);
605 char_array_0(buffer);
607 r = log_dispatch(level, file, line, func, buffer);
613 #pragma GCC diagnostic push
614 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
615 _noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) {
616 static char buffer[LINE_MAX];
618 snprintf(buffer, sizeof(buffer), format, text, file, line, func);
620 char_array_0(buffer);
621 log_abort_msg = buffer;
623 log_dispatch(LOG_CRIT, file, line, func, buffer);
626 #pragma GCC diagnostic pop
628 _noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) {
629 log_assert(text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
632 _noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
633 log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
636 int log_set_target_from_string(const char *e) {
639 t = log_target_from_string(e);
647 int log_set_max_level_from_string(const char *e) {
650 t = log_level_from_string(e);
654 log_set_max_level(t);
658 void log_parse_environment(void) {
661 if ((e = getenv("SYSTEMD_LOG_TARGET")))
662 if (log_set_target_from_string(e) < 0)
663 log_warning("Failed to parse log target %s. Ignoring.", e);
665 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
666 if (log_set_max_level_from_string(e) < 0)
667 log_warning("Failed to parse log level %s. Ignoring.", e);
669 if ((e = getenv("SYSTEMD_LOG_COLOR")))
670 if (log_show_color_from_string(e) < 0)
671 log_warning("Failed to parse bool %s. Ignoring.", e);
673 if ((e = getenv("SYSTEMD_LOG_LOCATION")))
674 if (log_show_location_from_string(e) < 0)
675 log_warning("Failed to parse bool %s. Ignoring.", e);
678 LogTarget log_get_target(void) {
682 int log_get_max_level(void) {
683 return log_max_level;
686 void log_show_color(bool b) {
690 void log_show_location(bool b) {
694 int log_show_color_from_string(const char *e) {
697 t = parse_boolean(e);
705 int log_show_location_from_string(const char *e) {
708 t = parse_boolean(e);
712 log_show_location(t);
716 static const char *const log_target_table[] = {
717 [LOG_TARGET_CONSOLE] = "console",
718 [LOG_TARGET_KMSG] = "kmsg",
719 [LOG_TARGET_JOURNAL] = "journal",
720 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
721 [LOG_TARGET_SYSLOG] = "syslog",
722 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
723 [LOG_TARGET_AUTO] = "auto",
724 [LOG_TARGET_NULL] = "null"
727 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);