• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14048528658

24 Mar 2025 08:06PM UTC coverage: 71.927% (-0.04%) from 71.966%
14048528658

push

github

web-flow
Ratelimit attempts to open watchdog, increase logging (#35708)

29 of 69 new or added lines in 4 files covered. (42.03%)

1083 existing lines in 38 files now uncovered.

296538 of 412279 relevant lines covered (71.93%)

716588.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.67
/src/basic/log.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <errno.h>
4
#include <fcntl.h>
5
#include <inttypes.h>
6
#include <limits.h>
7
#include <stdarg.h>
8
#include <stddef.h>
9
#include <sys/signalfd.h>
10
#include <sys/stat.h>
11
#include <sys/time.h>
12
#include <sys/uio.h>
13
#include <sys/un.h>
14
#include <threads.h>
15
#include <unistd.h>
16

17
#include "sd-messages.h"
18

19
#include "alloc-util.h"
20
#include "ansi-color.h"
21
#include "argv-util.h"
22
#include "env-util.h"
23
#include "errno-util.h"
24
#include "fd-util.h"
25
#include "format-util.h"
26
#include "iovec-util.h"
27
#include "log.h"
28
#include "macro.h"
29
#include "missing_syscall.h"
30
#include "parse-util.h"
31
#include "proc-cmdline.h"
32
#include "process-util.h"
33
#include "ratelimit.h"
34
#include "signal-util.h"
35
#include "socket-util.h"
36
#include "stdio-util.h"
37
#include "string-table.h"
38
#include "string-util.h"
39
#include "strv.h"
40
#include "syslog-util.h"
41
#include "terminal-util.h"
42
#include "time-util.h"
43
#include "utf8.h"
44

45
#define SNDBUF_SIZE (8*1024*1024)
46
#define IOVEC_MAX 256U
47

48
static log_syntax_callback_t log_syntax_callback = NULL;
49
static void *log_syntax_callback_userdata = NULL;
50

51
static LogTarget log_target = LOG_TARGET_CONSOLE;
52
static int log_max_level = LOG_INFO;
53
static int log_target_max_level[_LOG_TARGET_SINGLE_MAX] = {
54
        [LOG_TARGET_CONSOLE] = INT_MAX,
55
        [LOG_TARGET_KMSG]    = INT_MAX,
56
        [LOG_TARGET_SYSLOG]  = INT_MAX,
57
        [LOG_TARGET_JOURNAL] = INT_MAX,
58
};
59
static int log_facility = LOG_DAEMON;
60
static bool ratelimit_kmsg = true;
61

62
static int console_fd = STDERR_FILENO;
63
static int console_fd_is_tty = -1; /* tri-state: -1 means don't know */
64
static int syslog_fd = -EBADF;
65
static int kmsg_fd = -EBADF;
66
static int journal_fd = -EBADF;
67

68
static bool syslog_is_stream = false;
69

70
static int show_color = -1; /* tristate */
71
static bool show_location = false;
72
static bool show_time = false;
73
static bool show_tid = false;
74

75
static bool upgrade_syslog_to_journal = false;
76
static bool always_reopen_console = false;
77
static bool open_when_needed = false;
78
static bool prohibit_ipc = false;
79
static bool assert_return_is_critical = BUILD_MODE_DEVELOPER;
80

81
/* Akin to glibc's __abort_msg; which is private and we hence cannot
82
 * use here. */
83
static char *log_abort_msg = NULL;
84

85
typedef struct LogContext {
86
        unsigned n_ref;
87
        /* Depending on which destructor is used (log_context_free() or log_context_detach()) the memory
88
         * referenced by this is freed or not */
89
        char **fields;
90
        struct iovec *input_iovec;
91
        size_t n_input_iovec;
92
        char *key;
93
        char *value;
94
        bool owned;
95
        LIST_FIELDS(struct LogContext, ll);
96
} LogContext;
97

98
static thread_local LIST_HEAD(LogContext, _log_context) = NULL;
99
static thread_local size_t _log_context_num_fields = 0;
100

101
static thread_local const char *log_prefix = NULL;
102

103
#if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
104
bool _log_message_dummy = false; /* Always false */
105
#endif
106

107
/* An assert to use in logging functions that does not call recursively
108
 * into our logging functions (since that might lead to a loop). */
109
#define assert_raw(expr)                                                \
110
        do {                                                            \
111
                if (_unlikely_(!(expr))) {                              \
112
                        fputs(#expr "\n", stderr);                      \
113
                        abort();                                        \
114
                }                                                       \
115
        } while (false)
116

117
static void log_close_console(void) {
534,654✔
118
        /* See comment in log_close_journal() */
119
        (void) safe_close_above_stdio(TAKE_FD(console_fd));
534,654✔
120
        console_fd_is_tty = -1;
534,654✔
121
}
534,654✔
122

123
static int log_open_console(void) {
54,843✔
124

125
        if (!always_reopen_console) {
54,843✔
126
                console_fd = STDERR_FILENO;
18,986✔
127
                console_fd_is_tty = -1;
18,986✔
128
                return 0;
18,986✔
129
        }
130

131
        if (console_fd < 3) {
35,857✔
132
                int fd;
33,631✔
133

134
                fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
33,631✔
135
                if (fd < 0)
33,631✔
136
                        return fd;
137

138
                console_fd = fd_move_above_stdio(fd);
19,354✔
139
                console_fd_is_tty = true;
19,354✔
140
        }
141

142
        return 0;
143
}
144

145
static void log_close_kmsg(void) {
247,546✔
146
        /* See comment in log_close_journal() */
147
        (void) safe_close(TAKE_FD(kmsg_fd));
247,546✔
148
}
247,546✔
149

150
static int log_open_kmsg(void) {
47,687✔
151

152
        if (kmsg_fd >= 0)
47,687✔
153
                return 0;
154

155
        kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
40,117✔
156
        if (kmsg_fd < 0)
40,117✔
157
                return -errno;
21,355✔
158

159
        kmsg_fd = fd_move_above_stdio(kmsg_fd);
18,762✔
160
        return 0;
18,762✔
161
}
162

163
static void log_close_syslog(void) {
589,493✔
164
        /* See comment in log_close_journal() */
165
        (void) safe_close(TAKE_FD(syslog_fd));
589,493✔
166
}
589,493✔
167

168
static int create_log_socket(int type) {
250,374✔
169
        struct timeval tv;
250,374✔
170
        int fd;
250,374✔
171

172
        fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
250,374✔
173
        if (fd < 0)
250,374✔
174
                return -errno;
×
175

176
        fd = fd_move_above_stdio(fd);
250,374✔
177
        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
250,374✔
178

179
        /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
180
         * in the unlikely case of a deadlock. */
181
        if (getpid_cached() == 1)
250,374✔
182
                timeval_store(&tv, 10 * USEC_PER_MSEC);
398✔
183
        else
184
                timeval_store(&tv, 10 * USEC_PER_SEC);
249,976✔
185
        (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
250,374✔
186

187
        return fd;
250,374✔
188
}
189

190
static int log_open_syslog(void) {
2✔
191
        int r;
2✔
192

193
        if (syslog_fd >= 0)
2✔
194
                return 0;
195

196
        syslog_fd = create_log_socket(SOCK_DGRAM);
2✔
197
        if (syslog_fd < 0) {
2✔
198
                r = syslog_fd;
×
199
                goto fail;
×
200
        }
201

202
        r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
2✔
203
        if (r < 0) {
2✔
204
                safe_close(syslog_fd);
×
205

206
                /* Some legacy syslog systems still use stream sockets. They really shouldn't. But what can
207
                 * we do... */
208
                syslog_fd = create_log_socket(SOCK_STREAM);
×
209
                if (syslog_fd < 0) {
×
210
                        r = syslog_fd;
×
211
                        goto fail;
×
212
                }
213

214
                r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
×
215
                if (r < 0)
×
216
                        goto fail;
×
217

218
                syslog_is_stream = true;
×
219
        } else
220
                syslog_is_stream = false;
2✔
221

222
        return 0;
223

224
fail:
×
225
        log_close_syslog();
×
226
        return r;
×
227
}
228

229
static void log_close_journal(void) {
327,995✔
230
        /* If the journal FD is bad, safe_close will fail, and will try to log, which will fail, so we'll
231
         * try to close the journal FD, which is bad, so safe_close will fail... Whether we can close it
232
         * or not, invalidate it immediately so that we don't get in a recursive loop until we run out of
233
         * stack. */
234
        (void) safe_close(TAKE_FD(journal_fd));
327,995✔
235
}
327,995✔
236

237
static int log_open_journal(void) {
269,953✔
238
        int r;
269,953✔
239

240
        if (journal_fd >= 0)
269,953✔
241
                return 0;
242

243
        journal_fd = create_log_socket(SOCK_DGRAM);
250,372✔
244
        if (journal_fd < 0) {
250,372✔
245
                r = journal_fd;
×
246
                goto fail;
×
247
        }
248

249
        r = connect_unix_path(journal_fd, AT_FDCWD, "/run/systemd/journal/socket");
250,372✔
250
        if (r < 0)
250,372✔
251
                goto fail;
80✔
252

253
        return 0;
254

255
fail:
80✔
256
        log_close_journal();
80✔
257
        return r;
80✔
258
}
259

260
bool stderr_is_journal(void) {
317,795✔
261
        _cleanup_free_ char *w = NULL;
317,795✔
262
        const char *e;
317,795✔
263
        uint64_t dev, ino;
317,795✔
264
        struct stat st;
317,795✔
265

266
        e = getenv("JOURNAL_STREAM");
317,795✔
267
        if (!e)
317,795✔
268
                return false;
269

270
        if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
82,913✔
271
                return false;
272
        if (!e)
82,913✔
273
                return false;
274

275
        if (safe_atou64(w, &dev) < 0)
82,913✔
276
                return false;
277
        if (safe_atou64(e, &ino) < 0)
82,913✔
278
                return false;
279

280
        if (fstat(STDERR_FILENO, &st) < 0)
82,913✔
281
                return false;
282

283
        return st.st_dev == dev && st.st_ino == ino;
97,192✔
284
}
285

286
int log_open(void) {
341,949✔
287
        int r;
341,949✔
288

289
        /* Do not call from library code. */
290

291
        /* This function is often called in preparation for logging. Let's make sure we don't clobber errno,
292
         * so that a call to a logging function immediately following a log_open() call can still easily
293
         * reference an error that happened immediately before the log_open() call. */
294
        PROTECT_ERRNO;
341,949✔
295

296
        /* If we don't use the console, we close it here to not get killed by SAK. If we don't use syslog, we
297
         * close it here too, so that we are not confused by somebody deleting the socket in the fs, and to
298
         * make sure we don't use it if prohibit_ipc is set. If we don't use /dev/kmsg we still keep it open,
299
         * because there is no reason to close it. */
300

301
        if (log_target == LOG_TARGET_NULL) {
341,949✔
302
                log_close_journal();
1✔
303
                log_close_syslog();
1✔
304
                log_close_console();
1✔
305
                return 0;
306
        }
307

308
        if (getpid_cached() == 1 ||
655,456✔
309
            stderr_is_journal() ||
313,508✔
310
            IN_SET(log_target,
249,150✔
311
                   LOG_TARGET_KMSG,
312
                   LOG_TARGET_JOURNAL,
313
                   LOG_TARGET_JOURNAL_OR_KMSG,
314
                   LOG_TARGET_SYSLOG,
315
                   LOG_TARGET_SYSLOG_OR_KMSG)) {
316

317
                if (!prohibit_ipc) {
310,095✔
318
                        if (IN_SET(log_target,
300,889✔
319
                                   LOG_TARGET_AUTO,
320
                                   LOG_TARGET_JOURNAL_OR_KMSG,
321
                                   LOG_TARGET_JOURNAL)) {
322

323
                                r = log_open_journal();
261,651✔
324
                                if (r >= 0) {
261,651✔
325
                                        log_close_syslog();
261,584✔
326
                                        log_close_console();
261,584✔
327
                                        return r;
328
                                }
329
                        }
330

331
                        if (IN_SET(log_target,
39,305✔
332
                                   LOG_TARGET_SYSLOG_OR_KMSG,
333
                                   LOG_TARGET_SYSLOG)) {
334

335
                                r = log_open_syslog();
2✔
336
                                if (r >= 0) {
2✔
337
                                        log_close_journal();
2✔
338
                                        log_close_console();
2✔
339
                                        return r;
340
                                }
341
                        }
342
                }
343

344
                if (IN_SET(log_target, LOG_TARGET_AUTO,
48,509✔
345
                                       LOG_TARGET_JOURNAL_OR_KMSG,
346
                                       LOG_TARGET_SYSLOG_OR_KMSG,
347
                                       LOG_TARGET_KMSG)) {
348
                        r = log_open_kmsg();
46,874✔
349
                        if (r >= 0) {
46,874✔
350
                                log_close_journal();
25,520✔
351
                                log_close_syslog();
25,520✔
352
                                log_close_console();
25,520✔
353
                                return r;
354
                        }
355
                }
356
        }
357

358
        log_close_journal();
54,842✔
359
        log_close_syslog();
54,842✔
360

361
        return log_open_console();
54,842✔
362
}
363

364
void log_set_target(LogTarget target) {
111,169✔
365
        assert(target >= 0);
111,169✔
366
        assert(target < _LOG_TARGET_MAX);
111,169✔
367

368
        if (upgrade_syslog_to_journal) {
111,169✔
369
                if (target == LOG_TARGET_SYSLOG)
378✔
370
                        target = LOG_TARGET_JOURNAL;
371
                else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
378✔
372
                        target = LOG_TARGET_JOURNAL_OR_KMSG;
×
373
        }
374

375
        log_target = target;
111,169✔
376
}
111,169✔
377

378
void log_set_target_and_open(LogTarget target) {
303✔
379
        log_set_target(target);
303✔
380
        log_open();
303✔
381
}
303✔
382

383
void log_close(void) {
247,546✔
384
        /* Do not call from library code. */
385

386
        log_close_journal();
247,546✔
387
        log_close_syslog();
247,546✔
388
        log_close_kmsg();
247,546✔
389
        log_close_console();
247,546✔
390
}
247,546✔
391

392
void log_forget_fds(void) {
13,566✔
393
        /* Do not call from library code. */
394

395
        console_fd = kmsg_fd = syslog_fd = journal_fd = -EBADF;
13,566✔
396
        console_fd_is_tty = -1;
13,566✔
397
}
13,566✔
398

399
int log_set_max_level(int level) {
179,437✔
400
        assert(level == LOG_NULL || log_level_is_valid(level));
179,437✔
401

402
        int old = log_max_level;
179,437✔
403
        log_max_level = level;
179,437✔
404

405
        /* Also propagate max log level to libc's syslog(), just in case some other component loaded into our
406
         * process logs directly via syslog(). You might wonder why we maintain our own log level variable if
407
         * libc has the same functionality. This has multiple reasons, first and foremost that we want to
408
         * apply this to all our log targets, not just syslog and console. Moreover, we cannot query the
409
         * current log mask from glibc without changing it, but that's useful for testing the current log
410
         * level before even entering the log functions like we do in our macros. */
411
        setlogmask(LOG_UPTO(level));
179,437✔
412

413
        /* Ensure that our own LOG_NULL define maps sanely to the log mask */
414
        assert_cc(LOG_UPTO(LOG_NULL) == 0);
179,437✔
415

416
        return old;
179,437✔
417
}
418

419
void log_set_facility(int facility) {
298✔
420
        log_facility = facility;
298✔
421
}
298✔
422

423
static bool check_console_fd_is_tty(void) {
26,381✔
424
        if (console_fd < 0)
26,381✔
425
                return false;
426

427
        if (console_fd_is_tty < 0)
26,381✔
428
                console_fd_is_tty = isatty_safe(console_fd);
1,394✔
429

430
        return console_fd_is_tty;
26,381✔
431
}
432

433
static int write_to_console(
125,945✔
434
                int level,
435
                int error,
436
                const char *file,
437
                int line,
438
                const char *func,
439
                const char *buffer) {
440

441
        static int dumb = -1;
125,945✔
442

443
        char location[256],
125,945✔
444
             header_time[FORMAT_TIMESTAMP_MAX],
445
             prefix[1 + DECIMAL_STR_MAX(int) + 2],
446
             tid_string[3 + DECIMAL_STR_MAX(pid_t) + 1];
447
        struct iovec iovec[11];
125,945✔
448
        const char *on = NULL, *off = NULL;
125,945✔
449
        size_t n = 0;
125,945✔
450

451
        if (console_fd < 0)
125,945✔
452
                return 0;
125,945✔
453

454
        if (dumb < 0)
102,568✔
455
                dumb = getenv_terminal_is_dumb();
2,339✔
456

457
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_CONSOLE])
102,568✔
458
                return 0;
459

460
        if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
26,381✔
461
                xsprintf(prefix, "<%i>", level);
433✔
462
                iovec[n++] = IOVEC_MAKE_STRING(prefix);
433✔
463
        }
464

465
        if (show_time &&
26,381✔
466
            format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
×
467
                iovec[n++] = IOVEC_MAKE_STRING(header_time);
×
468
                iovec[n++] = IOVEC_MAKE_STRING(" ");
×
469
        }
470

471
        if (show_tid) {
26,381✔
472
                xsprintf(tid_string, "(" PID_FMT ") ", gettid());
×
473
                iovec[n++] = IOVEC_MAKE_STRING(tid_string);
×
474
        }
475

476
        if (log_get_show_color())
26,381✔
477
                get_log_colors(LOG_PRI(level), &on, &off, NULL);
10,856✔
478

479
        if (show_location) {
26,381✔
480
                const char *lon = "", *loff = "";
×
481
                if (log_get_show_color()) {
×
482
                        lon = ansi_highlight_yellow4();
×
483
                        loff = ansi_normal();
×
484
                }
485

486
                (void) snprintf(location, sizeof location, "%s%s:%i%s: ", lon, file, line, loff);
×
487
                iovec[n++] = IOVEC_MAKE_STRING(location);
×
488
        }
489

490
        if (on)
26,381✔
491
                iovec[n++] = IOVEC_MAKE_STRING(on);
10,442✔
492
        if (log_prefix) {
26,381✔
493
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
2,317✔
494
                iovec[n++] = IOVEC_MAKE_STRING(": ");
2,317✔
495
        }
496
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
26,381✔
497
        if (off)
26,381✔
498
                iovec[n++] = IOVEC_MAKE_STRING(off);
10,442✔
499

500
        /* When writing to a TTY we output an extra '\r' (i.e. CR) first, to generate CRNL rather than just
501
         * NL. This is a robustness thing in case the TTY is currently in raw mode (specifically: has the
502
         * ONLCR flag off). We want that subsequent output definitely starts at the beginning of the line
503
         * again, after all. If the TTY is not in raw mode the extra CR should not hurt. If we're writing to
504
         * a dumb terminal, only write NL as CRNL might be interpreted as a double newline. */
505
        iovec[n++] = IOVEC_MAKE_STRING(check_console_fd_is_tty() && !dumb ? "\r\n" : "\n");
52,762✔
506

507
        if (writev(console_fd, iovec, n) < 0) {
26,381✔
508

509
                if (errno == EIO && getpid_cached() == 1) {
85✔
510

511
                        /* If somebody tried to kick us from our console tty (via vhangup() or suchlike), try
512
                         * to reconnect. */
513

514
                        log_close_console();
1✔
515
                        (void) log_open_console();
1✔
516
                        if (console_fd < 0)
1✔
517
                                return 0;
518

519
                        if (writev(console_fd, iovec, n) < 0)
1✔
520
                                return -errno;
×
521
                } else
522
                        return -errno;
84✔
523
        }
524

525
        return 1;
526
}
527

528
static int write_to_syslog(
180✔
529
                int level,
530
                int error,
531
                const char *file,
532
                int line,
533
                const char *func,
534
                const char *buffer) {
535

536
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
180✔
537
             header_time[64],
538
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
539
        struct tm tm;
180✔
540
        int r;
180✔
541

542
        if (syslog_fd < 0)
180✔
543
                return 0;
180✔
544

545
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_SYSLOG])
180✔
546
                return 0;
547

548
        xsprintf(header_priority, "<%i>", level);
180✔
549

550
        r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
180✔
551
        if (r < 0)
180✔
552
                return r;
553

554
        if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
180✔
555
                return -EINVAL;
556

557
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
180✔
558

559
        struct iovec iovec[] = {
900✔
560
                IOVEC_MAKE_STRING(header_priority),
180✔
561
                IOVEC_MAKE_STRING(header_time),
180✔
562
                IOVEC_MAKE_STRING(program_invocation_short_name),
180✔
563
                IOVEC_MAKE_STRING(header_pid),
180✔
564
                IOVEC_MAKE_STRING(strempty(log_prefix)),
180✔
565
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
180✔
566
                IOVEC_MAKE_STRING(buffer),
180✔
567
        };
568
        const struct msghdr msghdr = {
180✔
569
                .msg_iov = iovec,
570
                .msg_iovlen = ELEMENTSOF(iovec),
571
        };
572

573
        /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
574
        if (syslog_is_stream)
180✔
575
                iovec[ELEMENTSOF(iovec) - 1].iov_len++;
×
576

577
        for (;;) {
180✔
578
                ssize_t n;
180✔
579

580
                n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
180✔
581
                if (n < 0)
180✔
582
                        return -errno;
×
583

584
                if (!syslog_is_stream)
180✔
585
                        break;
586

587
                if (iovec_increment(iovec, ELEMENTSOF(iovec), n))
×
588
                        break;
589
        }
590

591
        return 1;
592
}
593

594
static int write_to_kmsg(
184,803✔
595
                int level,
596
                int error,
597
                const char *file,
598
                int line,
599
                const char *func,
600
                const char *buffer) {
601

602
        /* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a
603
         * safety catch for the case where start indiscriminately logging in a loop. It will not catch cases
604
         * where we log excessively, but not in a tight loop.
605
         *
606
         * Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple
607
         * loggers.
608
         */
609
        static thread_local RateLimit ratelimit = { 5 * USEC_PER_SEC, 200 };
184,803✔
610

611
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
184,803✔
612
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
613

614
        if (kmsg_fd < 0)
184,803✔
615
                return 0;
184,803✔
616

617
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_KMSG])
132,541✔
618
                return 0;
619

620
        if (ratelimit_kmsg && !ratelimit_below(&ratelimit)) {
132,541✔
621
                if (ratelimit_num_dropped(&ratelimit) > 1)
×
622
                        return 0;
623

624
                buffer = "Too many messages being logged to kmsg, ignoring";
625
        }
626

627
        xsprintf(header_priority, "<%i>", level);
132,541✔
628
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
132,541✔
629

630
        const struct iovec iovec[] = {
662,705✔
631
                IOVEC_MAKE_STRING(header_priority),
132,541✔
632
                IOVEC_MAKE_STRING(program_invocation_short_name),
132,541✔
633
                IOVEC_MAKE_STRING(header_pid),
132,541✔
634
                IOVEC_MAKE_STRING(strempty(log_prefix)),
132,541✔
635
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
132,541✔
636
                IOVEC_MAKE_STRING(buffer),
132,541✔
637
                IOVEC_MAKE_STRING("\n"),
132,541✔
638
        };
639

640
        if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
132,541✔
641
                return -errno;
×
642

643
        return 1;
644
}
645

646
static int log_do_header(
4,928,054✔
647
                char *header,
648
                size_t size,
649
                int level,
650
                int error,
651
                const char *file, int line, const char *func,
652
                const char *object_field, const char *object,
653
                const char *extra_field, const char *extra) {
654
        int r;
4,928,054✔
655

656
        error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error);
4,928,054✔
657

658
        r = snprintf(header, size,
4,928,054✔
659
                     "PRIORITY=%i\n"
660
                     "SYSLOG_FACILITY=%i\n"
661
                     "TID=" PID_FMT "\n"
662
                     "%s%.256s%s"        /* CODE_FILE */
663
                     "%s%.*i%s"          /* CODE_LINE */
664
                     "%s%.256s%s"        /* CODE_FUNC */
665
                     "%s%.*i%s"          /* ERRNO */
666
                     "%s%.256s%s"        /* object */
667
                     "%s%.256s%s"        /* extra */
668
                     "SYSLOG_IDENTIFIER=%.256s\n",
669
                     LOG_PRI(level),
670
                     LOG_FAC(level),
4,928,054✔
671
                     gettid(),
672
                     isempty(file) ? "" : "CODE_FILE=",
4,928,054✔
673
                     isempty(file) ? "" : file,
4,928,054✔
674
                     isempty(file) ? "" : "\n",
4,928,054✔
675
                     line ? "CODE_LINE=" : "",
676
                     line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
677
                     line ? "\n" : "",
678
                     isempty(func) ? "" : "CODE_FUNC=",
4,928,054✔
679
                     isempty(func) ? "" : func,
4,928,054✔
680
                     isempty(func) ? "" : "\n",
4,928,054✔
681
                     error ? "ERRNO=" : "",
682
                     error ? 1 : 0, error,
683
                     error ? "\n" : "",
684
                     isempty(object) ? "" : ASSERT_PTR(object_field),
4,928,054✔
685
                     isempty(object) ? "" : object,
4,928,054✔
686
                     isempty(object) ? "" : "\n",
4,928,054✔
687
                     isempty(extra) ? "" : ASSERT_PTR(extra_field),
4,928,054✔
688
                     isempty(extra) ? "" : extra,
4,928,054✔
689
                     isempty(extra) ? "" : "\n",
4,928,054✔
690
                     program_invocation_short_name);
691
        assert_raw((size_t) r < size);
4,928,054✔
692

693
        return 0;
4,928,054✔
694
}
695

696
static void log_do_context(struct iovec *iovec, size_t iovec_len, size_t *n) {
4,928,054✔
697
        assert(iovec);
4,928,054✔
698
        assert(n);
4,928,054✔
699

700
        LIST_FOREACH(ll, c, _log_context) {
7,256,974✔
701
                STRV_FOREACH(s, c->fields) {
19,560,739✔
702
                        if (*n + 2 >= iovec_len)
17,217,618✔
703
                                return;
×
704

705
                        iovec[(*n)++] = IOVEC_MAKE_STRING(*s);
17,217,618✔
706
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
17,217,618✔
707
                }
708

709
                for (size_t i = 0; i < c->n_input_iovec; i++) {
2,347,218✔
710
                        if (*n + 2 >= iovec_len)
4,097✔
711
                                return;
×
712

713
                        iovec[(*n)++] = c->input_iovec[i];
4,097✔
714
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
4,097✔
715
                }
716

717
                if (c->key && c->value) {
2,343,121✔
718
                        if (*n + 3 >= iovec_len)
375,521✔
719
                                return;
14,201✔
720

721
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->key);
361,320✔
722
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->value);
361,320✔
723
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
361,320✔
724
                }
725
        }
726
}
727

728
static int write_to_journal(
5,080,523✔
729
                int level,
730
                int error,
731
                const char *file,
732
                int line,
733
                const char *func,
734
                const char *object_field,
735
                const char *object,
736
                const char *extra_field,
737
                const char *extra,
738
                const char *buffer) {
739

740
        char header[LINE_MAX];
5,080,523✔
741
        size_t n = 0, iovec_len;
5,080,523✔
742
        struct iovec *iovec;
5,080,523✔
743

744
        if (journal_fd < 0)
5,080,523✔
745
                return 0;
5,080,523✔
746

747
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL])
4,873,355✔
748
                return 0;
749

750
        iovec_len = MIN(6 + _log_context_num_fields * 3, IOVEC_MAX);
4,873,355✔
751
        iovec = newa(struct iovec, iovec_len);
4,873,355✔
752

753
        log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
4,873,355✔
754

755
        iovec[n++] = IOVEC_MAKE_STRING(header);
4,873,355✔
756
        iovec[n++] = IOVEC_MAKE_STRING("MESSAGE=");
4,873,355✔
757
        if (log_prefix) {
4,873,355✔
758
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
1,924,959✔
759
                iovec[n++] = IOVEC_MAKE_STRING(": ");
1,924,959✔
760
        }
761
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
4,873,355✔
762
        iovec[n++] = IOVEC_MAKE_STRING("\n");
4,873,355✔
763

764
        log_do_context(iovec, iovec_len, &n);
4,873,355✔
765

766
        const struct msghdr msghdr = {
4,873,355✔
767
                .msg_iov = iovec,
768
                .msg_iovlen = n,
769
        };
770

771
        if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0)
4,873,355✔
772
                return -errno;
824✔
773

774
        return 1;
775
}
776

777
int log_dispatch_internal(
5,121,549✔
778
                int level,
779
                int error,
780
                const char *file,
781
                int line,
782
                const char *func,
783
                const char *object_field,
784
                const char *object,
785
                const char *extra_field,
786
                const char *extra,
787
                char *buffer) {
788

789
        assert_raw(buffer);
5,121,549✔
790

791
        if (log_target == LOG_TARGET_NULL)
5,121,549✔
792
                return -ERRNO_VALUE(error);
10✔
793

794
        /* Patch in LOG_DAEMON facility if necessary */
795
        if (LOG_FAC(level) == 0)
5,121,539✔
796
                level |= log_facility;
5,104,578✔
797

798
        if (open_when_needed)
5,121,539✔
799
                (void) log_open();
205,203✔
800

801
        do {
5,175,125✔
802
                char *e;
5,175,125✔
803
                int k = 0;
5,175,125✔
804

805
                buffer += strspn(buffer, NEWLINE);
5,175,125✔
806

807
                if (buffer[0] == 0)
5,175,125✔
808
                        break;
809

810
                if ((e = strpbrk(buffer, NEWLINE)))
5,131,197✔
811
                        *(e++) = 0;
53,586✔
812

813
                if (IN_SET(log_target, LOG_TARGET_AUTO,
5,131,197✔
814
                                       LOG_TARGET_JOURNAL_OR_KMSG,
815
                                       LOG_TARGET_JOURNAL)) {
816

817
                        k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
5,080,523✔
818
                        if (k < 0 && k != -EAGAIN)
5,080,523✔
819
                                log_close_journal();
4✔
820
                }
821

822
                if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
5,131,197✔
823
                                       LOG_TARGET_SYSLOG)) {
824

825
                        k = write_to_syslog(level, error, file, line, func, buffer);
180✔
826
                        if (k < 0 && k != -EAGAIN)
180✔
827
                                log_close_syslog();
×
828
                }
829

830
                if (k <= 0 &&
5,131,197✔
831
                    IN_SET(log_target, LOG_TARGET_AUTO,
258,486✔
832
                                       LOG_TARGET_SYSLOG_OR_KMSG,
833
                                       LOG_TARGET_JOURNAL_OR_KMSG,
834
                                       LOG_TARGET_KMSG)) {
835

836
                        if (k < 0)
184,803✔
837
                                log_open_kmsg();
813✔
838

839
                        k = write_to_kmsg(level, error, file, line, func, buffer);
184,803✔
840
                        if (k < 0) {
184,803✔
841
                                log_close_kmsg();
×
842
                                (void) log_open_console();
×
843
                        }
844
                }
845

846
                if (k <= 0)
258,486✔
847
                        (void) write_to_console(level, error, file, line, func, buffer);
125,945✔
848

849
                buffer = e;
5,131,197✔
850
        } while (buffer);
5,131,197✔
851

852
        if (open_when_needed)
5,121,539✔
853
                log_close();
205,203✔
854

855
        return -ERRNO_VALUE(error);
5,121,539✔
856
}
857

858
int log_dump_internal(
6✔
859
                int level,
860
                int error,
861
                const char *file,
862
                int line,
863
                const char *func,
864
                char *buffer) {
865

866
        PROTECT_ERRNO;
6✔
867

868
        /* This modifies the buffer... */
869

870
        if (_likely_(LOG_PRI(level) > log_max_level))
6✔
871
                return -ERRNO_VALUE(error);
×
872

873
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
6✔
874
}
875

876
int log_internalv(
3,091,382✔
877
                int level,
878
                int error,
879
                const char *file,
880
                int line,
881
                const char *func,
882
                const char *format,
883
                va_list ap) {
884

885
        if (_likely_(LOG_PRI(level) > log_max_level))
3,091,382✔
886
                return -ERRNO_VALUE(error);
3,091,382✔
887

888
        /* Make sure that %m maps to the specified error (or "Success"). */
889
        char buffer[LINE_MAX];
3,091,158✔
890
        LOCAL_ERRNO(ERRNO_VALUE(error));
3,091,158✔
891

892
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
3,091,158✔
893

894
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
3,091,158✔
895
}
896

897
int log_internal(
3,090,774✔
898
                int level,
899
                int error,
900
                const char *file,
901
                int line,
902
                const char *func,
903
                const char *format, ...) {
904

905
        va_list ap;
3,090,774✔
906
        int r;
3,090,774✔
907

908
        va_start(ap, format);
3,090,774✔
909
        r = log_internalv(level, error, file, line, func, format, ap);
3,090,774✔
910
        va_end(ap);
3,090,774✔
911

912
        return r;
3,090,774✔
913
}
914

915
int log_object_internalv(
2,048,852✔
916
                int level,
917
                int error,
918
                const char *file,
919
                int line,
920
                const char *func,
921
                const char *object_field,
922
                const char *object,
923
                const char *extra_field,
924
                const char *extra,
925
                const char *format,
926
                va_list ap) {
927

928
        char *buffer, *b;
2,048,852✔
929

930
        if (_likely_(LOG_PRI(level) > log_max_level))
2,048,852✔
931
                return -ERRNO_VALUE(error);
2,048,852✔
932

933
        /* Make sure that %m maps to the specified error (or "Success"). */
934
        LOCAL_ERRNO(ERRNO_VALUE(error));
×
935

936
        LOG_SET_PREFIX(object);
4,027,268✔
937

938
        b = buffer = newa(char, LINE_MAX);
2,013,634✔
939
        (void) vsnprintf(b, LINE_MAX, format, ap);
2,013,634✔
940

941
        return log_dispatch_internal(level, error, file, line, func,
2,013,634✔
942
                                     object_field, object, extra_field, extra, buffer);
943
}
944

945
int log_object_internal(
2,037,768✔
946
                int level,
947
                int error,
948
                const char *file,
949
                int line,
950
                const char *func,
951
                const char *object_field,
952
                const char *object,
953
                const char *extra_field,
954
                const char *extra,
955
                const char *format, ...) {
956

957
        va_list ap;
2,037,768✔
958
        int r;
2,037,768✔
959

960
        va_start(ap, format);
2,037,768✔
961
        r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
2,037,768✔
962
        va_end(ap);
2,037,768✔
963

964
        return r;
2,037,768✔
965
}
966

967
static void log_assert(
378✔
968
                int level,
969
                const char *text,
970
                const char *file,
971
                int line,
972
                const char *func,
973
                const char *format) {
974

975
        static char buffer[LINE_MAX];
378✔
976

977
        if (_likely_(LOG_PRI(level) > log_max_level))
378✔
978
                return;
979

980
        DISABLE_WARNING_FORMAT_NONLITERAL;
378✔
981
        (void) snprintf(buffer, sizeof buffer, format, text, file, line, func);
378✔
982
        REENABLE_WARNING;
378✔
983

984
        log_abort_msg = buffer;
378✔
985

986
        log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer);
378✔
987
}
988

989
_noreturn_ void log_assert_failed(
×
990
                const char *text,
991
                const char *file,
992
                int line,
993
                const char *func) {
994
        log_assert(LOG_CRIT, text, file, line, func,
×
995
                   "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
996
        abort();
×
997
}
998

999
_noreturn_ void log_assert_failed_unreachable(
×
1000
                const char *file,
1001
                int line,
1002
                const char *func) {
1003
        log_assert(LOG_CRIT, "Code should not be reached", file, line, func,
×
1004
                   "%s at %s:%u, function %s(). Aborting. 💥");
1005
        abort();
×
1006
}
1007

1008
void log_assert_failed_return(
378✔
1009
                const char *text,
1010
                const char *file,
1011
                int line,
1012
                const char *func) {
1013

1014
        if (assert_return_is_critical)
378✔
1015
                log_assert_failed(text, file, line, func);
×
1016

1017
        PROTECT_ERRNO;
378✔
1018
        log_assert(LOG_DEBUG, text, file, line, func,
378✔
1019
                   "Assertion '%s' failed at %s:%u, function %s(), ignoring.");
1020
}
378✔
1021

1022
int log_oom_internal(int level, const char *file, int line, const char *func) {
×
1023
        return log_internal(level, ENOMEM, file, line, func, "Out of memory.");
×
1024
}
1025

1026
int log_format_iovec(
52,960✔
1027
                struct iovec *iovec,
1028
                size_t iovec_len,
1029
                size_t *n,
1030
                bool newline_separator,
1031
                int error,
1032
                const char *format,
1033
                va_list ap) {
1034

1035
        static const char nl = '\n';
52,960✔
1036

1037
        while (format && *n + 1 < iovec_len) {
333,347✔
1038
                va_list aq;
280,387✔
1039
                char *m;
280,387✔
1040
                int r;
280,387✔
1041

1042
                /* We need to copy the va_list structure,
1043
                 * since vasprintf() leaves it afterwards at
1044
                 * an undefined location */
1045

1046
                errno = ERRNO_VALUE(error);
280,387✔
1047

1048
                va_copy(aq, ap);
280,387✔
1049
                r = vasprintf(&m, format, aq);
280,387✔
1050
                va_end(aq);
280,387✔
1051
                if (r < 0)
280,387✔
1052
                        return -EINVAL;
×
1053

1054
                /* Now, jump enough ahead, so that we point to
1055
                 * the next format string */
1056
                VA_FORMAT_ADVANCE(format, ap);
912,612✔
1057

1058
                iovec[(*n)++] = IOVEC_MAKE_STRING(m);
280,387✔
1059
                if (newline_separator)
280,387✔
1060
                        iovec[(*n)++] = IOVEC_MAKE((char *)&nl, 1);
278,921✔
1061

1062
                format = va_arg(ap, char *);
280,387✔
1063
        }
1064
        return 0;
1065
}
1066

1067
int log_struct_internal(
69,103✔
1068
                int level,
1069
                int error,
1070
                const char *file,
1071
                int line,
1072
                const char *func,
1073
                const char *format, ...) {
1074

1075
        char buf[LINE_MAX];
69,103✔
1076
        bool found = false;
69,103✔
1077
        PROTECT_ERRNO;
69,103✔
1078
        va_list ap;
69,103✔
1079

1080
        if (_likely_(LOG_PRI(level) > log_max_level) ||
69,103✔
1081
            log_target == LOG_TARGET_NULL)
69,103✔
1082
                return -ERRNO_VALUE(error);
50✔
1083

1084
        if (LOG_FAC(level) == 0)
69,053✔
1085
                level |= log_facility;
69,053✔
1086

1087
        if (IN_SET(log_target,
69,053✔
1088
                   LOG_TARGET_AUTO,
1089
                   LOG_TARGET_JOURNAL_OR_KMSG,
1090
                   LOG_TARGET_JOURNAL)) {
1091

1092
                if (open_when_needed)
66,203✔
1093
                        log_open_journal();
8,302✔
1094

1095
                if (journal_fd >= 0) {
66,203✔
1096
                        char header[LINE_MAX];
52,809✔
1097
                        struct iovec *iovec;
52,809✔
1098
                        size_t n = 0, m, iovec_len;
52,809✔
1099
                        int r;
52,809✔
1100
                        bool fallback = false;
52,809✔
1101

1102
                        iovec_len = MIN(17 + _log_context_num_fields * 3, IOVEC_MAX);
52,809✔
1103
                        iovec = newa(struct iovec, iovec_len);
52,809✔
1104

1105
                        /* If the journal is available do structured logging.
1106
                         * Do not report the errno if it is synthetic. */
1107
                        log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
52,809✔
1108
                        iovec[n++] = IOVEC_MAKE_STRING(header);
52,809✔
1109

1110
                        va_start(ap, format);
52,809✔
1111
                        DISABLE_WARNING_FORMAT_NONLITERAL;
52,809✔
1112
                        r = log_format_iovec(iovec, iovec_len, &n, true, error, format, ap);
52,809✔
1113
                        REENABLE_WARNING;
52,809✔
1114
                        m = n;
52,809✔
1115
                        if (r < 0)
52,809✔
1116
                                fallback = true;
1117
                        else {
1118
                                log_do_context(iovec, iovec_len, &n);
52,809✔
1119

1120
                                const struct msghdr msghdr = {
52,809✔
1121
                                        .msg_iov = iovec,
1122
                                        .msg_iovlen = n,
1123
                                };
1124

1125
                                (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
52,809✔
1126
                        }
1127

1128
                        va_end(ap);
52,809✔
1129
                        for (size_t i = 1; i < m; i += 2)
331,730✔
1130
                                free(iovec[i].iov_base);
278,921✔
1131

1132
                        if (!fallback) {
52,809✔
1133
                                if (open_when_needed)
52,809✔
1134
                                        log_close();
8,289✔
1135

1136
                                return -ERRNO_VALUE(error);
52,809✔
1137
                        }
1138
                }
1139
        }
1140

1141
        /* Fallback if journal logging is not available or didn't work. */
1142

1143
        va_start(ap, format);
16,244✔
1144
        while (format) {
42,059✔
1145
                va_list aq;
42,059✔
1146

1147
                errno = ERRNO_VALUE(error);
42,059✔
1148

1149
                va_copy(aq, ap);
42,059✔
1150
                DISABLE_WARNING_FORMAT_NONLITERAL;
42,059✔
1151
                (void) vsnprintf(buf, sizeof buf, format, aq);
42,059✔
1152
                REENABLE_WARNING;
42,059✔
1153
                va_end(aq);
42,059✔
1154

1155
                if (startswith(buf, "MESSAGE=")) {
42,059✔
1156
                        found = true;
16,244✔
1157
                        break;
16,244✔
1158
                }
1159

1160
                VA_FORMAT_ADVANCE(format, ap);
76,137✔
1161

1162
                format = va_arg(ap, char *);
25,815✔
1163
        }
1164
        va_end(ap);
16,244✔
1165

1166
        if (!found) {
16,244✔
UNCOV
1167
                if (open_when_needed)
×
UNCOV
1168
                        log_close();
×
1169

UNCOV
1170
                return -ERRNO_VALUE(error);
×
1171
        }
1172

1173
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
16,244✔
1174
}
1175

1176
int log_struct_iovec_internal(
2,019✔
1177
                int level,
1178
                int error,
1179
                const char *file,
1180
                int line,
1181
                const char *func,
1182
                const struct iovec input_iovec[],
1183
                size_t n_input_iovec) {
1184

1185
        PROTECT_ERRNO;
2,019✔
1186

1187
        if (_likely_(LOG_PRI(level) > log_max_level) ||
2,019✔
1188
            log_target == LOG_TARGET_NULL)
2,019✔
UNCOV
1189
                return -ERRNO_VALUE(error);
×
1190

1191
        if (LOG_FAC(level) == 0)
2,019✔
1192
                level |= log_facility;
2,019✔
1193

1194
        if (IN_SET(log_target, LOG_TARGET_AUTO,
2,019✔
1195
                               LOG_TARGET_JOURNAL_OR_KMSG,
1196
                               LOG_TARGET_JOURNAL) &&
2,019✔
1197
            journal_fd >= 0) {
2,019✔
1198

1199
                char header[LINE_MAX];
1,890✔
1200
                struct iovec *iovec;
1,890✔
1201
                size_t n = 0, iovec_len;
1,890✔
1202

1203
                iovec_len = MIN(1 + n_input_iovec * 2 + _log_context_num_fields * 3, IOVEC_MAX);
1,890✔
1204
                iovec = newa(struct iovec, iovec_len);
1,890✔
1205

1206
                log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1,890✔
1207

1208
                iovec[n++] = IOVEC_MAKE_STRING(header);
1,890✔
1209
                for (size_t i = 0; i < n_input_iovec; i++) {
15,070✔
1210
                        iovec[n++] = input_iovec[i];
13,180✔
1211
                        iovec[n++] = IOVEC_MAKE_STRING("\n");
13,180✔
1212
                }
1213

1214
                log_do_context(iovec, iovec_len, &n);
1,890✔
1215

1216
                const struct msghdr msghdr = {
1,890✔
1217
                        .msg_iov = iovec,
1218
                        .msg_iovlen = n,
1219
                };
1220

1221
                if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0)
1,890✔
1222
                        return -ERRNO_VALUE(error);
1,890✔
1223
        }
1224

1225
        for (size_t i = 0; i < n_input_iovec; i++)
516✔
1226
                if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) {
516✔
1227
                        char *m;
129✔
1228

1229
                        m = strndupa_safe((char*) input_iovec[i].iov_base + STRLEN("MESSAGE="),
129✔
1230
                                          input_iovec[i].iov_len - STRLEN("MESSAGE="));
1231

1232
                        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
129✔
1233
                }
1234

1235
        /* Couldn't find MESSAGE=. */
UNCOV
1236
        return -ERRNO_VALUE(error);
×
1237
}
1238

1239
int log_set_target_from_string(const char *e) {
14,318✔
1240
        LogTarget t;
14,318✔
1241

1242
        t = log_target_from_string(e);
14,318✔
1243
        if (t < 0)
14,318✔
1244
                return t;
1245

1246
        log_set_target(t);
14,318✔
1247
        return 0;
14,318✔
1248
}
1249

1250
int log_set_max_level_from_string(const char *e) {
27,885✔
1251
        int r;
77,394✔
1252

1253
        for (;;) {
77,394✔
1254
                _cleanup_free_ char *word = NULL, *prefix = NULL;
77,394✔
1255
                LogTarget target;
77,394✔
1256
                const char *colon;
77,394✔
1257

1258
                r = extract_first_word(&e, &word, ",", 0);
77,394✔
1259
                if (r < 0)
77,394✔
1260
                        return r;
1261
                if (r == 0)
77,394✔
1262
                        break;
1263

1264
                colon = strchr(word, ':');
49,509✔
1265
                if (!colon) {
49,509✔
1266
                        r = log_level_from_string(word);
27,885✔
1267
                        if (r < 0)
27,885✔
1268
                                return r;
1269

1270
                        log_set_max_level(r);
27,885✔
1271
                        continue;
27,885✔
1272
                }
1273

1274
                prefix = strndup(word, colon - word);
21,624✔
1275
                if (!prefix)
21,624✔
1276
                        return -ENOMEM;
1277

1278
                target = log_target_from_string(prefix);
21,624✔
1279
                if (target < 0)
21,624✔
1280
                        return target;
1281

1282
                if (target >= _LOG_TARGET_SINGLE_MAX)
21,624✔
1283
                        return -EINVAL;
1284

1285
                r = log_level_from_string(colon + 1);
21,624✔
1286
                if (r < 0)
21,624✔
1287
                        return r;
1288

1289
                log_target_max_level[target] = r;
21,624✔
1290
        }
1291

1292
        return 0;
27,885✔
1293
}
1294

1295
int log_max_levels_to_string(int level, char **ret) {
3,882✔
1296
        _cleanup_free_ char *s = NULL;
3,882✔
1297
        int r;
3,882✔
1298

1299
        assert(ret);
3,882✔
1300

1301
        r = log_level_to_string_alloc(level, &s);
3,882✔
1302
        if (r < 0)
3,882✔
1303
                return r;
1304

1305
        for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
19,410✔
1306
                _cleanup_free_ char *l = NULL;
3,882✔
1307

1308
                if (log_target_max_level[target] == INT_MAX)
15,528✔
1309
                        continue;
11,646✔
1310

1311
                r = log_level_to_string_alloc(log_target_max_level[target], &l);
3,882✔
1312
                if (r < 0)
3,882✔
1313
                        return r;
1314

1315
                r = strextendf_with_separator(&s, ",", "%s:%s", log_target_to_string(target), l);
3,882✔
1316
                if (r < 0)
3,882✔
1317
                        return r;
1318
        }
1319

1320
        *ret = TAKE_PTR(s);
3,882✔
1321
        return 0;
3,882✔
1322
}
1323

1324
static int log_set_ratelimit_kmsg_from_string(const char *e) {
8,058✔
1325
        int r;
8,058✔
1326

1327
        r = parse_boolean(e);
8,058✔
1328
        if (r < 0)
8,058✔
1329
                return r;
1330

1331
        ratelimit_kmsg = r;
8,058✔
1332
        return 0;
8,058✔
1333
}
1334

1335
void log_set_assert_return_is_critical(bool b) {
1,159✔
1336
        assert_return_is_critical = b;
1,159✔
1337
}
1,159✔
1338

1339
bool log_get_assert_return_is_critical(void) {
386✔
1340
        return assert_return_is_critical;
386✔
1341
}
1342

1343
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
293,489✔
1344

1345
        /*
1346
         * The systemd.log_xyz= settings are parsed by all tools, and
1347
         * so is "debug".
1348
         *
1349
         * However, "quiet" is only parsed by PID 1, and only turns of
1350
         * status output to /dev/console, but does not alter the log
1351
         * level.
1352
         */
1353

1354
        if (streq(key, "debug") && !value)
293,489✔
1355
                log_set_max_level(LOG_DEBUG);
×
1356

1357
        else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
293,489✔
1358

1359
                if (proc_cmdline_value_missing(key, value))
×
1360
                        return 0;
1361

UNCOV
1362
                if (log_set_target_from_string(value) < 0)
×
UNCOV
1363
                        log_warning("Failed to parse log target '%s', ignoring.", value);
×
1364

1365
        } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
293,489✔
1366

1367
                if (proc_cmdline_value_missing(key, value))
8,058✔
1368
                        return 0;
1369

1370
                if (log_set_max_level_from_string(value) < 0)
8,058✔
1371
                        log_warning("Failed to parse log level setting '%s', ignoring.", value);
×
1372

1373
        } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
285,431✔
1374

UNCOV
1375
                if (log_show_color_from_string(value ?: "1") < 0)
×
1376
                        log_warning("Failed to parse log color setting '%s', ignoring.", value);
×
1377

1378
        } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
285,431✔
1379

UNCOV
1380
                if (log_show_location_from_string(value ?: "1") < 0)
×
1381
                        log_warning("Failed to parse log location setting '%s', ignoring.", value);
×
1382

1383
        } else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
285,431✔
1384

UNCOV
1385
                if (log_show_tid_from_string(value ?: "1") < 0)
×
1386
                        log_warning("Failed to parse log tid setting '%s', ignoring.", value);
×
1387

1388
        } else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
285,431✔
1389

UNCOV
1390
                if (log_show_time_from_string(value ?: "1") < 0)
×
UNCOV
1391
                        log_warning("Failed to parse log time setting '%s', ignoring.", value);
×
1392

1393
        } else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
285,431✔
1394

1395
                if (log_set_ratelimit_kmsg_from_string(value ?: "1") < 0)
8,058✔
UNCOV
1396
                        log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", value);
×
1397
        }
1398

1399
        return 0;
1400
}
1401

1402
static bool should_parse_proc_cmdline(void) {
91,673✔
1403
        /* PID1 always reads the kernel command line. */
1404
        if (getpid_cached() == 1)
91,673✔
1405
                return true;
1406

1407
        /* Otherwise, parse the command line if invoked directly by systemd. */
1408
        return invoked_by_systemd();
91,332✔
1409
}
1410

1411
void log_parse_environment_variables(void) {
93,832✔
1412
        const char *e;
93,832✔
1413
        int r;
93,832✔
1414

1415
        e = getenv("SYSTEMD_LOG_TARGET");
93,832✔
1416
        if (e && log_set_target_from_string(e) < 0)
93,832✔
UNCOV
1417
                log_warning("Failed to parse log target '%s', ignoring.", e);
×
1418

1419
        e = getenv("SYSTEMD_LOG_LEVEL");
93,832✔
1420
        if (e) {
93,832✔
1421
                r = log_set_max_level_from_string(e);
6,261✔
1422
                if (r < 0)
6,261✔
UNCOV
1423
                        log_warning_errno(r, "Failed to parse log level '%s', ignoring: %m", e);
×
1424
        } else {
1425
                /* If no explicit log level is specified then let's see if this is a debug invocation, and if
1426
                 * so raise the log level to debug too. Note that this is not symmetric: just because
1427
                 * DEBUG_INVOCATION is explicitly set to 0 we won't lower the log level below debug. This
1428
                 * follows the logic that debug logging is an opt-in thing anyway, and if there's any reason
1429
                 * to enable it we should not disable it here automatically. */
1430
                r = getenv_bool("DEBUG_INVOCATION");
87,571✔
1431
                if (r < 0 && r != -ENXIO)
87,571✔
UNCOV
1432
                        log_warning_errno(r, "Failed to parse $DEBUG_INVOCATION value, ignoring: %m");
×
1433
                else if (r > 0)
87,571✔
UNCOV
1434
                        log_set_max_level(LOG_DEBUG);
×
1435
        }
1436

1437
        e = getenv("SYSTEMD_LOG_COLOR");
93,832✔
1438
        if (e && log_show_color_from_string(e) < 0)
93,832✔
1439
                log_warning("Failed to parse log color '%s', ignoring.", e);
×
1440

1441
        e = getenv("SYSTEMD_LOG_LOCATION");
93,832✔
1442
        if (e && log_show_location_from_string(e) < 0)
93,832✔
1443
                log_warning("Failed to parse log location '%s', ignoring.", e);
×
1444

1445
        e = getenv("SYSTEMD_LOG_TIME");
93,832✔
1446
        if (e && log_show_time_from_string(e) < 0)
93,832✔
1447
                log_warning("Failed to parse log time '%s', ignoring.", e);
×
1448

1449
        e = getenv("SYSTEMD_LOG_TID");
93,832✔
1450
        if (e && log_show_tid_from_string(e) < 0)
93,832✔
1451
                log_warning("Failed to parse log tid '%s', ignoring.", e);
×
1452

1453
        e = getenv("SYSTEMD_LOG_RATELIMIT_KMSG");
93,832✔
1454
        if (e && log_set_ratelimit_kmsg_from_string(e) < 0)
93,832✔
UNCOV
1455
                log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", e);
×
1456
}
93,832✔
1457

1458
void log_parse_environment(void) {
91,673✔
1459
        /* Do not call from library code. */
1460

1461
        if (should_parse_proc_cmdline())
91,673✔
1462
                (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
8,131✔
1463

1464
        log_parse_environment_variables();
91,673✔
1465
}
91,673✔
1466

1467
LogTarget log_get_target(void) {
19,931✔
1468
        return log_target;
19,931✔
1469
}
1470

1471
void log_settle_target(void) {
17,320✔
1472

1473
        /* If we're using LOG_TARGET_AUTO and opening the log again on every single log call, we'll check if
1474
         * stderr is attached to the journal every single log call. However, if we then close all file
1475
         * descriptors later, that will stop working because stderr will be closed as well. To avoid that
1476
         * problem, this function is used to permanently change the log target depending on whether stderr is
1477
         * connected to the journal or not. */
1478

1479
        LogTarget t = log_get_target();
17,320✔
1480

1481
        if (t != LOG_TARGET_AUTO)
17,320✔
1482
                return;
1483

1484
        t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
8,375✔
1485
                                                        : LOG_TARGET_CONSOLE;
8,364✔
1486
        log_set_target(t);
4,199✔
1487
}
1488

1489
int log_get_max_level(void) {
13,879,027✔
1490
        return log_max_level;
13,879,027✔
1491
}
1492

1493
void log_show_color(bool b) {
32,640✔
1494
        show_color = b;
32,640✔
1495
}
32,640✔
1496

1497
bool log_get_show_color(void) {
68,625✔
1498
        return show_color > 0; /* Defaults to false. */
68,625✔
1499
}
1500

UNCOV
1501
void log_show_location(bool b) {
×
UNCOV
1502
        show_location = b;
×
UNCOV
1503
}
×
1504

1505
bool log_get_show_location(void) {
14✔
1506
        return show_location;
14✔
1507
}
1508

1509
void log_show_time(bool b) {
1✔
1510
        show_time = b;
1✔
1511
}
1✔
1512

1513
bool log_get_show_time(void) {
14✔
1514
        return show_time;
14✔
1515
}
1516

1517
void log_show_tid(bool b) {
1✔
1518
        show_tid = b;
1✔
1519
}
1✔
1520

1521
bool log_get_show_tid(void) {
×
1522
        return show_tid;
×
1523
}
1524

1525
int log_show_color_from_string(const char *e) {
×
UNCOV
1526
        int r;
×
1527

1528
        r = parse_boolean(e);
×
1529
        if (r < 0)
×
1530
                return r;
1531

1532
        log_show_color(r);
×
1533
        return 0;
×
1534
}
1535

1536
int log_show_location_from_string(const char *e) {
×
UNCOV
1537
        int r;
×
1538

1539
        r = parse_boolean(e);
×
1540
        if (r < 0)
×
1541
                return r;
1542

1543
        log_show_location(r);
×
1544
        return 0;
×
1545
}
1546

1547
int log_show_time_from_string(const char *e) {
×
UNCOV
1548
        int r;
×
1549

1550
        r = parse_boolean(e);
×
1551
        if (r < 0)
×
1552
                return r;
1553

1554
        log_show_time(r);
×
1555
        return 0;
×
1556
}
1557

1558
int log_show_tid_from_string(const char *e) {
×
UNCOV
1559
        int r;
×
1560

1561
        r = parse_boolean(e);
×
1562
        if (r < 0)
×
1563
                return r;
1564

UNCOV
1565
        log_show_tid(r);
×
UNCOV
1566
        return 0;
×
1567
}
1568

1569
bool log_on_console(void) {
117,545✔
1570
        if (IN_SET(log_target, LOG_TARGET_CONSOLE,
117,545✔
1571
                               LOG_TARGET_CONSOLE_PREFIXED))
1572
                return true;
1573

1574
        return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
201,186✔
1575
}
1576

1577
static const char *const log_target_table[_LOG_TARGET_MAX] = {
1578
        [LOG_TARGET_CONSOLE]          = "console",
1579
        [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1580
        [LOG_TARGET_KMSG]             = "kmsg",
1581
        [LOG_TARGET_JOURNAL]          = "journal",
1582
        [LOG_TARGET_JOURNAL_OR_KMSG]  = "journal-or-kmsg",
1583
        [LOG_TARGET_SYSLOG]           = "syslog",
1584
        [LOG_TARGET_SYSLOG_OR_KMSG]   = "syslog-or-kmsg",
1585
        [LOG_TARGET_AUTO]             = "auto",
1586
        [LOG_TARGET_NULL]             = "null",
1587
};
1588

1589
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
43,820✔
1590

1591
void log_received_signal(int level, const struct signalfd_siginfo *si) {
6,040✔
1592
        assert(si);
6,040✔
1593

1594
        if (pid_is_valid(si->ssi_pid)) {
6,040✔
1595
                _cleanup_free_ char *p = NULL;
6,040✔
1596

1597
                (void) pid_get_comm(si->ssi_pid, &p);
6,040✔
1598

1599
                log_full(level,
7,209✔
1600
                         "Received SIG%s from PID %"PRIu32" (%s).",
1601
                         signal_to_string(si->ssi_signo),
1602
                         si->ssi_pid, strna(p));
1603
        } else
UNCOV
1604
                log_full(level,
×
1605
                         "Received SIG%s.",
1606
                         signal_to_string(si->ssi_signo));
1607
}
6,040✔
1608

1609
void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata) {
850✔
1610
        assert(!log_syntax_callback || !cb);
850✔
1611
        assert(!log_syntax_callback_userdata || !userdata);
850✔
1612

1613
        log_syntax_callback = cb;
850✔
1614
        log_syntax_callback_userdata = userdata;
850✔
1615
}
850✔
1616

1617
int log_syntax_internal(
14,120✔
1618
                const char *unit,
1619
                int level,
1620
                const char *config_file,
1621
                unsigned config_line,
1622
                int error,
1623
                const char *file,
1624
                int line,
1625
                const char *func,
1626
                const char *format, ...) {
1627

1628
        PROTECT_ERRNO;
14,120✔
1629

1630
        if (log_syntax_callback)
14,120✔
1631
                log_syntax_callback(unit, level, log_syntax_callback_userdata);
846✔
1632

1633
        if (_likely_(LOG_PRI(level) > log_max_level) ||
14,120✔
1634
            log_target == LOG_TARGET_NULL)
14,120✔
1635
                return -ERRNO_VALUE(error);
30✔
1636

1637
        char buffer[LINE_MAX];
14,090✔
1638
        va_list ap;
14,090✔
1639
        const char *unit_fmt = NULL;
14,090✔
1640

1641
        errno = ERRNO_VALUE(error);
14,090✔
1642

1643
        va_start(ap, format);
14,090✔
1644
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
14,090✔
1645
        va_end(ap);
14,090✔
1646

1647
        if (unit)
14,090✔
1648
                unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
7,144✔
1649

1650
        if (config_file) {
14,090✔
1651
                if (config_line > 0)
14,080✔
1652
                        return log_struct_internal(
8,602✔
1653
                                        level,
1654
                                        error,
1655
                                        file, line, func,
1656
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
8,602✔
1657
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
8,602✔
1658
                                        LOG_ITEM("CONFIG_LINE=%u", config_line),
8,602✔
1659
                                        LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
8,602✔
1660
                                        unit_fmt, unit,
1661
                                        NULL);
1662
                else
1663
                        return log_struct_internal(
5,478✔
1664
                                        level,
1665
                                        error,
1666
                                        file, line, func,
1667
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
5,478✔
1668
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
5,478✔
1669
                                        LOG_MESSAGE("%s: %s", config_file, buffer),
5,478✔
1670
                                        unit_fmt, unit,
1671
                                        NULL);
1672
        } else if (unit)
10✔
UNCOV
1673
                return log_struct_internal(
×
1674
                                level,
1675
                                error,
1676
                                file, line, func,
UNCOV
1677
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
×
UNCOV
1678
                                LOG_MESSAGE("%s: %s", unit, buffer),
×
1679
                                unit_fmt, unit,
1680
                                NULL);
1681
        else
1682
                return log_struct_internal(
10✔
1683
                                level,
1684
                                error,
1685
                                file, line, func,
1686
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
10✔
1687
                                LOG_MESSAGE("%s", buffer),
10✔
1688
                                NULL);
1689
}
1690

1691
int log_syntax_invalid_utf8_internal(
1✔
1692
                const char *unit,
1693
                int level,
1694
                const char *config_file,
1695
                unsigned config_line,
1696
                const char *file,
1697
                int line,
1698
                const char *func,
1699
                const char *rvalue) {
1700

UNCOV
1701
        PROTECT_ERRNO;
×
1702
        _cleanup_free_ char *p = NULL;
1✔
1703

1704
        if (rvalue)
1✔
1705
                p = utf8_escape_invalid(rvalue);
1✔
1706

1707
        return log_syntax_internal(unit, level, config_file, config_line,
1✔
1708
                                   SYNTHETIC_ERRNO(EINVAL), file, line, func,
1709
                                   "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1710
}
1711

1712
int log_syntax_parse_error_internal(
110✔
1713
                const char *unit,
1714
                const char *config_file,
1715
                unsigned config_line,
1716
                int error,
1717
                bool critical,
1718
                const char *file,
1719
                int line,
1720
                const char *func,
1721
                const char *lvalue,
1722
                const char *rvalue) {
1723

UNCOV
1724
        PROTECT_ERRNO;
×
1725
        _cleanup_free_ char *escaped = NULL;
110✔
1726

1727
        /* OOM is always handled as critical. */
1728
        if (ERRNO_VALUE(error) == ENOMEM)
110✔
1729
                return log_oom_internal(LOG_ERR, file, line, func);
×
1730

1731
        if (rvalue && !utf8_is_valid(rvalue)) {
220✔
1732
                escaped = utf8_escape_invalid(rvalue);
×
UNCOV
1733
                if (!escaped)
×
1734
                        rvalue = "(oom)";
1735
                else
UNCOV
1736
                        rvalue = " (escaped)";
×
1737
        }
1738

1739
        log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
550✔
1740
                            file, line, func,
1741
                            "Failed to parse %s=%s%s%s%s%s",
1742
                            strna(lvalue), strempty(escaped), strempty(rvalue),
1743
                            critical ? "" : ", ignoring",
1744
                            error == 0 ? "." : ": ",
1745
                            error == 0 ? "" : STRERROR(error));
110✔
1746

1747
        return critical ? -ERRNO_VALUE(error) : 0;
110✔
1748
}
1749

1750
void log_set_upgrade_syslog_to_journal(bool b) {
250✔
1751
        upgrade_syslog_to_journal = b;
250✔
1752

1753
        /* Make the change effective immediately */
1754
        if (b) {
250✔
1755
                if (log_target == LOG_TARGET_SYSLOG)
250✔
UNCOV
1756
                        log_target = LOG_TARGET_JOURNAL;
×
1757
                else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
250✔
UNCOV
1758
                        log_target = LOG_TARGET_JOURNAL_OR_KMSG;
×
1759
        }
1760
}
250✔
1761

1762
void log_set_always_reopen_console(bool b) {
13,816✔
1763
        always_reopen_console = b;
13,816✔
1764
}
13,816✔
1765

1766
void log_set_open_when_needed(bool b) {
22,100✔
1767
        open_when_needed = b;
22,100✔
1768
}
22,100✔
1769

1770
void log_set_prohibit_ipc(bool b) {
55,139✔
1771
        prohibit_ipc = b;
55,139✔
1772
}
55,139✔
1773

1774
int log_emergency_level(void) {
×
1775
        /* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
1776
         * then the system of the whole system is obviously affected. */
1777

UNCOV
1778
        return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
×
1779
}
1780

1781
int log_dup_console(void) {
128✔
1782
        int copy;
128✔
1783

1784
        /* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
1785
         * whenever we want to continue logging through the original fd, but want to rearrange stderr. */
1786

1787
        if (console_fd < 0 || console_fd >= 3)
128✔
1788
                return 0;
1789

1790
        copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);
1✔
1791
        if (copy < 0)
1✔
UNCOV
1792
                return -errno;
×
1793

1794
        console_fd = copy;
1✔
1795
        return 0;
1✔
1796
}
1797

1798
void log_setup(void) {
90,823✔
1799
        log_set_target(LOG_TARGET_AUTO);
90,823✔
1800
        log_parse_environment();
90,823✔
1801
        (void) log_open();
90,823✔
1802
        if (log_on_console() && show_color < 0)
90,823✔
1803
                log_show_color(true);
32,485✔
1804
}
90,823✔
1805

1806
const char* _log_set_prefix(const char *prefix, bool force) {
4,027,656✔
1807
        const char *old = log_prefix;
4,027,656✔
1808

1809
        if (prefix || force)
4,027,656✔
1810
                log_prefix = prefix;
4,024,923✔
1811

1812
        return old;
4,027,656✔
1813
}
1814

1815
static int saved_log_context_enabled = -1;
1816

1817
bool log_context_enabled(void) {
775,885✔
1818
        int r;
775,885✔
1819

1820
        if (log_get_max_level() == LOG_DEBUG)
775,885✔
1821
                return true;
1822

1823
        if (saved_log_context_enabled >= 0)
102,829✔
1824
                return saved_log_context_enabled;
95,555✔
1825

1826
        r = secure_getenv_bool("SYSTEMD_ENABLE_LOG_CONTEXT");
7,274✔
1827
        if (r < 0 && r != -ENXIO)
7,274✔
UNCOV
1828
                log_debug_errno(r, "Failed to parse $SYSTEMD_ENABLE_LOG_CONTEXT, ignoring: %m");
×
1829

1830
        saved_log_context_enabled = r > 0;
7,274✔
1831

1832
        return saved_log_context_enabled;
7,274✔
1833
}
1834

1835
static LogContext* log_context_attach(LogContext *c) {
709,163✔
1836
        assert(c);
709,163✔
1837

1838
        _log_context_num_fields += strv_length(c->fields);
709,163✔
1839
        _log_context_num_fields += c->n_input_iovec;
709,163✔
1840
        _log_context_num_fields += !!c->key;
709,163✔
1841

1842
        return LIST_PREPEND(ll, _log_context, c);
709,163✔
1843
}
1844

1845
static LogContext* log_context_detach(LogContext *c) {
681,927✔
1846
        if (!c)
681,927✔
1847
                return NULL;
1848

1849
        assert(_log_context_num_fields >= strv_length(c->fields) + c->n_input_iovec +!!c->key);
681,927✔
1850
        _log_context_num_fields -= strv_length(c->fields);
681,927✔
1851
        _log_context_num_fields -= c->n_input_iovec;
681,927✔
1852
        _log_context_num_fields -= !!c->key;
681,927✔
1853

1854
        LIST_REMOVE(ll, _log_context, c);
681,927✔
1855
        return NULL;
681,927✔
1856
}
1857

1858
LogContext* log_context_new(const char *key, const char *value) {
34,886✔
1859
        assert(key);
34,886✔
1860
        assert(endswith(key, "="));
34,886✔
1861
        assert(value);
34,886✔
1862

1863
        LIST_FOREACH(ll, i, _log_context)
52,320✔
1864
                if (i->key == key && i->value == value)
17,443✔
1865
                        return log_context_ref(i);
9✔
1866

1867
        LogContext *c = new(LogContext, 1);
34,877✔
1868
        if (!c)
34,877✔
1869
                return NULL;
1870

1871
        *c = (LogContext) {
34,877✔
1872
                .n_ref = 1,
1873
                .key = (char *) key,
1874
                .value = (char *) value,
1875
        };
1876

1877
        return log_context_attach(c);
34,877✔
1878
}
1879

1880
LogContext* log_context_new_strv(char **fields, bool owned) {
675,201✔
1881
        if (!fields)
675,201✔
1882
                return NULL;
1883

1884
        LIST_FOREACH(ll, i, _log_context)
675,190✔
1885
                if (i->fields == fields) {
1,085✔
1886
                        assert(!owned);
18✔
1887
                        return log_context_ref(i);
18✔
1888
                }
1889

1890
        LogContext *c = new(LogContext, 1);
674,105✔
1891
        if (!c)
674,105✔
1892
                return NULL;
1893

1894
        *c = (LogContext) {
674,105✔
1895
                .n_ref = 1,
1896
                .fields = fields,
1897
                .owned = owned,
1898
        };
1899

1900
        return log_context_attach(c);
674,105✔
1901
}
1902

1903
LogContext* log_context_new_iov(struct iovec *input_iovec, size_t n_input_iovec, bool owned) {
354,911✔
1904
        if (!input_iovec || n_input_iovec == 0)
354,911✔
1905
                return NULL;
1906

1907
        LIST_FOREACH(ll, i, _log_context)
777✔
1908
                if (i->input_iovec == input_iovec && i->n_input_iovec == n_input_iovec) {
596✔
1909
                        assert(!owned);
261✔
1910
                        return log_context_ref(i);
261✔
1911
                }
1912

1913
        LogContext *c = new(LogContext, 1);
181✔
1914
        if (!c)
181✔
1915
                return NULL;
1916

1917
        *c = (LogContext) {
181✔
1918
                .n_ref = 1,
1919
                .input_iovec = input_iovec,
1920
                .n_input_iovec = n_input_iovec,
1921
                .owned = owned,
1922
        };
1923

1924
        return log_context_attach(c);
181✔
1925
}
1926

1927
static LogContext* log_context_free(LogContext *c) {
681,927✔
1928
        if (!c)
681,927✔
1929
                return NULL;
1930

1931
        log_context_detach(c);
681,927✔
1932

1933
        if (c->owned) {
681,927✔
1934
                strv_free(c->fields);
673,019✔
1935
                iovec_array_free(c->input_iovec, c->n_input_iovec);
673,019✔
1936
                free(c->key);
673,019✔
1937
                free(c->value);
673,019✔
1938
        }
1939

1940
        return mfree(c);
681,927✔
1941
}
1942

1943
DEFINE_TRIVIAL_REF_UNREF_FUNC(LogContext, log_context, log_context_free);
682,503✔
1944

1945
LogContext* log_context_new_strv_consume(char **fields) {
673,065✔
1946
        LogContext *c = log_context_new_strv(fields, /*owned=*/ true);
673,065✔
1947
        if (!c)
673,065✔
UNCOV
1948
                strv_free(fields);
×
1949

1950
        return c;
673,065✔
1951
}
1952

1953
LogContext* log_context_new_iov_consume(struct iovec *input_iovec, size_t n_input_iovec) {
9✔
1954
        LogContext *c = log_context_new_iov(input_iovec, n_input_iovec, /*owned=*/ true);
9✔
1955
        if (!c)
9✔
UNCOV
1956
                iovec_array_free(input_iovec, n_input_iovec);
×
1957

1958
        return c;
9✔
1959
}
1960

1961
size_t log_context_num_contexts(void) {
81✔
1962
        size_t n = 0;
81✔
1963

1964
        LIST_FOREACH(ll, c, _log_context)
225✔
1965
                n++;
144✔
1966

1967
        return n;
81✔
1968
}
1969

1970
size_t log_context_num_fields(void) {
81✔
1971
        return _log_context_num_fields;
81✔
1972
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc