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 #define SYSLOG_TIMEOUT_USEC (5*USEC_PER_SEC)
36 static LogTarget log_target = LOG_TARGET_CONSOLE;
37 static int log_max_level = LOG_INFO;
39 static int console_fd = STDERR_FILENO;
40 static int syslog_fd = -1;
41 static int kmsg_fd = -1;
43 static bool syslog_is_stream = false;
45 static bool show_color = false;
46 static bool show_location = false;
48 /* Akin to glibc's __abort_msg; which is private and we hence cannot
50 static char *log_abort_msg = NULL;
52 void log_close_console(void) {
59 close_nointr_nofail(console_fd);
65 static int log_open_console(void) {
72 if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
73 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
77 log_debug("Successfully opened /dev/console for logging.");
79 console_fd = STDERR_FILENO;
84 void log_close_kmsg(void) {
89 close_nointr_nofail(kmsg_fd);
93 static int log_open_kmsg(void) {
98 if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
99 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
103 log_debug("Successfully opened /dev/kmsg for logging.");
108 void log_close_syslog(void) {
113 close_nointr_nofail(syslog_fd);
117 static int create_log_socket(int type) {
121 if ((fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0)) < 0)
124 /* Make sure we don't block for more than 5s when talking to
126 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
127 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
128 close_nointr_nofail(fd);
135 static int log_open_syslog(void) {
138 struct sockaddr_un un;
146 sa.un.sun_family = AF_UNIX;
147 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
149 if ((syslog_fd = create_log_socket(SOCK_DGRAM)) < 0) {
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 if ((syslog_fd = create_log_socket(SOCK_STREAM)) < 0) {
165 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
170 syslog_is_stream = true;
172 syslog_is_stream = false;
174 log_debug("Successfully opened syslog for logging.");
180 log_debug("Failed to open syslog for logging: %s", strerror(-r));
187 /* If we don't use the console we close it here, to not get
188 * killed by SAK. If we don't use syslog we close it here so
189 * that we are not confused by somebody deleting the socket in
190 * the fs. If we don't use /dev/kmsg we still keep it open,
191 * because there is no reason to close it. */
193 if (log_target == LOG_TARGET_NULL) {
199 if (log_target != LOG_TARGET_AUTO ||
201 isatty(STDERR_FILENO) <= 0) {
203 if (log_target == LOG_TARGET_AUTO ||
204 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
205 log_target == LOG_TARGET_SYSLOG)
206 if ((r = log_open_syslog()) >= 0) {
210 if (log_target == LOG_TARGET_AUTO ||
211 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
212 log_target == LOG_TARGET_KMSG)
213 if ((r = log_open_kmsg()) >= 0) {
222 /* Get the real /dev/console if we are PID=1, hence reopen */
224 return log_open_console();
227 void log_set_target(LogTarget target) {
229 assert(target < _LOG_TARGET_MAX);
234 void log_close(void) {
240 void log_set_max_level(int level) {
241 assert((level & LOG_PRIMASK) == level);
243 log_max_level = level;
246 static int write_to_console(
251 const char *buffer) {
254 struct iovec iovec[5];
261 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
262 char_array_0(location);
264 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
268 IOVEC_SET_STRING(iovec[n++], location);
270 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
271 IOVEC_SET_STRING(iovec[n++], buffer);
273 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
274 IOVEC_SET_STRING(iovec[n++], "\n");
276 if (writev(console_fd, iovec, n) < 0)
282 static int write_to_syslog(
287 const char *buffer) {
289 char header_priority[16], header_time[64], header_pid[16];
290 struct iovec iovec[5];
291 struct msghdr msghdr;
298 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
299 char_array_0(header_priority);
301 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
302 if (!(tm = localtime(&t)))
305 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
308 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
309 char_array_0(header_pid);
312 IOVEC_SET_STRING(iovec[0], header_priority);
313 IOVEC_SET_STRING(iovec[1], header_time);
314 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
315 IOVEC_SET_STRING(iovec[3], header_pid);
316 IOVEC_SET_STRING(iovec[4], buffer);
318 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
319 if (syslog_is_stream)
323 msghdr.msg_iov = iovec;
324 msghdr.msg_iovlen = ELEMENTSOF(iovec);
329 if ((n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL)) < 0)
332 if (!syslog_is_stream ||
333 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
336 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
342 static int write_to_kmsg(
347 const char *buffer) {
349 char header_priority[16], header_pid[16];
350 struct iovec iovec[5];
355 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
356 char_array_0(header_priority);
358 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
359 char_array_0(header_pid);
362 IOVEC_SET_STRING(iovec[0], header_priority);
363 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
364 IOVEC_SET_STRING(iovec[2], header_pid);
365 IOVEC_SET_STRING(iovec[3], buffer);
366 IOVEC_SET_STRING(iovec[4], "\n");
368 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
374 static int log_dispatch(
383 if (log_target == LOG_TARGET_NULL)
386 /* Patch in LOG_DAEMON facility if necessary */
387 if ((level & LOG_FACMASK) == 0)
388 level = LOG_DAEMON | LOG_PRI(level);
394 buffer += strspn(buffer, NEWLINE);
399 if ((e = strpbrk(buffer, NEWLINE)))
402 if (log_target == LOG_TARGET_AUTO ||
403 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
404 log_target == LOG_TARGET_SYSLOG) {
406 if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
414 (log_target == LOG_TARGET_AUTO ||
415 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
416 log_target == LOG_TARGET_KMSG)) {
418 if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
426 (k = write_to_console(level, file, line, func, buffer)) < 0)
435 int log_dump_internal(
444 /* This modifies the buffer... */
446 if (_likely_(LOG_PRI(level) > log_max_level))
450 r = log_dispatch(level, file, line, func, buffer);
461 const char *format, ...) {
463 char buffer[LINE_MAX];
467 if (_likely_(LOG_PRI(level) > log_max_level))
472 va_start(ap, format);
473 vsnprintf(buffer, sizeof(buffer), format, ap);
476 char_array_0(buffer);
478 r = log_dispatch(level, file, line, func, buffer);
488 const char *format, ...) {
490 static char buffer[LINE_MAX];
491 int saved_errno = errno;
494 va_start(ap, format);
495 vsnprintf(buffer, sizeof(buffer), format, ap);
498 char_array_0(buffer);
499 log_abort_msg = buffer;
501 log_dispatch(LOG_CRIT, file, line, func, buffer);
504 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
508 int log_set_target_from_string(const char *e) {
511 if ((t = log_target_from_string(e)) < 0)
518 int log_set_max_level_from_string(const char *e) {
521 if ((t = log_level_from_string(e)) < 0)
524 log_set_max_level(t);
528 void log_parse_environment(void) {
531 if ((e = getenv("SYSTEMD_LOG_TARGET")))
532 if (log_set_target_from_string(e) < 0)
533 log_warning("Failed to parse log target %s. Ignoring.", e);
535 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
536 if (log_set_max_level_from_string(e) < 0)
537 log_warning("Failed to parse log level %s. Ignoring.", e);
539 if ((e = getenv("SYSTEMD_LOG_COLOR")))
540 if (log_show_color_from_string(e) < 0)
541 log_warning("Failed to parse bool %s. Ignoring.", e);
543 if ((e = getenv("SYSTEMD_LOG_LOCATION")))
544 if (log_show_location_from_string(e) < 0)
545 log_warning("Failed to parse bool %s. Ignoring.", e);
548 LogTarget log_get_target(void) {
552 int log_get_max_level(void) {
553 return log_max_level;
556 void log_show_color(bool b) {
560 void log_show_location(bool b) {
564 int log_show_color_from_string(const char *e) {
567 if ((t = parse_boolean(e)) < 0)
574 int log_show_location_from_string(const char *e) {
577 if ((t = parse_boolean(e)) < 0)
580 log_show_location(t);
584 static const char *const log_target_table[] = {
585 [LOG_TARGET_CONSOLE] = "console",
586 [LOG_TARGET_SYSLOG] = "syslog",
587 [LOG_TARGET_KMSG] = "kmsg",
588 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
589 [LOG_TARGET_NULL] = "null",
590 [LOG_TARGET_AUTO] = "auto"
593 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);