]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/log.c
Turn VALGRIND variable into a meson configuration switch
[thirdparty/systemd.git] / src / basic / log.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
a7334b09
LP
2/***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
a7334b09
LP
6***/
7
c9b97d2a 8#include <errno.h>
16801e90 9#include <fcntl.h>
11c3a366
TA
10#include <inttypes.h>
11#include <limits.h>
07630cea
LP
12#include <stdarg.h>
13#include <stddef.h>
14#include <stdio.h>
11c3a366
TA
15#include <string.h>
16#include <sys/signalfd.h>
16801e90 17#include <sys/socket.h>
11c3a366
TA
18#include <sys/time.h>
19#include <sys/uio.h>
16801e90 20#include <sys/un.h>
11c3a366 21#include <time.h>
07630cea 22#include <unistd.h>
5899f3b7 23
158350e8 24#include "sd-messages.h"
07630cea 25
b5efdb8a 26#include "alloc-util.h"
3ffd4af2 27#include "fd-util.h"
f97b34a6 28#include "format-util.h"
afc5dbf3 29#include "io-util.h"
3ffd4af2 30#include "log.h"
07630cea
LP
31#include "macro.h"
32#include "missing.h"
4e731273
LP
33#include "parse-util.h"
34#include "proc-cmdline.h"
0b452006 35#include "process-util.h"
24882e06 36#include "signal-util.h"
07630cea 37#include "socket-util.h"
15a5e950 38#include "stdio-util.h"
8b43440b 39#include "string-table.h"
07630cea 40#include "string-util.h"
7ccbd1ae 41#include "syslog-util.h"
07630cea 42#include "terminal-util.h"
93cc7779 43#include "time-util.h"
d04ce5a9 44#include "utf8.h"
07630cea 45#include "util.h"
5899f3b7 46
bb99a35a
LP
47#define SNDBUF_SIZE (8*1024*1024)
48
16801e90 49static LogTarget log_target = LOG_TARGET_CONSOLE;
ff524019
ZJS
50static int log_max_level[] = {LOG_INFO, LOG_INFO};
51assert_cc(ELEMENTSOF(log_max_level) == _LOG_REALM_MAX);
3eff4208 52static int log_facility = LOG_DAEMON;
16801e90 53
843d2643 54static int console_fd = STDERR_FILENO;
16801e90
LP
55static int syslog_fd = -1;
56static int kmsg_fd = -1;
5ba081b0 57static int journal_fd = -1;
16801e90 58
c31e1495
LP
59static bool syslog_is_stream = false;
60
bbe63281
LP
61static bool show_color = false;
62static bool show_location = false;
63
c1dc6153 64static bool upgrade_syslog_to_journal = false;
48a601fe 65static bool always_reopen_console = false;
16e4fd87 66static bool open_when_needed = false;
adf47c91 67static bool prohibit_ipc = false;
c1dc6153 68
35b8ca3a 69/* Akin to glibc's __abort_msg; which is private and we hence cannot
185986c6
LP
70 * use here. */
71static char *log_abort_msg = NULL;
72
780747da
ZJS
73/* An assert to use in logging functions that does not call recursively
74 * into our logging functions (since that might lead to a loop). */
75#define assert_raw(expr) \
76 do { \
77 if (_unlikely_(!(expr))) { \
78 fputs(#expr "\n", stderr); \
79 abort(); \
80 } \
81 } while (false)
82
cc2b9e6b 83static void log_close_console(void) {
e7685a77 84 console_fd = safe_close_above_stdio(console_fd);
16801e90
LP
85}
86
843d2643 87static int log_open_console(void) {
16801e90 88
cc2b9e6b
AJ
89 if (!always_reopen_console) {
90 console_fd = STDERR_FILENO;
16801e90 91 return 0;
cc2b9e6b 92 }
843d2643 93
cc2b9e6b 94 if (console_fd < 3) {
5ba081b0 95 console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
cd15c418 96 if (console_fd < 0)
843d2643 97 return console_fd;
7fe2903c
LP
98
99 console_fd = fd_move_above_stdio(console_fd);
cc2b9e6b 100 }
843d2643
LP
101
102 return 0;
103}
104
cc2b9e6b 105static void log_close_kmsg(void) {
03e334a1 106 kmsg_fd = safe_close(kmsg_fd);
843d2643
LP
107}
108
109static int log_open_kmsg(void) {
16801e90
LP
110
111 if (kmsg_fd >= 0)
112 return 0;
113
674f8283 114 kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
cd15c418 115 if (kmsg_fd < 0)
16801e90 116 return -errno;
0dae83f9 117
7fe2903c 118 kmsg_fd = fd_move_above_stdio(kmsg_fd);
16801e90
LP
119 return 0;
120}
121
cc2b9e6b 122static void log_close_syslog(void) {
03e334a1 123 syslog_fd = safe_close(syslog_fd);
16801e90
LP
124}
125
c31e1495 126static int create_log_socket(int type) {
8b18fdc1 127 struct timeval tv;
95066a90 128 int fd;
c31e1495 129
8b18fdc1 130 fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
5ba081b0 131 if (fd < 0)
c31e1495
LP
132 return -errno;
133
7fe2903c 134 fd = fd_move_above_stdio(fd);
0474ef7b 135 (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
bb99a35a 136
7fe2903c
LP
137 /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
138 * in the unlikely case of a deadlock. */
df0ff127 139 if (getpid_cached() == 1)
4d89874a
ZJS
140 timeval_store(&tv, 10 * USEC_PER_MSEC);
141 else
142 timeval_store(&tv, 10 * USEC_PER_SEC);
086891e5 143 (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
8b18fdc1 144
c31e1495
LP
145 return fd;
146}
147
843d2643 148static int log_open_syslog(void) {
95066a90
LP
149
150 static const union sockaddr_union sa = {
b92bea5d
ZJS
151 .un.sun_family = AF_UNIX,
152 .un.sun_path = "/dev/log",
153 };
16801e90 154
95066a90
LP
155 int r;
156
16801e90
LP
157 if (syslog_fd >= 0)
158 return 0;
159
5ba081b0
LP
160 syslog_fd = create_log_socket(SOCK_DGRAM);
161 if (syslog_fd < 0) {
162 r = syslog_fd;
843d2643 163 goto fail;
16801e90
LP
164 }
165
fc2fffe7 166 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
03e334a1 167 safe_close(syslog_fd);
c31e1495
LP
168
169 /* Some legacy syslog systems still use stream
170 * sockets. They really shouldn't. But what can we
171 * do... */
5ba081b0
LP
172 syslog_fd = create_log_socket(SOCK_STREAM);
173 if (syslog_fd < 0) {
174 r = syslog_fd;
c31e1495
LP
175 goto fail;
176 }
177
fc2fffe7 178 if (connect(syslog_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
c31e1495
LP
179 r = -errno;
180 goto fail;
181 }
182
183 syslog_is_stream = true;
184 } else
185 syslog_is_stream = false;
186
16801e90 187 return 0;
843d2643
LP
188
189fail:
190 log_close_syslog();
843d2643
LP
191 return r;
192}
193
cc2b9e6b 194static void log_close_journal(void) {
03e334a1 195 journal_fd = safe_close(journal_fd);
5ba081b0
LP
196}
197
198static int log_open_journal(void) {
95066a90
LP
199
200 static const union sockaddr_union sa = {
b92bea5d
ZJS
201 .un.sun_family = AF_UNIX,
202 .un.sun_path = "/run/systemd/journal/socket",
203 };
95066a90 204
5ba081b0
LP
205 int r;
206
207 if (journal_fd >= 0)
208 return 0;
209
210 journal_fd = create_log_socket(SOCK_DGRAM);
211 if (journal_fd < 0) {
212 r = journal_fd;
213 goto fail;
214 }
215
fc2fffe7 216 if (connect(journal_fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)) < 0) {
5ba081b0
LP
217 r = -errno;
218 goto fail;
219 }
220
5ba081b0
LP
221 return 0;
222
223fail:
224 log_close_journal();
5ba081b0
LP
225 return r;
226}
227
843d2643
LP
228int log_open(void) {
229 int r;
230
e3e42fc2
ZJS
231 /* Do not call from library code. */
232
843d2643
LP
233 /* If we don't use the console we close it here, to not get
234 * killed by SAK. If we don't use syslog we close it here so
235 * that we are not confused by somebody deleting the socket in
cc2b9e6b
AJ
236 * the fs, and to make sure we don't use it if prohibit_ipc is
237 * set. If we don't use /dev/kmsg we still keep it open,
843d2643
LP
238 * because there is no reason to close it. */
239
9fae33d2 240 if (log_target == LOG_TARGET_NULL) {
5ba081b0 241 log_close_journal();
9fae33d2
LP
242 log_close_syslog();
243 log_close_console();
244 return 0;
245 }
246
6c347d50 247 if (log_target != LOG_TARGET_AUTO ||
df0ff127 248 getpid_cached() == 1 ||
f41c094c 249 isatty(STDERR_FILENO) <= 0) {
bb7df0da 250
adf47c91
LP
251 if (!prohibit_ipc &&
252 IN_SET(log_target, LOG_TARGET_AUTO,
5b5688af
ZJS
253 LOG_TARGET_JOURNAL_OR_KMSG,
254 LOG_TARGET_JOURNAL)) {
5ba081b0
LP
255 r = log_open_journal();
256 if (r >= 0) {
257 log_close_syslog();
bb7df0da
LP
258 log_close_console();
259 return r;
260 }
5ba081b0
LP
261 }
262
adf47c91
LP
263 if (!prohibit_ipc &&
264 IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
5b5688af 265 LOG_TARGET_SYSLOG)) {
5ba081b0
LP
266 r = log_open_syslog();
267 if (r >= 0) {
268 log_close_journal();
269 log_close_console();
270 return r;
271 }
272 }
273
5b5688af 274 if (IN_SET(log_target, LOG_TARGET_AUTO,
5b5688af
ZJS
275 LOG_TARGET_JOURNAL_OR_KMSG,
276 LOG_TARGET_SYSLOG_OR_KMSG,
277 LOG_TARGET_KMSG)) {
5ba081b0
LP
278 r = log_open_kmsg();
279 if (r >= 0) {
280 log_close_journal();
bb7df0da
LP
281 log_close_syslog();
282 log_close_console();
283 return r;
284 }
5ba081b0 285 }
bb7df0da 286 }
843d2643 287
5ba081b0 288 log_close_journal();
843d2643 289 log_close_syslog();
dcdf86bb 290
843d2643 291 return log_open_console();
16801e90
LP
292}
293
294void log_set_target(LogTarget target) {
295 assert(target >= 0);
296 assert(target < _LOG_TARGET_MAX);
297
c1dc6153
LP
298 if (upgrade_syslog_to_journal) {
299 if (target == LOG_TARGET_SYSLOG)
300 target = LOG_TARGET_JOURNAL;
301 else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
302 target = LOG_TARGET_JOURNAL_OR_KMSG;
303 }
304
16801e90
LP
305 log_target = target;
306}
307
871e5809 308void log_close(void) {
e3e42fc2
ZJS
309 /* Do not call from library code. */
310
5ba081b0 311 log_close_journal();
871e5809 312 log_close_syslog();
5ba081b0
LP
313 log_close_kmsg();
314 log_close_console();
871e5809
LP
315}
316
4d8a7798 317void log_forget_fds(void) {
e3e42fc2
ZJS
318 /* Do not call from library code. */
319
5ba081b0 320 console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
4d8a7798
MS
321}
322
ff524019 323void log_set_max_level_realm(LogRealm realm, int level) {
16801e90 324 assert((level & LOG_PRIMASK) == level);
ff524019 325 assert(realm < ELEMENTSOF(log_max_level));
16801e90 326
ff524019 327 log_max_level[realm] = level;
16801e90
LP
328}
329
3eff4208
LP
330void log_set_facility(int facility) {
331 log_facility = facility;
332}
333
843d2643 334static int write_to_console(
20c03b7b 335 int level,
086891e5 336 int error,
95066a90 337 const char *file,
20c03b7b
LP
338 int line,
339 const char *func,
340 const char *buffer) {
5899f3b7 341
79c95440 342 char location[256], prefix[1 + DECIMAL_STR_MAX(int) + 2];
aca83a53 343 struct iovec iovec[6] = {};
843d2643 344 bool highlight;
da6053d0 345 size_t n = 0;
5899f3b7 346
843d2643
LP
347 if (console_fd < 0)
348 return 0;
349
aca83a53 350 if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
0474ef7b 351 xsprintf(prefix, "<%i>", level);
e6a7ec4b 352 iovec[n++] = IOVEC_MAKE_STRING(prefix);
aca83a53
LP
353 }
354
bbe63281 355 highlight = LOG_PRI(level) <= LOG_ERR && show_color;
843d2643 356
674f8283 357 if (show_location) {
4ad2b562 358 (void) snprintf(location, sizeof location, "(%s:%i) ", file, line);
e6a7ec4b 359 iovec[n++] = IOVEC_MAKE_STRING(location);
674f8283
LP
360 }
361
843d2643 362 if (highlight)
e6a7ec4b
LP
363 iovec[n++] = IOVEC_MAKE_STRING(ANSI_HIGHLIGHT_RED);
364 iovec[n++] = IOVEC_MAKE_STRING(buffer);
843d2643 365 if (highlight)
e6a7ec4b
LP
366 iovec[n++] = IOVEC_MAKE_STRING(ANSI_NORMAL);
367 iovec[n++] = IOVEC_MAKE_STRING("\n");
843d2643 368
0e6eaa2d
LP
369 if (writev(console_fd, iovec, n) < 0) {
370
df0ff127 371 if (errno == EIO && getpid_cached() == 1) {
0e6eaa2d
LP
372
373 /* If somebody tried to kick us from our
374 * console tty (via vhangup() or suchlike),
375 * try to reconnect */
376
377 log_close_console();
378 log_open_console();
379
380 if (console_fd < 0)
381 return 0;
382
383 if (writev(console_fd, iovec, n) < 0)
384 return -errno;
385 } else
386 return -errno;
387 }
5899f3b7 388
843d2643 389 return 1;
16801e90 390}
5899f3b7 391
16801e90 392static int write_to_syslog(
086891e5
LP
393 int level,
394 int error,
95066a90 395 const char *file,
086891e5
LP
396 int line,
397 const char *func,
086891e5 398 const char *buffer) {
16801e90 399
5ffa8c81
ZJS
400 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
401 header_time[64],
402 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
b92bea5d
ZJS
403 struct iovec iovec[5] = {};
404 struct msghdr msghdr = {
405 .msg_iov = iovec,
406 .msg_iovlen = ELEMENTSOF(iovec),
407 };
16801e90
LP
408 time_t t;
409 struct tm *tm;
410
411 if (syslog_fd < 0)
843d2643 412 return 0;
16801e90 413
5ffa8c81 414 xsprintf(header_priority, "<%i>", level);
16801e90
LP
415
416 t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
fdf9f9bb
ZJS
417 tm = localtime(&t);
418 if (!tm)
16801e90
LP
419 return -EINVAL;
420
421 if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
422 return -EINVAL;
423
df0ff127 424 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
16801e90 425
e6a7ec4b
LP
426 iovec[0] = IOVEC_MAKE_STRING(header_priority);
427 iovec[1] = IOVEC_MAKE_STRING(header_time);
428 iovec[2] = IOVEC_MAKE_STRING(program_invocation_short_name);
429 iovec[3] = IOVEC_MAKE_STRING(header_pid);
430 iovec[4] = IOVEC_MAKE_STRING(buffer);
16801e90 431
c899f8c6 432 /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
c31e1495
LP
433 if (syslog_is_stream)
434 iovec[4].iov_len++;
435
c31e1495
LP
436 for (;;) {
437 ssize_t n;
438
8f7f7a1b
MS
439 n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
440 if (n < 0)
c31e1495
LP
441 return -errno;
442
443 if (!syslog_is_stream ||
444 (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
445 break;
446
447 IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
448 }
16801e90 449
843d2643 450 return 1;
16801e90
LP
451}
452
453static int write_to_kmsg(
086891e5
LP
454 int level,
455 int error,
bcf5c276 456 const char *file,
086891e5
LP
457 int line,
458 const char *func,
086891e5 459 const char *buffer) {
16801e90 460
5ffa8c81
ZJS
461 char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
462 header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
b92bea5d 463 struct iovec iovec[5] = {};
16801e90
LP
464
465 if (kmsg_fd < 0)
843d2643 466 return 0;
16801e90 467
5ffa8c81 468 xsprintf(header_priority, "<%i>", level);
df0ff127 469 xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
16801e90 470
e6a7ec4b
LP
471 iovec[0] = IOVEC_MAKE_STRING(header_priority);
472 iovec[1] = IOVEC_MAKE_STRING(program_invocation_short_name);
473 iovec[2] = IOVEC_MAKE_STRING(header_pid);
474 iovec[3] = IOVEC_MAKE_STRING(buffer);
475 iovec[4] = IOVEC_MAKE_STRING("\n");
16801e90
LP
476
477 if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
478 return -errno;
479
843d2643 480 return 1;
16801e90
LP
481}
482
086891e5
LP
483static int log_do_header(
484 char *header,
485 size_t size,
486 int level,
487 int error,
488 const char *file, int line, const char *func,
4b58153d
LP
489 const char *object_field, const char *object,
490 const char *extra_field, const char *extra) {
f8e6f4aa 491 int r;
086891e5 492
f8e6f4aa
ZJS
493 r = snprintf(header, size,
494 "PRIORITY=%i\n"
495 "SYSLOG_FACILITY=%i\n"
496 "%s%.256s%s" /* CODE_FILE */
497 "%s%.*i%s" /* CODE_LINE */
498 "%s%.256s%s" /* CODE_FUNC */
499 "%s%.*i%s" /* ERRNO */
500 "%s%.256s%s" /* object */
501 "%s%.256s%s" /* extra */
502 "SYSLOG_IDENTIFIER=%.256s\n",
503 LOG_PRI(level),
504 LOG_FAC(level),
505 isempty(file) ? "" : "CODE_FILE=",
506 isempty(file) ? "" : file,
507 isempty(file) ? "" : "\n",
508 line ? "CODE_LINE=" : "",
509 line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
510 line ? "\n" : "",
511 isempty(func) ? "" : "CODE_FUNC=",
512 isempty(func) ? "" : func,
513 isempty(func) ? "" : "\n",
514 error ? "ERRNO=" : "",
515 error ? 1 : 0, error,
516 error ? "\n" : "",
517 isempty(object) ? "" : object_field,
518 isempty(object) ? "" : object,
519 isempty(object) ? "" : "\n",
520 isempty(extra) ? "" : extra_field,
521 isempty(extra) ? "" : extra,
522 isempty(extra) ? "" : "\n",
523 program_invocation_short_name);
780747da 524 assert_raw((size_t) r < size);
086891e5 525
41a79f10
ZJS
526 return 0;
527}
5ba081b0 528
41a79f10 529static int write_to_journal(
086891e5
LP
530 int level,
531 int error,
bcf5c276 532 const char *file,
086891e5
LP
533 int line,
534 const char *func,
535 const char *object_field,
536 const char *object,
4b58153d
LP
537 const char *extra_field,
538 const char *extra,
086891e5 539 const char *buffer) {
41a79f10
ZJS
540
541 char header[LINE_MAX];
b92bea5d
ZJS
542 struct iovec iovec[4] = {};
543 struct msghdr mh = {};
41a79f10
ZJS
544
545 if (journal_fd < 0)
546 return 0;
547
4b58153d 548 log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
5ba081b0 549
e6a7ec4b
LP
550 iovec[0] = IOVEC_MAKE_STRING(header);
551 iovec[1] = IOVEC_MAKE_STRING("MESSAGE=");
552 iovec[2] = IOVEC_MAKE_STRING(buffer);
553 iovec[3] = IOVEC_MAKE_STRING("\n");
5ba081b0 554
5ba081b0
LP
555 mh.msg_iov = iovec;
556 mh.msg_iovlen = ELEMENTSOF(iovec);
557
558 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
559 return -errno;
560
561 return 1;
562}
563
93484b47 564int log_dispatch_internal(
086891e5
LP
565 int level,
566 int error,
95066a90 567 const char *file,
086891e5
LP
568 int line,
569 const char *func,
570 const char *object_field,
571 const char *object,
4b58153d 572 const char *extra_field,
4dd09c6a 573 const char *extra,
086891e5 574 char *buffer) {
843d2643 575
780747da 576 assert_raw(buffer);
843d2643 577
0474ef7b
LP
578 if (error < 0)
579 error = -error;
580
9fae33d2 581 if (log_target == LOG_TARGET_NULL)
bf371116 582 return -error;
9fae33d2 583
29db5834 584 /* Patch in LOG_DAEMON facility if necessary */
7d76f312 585 if ((level & LOG_FACMASK) == 0)
3eff4208 586 level = log_facility | LOG_PRI(level);
29db5834 587
16e4fd87
LP
588 if (open_when_needed)
589 log_open();
590
9726b29e
LP
591 do {
592 char *e;
9499b235 593 int k = 0;
843d2643 594
9726b29e 595 buffer += strspn(buffer, NEWLINE);
843d2643 596
9726b29e
LP
597 if (buffer[0] == 0)
598 break;
843d2643 599
9726b29e
LP
600 if ((e = strpbrk(buffer, NEWLINE)))
601 *(e++) = 0;
843d2643 602
5b5688af
ZJS
603 if (IN_SET(log_target, LOG_TARGET_AUTO,
604 LOG_TARGET_JOURNAL_OR_KMSG,
605 LOG_TARGET_JOURNAL)) {
5ba081b0 606
4b58153d 607 k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
e38b8a40
LP
608 if (k < 0 && k != -EAGAIN)
609 log_close_journal();
5ba081b0
LP
610 }
611
5b5688af
ZJS
612 if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
613 LOG_TARGET_SYSLOG)) {
9726b29e 614
4b58153d 615 k = write_to_syslog(level, error, file, line, func, buffer);
e38b8a40
LP
616 if (k < 0 && k != -EAGAIN)
617 log_close_syslog();
9726b29e
LP
618 }
619
9499b235 620 if (k <= 0 &&
5b5688af 621 IN_SET(log_target, LOG_TARGET_AUTO,
5b5688af
ZJS
622 LOG_TARGET_SYSLOG_OR_KMSG,
623 LOG_TARGET_JOURNAL_OR_KMSG,
624 LOG_TARGET_KMSG)) {
9726b29e 625
e38b8a40
LP
626 if (k < 0)
627 log_open_kmsg();
628
4b58153d 629 k = write_to_kmsg(level, error, file, line, func, buffer);
4e7bc3f3
LP
630 if (k < 0) {
631 log_close_kmsg();
9726b29e 632 log_open_console();
bf371116 633 }
9726b29e
LP
634 }
635
bf371116 636 if (k <= 0)
4b58153d 637 (void) write_to_console(level, error, file, line, func, buffer);
9726b29e
LP
638
639 buffer = e;
640 } while (buffer);
641
16e4fd87
LP
642 if (open_when_needed)
643 log_close();
644
bf371116 645 return -error;
843d2643
LP
646}
647
2149e37c 648int log_dump_internal(
ff524019
ZJS
649 int level,
650 int error,
651 const char *file,
652 int line,
653 const char *func,
654 char *buffer) {
2149e37c 655
ff524019 656 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
5c0aa72a 657 PROTECT_ERRNO;
2149e37c
LP
658
659 /* This modifies the buffer... */
660
bf371116
LP
661 if (error < 0)
662 error = -error;
663
ff524019 664 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
bf371116 665 return -error;
2149e37c 666
93484b47 667 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
2149e37c
LP
668}
669
ff524019 670int log_internalv_realm(
086891e5
LP
671 int level,
672 int error,
bcf5c276 673 const char *file,
086891e5
LP
674 int line,
675 const char *func,
676 const char *format,
677 va_list ap) {
16801e90 678
ff524019 679 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
addab137 680 char buffer[LINE_MAX];
ff524019 681 PROTECT_ERRNO;
16801e90 682
bf371116
LP
683 if (error < 0)
684 error = -error;
685
ff524019 686 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
bf371116 687 return -error;
16801e90 688
b29f6480
ZJS
689 /* Make sure that %m maps to the specified error (or "Success"). */
690 errno = error;
086891e5 691
4ad2b562 692 (void) vsnprintf(buffer, sizeof buffer, format, ap);
843d2643 693
93484b47 694 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
185986c6 695}
16801e90 696
ff524019 697int log_internal_realm(
086891e5
LP
698 int level,
699 int error,
bcf5c276 700 const char *file,
086891e5
LP
701 int line,
702 const char *func,
703 const char *format, ...) {
17a94911 704
17a94911 705 va_list ap;
bf371116 706 int r;
17a94911
LP
707
708 va_start(ap, format);
ff524019 709 r = log_internalv_realm(level, error, file, line, func, format, ap);
17a94911
LP
710 va_end(ap);
711
712 return r;
713}
714
a3bf7137
ZJS
715_printf_(10,0)
716static int log_object_internalv(
086891e5
LP
717 int level,
718 int error,
bcf5c276 719 const char *file,
086891e5
LP
720 int line,
721 const char *func,
722 const char *object_field,
723 const char *object,
4b58153d
LP
724 const char *extra_field,
725 const char *extra,
086891e5
LP
726 const char *format,
727 va_list ap) {
fdf9f9bb 728
5c0aa72a 729 PROTECT_ERRNO;
f2341e0a 730 char *buffer, *b;
fdf9f9bb 731
bf371116
LP
732 if (error < 0)
733 error = -error;
734
ff524019 735 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
bf371116 736 return -error;
fdf9f9bb 737
d1a1f0aa
ZJS
738 /* Make sure that %m maps to the specified error (or "Success"). */
739 errno = error;
086891e5 740
f2341e0a
LP
741 /* Prepend the object name before the message */
742 if (object) {
743 size_t n;
744
745 n = strlen(object);
1741b25c 746 buffer = newa(char, n + 2 + LINE_MAX);
f2341e0a 747 b = stpcpy(stpcpy(buffer, object), ": ");
1741b25c
EV
748 } else
749 b = buffer = newa(char, LINE_MAX);
f2341e0a 750
4ad2b562 751 (void) vsnprintf(b, LINE_MAX, format, ap);
fdf9f9bb 752
93484b47
ZJS
753 return log_dispatch_internal(level, error, file, line, func,
754 object_field, object, extra_field, extra, buffer);
fdf9f9bb
ZJS
755}
756
79008bdd 757int log_object_internal(
086891e5
LP
758 int level,
759 int error,
bcf5c276 760 const char *file,
086891e5
LP
761 int line,
762 const char *func,
763 const char *object_field,
764 const char *object,
4b58153d
LP
765 const char *extra_field,
766 const char *extra,
086891e5 767 const char *format, ...) {
fdf9f9bb 768
fdf9f9bb 769 va_list ap;
bf371116 770 int r;
fdf9f9bb
ZJS
771
772 va_start(ap, format);
4b58153d 773 r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
fdf9f9bb
ZJS
774 va_end(ap);
775
776 return r;
777}
778
086891e5
LP
779static void log_assert(
780 int level,
781 const char *text,
782 const char *file,
783 int line,
784 const char *func,
785 const char *format) {
786
addab137 787 static char buffer[LINE_MAX];
ff524019 788 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
185986c6 789
ff524019 790 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
50f72bca
ZJS
791 return;
792
bcfce235 793 DISABLE_WARNING_FORMAT_NONLITERAL;
4ad2b562 794 (void) snprintf(buffer, sizeof buffer, format, text, file, line, func);
bcfce235 795 REENABLE_WARNING;
185986c6 796
185986c6
LP
797 log_abort_msg = buffer;
798
93484b47 799 log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer);
5899f3b7 800}
34f0e866 801
848e863a 802_noreturn_ void log_assert_failed_realm(
ff524019
ZJS
803 LogRealm realm,
804 const char *text,
805 const char *file,
806 int line,
807 const char *func) {
ea89a119 808 log_open();
ff524019
ZJS
809 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
810 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
80514f9c 811 abort();
b7f33638
MS
812}
813
848e863a 814_noreturn_ void log_assert_failed_unreachable_realm(
ff524019
ZJS
815 LogRealm realm,
816 const char *text,
817 const char *file,
818 int line,
819 const char *func) {
ea89a119 820 log_open();
ff524019
ZJS
821 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_CRIT), text, file, line, func,
822 "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
80514f9c
LP
823 abort();
824}
825
ff524019
ZJS
826void log_assert_failed_return_realm(
827 LogRealm realm,
828 const char *text,
829 const char *file,
830 int line,
831 const char *func) {
e5ca092c 832 PROTECT_ERRNO;
ff524019
ZJS
833 log_assert(LOG_REALM_PLUS_LEVEL(realm, LOG_DEBUG), text, file, line, func,
834 "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
b7f33638
MS
835}
836
ff524019 837int log_oom_internal(LogRealm realm, const char *file, int line, const char *func) {
7558e10c
LP
838 return log_internal_realm(LOG_REALM_PLUS_LEVEL(realm, LOG_ERR),
839 ENOMEM, file, line, func, "Out of memory.");
6dc1e7e0
MS
840}
841
8a03c9ef
ZJS
842int log_format_iovec(
843 struct iovec *iovec,
d3070fbd
LP
844 size_t iovec_len,
845 size_t *n,
8a03c9ef
ZJS
846 bool newline_separator,
847 int error,
848 const char *format,
849 va_list ap) {
850
851 static const char nl = '\n';
852
853 while (format && *n + 1 < iovec_len) {
854 va_list aq;
855 char *m;
856 int r;
857
858 /* We need to copy the va_list structure,
859 * since vasprintf() leaves it afterwards at
860 * an undefined location */
861
d1a1f0aa 862 errno = error;
8a03c9ef
ZJS
863
864 va_copy(aq, ap);
865 r = vasprintf(&m, format, aq);
866 va_end(aq);
867 if (r < 0)
868 return -EINVAL;
869
870 /* Now, jump enough ahead, so that we point to
871 * the next format string */
872 VA_FORMAT_ADVANCE(format, ap);
873
e6a7ec4b 874 iovec[(*n)++] = IOVEC_MAKE_STRING(m);
8a03c9ef
ZJS
875
876 if (newline_separator) {
877 iovec[*n].iov_base = (char*) &nl;
878 iovec[*n].iov_len = 1;
879 (*n)++;
880 }
881
882 format = va_arg(ap, char *);
883 }
884 return 0;
885}
886
877d54e9
LP
887int log_struct_internal(
888 int level,
086891e5 889 int error,
877d54e9
LP
890 const char *file,
891 int line,
892 const char *func,
893 const char *format, ...) {
894
e6a7ec4b 895 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
bf371116
LP
896 char buf[LINE_MAX];
897 bool found = false;
5c0aa72a 898 PROTECT_ERRNO;
877d54e9 899 va_list ap;
bf371116
LP
900
901 if (error < 0)
902 error = -error;
877d54e9 903
ff524019 904 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
bf371116 905 return -error;
877d54e9
LP
906
907 if (log_target == LOG_TARGET_NULL)
bf371116 908 return -error;
877d54e9
LP
909
910 if ((level & LOG_FACMASK) == 0)
911 level = log_facility | LOG_PRI(level);
912
16e4fd87
LP
913 if (IN_SET(log_target,
914 LOG_TARGET_AUTO,
915 LOG_TARGET_JOURNAL_OR_KMSG,
916 LOG_TARGET_JOURNAL)) {
917
918 if (open_when_needed)
919 log_open_journal();
920
921 if (journal_fd >= 0) {
922 char header[LINE_MAX];
923 struct iovec iovec[17] = {};
d3070fbd 924 size_t n = 0, i;
16e4fd87
LP
925 int r;
926 struct msghdr mh = {
927 .msg_iov = iovec,
928 };
929 bool fallback = false;
930
931 /* If the journal is available do structured logging */
932 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
933 iovec[n++] = IOVEC_MAKE_STRING(header);
934
935 va_start(ap, format);
936 r = log_format_iovec(iovec, ELEMENTSOF(iovec), &n, true, error, format, ap);
937 if (r < 0)
938 fallback = true;
939 else {
940 mh.msg_iovlen = n;
941 (void) sendmsg(journal_fd, &mh, MSG_NOSIGNAL);
942 }
877d54e9 943
16e4fd87
LP
944 va_end(ap);
945 for (i = 1; i < n; i += 2)
946 free(iovec[i].iov_base);
877d54e9 947
16e4fd87
LP
948 if (!fallback) {
949 if (open_when_needed)
950 log_close();
877d54e9 951
16e4fd87
LP
952 return -error;
953 }
954 }
bf371116 955 }
877d54e9 956
bf371116 957 /* Fallback if journal logging is not available or didn't work. */
6357ac66 958
bf371116
LP
959 va_start(ap, format);
960 while (format) {
961 va_list aq;
877d54e9 962
d1a1f0aa 963 errno = error;
877d54e9 964
bf371116 965 va_copy(aq, ap);
4ad2b562 966 (void) vsnprintf(buf, sizeof buf, format, aq);
bf371116 967 va_end(aq);
963ddb91 968
bf371116
LP
969 if (startswith(buf, "MESSAGE=")) {
970 found = true;
971 break;
877d54e9 972 }
877d54e9 973
bf371116
LP
974 VA_FORMAT_ADVANCE(format, ap);
975
976 format = va_arg(ap, char *);
877d54e9 977 }
bf371116 978 va_end(ap);
877d54e9 979
16e4fd87
LP
980 if (!found) {
981 if (open_when_needed)
982 log_close();
983
bf371116 984 return -error;
16e4fd87 985 }
bf371116 986
93484b47 987 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
877d54e9
LP
988}
989
915b1d01
LP
990int log_struct_iovec_internal(
991 int level,
992 int error,
993 const char *file,
994 int line,
995 const char *func,
996 const struct iovec input_iovec[],
997 size_t n_input_iovec) {
998
999 LogRealm realm = LOG_REALM_REMOVE_LEVEL(level);
1000 PROTECT_ERRNO;
1001 size_t i;
1002 char *m;
1003
1004 if (error < 0)
1005 error = -error;
1006
1007 if (_likely_(LOG_PRI(level) > log_max_level[realm]))
1008 return -error;
1009
1010 if (log_target == LOG_TARGET_NULL)
1011 return -error;
1012
1013 if ((level & LOG_FACMASK) == 0)
1014 level = log_facility | LOG_PRI(level);
1015
1016 if (IN_SET(log_target, LOG_TARGET_AUTO,
1017 LOG_TARGET_JOURNAL_OR_KMSG,
1018 LOG_TARGET_JOURNAL) &&
1019 journal_fd >= 0) {
1020
1021 struct iovec iovec[1 + n_input_iovec*2];
1022 char header[LINE_MAX];
1023 struct msghdr mh = {
1024 .msg_iov = iovec,
1025 .msg_iovlen = 1 + n_input_iovec*2,
1026 };
1027
1028 log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1029 iovec[0] = IOVEC_MAKE_STRING(header);
1030
1031 for (i = 0; i < n_input_iovec; i++) {
1032 iovec[1+i*2] = input_iovec[i];
1033 iovec[1+i*2+1] = IOVEC_MAKE_STRING("\n");
1034 }
1035
1036 if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) >= 0)
40056777 1037 return -error;
915b1d01
LP
1038 }
1039
1040 for (i = 0; i < n_input_iovec; i++) {
fbd0b64f 1041 if (input_iovec[i].iov_len < STRLEN("MESSAGE="))
915b1d01
LP
1042 continue;
1043
fbd0b64f 1044 if (memcmp(input_iovec[i].iov_base, "MESSAGE=", STRLEN("MESSAGE=")) == 0)
915b1d01
LP
1045 break;
1046 }
1047
1048 if (_unlikely_(i >= n_input_iovec)) /* Couldn't find MESSAGE=? */
1049 return -error;
1050
fbd0b64f
LP
1051 m = strndupa(input_iovec[i].iov_base + STRLEN("MESSAGE="),
1052 input_iovec[i].iov_len - STRLEN("MESSAGE="));
915b1d01
LP
1053
1054 return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
1055}
1056
34f0e866
LP
1057int log_set_target_from_string(const char *e) {
1058 LogTarget t;
1059
5ba081b0
LP
1060 t = log_target_from_string(e);
1061 if (t < 0)
34f0e866
LP
1062 return -EINVAL;
1063
1064 log_set_target(t);
1065 return 0;
1066}
1067
ff524019 1068int log_set_max_level_from_string_realm(LogRealm realm, const char *e) {
34f0e866
LP
1069 int t;
1070
5ba081b0
LP
1071 t = log_level_from_string(e);
1072 if (t < 0)
737af734 1073 return -EINVAL;
34f0e866 1074
ff524019 1075 log_set_max_level_realm(realm, t);
34f0e866
LP
1076 return 0;
1077}
1078
96287a49 1079static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
1de1c9c3
LP
1080
1081 /*
1082 * The systemd.log_xyz= settings are parsed by all tools, and
1083 * so is "debug".
1084 *
5e07a79e
LP
1085 * However, "quiet" is only parsed by PID 1, and only turns of
1086 * status output to /dev/console, but does not alter the log
1087 * level.
1de1c9c3
LP
1088 */
1089
1090 if (streq(key, "debug") && !value)
1091 log_set_max_level(LOG_DEBUG);
1092
1d84ad94
LP
1093 else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
1094
1095 if (proc_cmdline_value_missing(key, value))
1096 return 0;
1de1c9c3
LP
1097
1098 if (log_set_target_from_string(value) < 0)
1099 log_warning("Failed to parse log target '%s'. Ignoring.", value);
1100
1d84ad94
LP
1101 } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
1102
1103 if (proc_cmdline_value_missing(key, value))
1104 return 0;
1de1c9c3
LP
1105
1106 if (log_set_max_level_from_string(value) < 0)
1107 log_warning("Failed to parse log level '%s'. Ignoring.", value);
1108
1d84ad94 1109 } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
1de1c9c3 1110
1d84ad94 1111 if (log_show_color_from_string(value ?: "1") < 0)
1de1c9c3
LP
1112 log_warning("Failed to parse log color setting '%s'. Ignoring.", value);
1113
1d84ad94 1114 } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
1de1c9c3 1115
1d84ad94 1116 if (log_show_location_from_string(value ?: "1") < 0)
1de1c9c3
LP
1117 log_warning("Failed to parse log location setting '%s'. Ignoring.", value);
1118 }
1119
1120 return 0;
1121}
1122
ff524019 1123void log_parse_environment_realm(LogRealm realm) {
e3e42fc2
ZJS
1124 /* Do not call from library code. */
1125
34f0e866 1126 const char *e;
b8d0ffc2 1127
ee46e555 1128 if (get_ctty_devnr(0, NULL) < 0)
1d84ad94
LP
1129 /* Only try to read the command line in daemons. We assume that anything that has a controlling tty is
1130 user stuff. */
1131 (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
34f0e866 1132
a99e002c 1133 e = getenv("SYSTEMD_LOG_TARGET");
88fae6e0 1134 if (e && log_set_target_from_string(e) < 0)
f0ea29ea 1135 log_warning("Failed to parse log target '%s'. Ignoring.", e);
34f0e866 1136
a99e002c 1137 e = getenv("SYSTEMD_LOG_LEVEL");
ff524019 1138 if (e && log_set_max_level_from_string_realm(realm, e) < 0)
f0ea29ea 1139 log_warning("Failed to parse log level '%s'. Ignoring.", e);
bbe63281 1140
a99e002c 1141 e = getenv("SYSTEMD_LOG_COLOR");
88fae6e0 1142 if (e && log_show_color_from_string(e) < 0)
f0ea29ea 1143 log_warning("Failed to parse bool '%s'. Ignoring.", e);
bbe63281 1144
a99e002c 1145 e = getenv("SYSTEMD_LOG_LOCATION");
88fae6e0 1146 if (e && log_show_location_from_string(e) < 0)
f0ea29ea 1147 log_warning("Failed to parse bool '%s'. Ignoring.", e);
34f0e866
LP
1148}
1149
1adf1049
LP
1150LogTarget log_get_target(void) {
1151 return log_target;
1152}
1153
ff524019
ZJS
1154int log_get_max_level_realm(LogRealm realm) {
1155 return log_max_level[realm];
1adf1049
LP
1156}
1157
bbe63281
LP
1158void log_show_color(bool b) {
1159 show_color = b;
1160}
1161
b1e90ec5
ZJS
1162bool log_get_show_color(void) {
1163 return show_color;
1164}
1165
bbe63281
LP
1166void log_show_location(bool b) {
1167 show_location = b;
1168}
1169
b1e90ec5
ZJS
1170bool log_get_show_location(void) {
1171 return show_location;
1172}
1173
bbe63281
LP
1174int log_show_color_from_string(const char *e) {
1175 int t;
1176
5ba081b0
LP
1177 t = parse_boolean(e);
1178 if (t < 0)
1179 return t;
bbe63281
LP
1180
1181 log_show_color(t);
1182 return 0;
1183}
1184
1185int log_show_location_from_string(const char *e) {
1186 int t;
1187
5ba081b0
LP
1188 t = parse_boolean(e);
1189 if (t < 0)
1190 return t;
bbe63281
LP
1191
1192 log_show_location(t);
1193 return 0;
1194}
1195
81270860 1196bool log_on_console(void) {
5b5688af
ZJS
1197 if (IN_SET(log_target, LOG_TARGET_CONSOLE,
1198 LOG_TARGET_CONSOLE_PREFIXED))
81270860
LP
1199 return true;
1200
1201 return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
1202}
1203
2c5859af 1204static const char *const log_target_table[_LOG_TARGET_MAX] = {
34f0e866 1205 [LOG_TARGET_CONSOLE] = "console",
aca83a53 1206 [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
34f0e866 1207 [LOG_TARGET_KMSG] = "kmsg",
5ba081b0
LP
1208 [LOG_TARGET_JOURNAL] = "journal",
1209 [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
1210 [LOG_TARGET_SYSLOG] = "syslog",
843d2643 1211 [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
5ba081b0 1212 [LOG_TARGET_AUTO] = "auto",
6c347d50 1213 [LOG_TARGET_NULL] = "null",
34f0e866
LP
1214};
1215
1216DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
4daf54a8
ZJS
1217
1218void log_received_signal(int level, const struct signalfd_siginfo *si) {
a6ab2365
LP
1219 assert(si);
1220
1221 if (pid_is_valid(si->ssi_pid)) {
4daf54a8
ZJS
1222 _cleanup_free_ char *p = NULL;
1223
a6ab2365 1224 (void) get_process_comm(si->ssi_pid, &p);
4daf54a8
ZJS
1225
1226 log_full(level,
1fa2f38f 1227 "Received SIG%s from PID %"PRIu32" (%s).",
4daf54a8
ZJS
1228 signal_to_string(si->ssi_signo),
1229 si->ssi_pid, strna(p));
1230 } else
1231 log_full(level,
1232 "Received SIG%s.",
1233 signal_to_string(si->ssi_signo));
4daf54a8 1234}
c1dc6153 1235
158350e8
LP
1236int log_syntax_internal(
1237 const char *unit,
1238 int level,
1239 const char *config_file,
1240 unsigned config_line,
1241 int error,
1242 const char *file,
1243 int line,
1244 const char *func,
1245 const char *format, ...) {
1246
1247 PROTECT_ERRNO;
1248 char buffer[LINE_MAX];
158350e8 1249 va_list ap;
c2dec702 1250 const char *unit_fmt = NULL;
158350e8
LP
1251
1252 if (error < 0)
1253 error = -error;
1254
ff524019 1255 if (_likely_(LOG_PRI(level) > log_max_level[LOG_REALM_SYSTEMD]))
158350e8
LP
1256 return -error;
1257
1258 if (log_target == LOG_TARGET_NULL)
1259 return -error;
1260
d1a1f0aa 1261 errno = error;
158350e8
LP
1262
1263 va_start(ap, format);
4ad2b562 1264 (void) vsnprintf(buffer, sizeof buffer, format, ap);
158350e8
LP
1265 va_end(ap);
1266
1267 if (unit)
df0ff127 1268 unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
c2dec702
ZJS
1269
1270 return log_struct_internal(
ff524019
ZJS
1271 LOG_REALM_PLUS_LEVEL(LOG_REALM_SYSTEMD, level),
1272 error,
c2dec702 1273 file, line, func,
2b044526 1274 "MESSAGE_ID=" SD_MESSAGE_INVALID_CONFIGURATION_STR,
c2dec702
ZJS
1275 "CONFIG_FILE=%s", config_file,
1276 "CONFIG_LINE=%u", config_line,
e697dfef 1277 LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
c2dec702
ZJS
1278 unit_fmt, unit,
1279 NULL);
158350e8 1280}
48a601fe 1281
d04ce5a9
LP
1282int log_syntax_invalid_utf8_internal(
1283 const char *unit,
1284 int level,
1285 const char *config_file,
1286 unsigned config_line,
1287 const char *file,
1288 int line,
1289 const char *func,
1290 const char *rvalue) {
1291
1292 _cleanup_free_ char *p = NULL;
1293
1294 if (rvalue)
1295 p = utf8_escape_invalid(rvalue);
1296
1297 log_syntax_internal(unit, level, config_file, config_line, 0, file, line, func,
1298 "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1299
1300 return -EINVAL;
1301}
1302
16e4fd87
LP
1303void log_set_upgrade_syslog_to_journal(bool b) {
1304 upgrade_syslog_to_journal = b;
6fdb8de4
LP
1305
1306 /* Make the change effective immediately */
1307 if (b) {
1308 if (log_target == LOG_TARGET_SYSLOG)
1309 log_target = LOG_TARGET_JOURNAL;
1310 else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
1311 log_target = LOG_TARGET_JOURNAL_OR_KMSG;
1312 }
16e4fd87
LP
1313}
1314
48a601fe
LP
1315void log_set_always_reopen_console(bool b) {
1316 always_reopen_console = b;
1317}
16e4fd87
LP
1318
1319void log_set_open_when_needed(bool b) {
1320 open_when_needed = b;
1321}
dccca82b 1322
adf47c91
LP
1323void log_set_prohibit_ipc(bool b) {
1324 prohibit_ipc = b;
1325}
1326
dccca82b
LP
1327int log_emergency_level(void) {
1328 /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1329 * then the system of the whole system is obviously affected. */
1330
1331 return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
1332}