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