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