]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/log.c
Merge pull request #2085 from fbuihuu/more-use-of-check-load-state
[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_struct_internal(
809 int level,
810 int error,
811 const char *file,
812 int line,
813 const char *func,
814 const char *format, ...) {
815
816 char buf[LINE_MAX];
817 bool found = false;
818 PROTECT_ERRNO;
819 va_list ap;
820
821 if (error < 0)
822 error = -error;
823
824 if (_likely_(LOG_PRI(level) > log_max_level))
825 return -error;
826
827 if (log_target == LOG_TARGET_NULL)
828 return -error;
829
830 if ((level & LOG_FACMASK) == 0)
831 level = log_facility | LOG_PRI(level);
832
833 if ((log_target == LOG_TARGET_AUTO ||
834 log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
835 log_target == LOG_TARGET_JOURNAL) &&
836 journal_fd >= 0) {
837 char header[LINE_MAX];
838 struct iovec iovec[17] = {};
839 unsigned n = 0, i;
840 struct msghdr mh = {
841 .msg_iov = iovec,
842 };
843 static const char nl = '\n';
844 bool fallback = false;
845
846 /* If the journal is available do structured logging */
847 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL);
848 IOVEC_SET_STRING(iovec[n++], header);
849
850 va_start(ap, format);
851 while (format && n + 1 < ELEMENTSOF(iovec)) {
852 va_list aq;
853 char *m;
854
855 /* We need to copy the va_list structure,
856 * since vasprintf() leaves it afterwards at
857 * an undefined location */
858
859 if (error != 0)
860 errno = error;
861
862 va_copy(aq, ap);
863 if (vasprintf(&m, format, aq) < 0) {
864 va_end(aq);
865 fallback = true;
866 goto finish;
867 }
868 va_end(aq);
869
870 /* Now, jump enough ahead, so that we point to
871 * the next format string */
872 VA_FORMAT_ADVANCE(format, ap);
873
874 IOVEC_SET_STRING(iovec[n++], m);
875
876 iovec[n].iov_base = (char*) &nl;
877 iovec[n].iov_len = 1;
878 n++;
879
880 format = va_arg(ap, char *);
881 }
882
883 mh.msg_iovlen = n;
884
885 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
886
887 finish:
888 va_end(ap);
889 for (i = 1; i < n; i += 2)
890 free(iovec[i].iov_base);
891
892 if (!fallback)
893 return -error;
894 }
895
896 /* Fallback if journal logging is not available or didn't work. */
897
898 va_start(ap, format);
899 while (format) {
900 va_list aq;
901
902 if (error != 0)
903 errno = error;
904
905 va_copy(aq, ap);
906 vsnprintf(buf, sizeof(buf), format, aq);
907 va_end(aq);
908
909 if (startswith(buf, "MESSAGE=")) {
910 found = true;
911 break;
912 }
913
914 VA_FORMAT_ADVANCE(format, ap);
915
916 format = va_arg(ap, char *);
917 }
918 va_end(ap);
919
920 if (!found)
921 return -error;
922
923 return log_dispatch(level, error, file, line, func, NULL, NULL, buf + 8);
924 }
925
926 int log_set_target_from_string(const char *e) {
927 LogTarget t;
928
929 t = log_target_from_string(e);
930 if (t < 0)
931 return -EINVAL;
932
933 log_set_target(t);
934 return 0;
935 }
936
937 int log_set_max_level_from_string(const char *e) {
938 int t;
939
940 t = log_level_from_string(e);
941 if (t < 0)
942 return -EINVAL;
943
944 log_set_max_level(t);
945 return 0;
946 }
947
948 static int parse_proc_cmdline_item(const char *key, const char *value) {
949
950 /*
951 * The systemd.log_xyz= settings are parsed by all tools, and
952 * so is "debug".
953 *
954 * However, "quiet" is only parsed by PID 1, and only turns of
955 * status output to /dev/console, but does not alter the log
956 * level.
957 */
958
959 if (streq(key, "debug") && !value)
960 log_set_max_level(LOG_DEBUG);
961
962 else if (streq(key, "systemd.log_target") && value) {
963
964 if (log_set_target_from_string(value) < 0)
965 log_warning("Failed to parse log target '%s'. Ignoring.", value);
966
967 } else if (streq(key, "systemd.log_level") && value) {
968
969 if (log_set_max_level_from_string(value) < 0)
970 log_warning("Failed to parse log level '%s'. Ignoring.", value);
971
972 } else if (streq(key, "systemd.log_color") && value) {
973
974 if (log_show_color_from_string(value) < 0)
975 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
976
977 } else if (streq(key, "systemd.log_location") && value) {
978
979 if (log_show_location_from_string(value) < 0)
980 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
981 }
982
983 return 0;
984 }
985
986 void log_parse_environment(void) {
987 const char *e;
988
989 if (get_ctty_devnr(0, NULL) < 0)
990 /* Only try to read the command line in daemons.
991 We assume that anything that has a controlling
992 tty is user stuff. */
993 (void) parse_proc_cmdline(parse_proc_cmdline_item);
994
995 e = secure_getenv("SYSTEMD_LOG_TARGET");
996 if (e && log_set_target_from_string(e) < 0)
997 log_warning("Failed to parse log target '%s'. Ignoring.", e);
998
999 e = secure_getenv("SYSTEMD_LOG_LEVEL");
1000 if (e && log_set_max_level_from_string(e) < 0)
1001 log_warning("Failed to parse log level '%s'. Ignoring.", e);
1002
1003 e = secure_getenv("SYSTEMD_LOG_COLOR");
1004 if (e && log_show_color_from_string(e) < 0)
1005 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1006
1007 e = secure_getenv("SYSTEMD_LOG_LOCATION");
1008 if (e && log_show_location_from_string(e) < 0)
1009 log_warning("Failed to parse bool '%s'. Ignoring.", e);
1010 }
1011
1012 LogTarget log_get_target(void) {
1013 return log_target;
1014 }
1015
1016 int log_get_max_level(void) {
1017 return log_max_level;
1018 }
1019
1020 void log_show_color(bool b) {
1021 show_color = b;
1022 }
1023
1024 bool log_get_show_color(void) {
1025 return show_color;
1026 }
1027
1028 void log_show_location(bool b) {
1029 show_location = b;
1030 }
1031
1032 bool log_get_show_location(void) {
1033 return show_location;
1034 }
1035
1036 int log_show_color_from_string(const char *e) {
1037 int t;
1038
1039 t = parse_boolean(e);
1040 if (t < 0)
1041 return t;
1042
1043 log_show_color(t);
1044 return 0;
1045 }
1046
1047 int log_show_location_from_string(const char *e) {
1048 int t;
1049
1050 t = parse_boolean(e);
1051 if (t < 0)
1052 return t;
1053
1054 log_show_location(t);
1055 return 0;
1056 }
1057
1058 bool log_on_console(void) {
1059 if (log_target == LOG_TARGET_CONSOLE ||
1060 log_target == LOG_TARGET_CONSOLE_PREFIXED)
1061 return true;
1062
1063 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1064 }
1065
1066 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1067 [LOG_TARGET_CONSOLE] = "console",
1068 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1069 [LOG_TARGET_KMSG] = "kmsg",
1070 [LOG_TARGET_JOURNAL] = "journal",
1071 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1072 [LOG_TARGET_SYSLOG] = "syslog",
1073 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1074 [LOG_TARGET_AUTO] = "auto",
1075 [LOG_TARGET_SAFE] = "safe",
1076 [LOG_TARGET_NULL] = "null"
1077 };
1078
1079 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1080
1081 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1082 if (si->ssi_pid > 0) {
1083 _cleanup_free_ char *p = NULL;
1084
1085 get_process_comm(si->ssi_pid, &p);
1086
1087 log_full(level,
1088 "Received SIG%s from PID %"PRIu32" (%s).",
1089 signal_to_string(si->ssi_signo),
1090 si->ssi_pid, strna(p));
1091 } else
1092 log_full(level,
1093 "Received SIG%s.",
1094 signal_to_string(si->ssi_signo));
1095
1096 }
1097
1098 void log_set_upgrade_syslog_to_journal(bool b) {
1099 upgrade_syslog_to_journal = b;
1100 }
1101
1102 int log_syntax_internal(
1103 const char *unit,
1104 int level,
1105 const char *config_file,
1106 unsigned config_line,
1107 int error,
1108 const char *file,
1109 int line,
1110 const char *func,
1111 const char *format, ...) {
1112
1113 PROTECT_ERRNO;
1114 char buffer[LINE_MAX];
1115 int r;
1116 va_list ap;
1117
1118 if (error < 0)
1119 error = -error;
1120
1121 if (_likely_(LOG_PRI(level) > log_max_level))
1122 return -error;
1123
1124 if (log_target == LOG_TARGET_NULL)
1125 return -error;
1126
1127 if (error != 0)
1128 errno = error;
1129
1130 va_start(ap, format);
1131 vsnprintf(buffer, sizeof(buffer), format, ap);
1132 va_end(ap);
1133
1134 if (unit)
1135 r = log_struct_internal(
1136 level, error,
1137 file, line, func,
1138 getpid() == 1 ? "UNIT=%s" : "USER_UNIT=%s", unit,
1139 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
1140 "CONFIG_FILE=%s", config_file,
1141 "CONFIG_LINE=%u", config_line,
1142 LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
1143 NULL);
1144 else
1145 r = log_struct_internal(
1146 level, error,
1147 file, line, func,
1148 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION),
1149 "CONFIG_FILE=%s", config_file,
1150 "CONFIG_LINE=%u", config_line,
1151 LOG_MESSAGE("[%s:%u] %s", config_file, config_line, buffer),
1152 NULL);
1153
1154 return r;
1155 }