Debian Codegraph @

src/basic/log.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/signalfd.h>
5 #include <sys/stat.h>
6 #include <sys/uio.h>
7 #include <threads.h>
8 #include <unistd.h>
9
10 #include "sd-messages.h"
11
12 #include "alloc-util.h"
13 #include "ansi-color.h"
14 #include "argv-util.h"
15 #include "env-util.h"
16 #include "errno-util.h"
17 #include "extract-word.h"
18 #include "fd-util.h"
19 #include "format-util.h"
20 #include "iovec-util.h"
21 #include "list.h"
22 #include "log.h"
23 #include "log-context.h"
24 #include "parse-util.h"
25 #include "proc-cmdline.h"
26 #include "process-util.h"
27 #include "ratelimit.h"
28 #include "signal-util.h"
29 #include "socket-util.h"
30 #include "stdio-util.h"
31 #include "string-table.h"
32 #include "string-util.h"
33 #include "strv.h"
34 #include "syslog-util.h"
35 #include "terminal-util.h"
36 #include "time-util.h"
37 #include "utf8.h"
38
39 #define SNDBUF_SIZE (8*1024*1024)
40 #define IOVEC_MAX 256U
41
42 static log_syntax_callback_t log_syntax_callback = NULL;
43 static void *log_syntax_callback_userdata = NULL;
44
45 static LogTarget log_target = LOG_TARGET_CONSOLE;
46 static int log_max_level = LOG_INFO;
47 static int log_target_max_level[_LOG_TARGET_SINGLE_MAX] = {
48 [LOG_TARGET_CONSOLE] = INT_MAX,
49 [LOG_TARGET_KMSG] = INT_MAX,
50 [LOG_TARGET_SYSLOG] = INT_MAX,
51 [LOG_TARGET_JOURNAL] = INT_MAX,
52 };
53 static int log_facility = LOG_DAEMON;
54 static bool ratelimit_kmsg = true;
55
56 static int console_fd = STDERR_FILENO;
57 static int console_fd_is_tty = -1; /* tri-state: -1 means don't know */
58 static int syslog_fd = -EBADF;
59 static int kmsg_fd = -EBADF;
60 static int journal_fd = -EBADF;
61
62 static bool syslog_is_stream = false;
63
64 static int show_color = -1; /* tristate */
65 static bool show_location = false;
66 static bool show_time = false;
67 static bool show_tid = false;
68
69 static bool upgrade_syslog_to_journal = false;
70 static bool always_reopen_console = false;
71 static bool open_when_needed = false;
72 static bool prohibit_ipc = false;
73
74 static thread_local const char *log_prefix = NULL;
75
76 #if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
77 bool _log_message_dummy = false; /* Always false */
78 #endif
79
80 /* An assert to use in logging functions that does not call recursively
81 * into our logging functions (since that might lead to a loop). */
82 #define assert_raw(expr) \
83 do { \
84 if (_unlikely_(!(expr))) { \
85 fputs(#expr "\n", stderr); \
86 abort(); \
87 } \
88 } while (false)
89
90 void log_prefix_swap(const char **prefix) {
91 assert(prefix);
92
93 SWAP_TWO(log_prefix, *prefix);
94 }
95
96 static void log_close_console(void) {
97 /* See comment in log_close_journal() */
98 (void) safe_close_above_stdio(TAKE_FD(console_fd));
99 console_fd_is_tty = -1;
100 }
101
102 static int log_open_console(void) {
103
104 if (!always_reopen_console) {
105 console_fd = STDERR_FILENO;
106 console_fd_is_tty = -1;
107 return 0;
108 }
109
110 if (console_fd < 3) {
111 int fd;
112
113 fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
114 if (fd < 0)
115 return fd;
116
117 console_fd = fd_move_above_stdio(fd);
118 console_fd_is_tty = true;
119 }
120
121 return 0;
122 }
123
124 static void log_close_kmsg(void) {
125 /* See comment in log_close_journal() */
126 (void) safe_close(TAKE_FD(kmsg_fd));
127 }
128
129 static int log_open_kmsg(void) {
130
131 if (kmsg_fd >= 0)
132 return 0;
133
134 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
135 if (kmsg_fd < 0)
136 return -errno;
137
138 kmsg_fd = fd_move_above_stdio(kmsg_fd);
139 return 0;
140 }
141
142 static void log_close_syslog(void) {
143 /* See comment in log_close_journal() */
144 (void) safe_close(TAKE_FD(syslog_fd));
145 }
146
147 static int create_log_socket(int type) {
148 struct timeval tv;
149 int fd;
150
151 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
152 if (fd < 0)
153 return -errno;
154
155 fd = fd_move_above_stdio(fd);
156 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
157
158 /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
159 * in the unlikely case of a deadlock. */
160 if (getpid_cached() == 1)
161 timeval_store(&tv, 10 * USEC_PER_MSEC);
162 else
163 timeval_store(&tv, 10 * USEC_PER_SEC);
164 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
165
166 return fd;
167 }
168
169 static int log_open_syslog(void) {
170 int r;
171
172 if (syslog_fd >= 0)
173 return 0;
174
175 syslog_fd = create_log_socket(SOCK_DGRAM);
176 if (syslog_fd < 0) {
177 r = syslog_fd;
178 goto fail;
179 }
180
181 r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
182 if (r < 0) {
183 safe_close(syslog_fd);
184
185 /* Some legacy syslog systems still use stream sockets. They really shouldn't. But what can
186 * we do... */
187 syslog_fd = create_log_socket(SOCK_STREAM);
188 if (syslog_fd < 0) {
189 r = syslog_fd;
190 goto fail;
191 }
192
193 r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
194 if (r < 0)
195 goto fail;
196
197 syslog_is_stream = true;
198 } else
199 syslog_is_stream = false;
200
201 return 0;
202
203 fail:
204 log_close_syslog();
205 return r;
206 }
207
208 static void log_close_journal(void) {
209 /* If the journal FD is bad, safe_close will fail, and will try to log, which will fail, so we'll
210 * try to close the journal FD, which is bad, so safe_close will fail... Whether we can close it
211 * or not, invalidate it immediately so that we don't get in a recursive loop until we run out of
212 * stack. */
213 (void) safe_close(TAKE_FD(journal_fd));
214 }
215
216 static int log_open_journal(void) {
217 int r;
218
219 if (journal_fd >= 0)
220 return 0;
221
222 journal_fd = create_log_socket(SOCK_DGRAM);
223 if (journal_fd < 0) {
224 r = journal_fd;
225 goto fail;
226 }
227
228 r = connect_unix_path(journal_fd, AT_FDCWD, "/run/systemd/journal/socket");
229 if (r < 0)
230 goto fail;
231
232 return 0;
233
234 fail:
235 log_close_journal();
236 return r;
237 }
238
239 bool stderr_is_journal(void) {
240 _cleanup_free_ char *w = NULL;
241 const char *e;
242 uint64_t dev, ino;
243 struct stat st;
244
245 e = getenv("JOURNAL_STREAM");
246 if (!e)
247 return false;
248
249 if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
250 return false;
251 if (!e)
252 return false;
253
254 if (safe_atou64(w, &dev) < 0)
255 return false;
256 if (safe_atou64(e, &ino) < 0)
257 return false;
258
259 if (fstat(STDERR_FILENO, &st) < 0)
260 return false;
261
262 return st.st_dev == dev && st.st_ino == ino;
263 }
264
265 int log_open(void) {
266 int r;
267
268 /* Do not call from library code. */
269
270 /* This function is often called in preparation for logging. Let's make sure we don't clobber errno,
271 * so that a call to a logging function immediately following a log_open() call can still easily
272 * reference an error that happened immediately before the log_open() call. */
273 PROTECT_ERRNO;
274
275 /* If we don't use the console, we close it here to not get killed by SAK. If we don't use syslog, we
276 * close it here too, so that we are not confused by somebody deleting the socket in the fs, and to
277 * make sure we don't use it if prohibit_ipc is set. If we don't use /dev/kmsg we still keep it open,
278 * because there is no reason to close it. */
279
280 if (log_target == LOG_TARGET_NULL) {
281 log_close_journal();
282 log_close_syslog();
283 log_close_console();
284 return 0;
285 }
286
287 if (getpid_cached() == 1 ||
288 stderr_is_journal() ||
289 IN_SET(log_target,
290 LOG_TARGET_KMSG,
291 LOG_TARGET_JOURNAL,
292 LOG_TARGET_JOURNAL_OR_KMSG,
293 LOG_TARGET_SYSLOG,
294 LOG_TARGET_SYSLOG_OR_KMSG)) {
295
296 if (!prohibit_ipc) {
297 if (IN_SET(log_target,
298 LOG_TARGET_AUTO,
299 LOG_TARGET_JOURNAL_OR_KMSG,
300 LOG_TARGET_JOURNAL)) {
301
302 r = log_open_journal();
303 if (r >= 0) {
304 log_close_syslog();
305 log_close_console();
306 return r;
307 }
308 }
309
310 if (IN_SET(log_target,
311 LOG_TARGET_SYSLOG_OR_KMSG,
312 LOG_TARGET_SYSLOG)) {
313
314 r = log_open_syslog();
315 if (r >= 0) {
316 log_close_journal();
317 log_close_console();
318 return r;
319 }
320 }
321 }
322
323 if (IN_SET(log_target, LOG_TARGET_AUTO,
324 LOG_TARGET_JOURNAL_OR_KMSG,
325 LOG_TARGET_SYSLOG_OR_KMSG,
326 LOG_TARGET_KMSG)) {
327 r = log_open_kmsg();
328 if (r >= 0) {
329 log_close_journal();
330 log_close_syslog();
331 log_close_console();
332 return r;
333 }
334 }
335 }
336
337 log_close_journal();
338 log_close_syslog();
339
340 return log_open_console();
341 }
342
343 void log_set_target(LogTarget target) {
344 assert(target >= 0);
345 assert(target < _LOG_TARGET_MAX);
346
347 if (upgrade_syslog_to_journal) {
348 if (target == LOG_TARGET_SYSLOG)
349 target = LOG_TARGET_JOURNAL;
350 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
351 target = LOG_TARGET_JOURNAL_OR_KMSG;
352 }
353
354 log_target = target;
355 }
356
357 void log_set_target_and_open(LogTarget target) {
358 log_set_target(target);
359 log_open();
360 }
361
362 void log_close(void) {
363 /* Do not call from library code. */
364
365 log_close_journal();
366 log_close_syslog();
367 log_close_kmsg();
368 log_close_console();
369 }
370
371 void log_forget_fds(void) {
372 /* Do not call from library code. */
373
374 console_fd = kmsg_fd = syslog_fd = journal_fd = -EBADF;
375 console_fd_is_tty = -1;
376 }
377
378 int log_set_max_level(int level) {
379 assert(level == LOG_NULL || log_level_is_valid(level));
380
381 int old = log_max_level;
382 log_max_level = level;
383
384 /* Also propagate max log level to libc's syslog(), just in case some other component loaded into our
385 * process logs directly via syslog(). You might wonder why we maintain our own log level variable if
386 * libc has the same functionality. This has multiple reasons, first and foremost that we want to
387 * apply this to all our log targets, not just syslog and console. Moreover, we cannot query the
388 * current log mask from glibc without changing it, but that's useful for testing the current log
389 * level before even entering the log functions like we do in our macros. */
390 setlogmask(LOG_UPTO(level));
391
392 /* Ensure that our own LOG_NULL define maps sanely to the log mask */
393 assert_cc(LOG_UPTO(LOG_NULL) == 0);
394
395 return old;
396 }
397
398 void log_set_facility(int facility) {
399 log_facility = facility;
400 }
401
402 static bool check_console_fd_is_tty(void) {
403 if (console_fd < 0)
404 return false;
405
406 if (console_fd_is_tty < 0)
407 console_fd_is_tty = isatty_safe(console_fd);
408
409 return console_fd_is_tty;
410 }
411
412 static int write_to_console(
413 int level,
414 int error,
415 const char *file,
416 int line,
417 const char *func,
418 const char *buffer) {
419
420 static int dumb = -1;
421
422 char location[256],
423 header_time[FORMAT_TIMESTAMP_MAX],
424 prefix[1 + DECIMAL_STR_MAX(int) + 2],
425 tid_string[3 + DECIMAL_STR_MAX(pid_t) + 1];
426 struct iovec iovec[11];
427 const char *on = NULL, *off = NULL;
428 size_t n = 0;
429
430 if (console_fd < 0)
431 return 0;
432
433 if (dumb < 0)
434 dumb = getenv_terminal_is_dumb();
435
436 if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_CONSOLE])
437 return 0;
438
439 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
440 xsprintf(prefix, "<%i>", level);
441 iovec[n++] = IOVEC_MAKE_STRING(prefix);
442 }
443
444 if (show_time &&
445 format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
446 iovec[n++] = IOVEC_MAKE_STRING(header_time);
447 iovec[n++] = IOVEC_MAKE_STRING(" ");
448 }
449
450 if (show_tid) {
451 xsprintf(tid_string, "(" PID_FMT ") ", gettid());
452 iovec[n++] = IOVEC_MAKE_STRING(tid_string);
453 }
454
455 if (log_get_show_color())
456 get_log_colors(LOG_PRI(level), &on, &off, NULL);
457
458 if (show_location) {
459 const char *lon = "", *loff = "";
460 if (log_get_show_color()) {
461 lon = ansi_highlight_yellow4();
462 loff = ansi_normal();
463 }
464
465 (void) snprintf(location, sizeof location, "%s%s:%i%s: ", lon, file, line, loff);
466 iovec[n++] = IOVEC_MAKE_STRING(location);
467 }
468
469 if (on)
470 iovec[n++] = IOVEC_MAKE_STRING(on);
471 if (log_prefix) {
472 iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
473 iovec[n++] = IOVEC_MAKE_STRING(": ");
474 }
475 iovec[n++] = IOVEC_MAKE_STRING(buffer);
476 if (off)
477 iovec[n++] = IOVEC_MAKE_STRING(off);
478
479 /* When writing to a TTY we output an extra '\r' (i.e. CR) first, to generate CRNL rather than just
480 * NL. This is a robustness thing in case the TTY is currently in raw mode (specifically: has the
481 * ONLCR flag off). We want that subsequent output definitely starts at the beginning of the line
482 * again, after all. If the TTY is not in raw mode the extra CR should not hurt. If we're writing to
483 * a dumb terminal, only write NL as CRNL might be interpreted as a double newline. */
484 iovec[n++] = IOVEC_MAKE_STRING(check_console_fd_is_tty() && !dumb ? "\r\n" : "\n");
485
486 if (writev(console_fd, iovec, n) < 0) {
487
488 if (errno == EIO && getpid_cached() == 1) {
489
490 /* If somebody tried to kick us from our console tty (via vhangup() or suchlike), try
491 * to reconnect. */
492
493 log_close_console();
494 (void) log_open_console();
495 if (console_fd < 0)
496 return 0;
497
498 if (writev(console_fd, iovec, n) < 0)
499 return -errno;
500 } else
501 return -errno;
502 }
503
504 return 1;
505 }
506
507 static int write_to_syslog(
508 int level,
509 int error,
510 const char *file,
511 int line,
512 const char *func,
513 const char *buffer) {
514
515 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
516 header_time[64],
517 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
518 struct tm tm;
519 int r;
520
521 if (syslog_fd < 0)
522 return 0;
523
524 if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_SYSLOG])
525 return 0;
526
527 xsprintf(header_priority, "<%i>", level);
528
529 r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
530 if (r < 0)
531 return r;
532
533 if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
534 return -EINVAL;
535
536 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
537
538 struct iovec iovec[] = {
539 IOVEC_MAKE_STRING(header_priority),
540 IOVEC_MAKE_STRING(header_time),
541 IOVEC_MAKE_STRING(program_invocation_short_name),
542 IOVEC_MAKE_STRING(header_pid),
543 IOVEC_MAKE_STRING(strempty(log_prefix)),
544 IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
545 IOVEC_MAKE_STRING(buffer),
546 };
547 const struct msghdr msghdr = {
548 .msg_iov = iovec,
549 .msg_iovlen = ELEMENTSOF(iovec),
550 };
551
552 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
553 if (syslog_is_stream)
554 iovec[ELEMENTSOF(iovec) - 1].iov_len++;
555
556 for (;;) {
557 ssize_t n;
558
559 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
560 if (n < 0)
561 return -errno;
562
563 if (!syslog_is_stream)
564 break;
565
566 if (iovec_inc_many(iovec, ELEMENTSOF(iovec), n))
567 break;
568 }
569
570 return 1;
571 }
572
573 static int write_to_kmsg(
574 int level,
575 int error,
576 const char *file,
577 int line,
578 const char *func,
579 const char *buffer) {
580
581 /* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a
582 * safety catch for the case where start indiscriminately logging in a loop. It will not catch cases
583 * where we log excessively, but not in a tight loop.
584 *
585 * Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple
586 * loggers.
587 */
588 static thread_local RateLimit ratelimit = { 5 * USEC_PER_SEC, 200 };
589
590 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
591 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
592
593 if (kmsg_fd < 0)
594 return 0;
595
596 if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_KMSG])
597 return 0;
598
599 if (ratelimit_kmsg && !ratelimit_below(&ratelimit)) {
600 if (ratelimit_num_dropped(&ratelimit) > 1)
601 return 0;
602
603 buffer = "Too many messages being logged to kmsg, ignoring";
604 }
605
606 xsprintf(header_priority, "<%i>", level);
607 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
608
609 const struct iovec iovec[] = {
610 IOVEC_MAKE_STRING(header_priority),
611 IOVEC_MAKE_STRING(program_invocation_short_name),
612 IOVEC_MAKE_STRING(header_pid),
613 IOVEC_MAKE_STRING(strempty(log_prefix)),
614 IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
615 IOVEC_MAKE_STRING(buffer),
616 IOVEC_MAKE_STRING("\n"),
617 };
618
619 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
620 return -errno;
621
622 return 1;
623 }
624
625 static int log_do_header(
626 char *header,
627 size_t size,
628 int level,
629 int error,
630 const char *file, int line, const char *func,
631 const char *object_field, const char *object,
632 const char *extra_field, const char *extra) {
633 int r;
634
635 error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error);
636
637 r = snprintf(header, size,
638 "PRIORITY=%i\n"
639 "SYSLOG_FACILITY=%i\n"
640 "TID=" PID_FMT "\n"
641 "%s%.256s%s" /* CODE_FILE */
642 "%s%.*i%s" /* CODE_LINE */
643 "%s%.256s%s" /* CODE_FUNC */
644 "%s%.*i%s" /* ERRNO */
645 "%s%.256s%s" /* object */
646 "%s%.256s%s" /* extra */
647 "SYSLOG_IDENTIFIER=%.256s\n",
648 LOG_PRI(level),
649 LOG_FAC(level),
650 gettid(),
651 isempty(file) ? "" : "CODE_FILE=",
652 isempty(file) ? "" : file,
653 isempty(file) ? "" : "\n",
654 line ? "CODE_LINE=" : "",
655 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
656 line ? "\n" : "",
657 isempty(func) ? "" : "CODE_FUNC=",
658 isempty(func) ? "" : func,
659 isempty(func) ? "" : "\n",
660 error ? "ERRNO=" : "",
661 error ? 1 : 0, error,
662 error ? "\n" : "",
663 isempty(object) ? "" : ASSERT_PTR(object_field),
664 isempty(object) ? "" : object,
665 isempty(object) ? "" : "\n",
666 isempty(extra) ? "" : ASSERT_PTR(extra_field),
667 isempty(extra) ? "" : extra,
668 isempty(extra) ? "" : "\n",
669 program_invocation_short_name);
670 assert_raw((size_t) r < size);
671
672 return 0;
673 }
674
675 static void log_do_context(struct iovec *iovec, size_t iovec_len, size_t *n) {
676 assert(iovec);
677 assert(n);
678
679 LIST_FOREACH(ll, c, log_context_head()) {
680 STRV_FOREACH(s, c->fields) {
681 if (*n + 2 >= iovec_len)
682 return;
683
684 iovec[(*n)++] = IOVEC_MAKE_STRING(*s);
685 iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
686 }
687
688 for (size_t i = 0; i < c->n_input_iovec; i++) {
689 if (*n + 2 >= iovec_len)
690 return;
691
692 iovec[(*n)++] = c->input_iovec[i];
693 iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
694 }
695
696 if (c->key && c->value) {
697 if (*n + 3 >= iovec_len)
698 return;
699
700 iovec[(*n)++] = IOVEC_MAKE_STRING(c->key);
701 iovec[(*n)++] = IOVEC_MAKE_STRING(c->value);
702 iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
703 }
704 }
705 }
706
707 static int write_to_journal(
708 int level,
709 int error,
710 const char *file,
711 int line,
712 const char *func,
713 const char *object_field,
714 const char *object,
715 const char *extra_field,
716 const char *extra,
717 const char *buffer) {
718
719 char header[LINE_MAX];
720 size_t n = 0, iovec_len;
721 struct iovec *iovec;
722
723 if (journal_fd < 0)
724 return 0;
725
726 if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL])
727 return 0;
728
729 iovec_len = MIN(6 + log_context_num_fields() * 3, IOVEC_MAX);
730 iovec = newa(struct iovec, iovec_len);
731
732 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
733
734 iovec[n++] = IOVEC_MAKE_STRING(header);
735 iovec[n++] = IOVEC_MAKE_STRING("MESSAGE=");
736 if (log_prefix) {
737 iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
738 iovec[n++] = IOVEC_MAKE_STRING(": ");
739 }
740 iovec[n++] = IOVEC_MAKE_STRING(buffer);
741 iovec[n++] = IOVEC_MAKE_STRING("\n");
742
743 log_do_context(iovec, iovec_len, &n);
744
745 const struct msghdr msghdr = {
746 .msg_iov = iovec,
747 .msg_iovlen = n,
748 };
749
750 if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0)
751 return -errno;
752
753 return 1;
754 }
755
756 int log_dispatch_internal(
757 int level,
758 int error,
759 const char *file,
760 int line,
761 const char *func,
762 const char *object_field,
763 const char *object,
764 const char *extra_field,
765 const char *extra,
766 char *buffer) {
767
768 assert_raw(buffer);
769
770 if (log_target == LOG_TARGET_NULL)
771 return -ERRNO_VALUE(error);
772
773 /* Patch in LOG_DAEMON facility if necessary */
774 if (LOG_FAC(level) == 0)
775 level |= log_facility;
776
777 if (open_when_needed)
778 (void) log_open();
779
780 do {
781 char *e;
782 int k = 0;
783
784 buffer += strspn(buffer, NEWLINE);
785
786 if (buffer[0] == 0)
787 break;
788
789 if ((e = strpbrk(buffer, NEWLINE)))
790 *(e++) = 0;
791
792 if (IN_SET(log_target, LOG_TARGET_AUTO,
793 LOG_TARGET_JOURNAL_OR_KMSG,
794 LOG_TARGET_JOURNAL)) {
795
796 k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
797 if (k < 0 && k != -EAGAIN)
798 log_close_journal();
799 }
800
801 if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
802 LOG_TARGET_SYSLOG)) {
803
804 k = write_to_syslog(level, error, file, line, func, buffer);
805 if (k < 0 && k != -EAGAIN)
806 log_close_syslog();
807 }
808
809 if (k <= 0 &&
810 IN_SET(log_target, LOG_TARGET_AUTO,
811 LOG_TARGET_SYSLOG_OR_KMSG,
812 LOG_TARGET_JOURNAL_OR_KMSG,
813 LOG_TARGET_KMSG)) {
814
815 if (k < 0)
816 log_open_kmsg();
817
818 k = write_to_kmsg(level, error, file, line, func, buffer);
819 if (k < 0) {
820 log_close_kmsg();
821 (void) log_open_console();
822 }
823 }
824
825 if (k <= 0)
826 (void) write_to_console(level, error, file, line, func, buffer);
827
828 buffer = e;
829 } while (buffer);
830
831 if (open_when_needed)
832 log_close();
833
834 return -ERRNO_VALUE(error);
835 }
836
837 int log_dump_internal(
838 int level,
839 int error,
840 const char *file,
841 int line,
842 const char *func,
843 char *buffer) {
844
845 PROTECT_ERRNO;
846
847 /* This modifies the buffer... */
848
849 if (_likely_(LOG_PRI(level) > log_max_level))
850 return -ERRNO_VALUE(error);
851
852 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
853 }
854
855 int log_internalv(
856 int level,
857 int error,
858 const char *file,
859 int line,
860 const char *func,
861 const char *format,
862 va_list ap) {
863
864 if (_likely_(LOG_PRI(level) > log_max_level))
865 return -ERRNO_VALUE(error);
866
867 /* Make sure that %m maps to the specified error (or "Success"). */
868 char buffer[LINE_MAX];
869 LOCAL_ERRNO(ERRNO_VALUE(error));
870
871 (void) vsnprintf(buffer, sizeof buffer, format, ap);
872
873 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
874 }
875
876 int log_internal(
877 int level,
878 int error,
879 const char *file,
880 int line,
881 const char *func,
882 const char *format, ...) {
883
884 va_list ap;
885 int r;
886
887 va_start(ap, format);
888 r = log_internalv(level, error, file, line, func, format, ap);
889 va_end(ap);
890
891 return r;
892 }
893
894 int log_object_internalv(
895 int level,
896 int error,
897 const char *file,
898 int line,
899 const char *func,
900 const char *object_field,
901 const char *object,
902 const char *extra_field,
903 const char *extra,
904 const char *format,
905 va_list ap) {
906
907 char *buffer, *b;
908
909 if (_likely_(LOG_PRI(level) > log_max_level))
910 return -ERRNO_VALUE(error);
911
912 /* Make sure that %m maps to the specified error (or "Success"). */
913 LOCAL_ERRNO(ERRNO_VALUE(error));
914
915 LOG_SET_PREFIX(object);
916
917 b = buffer = newa(char, LINE_MAX);
918 (void) vsnprintf(b, LINE_MAX, format, ap);
919
920 return log_dispatch_internal(level, error, file, line, func,
921 object_field, object, extra_field, extra, buffer);
922 }
923
924 int log_object_internal(
925 int level,
926 int error,
927 const char *file,
928 int line,
929 const char *func,
930 const char *object_field,
931 const char *object,
932 const char *extra_field,
933 const char *extra,
934 const char *format, ...) {
935
936 va_list ap;
937 int r;
938
939 va_start(ap, format);
940 r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
941 va_end(ap);
942
943 return r;
944 }
945
946 int log_oom_internal(int level, const char *file, int line, const char *func) {
947 return log_internal(level, ENOMEM, file, line, func, "Out of memory.");
948 }
949
950 int log_format_iovec(
951 struct iovec *iovec,
952 size_t iovec_len,
953 size_t *n,
954 bool newline_separator,
955 int error,
956 const char *format,
957 va_list ap) {
958
959 assert(iovec);
960 assert(n);
961
962 while (format && *n + 1 < iovec_len) {
963 va_list aq;
964 char *m;
965 int r;
966
967 /* We need to copy the va_list structure,
968 * since vasprintf() leaves it afterwards at
969 * an undefined location */
970
971 errno = ERRNO_VALUE(error);
972
973 va_copy(aq, ap);
974 r = vasprintf(&m, format, aq);
975 va_end(aq);
976 if (r < 0)
977 return -EINVAL;
978
979 /* Now, jump enough ahead, so that we point to
980 * the next format string */
981 VA_FORMAT_ADVANCE(format, ap);
982
983 iovec[(*n)++] = IOVEC_MAKE_STRING(m);
984 if (newline_separator)
985 iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
986
987 format = va_arg(ap, char *);
988 }
989 return 0;
990 }
991
992 int log_struct_internal(
993 int level,
994 int error,
995 const char *file,
996 int line,
997 const char *func,
998 const char *format, ...) {
999
1000 char buf[LINE_MAX];
1001 bool found = false;
1002 PROTECT_ERRNO;
1003 va_list ap;
1004
1005 if (_likely_(LOG_PRI(level) > log_max_level) ||
1006 log_target == LOG_TARGET_NULL)
1007 return -ERRNO_VALUE(error);
1008
1009 if (LOG_FAC(level) == 0)
1010 level |= log_facility;
1011
1012 if (IN_SET(log_target,
1013 LOG_TARGET_AUTO,
1014 LOG_TARGET_JOURNAL_OR_KMSG,
1015 LOG_TARGET_JOURNAL)) {
1016
1017 if (open_when_needed)
1018 log_open_journal();
1019
1020 if (journal_fd >= 0) {
1021 char header[LINE_MAX];
1022 struct iovec *iovec;
1023 size_t n = 0, m, iovec_len;
1024 int r;
1025 bool fallback = false;
1026
1027 iovec_len = MIN(17 + log_context_num_fields() * 3, IOVEC_MAX);
1028 iovec = newa(struct iovec, iovec_len);
1029
1030 /* If the journal is available do structured logging.
1031 * Do not report the errno if it is synthetic. */
1032 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1033 iovec[n++] = IOVEC_MAKE_STRING(header);
1034
1035 va_start(ap, format);
1036 DISABLE_WARNING_FORMAT_NONLITERAL;
1037 r = log_format_iovec(iovec, iovec_len, &n, true, error, format, ap);
1038 REENABLE_WARNING;
1039 m = n;
1040 if (r < 0)
1041 fallback = true;
1042 else {
1043 log_do_context(iovec, iovec_len, &n);
1044
1045 const struct msghdr msghdr = {
1046 .msg_iov = iovec,
1047 .msg_iovlen = n,
1048 };
1049
1050 (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
1051 }
1052
1053 va_end(ap);
1054 for (size_t i = 1; i < m; i += 2)
1055 free(iovec[i].iov_base);
1056
1057 if (!fallback) {
1058 if (open_when_needed)
1059 log_close();
1060
1061 return -ERRNO_VALUE(error);
1062 }
1063 }
1064 }
1065
1066 /* Fallback if journal logging is not available or didn't work. */
1067
1068 va_start(ap, format);
1069 while (format) {
1070 va_list aq;
1071
1072 errno = ERRNO_VALUE(error);
1073
1074 va_copy(aq, ap);
1075 DISABLE_WARNING_FORMAT_NONLITERAL;
1076 (void) vsnprintf(buf, sizeof buf, format, aq);
1077 REENABLE_WARNING;
1078 va_end(aq);
1079
1080 if (startswith(buf, "MESSAGE=")) {
1081 found = true;
1082 break;
1083 }
1084
1085 VA_FORMAT_ADVANCE(format, ap);
1086
1087 format = va_arg(ap, char *);
1088 }
1089 va_end(ap);
1090
1091 if (!found) {
1092 if (open_when_needed)
1093 log_close();
1094
1095 return -ERRNO_VALUE(error);
1096 }
1097
1098 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
1099 }
1100
1101 int log_struct_iovec_internal(
1102 int level,
1103 int error,
1104 const char *file,
1105 int line,
1106 const char *func,
1107 const struct iovec input_iovec[],
1108 size_t n_input_iovec) {
1109
1110 PROTECT_ERRNO;
1111
1112 if (_likely_(LOG_PRI(level) > log_max_level) ||
1113 log_target == LOG_TARGET_NULL)
1114 return -ERRNO_VALUE(error);
1115
1116 if (LOG_FAC(level) == 0)
1117 level |= log_facility;
1118
1119 if (IN_SET(log_target, LOG_TARGET_AUTO,
1120 LOG_TARGET_JOURNAL_OR_KMSG,
1121 LOG_TARGET_JOURNAL) &&
1122 journal_fd >= 0) {
1123
1124 char header[LINE_MAX];
1125 struct iovec *iovec;
1126 size_t n = 0, iovec_len;
1127
1128 iovec_len = MIN(1 + n_input_iovec * 2 + log_context_num_fields() * 3, IOVEC_MAX);
1129 iovec = newa(struct iovec, iovec_len);
1130
1131 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1132
1133 iovec[n++] = IOVEC_MAKE_STRING(header);
1134 for (size_t i = 0; i < n_input_iovec; i++) {
1135 iovec[n++] = input_iovec[i];
1136 iovec[n++] = IOVEC_MAKE_STRING("\n");
1137 }
1138
1139 log_do_context(iovec, iovec_len, &n);
1140
1141 const struct msghdr msghdr = {
1142 .msg_iov = iovec,
1143 .msg_iovlen = n,
1144 };
1145
1146 if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0)
1147 return -ERRNO_VALUE(error);
1148 }
1149
1150 for (size_t i = 0; i < n_input_iovec; i++)
1151 if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) {
1152 char *m;
1153
1154 m = strndupa_safe((char*) input_iovec[i].iov_base + STRLEN("MESSAGE="),
1155 input_iovec[i].iov_len - STRLEN("MESSAGE="));
1156
1157 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
1158 }
1159
1160 /* Couldn't find MESSAGE=. */
1161 return -ERRNO_VALUE(error);
1162 }
1163
1164 int log_set_target_from_string(const char *e) {
1165 LogTarget t;
1166
1167 t = log_target_from_string(e);
1168 if (t < 0)
1169 return t;
1170
1171 log_set_target(t);
1172 return 0;
1173 }
1174
1175 int log_set_max_level_from_string(const char *e) {
1176 int r;
1177
1178 for (;;) {
1179 _cleanup_free_ char *word = NULL, *prefix = NULL;
1180 LogTarget target;
1181 const char *colon;
1182
1183 r = extract_first_word(&e, &word, ",", 0);
1184 if (r < 0)
1185 return r;
1186 if (r == 0)
1187 break;
1188
1189 colon = strchr(word, ':');
1190 if (!colon) {
1191 r = log_level_from_string(word);
1192 if (r < 0)
1193 return r;
1194
1195 log_set_max_level(r);
1196 continue;
1197 }
1198
1199 prefix = strndup(word, colon - word);
1200 if (!prefix)
1201 return -ENOMEM;
1202
1203 target = log_target_from_string(prefix);
1204 if (target < 0)
1205 return target;
1206
1207 if (target >= _LOG_TARGET_SINGLE_MAX)
1208 return -EINVAL;
1209
1210 r = log_level_from_string(colon + 1);
1211 if (r < 0)
1212 return r;
1213
1214 log_target_max_level[target] = r;
1215 }
1216
1217 return 0;
1218 }
1219
1220 int log_max_levels_to_string(int level, char **ret) {
1221 _cleanup_free_ char *s = NULL;
1222 int r;
1223
1224 assert(ret);
1225
1226 r = log_level_to_string_alloc(level, &s);
1227 if (r < 0)
1228 return r;
1229
1230 for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
1231 _cleanup_free_ char *l = NULL;
1232
1233 if (log_target_max_level[target] == INT_MAX)
1234 continue;
1235
1236 r = log_level_to_string_alloc(log_target_max_level[target], &l);
1237 if (r < 0)
1238 return r;
1239
1240 r = strextendf_with_separator(&s, ",", "%s:%s", log_target_to_string(target), l);
1241 if (r < 0)
1242 return r;
1243 }
1244
1245 *ret = TAKE_PTR(s);
1246 return 0;
1247 }
1248
1249 static int log_set_ratelimit_kmsg_from_string(const char *e) {
1250 int r;
1251
1252 r = parse_boolean(e);
1253 if (r < 0)
1254 return r;
1255
1256 ratelimit_kmsg = r;
1257 return 0;
1258 }
1259
1260 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
1261
1262 /*
1263 * The systemd.log_xyz= settings are parsed by all tools, and
1264 * so is "debug".
1265 *
1266 * However, "quiet" is only parsed by PID 1, and only turns of
1267 * status output to /dev/console, but does not alter the log
1268 * level.
1269 */
1270
1271 if (streq(key, "debug") && !value)
1272 log_set_max_level(LOG_DEBUG);
1273
1274 else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
1275
1276 if (proc_cmdline_value_missing(key, value))
1277 return 0;
1278
1279 if (log_set_target_from_string(value) < 0)
1280 log_warning("Failed to parse log target '%s', ignoring.", value);
1281
1282 } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
1283
1284 if (proc_cmdline_value_missing(key, value))
1285 return 0;
1286
1287 if (log_set_max_level_from_string(value) < 0)
1288 log_warning("Failed to parse log level setting '%s', ignoring.", value);
1289
1290 } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
1291
1292 if (log_show_color_from_string(value ?: "1") < 0)
1293 log_warning("Failed to parse log color setting '%s', ignoring.", value);
1294
1295 } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
1296
1297 if (log_show_location_from_string(value ?: "1") < 0)
1298 log_warning("Failed to parse log location setting '%s', ignoring.", value);
1299
1300 } else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
1301
1302 if (log_show_tid_from_string(value ?: "1") < 0)
1303 log_warning("Failed to parse log tid setting '%s', ignoring.", value);
1304
1305 } else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
1306
1307 if (log_show_time_from_string(value ?: "1") < 0)
1308 log_warning("Failed to parse log time setting '%s', ignoring.", value);
1309
1310 } else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
1311
1312 if (log_set_ratelimit_kmsg_from_string(value ?: "1") < 0)
1313 log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", value);
1314 }
1315
1316 return 0;
1317 }
1318
1319 static bool should_parse_proc_cmdline(void) {
1320 /* PID1 always reads the kernel command line. */
1321 if (getpid_cached() == 1)
1322 return true;
1323
1324 /* Otherwise, parse the command line if invoked directly by systemd. */
1325 return invoked_by_systemd();
1326 }
1327
1328 void log_parse_environment_variables(void) {
1329 const char *e;
1330 int r;
1331
1332 e = getenv("SYSTEMD_LOG_TARGET");
1333 if (e && log_set_target_from_string(e) < 0)
1334 log_warning("Failed to parse log target '%s', ignoring.", e);
1335
1336 e = getenv("SYSTEMD_LOG_LEVEL");
1337 if (e) {
1338 r = log_set_max_level_from_string(e);
1339 if (r < 0)
1340 log_warning_errno(r, "Failed to parse log level '%s', ignoring: %m", e);
1341 } else {
1342 /* If no explicit log level is specified then let's see if this is a debug invocation, and if
1343 * so raise the log level to debug too. Note that this is not symmetric: just because
1344 * DEBUG_INVOCATION is explicitly set to 0 we won't lower the log level below debug. This
1345 * follows the logic that debug logging is an opt-in thing anyway, and if there's any reason
1346 * to enable it we should not disable it here automatically. */
1347 r = getenv_bool("DEBUG_INVOCATION");
1348 if (r < 0 && r != -ENXIO)
1349 log_warning_errno(r, "Failed to parse $DEBUG_INVOCATION value, ignoring: %m");
1350 else if (r > 0)
1351 log_set_max_level(LOG_DEBUG);
1352 }
1353
1354 e = getenv("SYSTEMD_LOG_COLOR");
1355 if (e && log_show_color_from_string(e) < 0)
1356 log_warning("Failed to parse log color '%s', ignoring.", e);
1357
1358 e = getenv("SYSTEMD_LOG_LOCATION");
1359 if (e && log_show_location_from_string(e) < 0)
1360 log_warning("Failed to parse log location '%s', ignoring.", e);
1361
1362 e = getenv("SYSTEMD_LOG_TIME");
1363 if (e && log_show_time_from_string(e) < 0)
1364 log_warning("Failed to parse log time '%s', ignoring.", e);
1365
1366 e = getenv("SYSTEMD_LOG_TID");
1367 if (e && log_show_tid_from_string(e) < 0)
1368 log_warning("Failed to parse log tid '%s', ignoring.", e);
1369
1370 e = getenv("SYSTEMD_LOG_RATELIMIT_KMSG");
1371 if (e && log_set_ratelimit_kmsg_from_string(e) < 0)
1372 log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", e);
1373 }
1374
1375 void log_parse_environment(void) {
1376 /* Do not call from library code. */
1377
1378 if (should_parse_proc_cmdline())
1379 (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
1380
1381 log_parse_environment_variables();
1382 }
1383
1384 LogTarget log_get_target(void) {
1385 return log_target;
1386 }
1387
1388 void log_settle_target(void) {
1389
1390 /* If we're using LOG_TARGET_AUTO and opening the log again on every single log call, we'll check if
1391 * stderr is attached to the journal every single log call. However, if we then close all file
1392 * descriptors later, that will stop working because stderr will be closed as well. To avoid that
1393 * problem, this function is used to permanently change the log target depending on whether stderr is
1394 * connected to the journal or not. */
1395
1396 LogTarget t = log_get_target();
1397
1398 if (t != LOG_TARGET_AUTO)
1399 return;
1400
1401 t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
1402 : LOG_TARGET_CONSOLE;
1403 log_set_target(t);
1404 }
1405
1406 int log_get_max_level(void) {
1407 return log_max_level;
1408 }
1409
1410 int log_get_target_max_level(LogTarget target) {
1411 assert(target >= 0);
1412 assert(target < _LOG_TARGET_SINGLE_MAX);
1413 return log_target_max_level[target];
1414 }
1415
1416 void log_show_color(bool b) {
1417 show_color = b;
1418 }
1419
1420 bool log_get_show_color(void) {
1421 return show_color > 0; /* Defaults to false. */
1422 }
1423
1424 void log_show_location(bool b) {
1425 show_location = b;
1426 }
1427
1428 bool log_get_show_location(void) {
1429 return show_location;
1430 }
1431
1432 void log_show_time(bool b) {
1433 show_time = b;
1434 }
1435
1436 bool log_get_show_time(void) {
1437 return show_time;
1438 }
1439
1440 void log_show_tid(bool b) {
1441 show_tid = b;
1442 }
1443
1444 bool log_get_show_tid(void) {
1445 return show_tid;
1446 }
1447
1448 int log_show_color_from_string(const char *e) {
1449 int r;
1450
1451 r = parse_boolean(e);
1452 if (r < 0)
1453 return r;
1454
1455 log_show_color(r);
1456 return 0;
1457 }
1458
1459 int log_show_location_from_string(const char *e) {
1460 int r;
1461
1462 r = parse_boolean(e);
1463 if (r < 0)
1464 return r;
1465
1466 log_show_location(r);
1467 return 0;
1468 }
1469
1470 int log_show_time_from_string(const char *e) {
1471 int r;
1472
1473 r = parse_boolean(e);
1474 if (r < 0)
1475 return r;
1476
1477 log_show_time(r);
1478 return 0;
1479 }
1480
1481 int log_show_tid_from_string(const char *e) {
1482 int r;
1483
1484 r = parse_boolean(e);
1485 if (r < 0)
1486 return r;
1487
1488 log_show_tid(r);
1489 return 0;
1490 }
1491
1492 bool log_on_console(void) {
1493 if (IN_SET(log_target, LOG_TARGET_CONSOLE,
1494 LOG_TARGET_CONSOLE_PREFIXED))
1495 return true;
1496
1497 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1498 }
1499
1500 static const char *const log_target_table[_LOG_TARGET_MAX] = {
1501 [LOG_TARGET_CONSOLE] = "console",
1502 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1503 [LOG_TARGET_KMSG] = "kmsg",
1504 [LOG_TARGET_JOURNAL] = "journal",
1505 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1506 [LOG_TARGET_SYSLOG] = "syslog",
1507 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
1508 [LOG_TARGET_AUTO] = "auto",
1509 [LOG_TARGET_NULL] = "null",
1510 };
1511
1512 DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
1513
1514 void log_received_signal(int level, const struct signalfd_siginfo *si) {
1515 assert(si);
1516
1517 if (si_code_from_process(si->ssi_code) && pid_is_valid(si->ssi_pid)) {
1518 _cleanup_free_ char *p = NULL;
1519
1520 (void) pid_get_comm(si->ssi_pid, &p);
1521
1522 log_full(level,
1523 "Received SIG%s from PID %"PRIu32" (%s).",
1524 signal_to_string(si->ssi_signo),
1525 si->ssi_pid, strna(p));
1526 } else
1527 log_full(level,
1528 "Received SIG%s.",
1529 signal_to_string(si->ssi_signo));
1530 }
1531
1532 void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata) {
1533 assert(!log_syntax_callback || !cb);
1534 assert(!log_syntax_callback_userdata || !userdata);
1535
1536 log_syntax_callback = cb;
1537 log_syntax_callback_userdata = userdata;
1538 }
1539
1540 int log_syntax_internal(
1541 const char *unit,
1542 int level,
1543 const char *config_file,
1544 unsigned config_line,
1545 int error,
1546 const char *file,
1547 int line,
1548 const char *func,
1549 const char *format, ...) {
1550
1551 PROTECT_ERRNO;
1552
1553 if (log_syntax_callback)
1554 log_syntax_callback(unit, level, log_syntax_callback_userdata);
1555
1556 if (_likely_(LOG_PRI(level) > log_max_level) ||
1557 log_target == LOG_TARGET_NULL)
1558 return -ERRNO_VALUE(error);
1559
1560 char buffer[LINE_MAX];
1561 va_list ap;
1562 const char *unit_fmt = NULL;
1563
1564 errno = ERRNO_VALUE(error);
1565
1566 va_start(ap, format);
1567 (void) vsnprintf(buffer, sizeof buffer, format, ap);
1568 va_end(ap);
1569
1570 if (unit)
1571 unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
1572
1573 if (config_file) {
1574 if (config_line > 0)
1575 return log_struct_internal(
1576 level,
1577 error,
1578 file, line, func,
1579 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
1580 LOG_ITEM("CONFIG_FILE=%s", config_file),
1581 LOG_ITEM("CONFIG_LINE=%u", config_line),
1582 LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
1583 unit_fmt, unit,
1584 NULL);
1585 else
1586 return log_struct_internal(
1587 level,
1588 error,
1589 file, line, func,
1590 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
1591 LOG_ITEM("CONFIG_FILE=%s", config_file),
1592 LOG_MESSAGE("%s: %s", config_file, buffer),
1593 unit_fmt, unit,
1594 NULL);
1595 } else if (unit)
1596 return log_struct_internal(
1597 level,
1598 error,
1599 file, line, func,
1600 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
1601 LOG_MESSAGE("%s: %s", unit, buffer),
1602 unit_fmt, unit,
1603 NULL);
1604 else
1605 return log_struct_internal(
1606 level,
1607 error,
1608 file, line, func,
1609 LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
1610 LOG_MESSAGE("%s", buffer),
1611 NULL);
1612 }
1613
1614 int log_syntax_invalid_utf8_internal(
1615 const char *unit,
1616 int level,
1617 const char *config_file,
1618 unsigned config_line,
1619 const char *file,
1620 int line,
1621 const char *func,
1622 const char *rvalue) {
1623
1624 PROTECT_ERRNO;
1625 _cleanup_free_ char *p = NULL;
1626
1627 if (rvalue)
1628 p = utf8_escape_invalid(rvalue);
1629
1630 return log_syntax_internal(unit, level, config_file, config_line,
1631 SYNTHETIC_ERRNO(EINVAL), file, line, func,
1632 "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1633 }
1634
1635 int log_syntax_parse_error_internal(
1636 const char *unit,
1637 const char *config_file,
1638 unsigned config_line,
1639 int error,
1640 bool critical,
1641 const char *file,
1642 int line,
1643 const char *func,
1644 const char *lvalue,
1645 const char *rvalue) {
1646
1647 PROTECT_ERRNO;
1648 _cleanup_free_ char *escaped = NULL;
1649
1650 /* OOM is always handled as critical. */
1651 if (ERRNO_VALUE(error) == ENOMEM)
1652 return log_oom_internal(LOG_ERR, file, line, func);
1653
1654 if (rvalue && !utf8_is_valid(rvalue)) {
1655 escaped = utf8_escape_invalid(rvalue);
1656 if (!escaped)
1657 rvalue = "(oom)";
1658 else
1659 rvalue = " (escaped)";
1660 }
1661
1662 log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
1663 file, line, func,
1664 "Failed to parse %s=%s%s%s%s%s",
1665 strna(lvalue), strempty(escaped), strempty(rvalue),
1666 critical ? "" : ", ignoring",
1667 error == 0 ? "." : ": ",
1668 error == 0 ? "" : STRERROR(error));
1669
1670 return critical ? -ERRNO_VALUE(error) : 0;
1671 }
1672
1673 void log_set_upgrade_syslog_to_journal(bool b) {
1674 upgrade_syslog_to_journal = b;
1675
1676 /* Make the change effective immediately */
1677 if (b) {
1678 if (log_target == LOG_TARGET_SYSLOG)
1679 log_target = LOG_TARGET_JOURNAL;
1680 else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
1681 log_target = LOG_TARGET_JOURNAL_OR_KMSG;
1682 }
1683 }
1684
1685 void log_set_always_reopen_console(bool b) {
1686 always_reopen_console = b;
1687 }
1688
1689 void log_set_open_when_needed(bool b) {
1690 open_when_needed = b;
1691 }
1692
1693 void log_set_prohibit_ipc(bool b) {
1694 prohibit_ipc = b;
1695 }
1696
1697 int log_emergency_level(void) {
1698 /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1699 * then the system of the whole system is obviously affected. */
1700
1701 return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
1702 }
1703
1704 int log_dup_console(void) {
1705 int copy;
1706
1707 /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
1708 * whenever we want to continue logging through the original fd, but want to rearrange stderr. */
1709
1710 if (console_fd < 0 || console_fd >= 3)
1711 return 0;
1712
1713 copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);
1714 if (copy < 0)
1715 return -errno;
1716
1717 console_fd = copy;
1718 return 0;
1719 }
1720
1721 void log_setup(void) {
1722 log_set_target(LOG_TARGET_AUTO);
1723 log_parse_environment();
1724 (void) log_open();
1725 if (log_on_console() && show_color < 0)
1726 log_show_color(true);
1727 }
1728
1729 const char* _log_set_prefix(const char *prefix, bool force) {
1730 const char *old = log_prefix;
1731
1732 if (prefix || force)
1733 log_prefix = prefix;
1734
1735 return old;
1736 }