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