1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
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)
35 #define LOG_BUFFER_MAX 1024
37 static LogTarget log_target = LOG_TARGET_CONSOLE;
38 static int log_max_level = LOG_DEBUG;
40 static int console_fd = STDERR_FILENO;
41 static int syslog_fd = -1;
42 static int kmsg_fd = -1;
44 /* Akin to glibc's __abort_msg; which is private and we hance cannot
46 static char *log_abort_msg = NULL;
48 void log_close_console(void) {
55 close_nointr_nofail(console_fd);
61 static int log_open_console(void) {
68 if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
69 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
73 log_info("Succesfully opened /dev/console for logging.");
75 console_fd = STDERR_FILENO;
80 void log_close_kmsg(void) {
85 close_nointr_nofail(kmsg_fd);
89 static int log_open_kmsg(void) {
94 if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
95 log_info("Failed to open /dev/kmsg for logging: %s", strerror(errno));
99 log_info("Succesfully opened /dev/kmsg for logging.");
104 void log_close_syslog(void) {
109 close_nointr_nofail(syslog_fd);
113 static int log_open_syslog(void) {
116 struct sockaddr_un un;
124 if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
129 /* Make sure we don't block for more than 5s when talking to
131 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
132 if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
138 sa.un.sun_family = AF_UNIX;
139 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
141 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
146 log_info("Succesfully opened syslog for logging.");
152 log_info("Failed to open syslog for logging: %s", strerror(-r));
159 /* If we don't use the console we close it here, to not get
160 * killed by SAK. If we don't use syslog we close it here so
161 * that we are not confused by somebody deleting the socket in
162 * the fs. If we don't use /dev/kmsg we still keep it open,
163 * because there is no reason to close it. */
165 if (log_target == LOG_TARGET_NULL) {
171 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
172 log_target == LOG_TARGET_SYSLOG)
173 if ((r = log_open_syslog()) >= 0) {
178 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
179 log_target == LOG_TARGET_KMSG)
180 if ((r = log_open_kmsg()) >= 0) {
187 return log_open_console();
190 void log_set_target(LogTarget target) {
192 assert(target < _LOG_TARGET_MAX);
197 void log_set_max_level(int level) {
198 assert((level & LOG_PRIMASK) == level);
200 log_max_level = level;
203 static int write_to_console(
208 const char *buffer) {
211 struct iovec iovec[5];
218 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
219 char_array_0(location);
221 highlight = LOG_PRI(level) <= LOG_ERR;
224 IOVEC_SET_STRING(iovec[n++], location);
226 IOVEC_SET_STRING(iovec[n++], "\x1B[1;31m");
227 IOVEC_SET_STRING(iovec[n++], buffer);
229 IOVEC_SET_STRING(iovec[n++], "\x1B[0m");
230 IOVEC_SET_STRING(iovec[n++], "\n");
232 if (writev(console_fd, iovec, n) < 0)
238 static int write_to_syslog(
243 const char *buffer) {
245 char header_priority[16], header_time[64], header_pid[16];
246 struct iovec iovec[5];
247 struct msghdr msghdr;
254 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
255 char_array_0(header_priority);
257 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
258 if (!(tm = localtime(&t)))
261 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
264 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
265 char_array_0(header_pid);
268 IOVEC_SET_STRING(iovec[0], header_priority);
269 IOVEC_SET_STRING(iovec[1], header_time);
270 IOVEC_SET_STRING(iovec[2], __progname);
271 IOVEC_SET_STRING(iovec[3], header_pid);
272 IOVEC_SET_STRING(iovec[4], buffer);
275 msghdr.msg_iov = iovec;
276 msghdr.msg_iovlen = ELEMENTSOF(iovec);
278 if (sendmsg(syslog_fd, &msghdr, 0) < 0)
284 static int write_to_kmsg(
289 const char *buffer) {
291 char header_priority[16], header_pid[16];
292 struct iovec iovec[5];
297 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
298 char_array_0(header_priority);
300 snprintf(header_pid, sizeof(header_pid), "[%llu]: ", (unsigned long long) getpid());
301 char_array_0(header_pid);
304 IOVEC_SET_STRING(iovec[0], header_priority);
305 IOVEC_SET_STRING(iovec[1], __progname);
306 IOVEC_SET_STRING(iovec[2], header_pid);
307 IOVEC_SET_STRING(iovec[3], buffer);
308 IOVEC_SET_STRING(iovec[4], "\n");
310 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
316 static int log_dispatch(
325 if (log_target == LOG_TARGET_NULL)
332 buffer += strspn(buffer, NEWLINE);
337 if ((e = strpbrk(buffer, NEWLINE)))
340 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
341 log_target == LOG_TARGET_SYSLOG) {
343 if ((r = write_to_syslog(level, file, line, func, buffer)) < 0) {
350 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
351 log_target == LOG_TARGET_KMSG) {
353 if ((r = write_to_kmsg(level, file, line, func, buffer)) < 0) {
360 if ((k = write_to_console(level, file, line, func, buffer)) < 0)
369 int log_dump_internal(
378 /* This modifies the buffer... */
380 if (_likely_(LOG_PRI(level) > log_max_level))
384 r = log_dispatch(level, file, line, func, buffer);
395 const char *format, ...) {
397 char buffer[LOG_BUFFER_MAX];
401 if (_likely_(LOG_PRI(level) > log_max_level))
406 va_start(ap, format);
407 vsnprintf(buffer, sizeof(buffer), format, ap);
410 char_array_0(buffer);
412 r = log_dispatch(level, file, line, func, buffer);
422 const char *format, ...) {
424 static char buffer[LOG_BUFFER_MAX];
425 int saved_errno = errno;
428 va_start(ap, format);
429 vsnprintf(buffer, sizeof(buffer), format, ap);
432 char_array_0(buffer);
433 log_abort_msg = buffer;
435 log_dispatch(LOG_CRIT, file, line, func, buffer);
438 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
442 int log_set_target_from_string(const char *e) {
445 if ((t = log_target_from_string(e)) < 0)
452 int log_set_max_level_from_string(const char *e) {
455 if ((t = log_level_from_string(e)) < 0)
458 log_set_max_level(t);
462 void log_parse_environment(void) {
465 if ((e = getenv("SYSTEMD_LOG_TARGET")))
466 if (log_set_target_from_string(e) < 0)
467 log_warning("Failed to parse log target %s. Ignoring.", e);
469 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
470 if (log_set_max_level_from_string(e) < 0)
471 log_warning("Failed to parse log level %s. Ignoring.", e);
474 LogTarget log_get_target(void) {
478 int log_get_max_level(void) {
479 return log_max_level;
482 static const char *const log_target_table[] = {
483 [LOG_TARGET_CONSOLE] = "console",
484 [LOG_TARGET_SYSLOG] = "syslog",
485 [LOG_TARGET_KMSG] = "kmsg",
486 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
487 [LOG_TARGET_NULL] = "null"
490 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);