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