]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/log.c
dbus: more efficient implementation of properties
[thirdparty/systemd.git] / src / log.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 #include <stddef.h>
30
31 #include "log.h"
32 #include "util.h"
33 #include "macro.h"
34 #include "socket-util.h"
35
36 #define SOCKET_TIMEOUT_USEC (5*USEC_PER_SEC)
37
38 static LogTarget log_target = LOG_TARGET_CONSOLE;
39 static int log_max_level = LOG_INFO;
40
41 static int console_fd = STDERR_FILENO;
42 static int syslog_fd = -1;
43 static int kmsg_fd = -1;
44 static int journal_fd = -1;
45
46 static bool syslog_is_stream = false;
47
48 static bool show_color = false;
49 static bool show_location = false;
50
51 /* Akin to glibc's __abort_msg; which is private and we hence cannot
52 * use here. */
53 static char *log_abort_msg = NULL;
54
55 void log_close_console(void) {
56
57 if (console_fd < 0)
58 return;
59
60 if (getpid() == 1) {
61 if (console_fd >= 3)
62 close_nointr_nofail(console_fd);
63
64 console_fd = -1;
65 }
66 }
67
68 static int log_open_console(void) {
69
70 if (console_fd >= 0)
71 return 0;
72
73 if (getpid() == 1) {
74
75 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
76 if (console_fd < 0) {
77 log_error("Failed to open /dev/console for logging: %s", strerror(-console_fd));
78 return console_fd;
79 }
80
81 log_debug("Successfully opened /dev/console for logging.");
82 } else
83 console_fd = STDERR_FILENO;
84
85 return 0;
86 }
87
88 void log_close_kmsg(void) {
89
90 if (kmsg_fd < 0)
91 return;
92
93 close_nointr_nofail(kmsg_fd);
94 kmsg_fd = -1;
95 }
96
97 static int log_open_kmsg(void) {
98
99 if (kmsg_fd >= 0)
100 return 0;
101
102 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
103 if (kmsg_fd < 0) {
104 log_error("Failed to open /dev/kmsg for logging: %s", strerror(errno));
105 return -errno;
106 }
107
108 log_debug("Successfully opened /dev/kmsg for logging.");
109
110 return 0;
111 }
112
113 void log_close_syslog(void) {
114
115 if (syslog_fd < 0)
116 return;
117
118 close_nointr_nofail(syslog_fd);
119 syslog_fd = -1;
120 }
121
122 static int create_log_socket(int type) {
123 struct timeval tv;
124 int fd;
125
126 if (getpid() == 1)
127 /* systemd should not block on syslog */
128 type |= SOCK_NONBLOCK;
129
130 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
131 if (fd < 0)
132 return -errno;
133
134 /* Make sure we don't block for more than 5s when talking to
135 * syslog */
136 timeval_store(&tv, SOCKET_TIMEOUT_USEC);
137 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) < 0) {
138 close_nointr_nofail(fd);
139 return -errno;
140 }
141
142 return fd;
143 }
144
145 static int log_open_syslog(void) {
146 union sockaddr_union sa;
147 int r;
148
149 if (syslog_fd >= 0)
150 return 0;
151
152 zero(sa);
153 sa.un.sun_family = AF_UNIX;
154 strncpy(sa.un.sun_path, "/dev/log", sizeof(sa.un.sun_path));
155
156 syslog_fd = create_log_socket(SOCK_DGRAM);
157 if (syslog_fd < 0) {
158 r = syslog_fd;
159 goto fail;
160 }
161
162 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
163 close_nointr_nofail(syslog_fd);
164
165 /* Some legacy syslog systems still use stream
166 * sockets. They really shouldn't. But what can we
167 * do... */
168 syslog_fd = create_log_socket(SOCK_STREAM);
169 if (syslog_fd < 0) {
170 r = syslog_fd;
171 goto fail;
172 }
173
174 if (connect(syslog_fd, &sa.sa, sizeof(sa)) < 0) {
175 r = -errno;
176 goto fail;
177 }
178
179 syslog_is_stream = true;
180 } else
181 syslog_is_stream = false;
182
183 log_debug("Successfully opened syslog for logging.");
184
185 return 0;
186
187 fail:
188 log_close_syslog();
189 log_debug("Failed to open syslog for logging: %s", strerror(-r));
190 return r;
191 }
192
193 void log_close_journal(void) {
194
195 if (journal_fd < 0)
196 return;
197
198 close_nointr_nofail(journal_fd);
199 journal_fd = -1;
200 }
201
202 static int log_open_journal(void) {
203 union sockaddr_union sa;
204 int r;
205
206 if (journal_fd >= 0)
207 return 0;
208
209 journal_fd = create_log_socket(SOCK_DGRAM);
210 if (journal_fd < 0) {
211 r = journal_fd;
212 goto fail;
213 }
214
215 zero(sa);
216 sa.un.sun_family = AF_UNIX;
217 strncpy(sa.un.sun_path, "/run/systemd/journal/socket", sizeof(sa.un.sun_path));
218
219 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
220 r = -errno;
221 goto fail;
222 }
223
224 log_debug("Successfully opened journal for logging.");
225
226 return 0;
227
228 fail:
229 log_close_journal();
230 log_debug("Failed to open journal for logging: %s", strerror(-r));
231 return r;
232 }
233
234 int log_open(void) {
235 int r;
236
237 /* If we don't use the console we close it here, to not get
238 * killed by SAK. If we don't use syslog we close it here so
239 * that we are not confused by somebody deleting the socket in
240 * the fs. If we don't use /dev/kmsg we still keep it open,
241 * because there is no reason to close it. */
242
243 if (log_target == LOG_TARGET_NULL) {
244 log_close_journal();
245 log_close_syslog();
246 log_close_console();
247 return 0;
248 }
249
250 if (log_target != LOG_TARGET_AUTO ||
251 getpid() == 1 ||
252 isatty(STDERR_FILENO) <= 0) {
253
254 if (log_target == LOG_TARGET_AUTO ||
255 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
256 log_target == LOG_TARGET_JOURNAL) {
257 r = log_open_journal();
258 if (r >= 0) {
259 log_close_syslog();
260 log_close_console();
261 return r;
262 }
263 }
264
265 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
266 log_target == LOG_TARGET_SYSLOG) {
267 r = log_open_syslog();
268 if (r >= 0) {
269 log_close_journal();
270 log_close_console();
271 return r;
272 }
273 }
274
275 if (log_target == LOG_TARGET_AUTO ||
276 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
277 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
278 log_target == LOG_TARGET_KMSG) {
279 r = log_open_kmsg();
280 if (r >= 0) {
281 log_close_journal();
282 log_close_syslog();
283 log_close_console();
284 return r;
285 }
286 }
287 }
288
289 log_close_journal();
290 log_close_syslog();
291
292 /* Get the real /dev/console if we are PID=1, hence reopen */
293 log_close_console();
294 return log_open_console();
295 }
296
297 void log_set_target(LogTarget target) {
298 assert(target >= 0);
299 assert(target < _LOG_TARGET_MAX);
300
301 log_target = target;
302 }
303
304 void log_close(void) {
305 log_close_journal();
306 log_close_syslog();
307 log_close_kmsg();
308 log_close_console();
309 }
310
311 void log_forget_fds(void) {
312 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
313 }
314
315 void log_set_max_level(int level) {
316 assert((level & LOG_PRIMASK) == level);
317
318 log_max_level = level;
319 }
320
321 static int write_to_console(
322 int level,
323 const char*file,
324 int line,
325 const char *func,
326 const char *buffer) {
327
328 char location[64];
329 struct iovec iovec[5];
330 unsigned n = 0;
331 bool highlight;
332
333 if (console_fd < 0)
334 return 0;
335
336 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
337
338 zero(iovec);
339
340 if (show_location) {
341 snprintf(location, sizeof(location), "(%s:%u) ", file, line);
342 char_array_0(location);
343 IOVEC_SET_STRING(iovec[n++], location);
344 }
345
346 if (highlight)
347 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
348 IOVEC_SET_STRING(iovec[n++], buffer);
349 if (highlight)
350 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
351 IOVEC_SET_STRING(iovec[n++], "\n");
352
353 if (writev(console_fd, iovec, n) < 0)
354 return -errno;
355
356 return 1;
357 }
358
359 static int write_to_syslog(
360 int level,
361 const char*file,
362 int line,
363 const char *func,
364 const char *buffer) {
365
366 char header_priority[16], header_time[64], header_pid[16];
367 struct iovec iovec[5];
368 struct msghdr msghdr;
369 time_t t;
370 struct tm *tm;
371
372 if (syslog_fd < 0)
373 return 0;
374
375 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
376 char_array_0(header_priority);
377
378 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
379 if (!(tm = localtime(&t)))
380 return -EINVAL;
381
382 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
383 return -EINVAL;
384
385 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
386 char_array_0(header_pid);
387
388 zero(iovec);
389 IOVEC_SET_STRING(iovec[0], header_priority);
390 IOVEC_SET_STRING(iovec[1], header_time);
391 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
392 IOVEC_SET_STRING(iovec[3], header_pid);
393 IOVEC_SET_STRING(iovec[4], buffer);
394
395 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
396 if (syslog_is_stream)
397 iovec[4].iov_len++;
398
399 zero(msghdr);
400 msghdr.msg_iov = iovec;
401 msghdr.msg_iovlen = ELEMENTSOF(iovec);
402
403 for (;;) {
404 ssize_t n;
405
406 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
407 if (n < 0)
408 return -errno;
409
410 if (!syslog_is_stream ||
411 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
412 break;
413
414 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
415 }
416
417 return 1;
418 }
419
420 static int write_to_kmsg(
421 int level,
422 const char*file,
423 int line,
424 const char *func,
425 const char *buffer) {
426
427 char header_priority[16], header_pid[16];
428 struct iovec iovec[5];
429
430 if (kmsg_fd < 0)
431 return 0;
432
433 snprintf(header_priority, sizeof(header_priority), "<%i>", level);
434 char_array_0(header_priority);
435
436 snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
437 char_array_0(header_pid);
438
439 zero(iovec);
440 IOVEC_SET_STRING(iovec[0], header_priority);
441 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
442 IOVEC_SET_STRING(iovec[2], header_pid);
443 IOVEC_SET_STRING(iovec[3], buffer);
444 IOVEC_SET_STRING(iovec[4], "\n");
445
446 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
447 return -errno;
448
449 return 1;
450 }
451
452 static int write_to_journal(
453 int level,
454 const char*file,
455 int line,
456 const char *func,
457 const char *buffer) {
458
459 char header[LINE_MAX];
460 struct iovec iovec[3];
461 struct msghdr mh;
462
463 if (journal_fd < 0)
464 return 0;
465
466 snprintf(header, sizeof(header),
467 "PRIORITY=%i\n"
468 "CODE_FILE=%s\n"
469 "CODE_LINE=%i\n"
470 "CODE_FUNCTION=%s\n"
471 "MESSAGE=",
472 LOG_PRI(level),
473 file,
474 line,
475 func);
476
477 char_array_0(header);
478
479 zero(iovec);
480 IOVEC_SET_STRING(iovec[0], header);
481 IOVEC_SET_STRING(iovec[1], buffer);
482 IOVEC_SET_STRING(iovec[2], "\n");
483
484 zero(mh);
485 mh.msg_iov = iovec;
486 mh.msg_iovlen = ELEMENTSOF(iovec);
487
488 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
489 return -errno;
490
491 return 1;
492 }
493
494 static int log_dispatch(
495 int level,
496 const char*file,
497 int line,
498 const char *func,
499 char *buffer) {
500
501 int r = 0;
502
503 if (log_target == LOG_TARGET_NULL)
504 return 0;
505
506 /* Patch in LOG_DAEMON facility if necessary */
507 if ((level & LOG_FACMASK) == 0)
508 level = LOG_DAEMON | LOG_PRI(level);
509
510 do {
511 char *e;
512 int k = 0;
513
514 buffer += strspn(buffer, NEWLINE);
515
516 if (buffer[0] == 0)
517 break;
518
519 if ((e = strpbrk(buffer, NEWLINE)))
520 *(e++) = 0;
521
522 if (log_target == LOG_TARGET_AUTO ||
523 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
524 log_target == LOG_TARGET_JOURNAL) {
525
526 k = write_to_journal(level, file, line, func, buffer);
527 if (k < 0) {
528 if (k != -EAGAIN)
529 log_close_journal();
530 log_open_kmsg();
531 } else if (k > 0)
532 r++;
533 }
534
535 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
536 log_target == LOG_TARGET_SYSLOG) {
537
538 k = write_to_syslog(level, file, line, func, buffer);
539 if (k < 0) {
540 if (k != -EAGAIN)
541 log_close_syslog();
542 log_open_kmsg();
543 } else if (k > 0)
544 r++;
545 }
546
547 if (k <= 0 &&
548 (log_target == LOG_TARGET_AUTO ||
549 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
550 log_target == LOG_TARGET_KMSG)) {
551
552 k = write_to_kmsg(level, file, line, func, buffer);
553 if (k < 0) {
554 log_close_kmsg();
555 log_open_console();
556 } else if (k > 0)
557 r++;
558 }
559
560 if (k <= 0) {
561 k = write_to_console(level, file, line, func, buffer);
562 if (k < 0)
563 return k;
564 }
565
566 buffer = e;
567 } while (buffer);
568
569 return r;
570 }
571
572 int log_dump_internal(
573 int level,
574 const char*file,
575 int line,
576 const char *func,
577 char *buffer) {
578
579 int saved_errno, r;
580
581 /* This modifies the buffer... */
582
583 if (_likely_(LOG_PRI(level) > log_max_level))
584 return 0;
585
586 saved_errno = errno;
587 r = log_dispatch(level, file, line, func, buffer);
588 errno = saved_errno;
589
590 return r;
591 }
592
593 int log_meta(
594 int level,
595 const char*file,
596 int line,
597 const char *func,
598 const char *format, ...) {
599
600 char buffer[LINE_MAX];
601 int saved_errno, r;
602 va_list ap;
603
604 if (_likely_(LOG_PRI(level) > log_max_level))
605 return 0;
606
607 saved_errno = errno;
608
609 va_start(ap, format);
610 vsnprintf(buffer, sizeof(buffer), format, ap);
611 va_end(ap);
612
613 char_array_0(buffer);
614
615 r = log_dispatch(level, file, line, func, buffer);
616 errno = saved_errno;
617
618 return r;
619 }
620
621 void log_assert(
622 const char*file,
623 int line,
624 const char *func,
625 const char *format, ...) {
626
627 static char buffer[LINE_MAX];
628 int saved_errno = errno;
629 va_list ap;
630
631 va_start(ap, format);
632 vsnprintf(buffer, sizeof(buffer), format, ap);
633 va_end(ap);
634
635 char_array_0(buffer);
636 log_abort_msg = buffer;
637
638 log_dispatch(LOG_CRIT, file, line, func, buffer);
639 abort();
640
641 /* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
642 errno = saved_errno;
643 }
644
645 int log_set_target_from_string(const char *e) {
646 LogTarget t;
647
648 t = log_target_from_string(e);
649 if (t < 0)
650 return -EINVAL;
651
652 log_set_target(t);
653 return 0;
654 }
655
656 int log_set_max_level_from_string(const char *e) {
657 int t;
658
659 t = log_level_from_string(e);
660 if (t < 0)
661 return t;
662
663 log_set_max_level(t);
664 return 0;
665 }
666
667 void log_parse_environment(void) {
668 const char *e;
669
670 if ((e = getenv("SYSTEMD_LOG_TARGET")))
671 if (log_set_target_from_string(e) < 0)
672 log_warning("Failed to parse log target %s. Ignoring.", e);
673
674 if ((e = getenv("SYSTEMD_LOG_LEVEL")))
675 if (log_set_max_level_from_string(e) < 0)
676 log_warning("Failed to parse log level %s. Ignoring.", e);
677
678 if ((e = getenv("SYSTEMD_LOG_COLOR")))
679 if (log_show_color_from_string(e) < 0)
680 log_warning("Failed to parse bool %s. Ignoring.", e);
681
682 if ((e = getenv("SYSTEMD_LOG_LOCATION")))
683 if (log_show_location_from_string(e) < 0)
684 log_warning("Failed to parse bool %s. Ignoring.", e);
685 }
686
687 LogTarget log_get_target(void) {
688 return log_target;
689 }
690
691 int log_get_max_level(void) {
692 return log_max_level;
693 }
694
695 void log_show_color(bool b) {
696 show_color = b;
697 }
698
699 void log_show_location(bool b) {
700 show_location = b;
701 }
702
703 int log_show_color_from_string(const char *e) {
704 int t;
705
706 t = parse_boolean(e);
707 if (t < 0)
708 return t;
709
710 log_show_color(t);
711 return 0;
712 }
713
714 int log_show_location_from_string(const char *e) {
715 int t;
716
717 t = parse_boolean(e);
718 if (t < 0)
719 return t;
720
721 log_show_location(t);
722 return 0;
723 }
724
725 static const char *const log_target_table[] = {
726 [LOG_TARGET_CONSOLE] = "console",
727 [LOG_TARGET_KMSG] = "kmsg",
728 [LOG_TARGET_JOURNAL] = "journal",
729 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
730 [LOG_TARGET_SYSLOG] = "syslog",
731 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
732 [LOG_TARGET_AUTO] = "auto",
733 [LOG_TARGET_NULL] = "null"
734 };
735
736 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);