]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.c
Merge pull request #2424 from keszybz/journald-disk-usage
[thirdparty/systemd.git] / src / basic / 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 Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <limits.h>
26 #include <stdarg.h>
27 #include <stddef.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <sys/signalfd.h>
31 #include <sys/socket.h>
32 #include <sys/time.h>
33 #include <sys/uio.h>
34 #include <sys/un.h>
35 #include <time.h>
36 #include <unistd.h>
37
38 #include "sd-messages.h"
39
40 #include "alloc-util.h"
41 #include "fd-util.h"
42 #include "formats-util.h"
43 #include "io-util.h"
44 #include "log.h"
45 #include "macro.h"
46 #include "missing.h"
47 #include "parse-util.h"
48 #include "proc-cmdline.h"
49 #include "process-util.h"
50 #include "signal-util.h"
51 #include "socket-util.h"
52 #include "stdio-util.h"
53 #include "string-table.h"
54 #include "string-util.h"
55 #include "syslog-util.h"
56 #include "terminal-util.h"
57 #include "time-util.h"
58 #include "util.h"
59
60 #define SNDBUF_SIZE (8*1024*1024)
61
62 static LogTarget log_target = LOG_TARGET_CONSOLE;
63 static int log_max_level = LOG_INFO;
64 static int log_facility = LOG_DAEMON;
65
66 static int console_fd = STDERR_FILENO;
67 static int syslog_fd = -1;
68 static int kmsg_fd = -1;
69 static int journal_fd = -1;
70
71 static bool syslog_is_stream = false;
72
73 static bool show_color = false;
74 static bool show_location = false;
75
76 static bool upgrade_syslog_to_journal = false;
77
78 /* Akin to glibc's __abort_msg; which is private and we hence cannot
79 * use here. */
80 static char *log_abort_msg = NULL;
81
82 void log_close_console(void) {
83
84 if (console_fd < 0)
85 return;
86
87 if (getpid() == 1) {
88 if (console_fd >= 3)
89 safe_close(console_fd);
90
91 console_fd = -1;
92 }
93 }
94
95 static int log_open_console(void) {
96
97 if (console_fd >= 0)
98 return 0;
99
100 if (getpid() == 1) {
101 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
102 if (console_fd < 0)
103 return console_fd;
104 } else
105 console_fd = STDERR_FILENO;
106
107 return 0;
108 }
109
110 void log_close_kmsg(void) {
111 kmsg_fd = safe_close(kmsg_fd);
112 }
113
114 static int log_open_kmsg(void) {
115
116 if (kmsg_fd >= 0)
117 return 0;
118
119 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
120 if (kmsg_fd < 0)
121 return -errno;
122
123 return 0;
124 }
125
126 void log_close_syslog(void) {
127 syslog_fd = safe_close(syslog_fd);
128 }
129
130 static int create_log_socket(int type) {
131 struct timeval tv;
132 int fd;
133
134 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
135 if (fd < 0)
136 return -errno;
137
138 fd_inc_sndbuf(fd, SNDBUF_SIZE);
139
140 /* We need a blocking fd here since we'd otherwise lose
141 messages way too early. However, let's not hang forever in the
142 unlikely case of a deadlock. */
143 if (getpid() == 1)
144 timeval_store(&tv, 10 * USEC_PER_MSEC);
145 else
146 timeval_store(&tv, 10 * USEC_PER_SEC);
147 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
148
149 return fd;
150 }
151
152 static int log_open_syslog(void) {
153
154 static const union sockaddr_union sa = {
155 .un.sun_family = AF_UNIX,
156 .un.sun_path = "/dev/log",
157 };
158
159 int r;
160
161 if (syslog_fd >= 0)
162 return 0;
163
164 syslog_fd = create_log_socket(SOCK_DGRAM);
165 if (syslog_fd < 0) {
166 r = syslog_fd;
167 goto fail;
168 }
169
170 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
171 safe_close(syslog_fd);
172
173 /* Some legacy syslog systems still use stream
174 * sockets. They really shouldn't. But what can we
175 * do... */
176 syslog_fd = create_log_socket(SOCK_STREAM);
177 if (syslog_fd < 0) {
178 r = syslog_fd;
179 goto fail;
180 }
181
182 if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
183 r = -errno;
184 goto fail;
185 }
186
187 syslog_is_stream = true;
188 } else
189 syslog_is_stream = false;
190
191 return 0;
192
193 fail:
194 log_close_syslog();
195 return r;
196 }
197
198 void log_close_journal(void) {
199 journal_fd = safe_close(journal_fd);
200 }
201
202 static int log_open_journal(void) {
203
204 static const union sockaddr_union sa = {
205 .un.sun_family = AF_UNIX,
206 .un.sun_path = "/run/systemd/journal/socket",
207 };
208
209 int r;
210
211 if (journal_fd >= 0)
212 return 0;
213
214 journal_fd = create_log_socket(SOCK_DGRAM);
215 if (journal_fd < 0) {
216 r = journal_fd;
217 goto fail;
218 }
219
220 if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
221 r = -errno;
222 goto fail;
223 }
224
225 return 0;
226
227 fail:
228 log_close_journal();
229 return r;
230 }
231
232 int log_open(void) {
233 int r;
234
235 /* If we don't use the console we close it here, to not get
236 * killed by SAK. If we don't use syslog we close it here so
237 * that we are not confused by somebody deleting the socket in
238 * the fs. If we don't use /dev/kmsg we still keep it open,
239 * because there is no reason to close it. */
240
241 if (log_target == LOG_TARGET_NULL) {
242 log_close_journal();
243 log_close_syslog();
244 log_close_console();
245 return 0;
246 }
247
248 if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
249 getpid() == 1 ||
250 isatty(STDERR_FILENO) <= 0) {
251
252 if (log_target == LOG_TARGET_AUTO ||
253 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
254 log_target == LOG_TARGET_JOURNAL) {
255 r = log_open_journal();
256 if (r >= 0) {
257 log_close_syslog();
258 log_close_console();
259 return r;
260 }
261 }
262
263 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
264 log_target == LOG_TARGET_SYSLOG) {
265 r = log_open_syslog();
266 if (r >= 0) {
267 log_close_journal();
268 log_close_console();
269 return r;
270 }
271 }
272
273 if (log_target == LOG_TARGET_AUTO ||
274 log_target == LOG_TARGET_SAFE ||
275 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
276 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
277 log_target == LOG_TARGET_KMSG) {
278 r = log_open_kmsg();
279 if (r >= 0) {
280 log_close_journal();
281 log_close_syslog();
282 log_close_console();
283 return r;
284 }
285 }
286 }
287
288 log_close_journal();
289 log_close_syslog();
290
291 return log_open_console();
292 }
293
294 void log_set_target(LogTarget target) {
295 assert(target >= 0);
296 assert(target < _LOG_TARGET_MAX);
297
298 if (upgrade_syslog_to_journal) {
299 if (target == LOG_TARGET_SYSLOG)
300 target = LOG_TARGET_JOURNAL;
301 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
302 target = LOG_TARGET_JOURNAL_OR_KMSG;
303 }
304
305 log_target = target;
306 }
307
308 void log_close(void) {
309 log_close_journal();
310 log_close_syslog();
311 log_close_kmsg();
312 log_close_console();
313 }
314
315 void log_forget_fds(void) {
316 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
317 }
318
319 void log_set_max_level(int level) {
320 assert((level & LOG_PRIMASK) == level);
321
322 log_max_level = level;
323 }
324
325 void log_set_facility(int facility) {
326 log_facility = facility;
327 }
328
329 static int write_to_console(
330 int level,
331 int error,
332 const char *file,
333 int line,
334 const char *func,
335 const char *object_field,
336 const char *object,
337 const char *buffer) {
338
339 char location[64], prefix[1 + DECIMAL_STR_MAX(int) + 2];
340 struct iovec iovec[6] = {};
341 unsigned n = 0;
342 bool highlight;
343
344 if (console_fd < 0)
345 return 0;
346
347 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
348 sprintf(prefix, "<%i>", level);
349 IOVEC_SET_STRING(iovec[n++], prefix);
350 }
351
352 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
353
354 if (show_location) {
355 xsprintf(location, "(%s:%i) ", file, line);
356 IOVEC_SET_STRING(iovec[n++], location);
357 }
358
359 if (highlight)
360 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED);
361 IOVEC_SET_STRING(iovec[n++], buffer);
362 if (highlight)
363 IOVEC_SET_STRING(iovec[n++], ANSI_NORMAL);
364 IOVEC_SET_STRING(iovec[n++], "\n");
365
366 if (writev(console_fd, iovec, n) < 0) {
367
368 if (errno == EIO && getpid() == 1) {
369
370 /* If somebody tried to kick us from our
371 * console tty (via vhangup() or suchlike),
372 * try to reconnect */
373
374 log_close_console();
375 log_open_console();
376
377 if (console_fd < 0)
378 return 0;
379
380 if (writev(console_fd, iovec, n) < 0)
381 return -errno;
382 } else
383 return -errno;
384 }
385
386 return 1;
387 }
388
389 static int write_to_syslog(
390 int level,
391 int error,
392 const char *file,
393 int line,
394 const char *func,
395 const char *object_field,
396 const char *object,
397 const char *buffer) {
398
399 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
400 header_time[64],
401 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
402 struct iovec iovec[5] = {};
403 struct msghdr msghdr = {
404 .msg_iov = iovec,
405 .msg_iovlen = ELEMENTSOF(iovec),
406 };
407 time_t t;
408 struct tm *tm;
409
410 if (syslog_fd < 0)
411 return 0;
412
413 xsprintf(header_priority, "<%i>", level);
414
415 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
416 tm = localtime(&t);
417 if (!tm)
418 return -EINVAL;
419
420 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
421 return -EINVAL;
422
423 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
424
425 IOVEC_SET_STRING(iovec[0], header_priority);
426 IOVEC_SET_STRING(iovec[1], header_time);
427 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
428 IOVEC_SET_STRING(iovec[3], header_pid);
429 IOVEC_SET_STRING(iovec[4], buffer);
430
431 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
432 if (syslog_is_stream)
433 iovec[4].iov_len++;
434
435 for (;;) {
436 ssize_t n;
437
438 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
439 if (n < 0)
440 return -errno;
441
442 if (!syslog_is_stream ||
443 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
444 break;
445
446 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
447 }
448
449 return 1;
450 }
451
452 static int write_to_kmsg(
453 int level,
454 int error,
455 const char *file,
456 int line,
457 const char *func,
458 const char *object_field,
459 const char *object,
460 const char *buffer) {
461
462 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
463 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
464 struct iovec iovec[5] = {};
465
466 if (kmsg_fd < 0)
467 return 0;
468
469 xsprintf(header_priority, "<%i>", level);
470 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
471
472 IOVEC_SET_STRING(iovec[0], header_priority);
473 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
474 IOVEC_SET_STRING(iovec[2], header_pid);
475 IOVEC_SET_STRING(iovec[3], buffer);
476 IOVEC_SET_STRING(iovec[4], "\n");
477
478 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
479 return -errno;
480
481 return 1;
482 }
483
484 static int log_do_header(
485 char *header,
486 size_t size,
487 int level,
488 int error,
489 const char *file, int line, const char *func,
490 const char *object_field, const char *object) {
491
492 snprintf(header, size,
493 "PRIORITY=%i\n"
494 "SYSLOG_FACILITY=%i\n"
495 "%s%s%s"
496 "%s%.*i%s"
497 "%s%s%s"
498 "%s%.*i%s"
499 "%s%s%s"
500 "SYSLOG_IDENTIFIER=%s\n",
501 LOG_PRI(level),
502 LOG_FAC(level),
503 isempty(file) ? "" : "CODE_FILE=",
504 isempty(file) ? "" : file,
505 isempty(file) ? "" : "\n",
506 line ? "CODE_LINE=" : "",
507 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
508 line ? "\n" : "",
509 isempty(func) ? "" : "CODE_FUNCTION=",
510 isempty(func) ? "" : func,
511 isempty(func) ? "" : "\n",
512 error ? "ERRNO=" : "",
513 error ? 1 : 0, error,
514 error ? "\n" : "",
515 isempty(object) ? "" : object_field,
516 isempty(object) ? "" : object,
517 isempty(object) ? "" : "\n",
518 program_invocation_short_name);
519
520 return 0;
521 }
522
523 static int write_to_journal(
524 int level,
525 int error,
526 const char *file,
527 int line,
528 const char *func,
529 const char *object_field,
530 const char *object,
531 const char *buffer) {
532
533 char header[LINE_MAX];
534 struct iovec iovec[4] = {};
535 struct msghdr mh = {};
536
537 if (journal_fd < 0)
538 return 0;
539
540 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object);
541
542 IOVEC_SET_STRING(iovec[0], header);
543 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
544 IOVEC_SET_STRING(iovec[2], buffer);
545 IOVEC_SET_STRING(iovec[3], "\n");
546
547 mh.msg_iov = iovec;
548 mh.msg_iovlen = ELEMENTSOF(iovec);
549
550 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
551 return -errno;
552
553 return 1;
554 }
555
556 static int log_dispatch(
557 int level,
558 int error,
559 const char *file,
560 int line,
561 const char *func,
562 const char *object_field,
563 const char *object,
564 char *buffer) {
565
566 assert(buffer);
567
568 if (log_target == LOG_TARGET_NULL)
569 return -error;
570
571 /* Patch in LOG_DAEMON facility if necessary */
572 if ((level & LOG_FACMASK) == 0)
573 level = log_facility | LOG_PRI(level);
574
575 if (error < 0)
576 error = -error;
577
578 do {
579 char *e;
580 int k = 0;
581
582 buffer += strspn(buffer, NEWLINE);
583
584 if (buffer[0] == 0)
585 break;
586
587 if ((e = strpbrk(buffer, NEWLINE)))
588 *(e++) = 0;
589
590 if (log_target == LOG_TARGET_AUTO ||
591 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
592 log_target == LOG_TARGET_JOURNAL) {
593
594 k = write_to_journal(level, error, file, line, func, object_field, object, buffer);
595 if (k < 0) {
596 if (k != -EAGAIN)
597 log_close_journal();
598 log_open_kmsg();
599 }
600 }
601
602 if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
603 log_target == LOG_TARGET_SYSLOG) {
604
605 k = write_to_syslog(level, error, file, line, func, object_field, object, buffer);
606 if (k < 0) {
607 if (k != -EAGAIN)
608 log_close_syslog();
609 log_open_kmsg();
610 }
611 }
612
613 if (k <= 0 &&
614 (log_target == LOG_TARGET_AUTO ||
615 log_target == LOG_TARGET_SAFE ||
616 log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
617 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
618 log_target == LOG_TARGET_KMSG)) {
619
620 k = write_to_kmsg(level, error, file, line, func, object_field, object, buffer);
621 if (k < 0) {
622 log_close_kmsg();
623 log_open_console();
624 }
625 }
626
627 if (k <= 0)
628 (void) write_to_console(level, error, file, line, func, object_field, object, buffer);
629
630 buffer = e;
631 } while (buffer);
632
633 return -error;
634 }
635
636 int log_dump_internal(
637 int level,
638 int error,
639 const char *file,
640 int line,
641 const char *func,
642 char *buffer) {
643
644 PROTECT_ERRNO;
645
646 /* This modifies the buffer... */
647
648 if (error < 0)
649 error = -error;
650
651 if (_likely_(LOG_PRI(level) > log_max_level))
652 return -error;
653
654 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
655 }
656
657 int log_internalv(
658 int level,
659 int error,
660 const char *file,
661 int line,
662 const char *func,
663 const char *format,
664 va_list ap) {
665
666 PROTECT_ERRNO;
667 char buffer[LINE_MAX];
668
669 if (error < 0)
670 error = -error;
671
672 if (_likely_(LOG_PRI(level) > log_max_level))
673 return -error;
674
675 /* Make sure that %m maps to the specified error */
676 if (error != 0)
677 errno = error;
678
679 vsnprintf(buffer, sizeof(buffer), format, ap);
680
681 return log_dispatch(level, error, file, line, func, NULL, NULL, buffer);
682 }
683
684 int log_internal(
685 int level,
686 int error,
687 const char *file,
688 int line,
689 const char *func,
690 const char *format, ...) {
691
692 va_list ap;
693 int r;
694
695 va_start(ap, format);
696 r = log_internalv(level, error, file, line, func, format, ap);
697 va_end(ap);
698
699 return r;
700 }
701
702 int log_object_internalv(
703 int level,
704 int error,
705 const char *file,
706 int line,
707 const char *func,
708 const char *object_field,
709 const char *object,
710 const char *format,
711 va_list ap) {
712
713 PROTECT_ERRNO;
714 char *buffer, *b;
715 size_t l;
716
717 if (error < 0)
718 error = -error;
719
720 if (_likely_(LOG_PRI(level) > log_max_level))
721 return -error;
722
723 /* Make sure that %m maps to the specified error */
724 if (error != 0)
725 errno = error;
726
727 /* Prepend the object name before the message */
728 if (object) {
729 size_t n;
730
731 n = strlen(object);
732 l = n + 2 + LINE_MAX;
733
734 buffer = newa(char, l);
735 b = stpcpy(stpcpy(buffer, object), ": ");
736 } else {
737 l = LINE_MAX;
738 b = buffer = newa(char, l);
739 }
740
741 vsnprintf(b, l, format, ap);
742
743 return log_dispatch(level, error, file, line, func, object_field, object, buffer);
744 }
745
746 int log_object_internal(
747 int level,
748 int error,
749 const char *file,
750 int line,
751 const char *func,
752 const char *object_field,
753 const char *object,
754 const char *format, ...) {
755
756 va_list ap;
757 int r;
758
759 va_start(ap, format);
760 r = log_object_internalv(level, error, file, line, func, object_field, object, format, ap);
761 va_end(ap);
762
763 return r;
764 }
765
766 static void log_assert(
767 int level,
768 const char *text,
769 const char *file,
770 int line,
771 const char *func,
772 const char *format) {
773
774 static char buffer[LINE_MAX];
775
776 if (_likely_(LOG_PRI(level) > log_max_level))
777 return;
778
779 DISABLE_WARNING_FORMAT_NONLITERAL;
780 xsprintf(buffer, format, text, file, line, func);
781 REENABLE_WARNING;
782
783 log_abort_msg = buffer;
784
785 log_dispatch(level, 0, file, line, func, NULL, NULL, buffer);
786 }
787
788 noreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
789 log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
790 abort();
791 }
792
793 noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
794 log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
795 abort();
796 }
797
798 void log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
799 PROTECT_ERRNO;
800 log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
801 }
802
803 int log_oom_internal(const char *file, int line, const char *func) {
804 log_internal(LOG_ERR, ENOMEM, file, line, func, "Out of memory.");
805 return -ENOMEM;
806 }
807
808 int log_format_iovec(
809 struct iovec *iovec,
810 unsigned iovec_len,
811 unsigned *n,
812 bool newline_separator,
813 int error,
814 const char *format,
815 va_list ap) {
816
817 static const char nl = '\n';
818
819 while (format && *n + 1 < iovec_len) {
820 va_list aq;
821 char *m;
822 int r;
823
824 /* We need to copy the va_list structure,
825 * since vasprintf() leaves it afterwards at
826 * an undefined location */
827
828 if (error != 0)
829 errno = error;
830
831 va_copy(aq, ap);
832 r = vasprintf(&m, format, aq);
833 va_end(aq);
834 if (r < 0)
835 return -EINVAL;
836
837 /* Now, jump enough ahead, so that we point to
838 * the next format string */
839 VA_FORMAT_ADVANCE(format, ap);
840
841 IOVEC_SET_STRING(iovec[(*n)++], m);
842
843 if (newline_separator) {
844 iovec[*n].iov_base = (char*) &nl;
845 iovec[*n].iov_len = 1;
846 (*n)++;
847 }
848
849 format = va_arg(ap, char *);
850 }
851 return 0;
852 }
853
854 int log_struct_internal(
855 int level,
856 int error,
857 const char *file,
858 int line,
859 const char *func,
860 const char *format, ...) {
861
862 char buf[LINE_MAX];
863 bool found = false;
864 PROTECT_ERRNO;
865 va_list ap;
866
867 if (error < 0)
868 error = -error;
869
870 if (_likely_(LOG_PRI(level) > log_max_level))
871 return -error;
872
873 if (log_target == LOG_TARGET_NULL)
874 return -error;
875
876 if ((level & LOG_FACMASK) == 0)
877 level = log_facility | LOG_PRI(level);
878
879 if ((log_target == LOG_TARGET_AUTO ||
880 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
881 log_target == LOG_TARGET_JOURNAL) &&
882 journal_fd >= 0) {
883 char header[LINE_MAX];
884 struct iovec iovec[17] = {};
885 unsigned n = 0, i;
886 int r;
887 struct msghdr mh = {
888 .msg_iov = iovec,
889 };
890 bool fallback = false;
891
892 /* If the journal is available do structured logging */
893 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
894 IOVEC_SET_STRING(iovec[n++], header);
895
896 va_start(ap, format);
897 r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap);
898 if (r < 0)
899 fallback = true;
900 else {
901 mh.msg_iovlen = n;
902 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
903 }
904
905 va_end(ap);
906 for (i = 1; i < n; i += 2)
907 free(iovec[i].iov_base);
908
909 if (!fallback)
910 return -error;
911 }
912
913 /* Fallback if journal logging is not available or didn't work. */
914
915 va_start(ap, format);
916 while (format) {
917 va_list aq;
918
919 if (error != 0)
920 errno = error;
921
922 va_copy(aq, ap);
923 vsnprintf(buf, sizeof(buf), format, aq);
924 va_end(aq);
925
926 if (startswith(buf, "MESSAGE=")) {
927 found = true;
928 break;
929 }
930
931 VA_FORMAT_ADVANCE(format, ap);
932
933 format = va_arg(ap, char *);
934 }
935 va_end(ap);
936
937 if (!found)
938 return -error;
939
940 return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
941 }
942
943 int log_set_target_from_string(const char *e) {
944 LogTarget t;
945
946 t = log_target_from_string(e);
947 if (t < 0)
948 return -EINVAL;
949
950 log_set_target(t);
951 return 0;
952 }
953
954 int log_set_max_level_from_string(const char *e) {
955 int t;
956
957 t = log_level_from_string(e);
958 if (t < 0)
959 return -EINVAL;
960
961 log_set_max_level(t);
962 return 0;
963 }
964
965 static int parse_proc_cmdline_item(const char *key, const char *value) {
966
967 /*
968 * The systemd.log_xyz= settings are parsed by all tools, and
969 * so is "debug".
970 *
971 * However, "quiet" is only parsed by PID 1, and only turns of
972 * status output to /dev/console, but does not alter the log
973 * level.
974 */
975
976 if (streq(key, "debug") && !value)
977 log_set_max_level(LOG_DEBUG);
978
979 else if (streq(key, "systemd.log_target") && value) {
980
981 if (log_set_target_from_string(value) < 0)
982 log_warning("Failed to parse log target '%s'. Ignoring.", value);
983
984 } else if (streq(key, "systemd.log_level") && value) {
985
986 if (log_set_max_level_from_string(value) < 0)
987 log_warning("Failed to parse log level '%s'. Ignoring.", value);
988
989 } else if (streq(key, "systemd.log_color") && value) {
990
991 if (log_show_color_from_string(value) < 0)
992 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
993
994 } else if (streq(key, "systemd.log_location") && value) {
995
996 if (log_show_location_from_string(value) < 0)
997 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
998 }
999
1000 return 0;
1001 }
1002
1003 void log_parse_environment(void) {
1004 const char *e;
1005
1006 if (get_ctty_devnr(0, NULL) < 0)
1007 /* Only try to read the command line in daemons.
1008 We assume that anything that has a controlling
1009 tty is user stuff. */
1010 (void) parse_proc_cmdline(parse_proc_cmdline_item);
1011
1012 e = secure_getenv("SYSTEMD_LOG_TARGET");
1013 if (e && log_set_target_from_string(e) < 0)
1014 log_warning("Failed to parse log target '%s'. Ignoring.", e);
1015
1016 e = secure_getenv("SYSTEMD_LOG_LEVEL");
1017 if (e && log_set_max_level_from_string(e) < 0)
1018 log_warning("Failed to parse log level '%s'. Ignoring.", e);
1019
1020 e = secure_getenv("SYSTEMD_LOG_COLOR");
1021 if (e && log_show_color_from_string(e) < 0)
1022 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1023
1024 e = secure_getenv("SYSTEMD_LOG_LOCATION");
1025 if (e && log_show_location_from_string(e) < 0)
1026 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1027 }
1028
1029 LogTarget log_get_target(void) {
1030 return log_target;
1031 }
1032
1033 int log_get_max_level(void) {
1034 return log_max_level;
1035 }
1036
1037 void log_show_color(bool b) {
1038 show_color = b;
1039 }
1040
1041 bool log_get_show_color(void) {
1042 return show_color;
1043 }
1044
1045 void log_show_location(bool b) {
1046 show_location = b;
1047 }
1048
1049 bool log_get_show_location(void) {
1050 return show_location;
1051 }
1052
1053 int log_show_color_from_string(const char *e) {
1054 int t;
1055
1056 t = parse_boolean(e);
1057 if (t < 0)
1058 return t;
1059
1060 log_show_color(t);
1061 return 0;
1062 }
1063
1064 int log_show_location_from_string(const char *e) {
1065 int t;
1066
1067 t = parse_boolean(e);
1068 if (t < 0)
1069 return t;
1070
1071 log_show_location(t);
1072 return 0;
1073 }
1074
1075 bool log_on_console(void) {
1076 if (log_target == LOG_TARGET_CONSOLE ||
1077 log_target == LOG_TARGET_CONSOLE_PREFIXED)
1078 return true;
1079
1080 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1081 }
1082
1083 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1084 [LOG_TARGET_CONSOLE] = "console",
1085 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1086 [LOG_TARGET_KMSG] = "kmsg",
1087 [LOG_TARGET_JOURNAL] = "journal",
1088 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1089 [LOG_TARGET_SYSLOG] = "syslog",
1090 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1091 [LOG_TARGET_AUTO] = "auto",
1092 [LOG_TARGET_SAFE] = "safe",
1093 [LOG_TARGET_NULL] = "null"
1094 };
1095
1096 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1097
1098 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1099 if (si->ssi_pid > 0) {
1100 _cleanup_free_ char *p = NULL;
1101
1102 get_process_comm(si->ssi_pid, &p);
1103
1104 log_full(level,
1105 "Received SIG%s from PID %"PRIu32" (%s).",
1106 signal_to_string(si->ssi_signo),
1107 si->ssi_pid, strna(p));
1108 } else
1109 log_full(level,
1110 "Received SIG%s.",
1111 signal_to_string(si->ssi_signo));
1112
1113 }
1114
1115 void log_set_upgrade_syslog_to_journal(bool b) {
1116 upgrade_syslog_to_journal = b;
1117 }
1118
1119 int log_syntax_internal(
1120 const char *unit,
1121 int level,
1122 const char *config_file,
1123 unsigned config_line,
1124 int error,
1125 const char *file,
1126 int line,
1127 const char *func,
1128 const char *format, ...) {
1129
1130 PROTECT_ERRNO;
1131 char buffer[LINE_MAX];
1132 int r;
1133 va_list ap;
1134
1135 if (error < 0)
1136 error = -error;
1137
1138 if (_likely_(LOG_PRI(level) > log_max_level))
1139 return -error;
1140
1141 if (log_target == LOG_TARGET_NULL)
1142 return -error;
1143
1144 if (error != 0)
1145 errno = error;
1146
1147 va_start(ap, format);
1148 vsnprintf(buffer, sizeof(buffer), format, ap);
1149 va_end(ap);
1150
1151 if (unit)
1152 r = log_struct_internal(
1153 level, error,
1154 file, line, func,
1155 getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit,
1156 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
1157 "CONFIG_FILE=%s", config_file,
1158 "CONFIG_LINE=%u", config_line,
1159 LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
1160 NULL);
1161 else
1162 r = log_struct_internal(
1163 level, error,
1164 file, line, func,
1165 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
1166 "CONFIG_FILE=%s", config_file,
1167 "CONFIG_LINE=%u", config_line,
1168 LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
1169 NULL);
1170
1171 return r;
1172 }