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 show_color = false;
44 static bool show_location = false;
46 /* Akin to glibc's __abort_msg; which is private and we hance cannot
48 static char *log_abort_msg = NULL;
50 void log_close_console(void) {
57 close_nointr_nofail(console_fd);
63 static int log_open_console(void) {
70 if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
71 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
75 log_debug("Succesfully opened /dev/console for logging.");
77 console_fd = STDERR_FILENO;
82 void log_close_kmsg(void) {
87 close_nointr_nofail(kmsg_fd);
91 static int log_open_kmsg(void) {
96 if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
97 log_info("Failed to open /dev/kmsg for logging: %s", strerror(errno));
101 log_debug("Succesfully opened /dev/kmsg for logging.");
106 void log_close_syslog(void) {
111 close_nointr_nofail(syslog_fd);
115 static int log_open_syslog(void) {
118 struct sockaddr_un un;
126 if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
131 /* Make sure we don't block for more than 5s when talking to
133 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
134 if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
140 sa.un.sun_family = AF_UNIX;
141 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
143 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
148 log_debug("Succesfully opened syslog for logging.");
154 log_info("Failed to open syslog for logging: %s", strerror(-r));
161 /* If we don't use the console we close it here, to not get
162 * killed by SAK. If we don't use syslog we close it here so
163 * that we are not confused by somebody deleting the socket in
164 * the fs. If we don't use /dev/kmsg we still keep it open,
165 * because there is no reason to close it. */
167 if (log_target == LOG_TARGET_NULL) {
173 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
174 log_target == LOG_TARGET_SYSLOG)
175 if ((r = log_open_syslog()) >= 0) {
180 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
181 log_target == LOG_TARGET_KMSG)
182 if ((r = log_open_kmsg()) >= 0) {
189 return log_open_console();
192 void log_set_target(LogTarget target) {
194 assert(target < _LOG_TARGET_MAX);
199 void log_set_max_level(int level) {
200 assert((level & LOG_PRIMASK) == level);
202 log_max_level = level;
205 static int write_to_console(
210 const char *buffer) {
213 struct iovec iovec[5];
220 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
221 char_array_0(location);
223 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
227 IOVEC_SET_STRING(iovec[n++], location);
229 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
230 IOVEC_SET_STRING(iovec[n++], buffer);
232 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
233 IOVEC_SET_STRING(iovec[n++], "\n");
235 if (writev(console_fd, iovec, n) < 0)
241 static int write_to_syslog(
246 const char *buffer) {
248 char header_priority[16], header_time[64], header_pid[16];
249 struct iovec iovec[5];
250 struct msghdr msghdr;
257 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
258 char_array_0(header_priority);
260 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
261 if (!(tm = localtime(&t)))
264 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
267 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
268 char_array_0(header_pid);
271 IOVEC_SET_STRING(iovec[0], header_priority);
272 IOVEC_SET_STRING(iovec[1], header_time);
273 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
274 IOVEC_SET_STRING(iovec[3], header_pid);
275 IOVEC_SET_STRING(iovec[4], buffer);
278 msghdr.msg_iov = iovec;
279 msghdr.msg_iovlen = ELEMENTSOF(iovec);
281 if (sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
287 static int write_to_kmsg(
292 const char *buffer) {
294 char header_priority[16], header_pid[16];
295 struct iovec iovec[5];
300 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
301 char_array_0(header_priority);
303 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
304 char_array_0(header_pid);
307 IOVEC_SET_STRING(iovec[0], header_priority);
308 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
309 IOVEC_SET_STRING(iovec[2], header_pid);
310 IOVEC_SET_STRING(iovec[3], buffer);
311 IOVEC_SET_STRING(iovec[4], "\n");
313 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
319 static int log_dispatch(
328 if (log_target == LOG_TARGET_NULL)
335 buffer += strspn(buffer, NEWLINE);
340 if ((e = strpbrk(buffer, NEWLINE)))
343 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
344 log_target == LOG_TARGET_SYSLOG) {
346 if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
354 (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
355 log_target == LOG_TARGET_KMSG)) {
357 if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
365 (k = write_to_console(level, file, line, func, buffer)) < 0)
374 int log_dump_internal(
383 /* This modifies the buffer... */
385 if (_likely_(LOG_PRI(level) > log_max_level))
389 r = log_dispatch(level, file, line, func, buffer);
400 const char *format, ...) {
402 char buffer[LINE_MAX];
406 if (_likely_(LOG_PRI(level) > log_max_level))
411 va_start(ap, format);
412 vsnprintf(buffer, sizeof(buffer), format, ap);
415 char_array_0(buffer);
417 r = log_dispatch(level, file, line, func, buffer);
427 const char *format, ...) {
429 static char buffer[LINE_MAX];
430 int saved_errno = errno;
433 va_start(ap, format);
434 vsnprintf(buffer, sizeof(buffer), format, ap);
437 char_array_0(buffer);
438 log_abort_msg = buffer;
440 log_dispatch(LOG_CRIT, file, line, func, buffer);
443 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
447 int log_set_target_from_string(const char *e) {
450 if ((t = log_target_from_string(e)) < 0)
457 int log_set_max_level_from_string(const char *e) {
460 if ((t = log_level_from_string(e)) < 0)
463 log_set_max_level(t);
467 void log_parse_environment(void) {
470 if ((e = getenv("SYSTEMD_LOG_TARGET")))
471 if (log_set_target_from_string(e) < 0)
472 log_warning("Failed to parse log target %s. Ignoring.", e);
474 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
475 if (log_set_max_level_from_string(e) < 0)
476 log_warning("Failed to parse log level %s. Ignoring.", e);
478 if ((e = getenv("SYSTEMD_LOG_COLOR")))
479 if (log_show_color_from_string(e) < 0)
480 log_warning("Failed to parse bool %s. Ignoring.", e);
482 if ((e = getenv("SYSTEMD_LOG_LOCATION"))) {
483 if (log_show_location_from_string(e) < 0)
484 log_warning("Failed to parse bool %s. Ignoring.", e);
488 LogTarget log_get_target(void) {
492 int log_get_max_level(void) {
493 return log_max_level;
496 void log_show_color(bool b) {
500 void log_show_location(bool b) {
504 int log_show_color_from_string(const char *e) {
507 if ((t = parse_boolean(e)) < 0)
514 int log_show_location_from_string(const char *e) {
517 if ((t = parse_boolean(e)) < 0)
520 log_show_location(t);
524 static const char *const log_target_table[] = {
525 [LOG_TARGET_CONSOLE] = "console",
526 [LOG_TARGET_SYSLOG] = "syslog",
527 [LOG_TARGET_KMSG] = "kmsg",
528 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
529 [LOG_TARGET_NULL] = "null"
532 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);