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 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
100 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
104 log_debug("Successfully opened /dev/kmsg for logging.");
109 void log_close_syslog(void) {
114 close_nointr_nofail(syslog_fd);
118 static int create_log_socket(int type) {
123 /* systemd should not block on syslog */
124 type |= SOCK_NONBLOCK;
125 if ((fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0)) < 0)
128 /* Make sure we don't block for more than 5s when talking to
130 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
131 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
132 close_nointr_nofail(fd);
139 static int log_open_syslog(void) {
142 struct sockaddr_un un;
150 sa.un.sun_family = AF_UNIX;
151 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
153 if ((syslog_fd = create_log_socket(SOCK_DGRAM)) < 0) {
158 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
159 close_nointr_nofail(syslog_fd);
161 /* Some legacy syslog systems still use stream
162 * sockets. They really shouldn't. But what can we
164 if ((syslog_fd = create_log_socket(SOCK_STREAM)) < 0) {
169 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
174 syslog_is_stream = true;
176 syslog_is_stream = false;
178 log_debug("Successfully opened syslog for logging.");
184 log_debug("Failed to open syslog for logging: %s", strerror(-r));
191 /* If we don't use the console we close it here, to not get
192 * killed by SAK. If we don't use syslog we close it here so
193 * that we are not confused by somebody deleting the socket in
194 * the fs. If we don't use /dev/kmsg we still keep it open,
195 * because there is no reason to close it. */
197 if (log_target == LOG_TARGET_NULL) {
203 if (log_target != LOG_TARGET_AUTO ||
205 isatty(STDERR_FILENO) <= 0) {
207 if (log_target == LOG_TARGET_AUTO ||
208 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
209 log_target == LOG_TARGET_SYSLOG)
210 if ((r = log_open_syslog()) >= 0) {
214 if (log_target == LOG_TARGET_AUTO ||
215 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
216 log_target == LOG_TARGET_KMSG)
217 if ((r = log_open_kmsg()) >= 0) {
226 /* Get the real /dev/console if we are PID=1, hence reopen */
228 return log_open_console();
231 void log_set_target(LogTarget target) {
233 assert(target < _LOG_TARGET_MAX);
238 void log_close(void) {
244 void log_forget_fds(void) {
245 console_fd = kmsg_fd = syslog_fd = -1;
248 void log_set_max_level(int level) {
249 assert((level & LOG_PRIMASK) == level);
251 log_max_level = level;
254 static int write_to_console(
259 const char *buffer) {
262 struct iovec iovec[5];
269 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
274 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
275 char_array_0(location);
276 IOVEC_SET_STRING(iovec[n++], location);
280 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
281 IOVEC_SET_STRING(iovec[n++], buffer);
283 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
284 IOVEC_SET_STRING(iovec[n++], "\n");
286 if (writev(console_fd, iovec, n) < 0)
292 static int write_to_syslog(
297 const char *buffer) {
299 char header_priority[16], header_time[64], header_pid[16];
300 struct iovec iovec[5];
301 struct msghdr msghdr;
308 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
309 char_array_0(header_priority);
311 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
312 if (!(tm = localtime(&t)))
315 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
318 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
319 char_array_0(header_pid);
322 IOVEC_SET_STRING(iovec[0], header_priority);
323 IOVEC_SET_STRING(iovec[1], header_time);
324 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
325 IOVEC_SET_STRING(iovec[3], header_pid);
326 IOVEC_SET_STRING(iovec[4], buffer);
328 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
329 if (syslog_is_stream)
333 msghdr.msg_iov = iovec;
334 msghdr.msg_iovlen = ELEMENTSOF(iovec);
339 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
343 if (!syslog_is_stream ||
344 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
347 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
353 static int write_to_kmsg(
358 const char *buffer) {
360 char header_priority[16], header_pid[16];
361 struct iovec iovec[5];
366 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
367 char_array_0(header_priority);
369 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
370 char_array_0(header_pid);
373 IOVEC_SET_STRING(iovec[0], header_priority);
374 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
375 IOVEC_SET_STRING(iovec[2], header_pid);
376 IOVEC_SET_STRING(iovec[3], buffer);
377 IOVEC_SET_STRING(iovec[4], "\n");
379 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
385 static int log_dispatch(
394 if (log_target == LOG_TARGET_NULL)
397 /* Patch in LOG_DAEMON facility if necessary */
398 if ((level & LOG_FACMASK) == 0)
399 level = LOG_DAEMON | LOG_PRI(level);
405 buffer += strspn(buffer, NEWLINE);
410 if ((e = strpbrk(buffer, NEWLINE)))
413 if (log_target == LOG_TARGET_AUTO ||
414 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
415 log_target == LOG_TARGET_SYSLOG) {
417 k = write_to_syslog(level, file, line, func, buffer);
427 (log_target == LOG_TARGET_AUTO ||
428 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
429 log_target == LOG_TARGET_KMSG)) {
431 k = write_to_kmsg(level, file, line, func, buffer);
440 k = write_to_console(level, file, line, func, buffer);
451 int log_dump_internal(
460 /* This modifies the buffer... */
462 if (_likely_(LOG_PRI(level) > log_max_level))
466 r = log_dispatch(level, file, line, func, buffer);
477 const char *format, ...) {
479 char buffer[LINE_MAX];
483 if (_likely_(LOG_PRI(level) > log_max_level))
488 va_start(ap, format);
489 vsnprintf(buffer, sizeof(buffer), format, ap);
492 char_array_0(buffer);
494 r = log_dispatch(level, file, line, func, buffer);
504 const char *format, ...) {
506 static char buffer[LINE_MAX];
507 int saved_errno = errno;
510 va_start(ap, format);
511 vsnprintf(buffer, sizeof(buffer), format, ap);
514 char_array_0(buffer);
515 log_abort_msg = buffer;
517 log_dispatch(LOG_CRIT, file, line, func, buffer);
520 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
524 int log_set_target_from_string(const char *e) {
527 if ((t = log_target_from_string(e)) < 0)
534 int log_set_max_level_from_string(const char *e) {
537 if ((t = log_level_from_string(e)) < 0)
540 log_set_max_level(t);
544 void log_parse_environment(void) {
547 if ((e = getenv("SYSTEMD_LOG_TARGET")))
548 if (log_set_target_from_string(e) < 0)
549 log_warning("Failed to parse log target %s. Ignoring.", e);
551 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
552 if (log_set_max_level_from_string(e) < 0)
553 log_warning("Failed to parse log level %s. Ignoring.", e);
555 if ((e = getenv("SYSTEMD_LOG_COLOR")))
556 if (log_show_color_from_string(e) < 0)
557 log_warning("Failed to parse bool %s. Ignoring.", e);
559 if ((e = getenv("SYSTEMD_LOG_LOCATION")))
560 if (log_show_location_from_string(e) < 0)
561 log_warning("Failed to parse bool %s. Ignoring.", e);
564 LogTarget log_get_target(void) {
568 int log_get_max_level(void) {
569 return log_max_level;
572 void log_show_color(bool b) {
576 void log_show_location(bool b) {
580 int log_show_color_from_string(const char *e) {
583 if ((t = parse_boolean(e)) < 0)
590 int log_show_location_from_string(const char *e) {
593 if ((t = parse_boolean(e)) < 0)
596 log_show_location(t);
600 static const char *const log_target_table[] = {
601 [LOG_TARGET_CONSOLE] = "console",
602 [LOG_TARGET_SYSLOG] = "syslog",
603 [LOG_TARGET_KMSG] = "kmsg",
604 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
605 [LOG_TARGET_NULL] = "null",
606 [LOG_TARGET_AUTO] = "auto"
609 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);