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