]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.c
log: pass the correct length to vsnprintf (#6168)
[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, LOG_INFO};
62 assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX);
63 static int log_facility = LOG_DAEMON;
64
65 static int console_fd = STDERR_FILENO;
66 static int syslog_fd = -1;
67 static int kmsg_fd = -1;
68 static int journal_fd = -1;
69
70 static bool syslog_is_stream = false;
71
72 static bool show_color = false;
73 static bool show_location = false;
74
75 static bool upgrade_syslog_to_journal = false;
76 static bool always_reopen_console = 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 (always_reopen_console) {
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 (void) 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, SOCKADDR_UN_LEN(sa.un)) < 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, SOCKADDR_UN_LEN(sa.un)) < 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, SOCKADDR_UN_LEN(sa.un)) < 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 (!IN_SET(log_target, LOG_TARGET_AUTO, LOG_TARGET_SAFE) ||
249 getpid() == 1 ||
250 isatty(STDERR_FILENO) <= 0) {
251
252 if (IN_SET(log_target, LOG_TARGET_AUTO,
253 LOG_TARGET_JOURNAL_OR_KMSG,
254 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 (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
264 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 (IN_SET(log_target, LOG_TARGET_AUTO,
274 LOG_TARGET_SAFE,
275 LOG_TARGET_JOURNAL_OR_KMSG,
276 LOG_TARGET_SYSLOG_OR_KMSG,
277 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_realm(LogRealm realm, int level) {
320 assert((level & LOG_PRIMASK) == level);
321 assert(realm < ELEMENTSOF(log_max_level));
322
323 log_max_level[realm] = level;
324 }
325
326 void log_set_facility(int facility) {
327 log_facility = facility;
328 }
329
330 static int write_to_console(
331 int level,
332 int error,
333 const char *file,
334 int line,
335 const char *func,
336 const char *buffer) {
337
338 char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
339 struct iovec iovec[6] = {};
340 unsigned n = 0;
341 bool highlight;
342
343 if (console_fd < 0)
344 return 0;
345
346 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
347 xsprintf(prefix, "<%i>", level);
348 IOVEC_SET_STRING(iovec[n++], prefix);
349 }
350
351 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
352
353 if (show_location) {
354 snprintf(location, sizeof(location), "(%s:%i) ", file, line);
355 IOVEC_SET_STRING(iovec[n++], location);
356 }
357
358 if (highlight)
359 IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED);
360 IOVEC_SET_STRING(iovec[n++], buffer);
361 if (highlight)
362 IOVEC_SET_STRING(iovec[n++], ANSI_NORMAL);
363 IOVEC_SET_STRING(iovec[n++], "\n");
364
365 if (writev(console_fd, iovec, n) < 0) {
366
367 if (errno == EIO && getpid() == 1) {
368
369 /* If somebody tried to kick us from our
370 * console tty (via vhangup() or suchlike),
371 * try to reconnect */
372
373 log_close_console();
374 log_open_console();
375
376 if (console_fd < 0)
377 return 0;
378
379 if (writev(console_fd, iovec, n) < 0)
380 return -errno;
381 } else
382 return -errno;
383 }
384
385 return 1;
386 }
387
388 static int write_to_syslog(
389 int level,
390 int error,
391 const char *file,
392 int line,
393 const char *func,
394 const char *buffer) {
395
396 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
397 header_time[64],
398 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
399 struct iovec iovec[5] = {};
400 struct msghdr msghdr = {
401 .msg_iov = iovec,
402 .msg_iovlen = ELEMENTSOF(iovec),
403 };
404 time_t t;
405 struct tm *tm;
406
407 if (syslog_fd < 0)
408 return 0;
409
410 xsprintf(header_priority, "<%i>", level);
411
412 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
413 tm = localtime(&t);
414 if (!tm)
415 return -EINVAL;
416
417 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
418 return -EINVAL;
419
420 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
421
422 IOVEC_SET_STRING(iovec[0], header_priority);
423 IOVEC_SET_STRING(iovec[1], header_time);
424 IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
425 IOVEC_SET_STRING(iovec[3], header_pid);
426 IOVEC_SET_STRING(iovec[4], buffer);
427
428 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
429 if (syslog_is_stream)
430 iovec[4].iov_len++;
431
432 for (;;) {
433 ssize_t n;
434
435 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
436 if (n < 0)
437 return -errno;
438
439 if (!syslog_is_stream ||
440 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
441 break;
442
443 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
444 }
445
446 return 1;
447 }
448
449 static int write_to_kmsg(
450 int level,
451 int error,
452 const char *file,
453 int line,
454 const char *func,
455 const char *buffer) {
456
457 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
458 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
459 struct iovec iovec[5] = {};
460
461 if (kmsg_fd < 0)
462 return 0;
463
464 xsprintf(header_priority, "<%i>", level);
465 xsprintf(header_pid, "["PID_FMT"]: ", getpid());
466
467 IOVEC_SET_STRING(iovec[0], header_priority);
468 IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
469 IOVEC_SET_STRING(iovec[2], header_pid);
470 IOVEC_SET_STRING(iovec[3], buffer);
471 IOVEC_SET_STRING(iovec[4], "\n");
472
473 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
474 return -errno;
475
476 return 1;
477 }
478
479 static int log_do_header(
480 char *header,
481 size_t size,
482 int level,
483 int error,
484 const char *file, int line, const char *func,
485 const char *object_field, const char *object,
486 const char *extra_field, const char *extra) {
487
488 snprintf(header, size,
489 "PRIORITY=%i\n"
490 "SYSLOG_FACILITY=%i\n"
491 "%s%s%s"
492 "%s%.*i%s"
493 "%s%s%s"
494 "%s%.*i%s"
495 "%s%s%s"
496 "%s%s%s"
497 "SYSLOG_IDENTIFIER=%s\n",
498 LOG_PRI(level),
499 LOG_FAC(level),
500 isempty(file) ? "" : "CODE_FILE=",
501 isempty(file) ? "" : file,
502 isempty(file) ? "" : "\n",
503 line ? "CODE_LINE=" : "",
504 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
505 line ? "\n" : "",
506 isempty(func) ? "" : "CODE_FUNC=",
507 isempty(func) ? "" : func,
508 isempty(func) ? "" : "\n",
509 error ? "ERRNO=" : "",
510 error ? 1 : 0, error,
511 error ? "\n" : "",
512 isempty(object) ? "" : object_field,
513 isempty(object) ? "" : object,
514 isempty(object) ? "" : "\n",
515 isempty(extra) ? "" : extra_field,
516 isempty(extra) ? "" : extra,
517 isempty(extra) ? "" : "\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 *extra_field,
532 const char *extra,
533 const char *buffer) {
534
535 char header[LINE_MAX];
536 struct iovec iovec[4] = {};
537 struct msghdr mh = {};
538
539 if (journal_fd < 0)
540 return 0;
541
542 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
543
544 IOVEC_SET_STRING(iovec[0], header);
545 IOVEC_SET_STRING(iovec[1], "MESSAGE=");
546 IOVEC_SET_STRING(iovec[2], buffer);
547 IOVEC_SET_STRING(iovec[3], "\n");
548
549 mh.msg_iov = iovec;
550 mh.msg_iovlen = ELEMENTSOF(iovec);
551
552 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
553 return -errno;
554
555 return 1;
556 }
557
558 int log_dispatch_internal(
559 int level,
560 int error,
561 const char *file,
562 int line,
563 const char *func,
564 const char *object_field,
565 const char *object,
566 const char *extra,
567 const char *extra_field,
568 char *buffer) {
569
570 assert(buffer);
571
572 if (error < 0)
573 error = -error;
574
575 if (log_target == LOG_TARGET_NULL)
576 return -error;
577
578 /* Patch in LOG_DAEMON facility if necessary */
579 if ((level & LOG_FACMASK) == 0)
580 level = log_facility | LOG_PRI(level);
581
582 do {
583 char *e;
584 int k = 0;
585
586 buffer += strspn(buffer, NEWLINE);
587
588 if (buffer[0] == 0)
589 break;
590
591 if ((e = strpbrk(buffer, NEWLINE)))
592 *(e++) = 0;
593
594 if (IN_SET(log_target, LOG_TARGET_AUTO,
595 LOG_TARGET_JOURNAL_OR_KMSG,
596 LOG_TARGET_JOURNAL)) {
597
598 k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
599 if (k < 0) {
600 if (k != -EAGAIN)
601 log_close_journal();
602 log_open_kmsg();
603 }
604 }
605
606 if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
607 LOG_TARGET_SYSLOG)) {
608
609 k = write_to_syslog(level, error, file, line, func, buffer);
610 if (k < 0) {
611 if (k != -EAGAIN)
612 log_close_syslog();
613 log_open_kmsg();
614 }
615 }
616
617 if (k <= 0 &&
618 IN_SET(log_target, LOG_TARGET_AUTO,
619 LOG_TARGET_SAFE,
620 LOG_TARGET_SYSLOG_OR_KMSG,
621 LOG_TARGET_JOURNAL_OR_KMSG,
622 LOG_TARGET_KMSG)) {
623
624 k = write_to_kmsg(level, error, file, line, func, buffer);
625 if (k < 0) {
626 log_close_kmsg();
627 log_open_console();
628 }
629 }
630
631 if (k <= 0)
632 (void) write_to_console(level, error, file, line, func, buffer);
633
634 buffer = e;
635 } while (buffer);
636
637 return -error;
638 }
639
640 int log_dump_internal(
641 int level,
642 int error,
643 const char *file,
644 int line,
645 const char *func,
646 char *buffer) {
647
648 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
649 PROTECT_ERRNO;
650
651 /* This modifies the buffer... */
652
653 if (error < 0)
654 error = -error;
655
656 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
657 return -error;
658
659 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
660 }
661
662 int log_internalv_realm(
663 int level,
664 int error,
665 const char *file,
666 int line,
667 const char *func,
668 const char *format,
669 va_list ap) {
670
671 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
672 char buffer[LINE_MAX];
673 PROTECT_ERRNO;
674
675 if (error < 0)
676 error = -error;
677
678 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
679 return -error;
680
681 /* Make sure that %m maps to the specified error */
682 if (error != 0)
683 errno = error;
684
685 vsnprintf(buffer, sizeof(buffer), format, ap);
686
687 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
688 }
689
690 int log_internal_realm(
691 int level,
692 int error,
693 const char *file,
694 int line,
695 const char *func,
696 const char *format, ...) {
697
698 va_list ap;
699 int r;
700
701 va_start(ap, format);
702 r = log_internalv_realm(level, error, file, line, func, format, ap);
703 va_end(ap);
704
705 return r;
706 }
707
708 int log_object_internalv(
709 int level,
710 int error,
711 const char *file,
712 int line,
713 const char *func,
714 const char *object_field,
715 const char *object,
716 const char *extra_field,
717 const char *extra,
718 const char *format,
719 va_list ap) {
720
721 PROTECT_ERRNO;
722 char *buffer, *b;
723
724 if (error < 0)
725 error = -error;
726
727 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
728 return -error;
729
730 /* Make sure that %m maps to the specified error */
731 if (error != 0)
732 errno = error;
733
734 /* Prepend the object name before the message */
735 if (object) {
736 size_t n;
737
738 n = strlen(object);
739 buffer = newa(char, n + 2 + LINE_MAX);
740 b = stpcpy(stpcpy(buffer, object), ": ");
741 } else
742 b = buffer = newa(char, LINE_MAX);
743
744 vsnprintf(b, LINE_MAX, format, ap);
745
746 return log_dispatch_internal(level, error, file, line, func,
747 object_field, object, extra_field, extra, buffer);
748 }
749
750 int log_object_internal(
751 int level,
752 int error,
753 const char *file,
754 int line,
755 const char *func,
756 const char *object_field,
757 const char *object,
758 const char *extra_field,
759 const char *extra,
760 const char *format, ...) {
761
762 va_list ap;
763 int r;
764
765 va_start(ap, format);
766 r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
767 va_end(ap);
768
769 return r;
770 }
771
772 static void log_assert(
773 int level,
774 const char *text,
775 const char *file,
776 int line,
777 const char *func,
778 const char *format) {
779
780 static char buffer[LINE_MAX];
781 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
782
783 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
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_realm(
796 LogRealm realm,
797 const char *text,
798 const char *file,
799 int line,
800 const char *func) {
801 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
802 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
803 abort();
804 }
805
806 noreturn void log_assert_failed_unreachable_realm(
807 LogRealm realm,
808 const char *text,
809 const char *file,
810 int line,
811 const char *func) {
812 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
813 "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
814 abort();
815 }
816
817 void log_assert_failed_return_realm(
818 LogRealm realm,
819 const char *text,
820 const char *file,
821 int line,
822 const char *func) {
823 PROTECT_ERRNO;
824 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func,
825 "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
826 }
827
828 int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) {
829 log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR),
830 ENOMEM, file, line, func, "Out of memory.");
831 return -ENOMEM;
832 }
833
834 int log_format_iovec(
835 struct iovec *iovec,
836 unsigned iovec_len,
837 unsigned *n,
838 bool newline_separator,
839 int error,
840 const char *format,
841 va_list ap) {
842
843 static const char nl = '\n';
844
845 while (format && *n + 1 < iovec_len) {
846 va_list aq;
847 char *m;
848 int r;
849
850 /* We need to copy the va_list structure,
851 * since vasprintf() leaves it afterwards at
852 * an undefined location */
853
854 if (error != 0)
855 errno = error;
856
857 va_copy(aq, ap);
858 r = vasprintf(&m, format, aq);
859 va_end(aq);
860 if (r < 0)
861 return -EINVAL;
862
863 /* Now, jump enough ahead, so that we point to
864 * the next format string */
865 VA_FORMAT_ADVANCE(format, ap);
866
867 IOVEC_SET_STRING(iovec[(*n)++], m);
868
869 if (newline_separator) {
870 iovec[*n].iov_base = (char*) &nl;
871 iovec[*n].iov_len = 1;
872 (*n)++;
873 }
874
875 format = va_arg(ap, char *);
876 }
877 return 0;
878 }
879
880 int log_struct_internal(
881 int level,
882 int error,
883 const char *file,
884 int line,
885 const char *func,
886 const char *format, ...) {
887
888 char buf[LINE_MAX];
889 bool found = false;
890 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
891 PROTECT_ERRNO;
892 va_list ap;
893
894 if (error < 0)
895 error = -error;
896
897 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
898 return -error;
899
900 if (log_target == LOG_TARGET_NULL)
901 return -error;
902
903 if ((level & LOG_FACMASK) == 0)
904 level = log_facility | LOG_PRI(level);
905
906 if (IN_SET(log_target, LOG_TARGET_AUTO,
907 LOG_TARGET_JOURNAL_OR_KMSG,
908 LOG_TARGET_JOURNAL) &&
909 journal_fd >= 0) {
910 char header[LINE_MAX];
911 struct iovec iovec[17] = {};
912 unsigned n = 0, i;
913 int r;
914 struct msghdr mh = {
915 .msg_iov = iovec,
916 };
917 bool fallback = false;
918
919 /* If the journal is available do structured logging */
920 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
921 IOVEC_SET_STRING(iovec[n++], header);
922
923 va_start(ap, format);
924 r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap);
925 if (r < 0)
926 fallback = true;
927 else {
928 mh.msg_iovlen = n;
929 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
930 }
931
932 va_end(ap);
933 for (i = 1; i < n; i += 2)
934 free(iovec[i].iov_base);
935
936 if (!fallback)
937 return -error;
938 }
939
940 /* Fallback if journal logging is not available or didn't work. */
941
942 va_start(ap, format);
943 while (format) {
944 va_list aq;
945
946 if (error != 0)
947 errno = error;
948
949 va_copy(aq, ap);
950 vsnprintf(buf, sizeof(buf), format, aq);
951 va_end(aq);
952
953 if (startswith(buf, "MESSAGE=")) {
954 found = true;
955 break;
956 }
957
958 VA_FORMAT_ADVANCE(format, ap);
959
960 format = va_arg(ap, char *);
961 }
962 va_end(ap);
963
964 if (!found)
965 return -error;
966
967 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
968 }
969
970 int log_set_target_from_string(const char *e) {
971 LogTarget t;
972
973 t = log_target_from_string(e);
974 if (t < 0)
975 return -EINVAL;
976
977 log_set_target(t);
978 return 0;
979 }
980
981 int log_set_max_level_from_string_realm(LogRealm realm, const char *e) {
982 int t;
983
984 t = log_level_from_string(e);
985 if (t < 0)
986 return -EINVAL;
987
988 log_set_max_level_realm(realm, t);
989 return 0;
990 }
991
992 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
993
994 /*
995 * The systemd.log_xyz= settings are parsed by all tools, and
996 * so is "debug".
997 *
998 * However, "quiet" is only parsed by PID 1, and only turns of
999 * status output to /dev/console, but does not alter the log
1000 * level.
1001 */
1002
1003 if (streq(key, "debug") && !value)
1004 log_set_max_level(LOG_DEBUG);
1005
1006 else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
1007
1008 if (proc_cmdline_value_missing(key, value))
1009 return 0;
1010
1011 if (log_set_target_from_string(value) < 0)
1012 log_warning("Failed to parse log target '%s'. Ignoring.", value);
1013
1014 } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
1015
1016 if (proc_cmdline_value_missing(key, value))
1017 return 0;
1018
1019 if (log_set_max_level_from_string(value) < 0)
1020 log_warning("Failed to parse log level '%s'. Ignoring.", value);
1021
1022 } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
1023
1024 if (log_show_color_from_string(value ?: "1") < 0)
1025 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
1026
1027 } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
1028
1029 if (log_show_location_from_string(value ?: "1") < 0)
1030 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
1031 }
1032
1033 return 0;
1034 }
1035
1036 void log_parse_environment_realm(LogRealm realm) {
1037 const char *e;
1038
1039 if (get_ctty_devnr(0, NULL) < 0)
1040 /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is
1041 user stuff. */
1042 (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1043
1044 e = secure_getenv("SYSTEMD_LOG_TARGET");
1045 if (e && log_set_target_from_string(e) < 0)
1046 log_warning("Failed to parse log target '%s'. Ignoring.", e);
1047
1048 e = secure_getenv("SYSTEMD_LOG_LEVEL");
1049 if (e && log_set_max_level_from_string_realm(realm, e) < 0)
1050 log_warning("Failed to parse log level '%s'. Ignoring.", e);
1051
1052 e = secure_getenv("SYSTEMD_LOG_COLOR");
1053 if (e && log_show_color_from_string(e) < 0)
1054 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1055
1056 e = secure_getenv("SYSTEMD_LOG_LOCATION");
1057 if (e && log_show_location_from_string(e) < 0)
1058 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1059 }
1060
1061 LogTarget log_get_target(void) {
1062 return log_target;
1063 }
1064
1065 int log_get_max_level_realm(LogRealm realm) {
1066 return log_max_level[realm];
1067 }
1068
1069 void log_show_color(bool b) {
1070 show_color = b;
1071 }
1072
1073 bool log_get_show_color(void) {
1074 return show_color;
1075 }
1076
1077 void log_show_location(bool b) {
1078 show_location = b;
1079 }
1080
1081 bool log_get_show_location(void) {
1082 return show_location;
1083 }
1084
1085 int log_show_color_from_string(const char *e) {
1086 int t;
1087
1088 t = parse_boolean(e);
1089 if (t < 0)
1090 return t;
1091
1092 log_show_color(t);
1093 return 0;
1094 }
1095
1096 int log_show_location_from_string(const char *e) {
1097 int t;
1098
1099 t = parse_boolean(e);
1100 if (t < 0)
1101 return t;
1102
1103 log_show_location(t);
1104 return 0;
1105 }
1106
1107 bool log_on_console(void) {
1108 if (IN_SET(log_target, LOG_TARGET_CONSOLE,
1109 LOG_TARGET_CONSOLE_PREFIXED))
1110 return true;
1111
1112 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1113 }
1114
1115 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1116 [LOG_TARGET_CONSOLE] = "console",
1117 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1118 [LOG_TARGET_KMSG] = "kmsg",
1119 [LOG_TARGET_JOURNAL] = "journal",
1120 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1121 [LOG_TARGET_SYSLOG] = "syslog",
1122 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1123 [LOG_TARGET_AUTO] = "auto",
1124 [LOG_TARGET_SAFE] = "safe",
1125 [LOG_TARGET_NULL] = "null"
1126 };
1127
1128 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1129
1130 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1131 if (si->ssi_pid > 0) {
1132 _cleanup_free_ char *p = NULL;
1133
1134 get_process_comm(si->ssi_pid, &p);
1135
1136 log_full(level,
1137 "Received SIG%s from PID %"PRIu32" (%s).",
1138 signal_to_string(si->ssi_signo),
1139 si->ssi_pid, strna(p));
1140 } else
1141 log_full(level,
1142 "Received SIG%s.",
1143 signal_to_string(si->ssi_signo));
1144
1145 }
1146
1147 void log_set_upgrade_syslog_to_journal(bool b) {
1148 upgrade_syslog_to_journal = b;
1149 }
1150
1151 int log_syntax_internal(
1152 const char *unit,
1153 int level,
1154 const char *config_file,
1155 unsigned config_line,
1156 int error,
1157 const char *file,
1158 int line,
1159 const char *func,
1160 const char *format, ...) {
1161
1162 PROTECT_ERRNO;
1163 char buffer[LINE_MAX];
1164 va_list ap;
1165 const char *unit_fmt = NULL;
1166
1167 if (error < 0)
1168 error = -error;
1169
1170 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
1171 return -error;
1172
1173 if (log_target == LOG_TARGET_NULL)
1174 return -error;
1175
1176 if (error != 0)
1177 errno = error;
1178
1179 va_start(ap, format);
1180 vsnprintf(buffer, sizeof(buffer), format, ap);
1181 va_end(ap);
1182
1183 if (unit)
1184 unit_fmt = getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
1185
1186 return log_struct_internal(
1187 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),
1188 error,
1189 file, line, func,
1190 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
1191 "CONFIG_FILE=%s", config_file,
1192 "CONFIG_LINE=%u", config_line,
1193 LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
1194 unit_fmt, unit,
1195 NULL);
1196 }
1197
1198 void log_set_always_reopen_console(bool b) {
1199 always_reopen_console = b;
1200 }