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)
35 #define LOG_BUFFER_MAX 1024
37 static LogTarget log_target = LOG_TARGET_CONSOLE;
38 static int log_max_level = LOG_INFO;
40 static int console_fd = STDERR_FILENO;
41 static int syslog_fd = -1;
42 static int kmsg_fd = -1;
44 static bool show_color = false;
45 static bool show_location = false;
47 /* Akin to glibc's __abort_msg; which is private and we hance cannot
49 static char *log_abort_msg = NULL;
51 void log_close_console(void) {
58 close_nointr_nofail(console_fd);
64 static int log_open_console(void) {
71 if ((console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
72 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
76 log_debug("Succesfully opened /dev/console for logging.");
78 console_fd = STDERR_FILENO;
83 void log_close_kmsg(void) {
88 close_nointr_nofail(kmsg_fd);
92 static int log_open_kmsg(void) {
97 if ((kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) {
98 log_info("Failed to open /dev/kmsg for logging: %s", strerror(errno));
102 log_debug("Succesfully opened /dev/kmsg for logging.");
107 void log_close_syslog(void) {
112 close_nointr_nofail(syslog_fd);
116 static int log_open_syslog(void) {
119 struct sockaddr_un un;
127 if ((syslog_fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
132 /* Make sure we don't block for more than 5s when talking to
134 timeval_store(&tv, SYSLOG_TIMEOUT_USEC);
135 if (setsockopt(syslog_fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
141 sa.un.sun_family = AF_UNIX;
142 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
144 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
149 log_debug("Succesfully opened syslog for logging.");
155 log_info("Failed to open syslog for logging: %s", strerror(-r));
162 /* If we don't use the console we close it here, to not get
163 * killed by SAK. If we don't use syslog we close it here so
164 * that we are not confused by somebody deleting the socket in
165 * the fs. If we don't use /dev/kmsg we still keep it open,
166 * because there is no reason to close it. */
168 if (log_target == LOG_TARGET_NULL) {
174 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
175 log_target == LOG_TARGET_SYSLOG)
176 if ((r = log_open_syslog()) >= 0) {
181 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
182 log_target == LOG_TARGET_KMSG)
183 if ((r = log_open_kmsg()) >= 0) {
190 return log_open_console();
193 void log_set_target(LogTarget target) {
195 assert(target < _LOG_TARGET_MAX);
200 void log_set_max_level(int level) {
201 assert((level & LOG_PRIMASK) == level);
203 log_max_level = level;
206 static int write_to_console(
211 const char *buffer) {
214 struct iovec iovec[5];
221 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
222 char_array_0(location);
224 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
228 IOVEC_SET_STRING(iovec[n++], location);
230 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_ON);
231 IOVEC_SET_STRING(iovec[n++], buffer);
233 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
234 IOVEC_SET_STRING(iovec[n++], "\n");
236 if (writev(console_fd, iovec, n) < 0)
242 static int write_to_syslog(
247 const char *buffer) {
249 char header_priority[16], header_time[64], header_pid[16];
250 struct iovec iovec[5];
251 struct msghdr msghdr;
253 struct cmsghdr cmsghdr;
254 uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
263 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
264 char_array_0(header_priority);
266 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
267 if (!(tm = localtime(&t)))
270 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
273 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
274 char_array_0(header_pid);
277 IOVEC_SET_STRING(iovec[0], header_priority);
278 IOVEC_SET_STRING(iovec[1], header_time);
279 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
280 IOVEC_SET_STRING(iovec[3], header_pid);
281 IOVEC_SET_STRING(iovec[4], buffer);
284 control.cmsghdr.cmsg_level = SOL_SOCKET;
285 control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
286 control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
288 ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
289 ucred->pid = getpid();
290 ucred->uid = getuid();
291 ucred->gid = getgid();
294 msghdr.msg_iov = iovec;
295 msghdr.msg_iovlen = ELEMENTSOF(iovec);
296 msghdr.msg_control = &control;
297 msghdr.msg_controllen = control.cmsghdr.cmsg_len;
299 if (sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL) < 0)
305 static int write_to_kmsg(
310 const char *buffer) {
312 char header_priority[16], header_pid[16];
313 struct iovec iovec[5];
318 snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_PRI(level));
319 char_array_0(header_priority);
321 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
322 char_array_0(header_pid);
325 IOVEC_SET_STRING(iovec[0], header_priority);
326 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
327 IOVEC_SET_STRING(iovec[2], header_pid);
328 IOVEC_SET_STRING(iovec[3], buffer);
329 IOVEC_SET_STRING(iovec[4], "\n");
331 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
337 static int log_dispatch(
346 if (log_target == LOG_TARGET_NULL)
353 buffer += strspn(buffer, NEWLINE);
358 if ((e = strpbrk(buffer, NEWLINE)))
361 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
362 log_target == LOG_TARGET_SYSLOG) {
364 if ((k = write_to_syslog(level, file, line, func, buffer)) < 0) {
372 (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
373 log_target == LOG_TARGET_KMSG)) {
375 if ((k = write_to_kmsg(level, file, line, func, buffer)) < 0) {
383 (k = write_to_console(level, file, line, func, buffer)) < 0)
392 int log_dump_internal(
401 /* This modifies the buffer... */
403 if (_likely_(LOG_PRI(level) > log_max_level))
407 r = log_dispatch(level, file, line, func, buffer);
418 const char *format, ...) {
420 char buffer[LOG_BUFFER_MAX];
424 if (_likely_(LOG_PRI(level) > log_max_level))
429 va_start(ap, format);
430 vsnprintf(buffer, sizeof(buffer), format, ap);
433 char_array_0(buffer);
435 r = log_dispatch(level, file, line, func, buffer);
445 const char *format, ...) {
447 static char buffer[LOG_BUFFER_MAX];
448 int saved_errno = errno;
451 va_start(ap, format);
452 vsnprintf(buffer, sizeof(buffer), format, ap);
455 char_array_0(buffer);
456 log_abort_msg = buffer;
458 log_dispatch(LOG_CRIT, file, line, func, buffer);
461 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
465 int log_set_target_from_string(const char *e) {
468 if ((t = log_target_from_string(e)) < 0)
475 int log_set_max_level_from_string(const char *e) {
478 if ((t = log_level_from_string(e)) < 0)
481 log_set_max_level(t);
485 void log_parse_environment(void) {
488 if ((e = getenv("SYSTEMD_LOG_TARGET")))
489 if (log_set_target_from_string(e) < 0)
490 log_warning("Failed to parse log target %s. Ignoring.", e);
492 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
493 if (log_set_max_level_from_string(e) < 0)
494 log_warning("Failed to parse log level %s. Ignoring.", e);
496 if ((e = getenv("SYSTEMD_LOG_COLOR")))
497 if (log_show_color_from_string(e) < 0)
498 log_warning("Failed to parse bool %s. Ignoring.", e);
500 if ((e = getenv("SYSTEMD_LOG_LOCATION"))) {
501 if (log_show_location_from_string(e) < 0)
502 log_warning("Failed to parse bool %s. Ignoring.", e);
506 LogTarget log_get_target(void) {
510 int log_get_max_level(void) {
511 return log_max_level;
514 void log_show_color(bool b) {
518 void log_show_location(bool b) {
522 int log_show_color_from_string(const char *e) {
525 if ((t = parse_boolean(e)) < 0)
532 int log_show_location_from_string(const char *e) {
535 if ((t = parse_boolean(e)) < 0)
538 log_show_location(t);
542 static const char *const log_target_table[] = {
543 [LOG_TARGET_CONSOLE] = "console",
544 [LOG_TARGET_SYSLOG] = "syslog",
545 [LOG_TARGET_KMSG] = "kmsg",
546 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
547 [LOG_TARGET_NULL] = "null"
550 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);