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

systemd / systemd / 28724371642

04 Jul 2026 01:10PM UTC coverage: 72.894% (-0.006%) from 72.9%
28724371642

push

github

bluca
hwdb: Make Amlogic burn mode work out-of-box

343271 of 470919 relevant lines covered (72.89%)

1354054.74 hits per line

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

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

3
#include <fcntl.h>
4
#include <sys/signalfd.h>
5
#include <sys/stat.h>
6
#include <sys/uio.h>
7
#include <threads.h>
8
#include <unistd.h>
9

10
#include "sd-messages.h"
11

12
#include "alloc-util.h"
13
#include "ansi-color.h"
14
#include "argv-util.h"
15
#include "env-util.h"
16
#include "errno-util.h"
17
#include "extract-word.h"
18
#include "fd-util.h"
19
#include "format-util.h"
20
#include "iovec-util.h"
21
#include "list.h"
22
#include "log.h"
23
#include "log-context.h"
24
#include "parse-util.h"
25
#include "proc-cmdline.h"
26
#include "process-util.h"
27
#include "ratelimit.h"
28
#include "signal-util.h"
29
#include "socket-util.h"
30
#include "stdio-util.h"
31
#include "string-table.h"
32
#include "string-util.h"
33
#include "strv.h"
34
#include "syslog-util.h"
35
#include "terminal-util.h"
36
#include "time-util.h"
37
#include "utf8.h"
38

39
#define SNDBUF_SIZE (8*1024*1024)
40
#define IOVEC_MAX 256U
41

42
static log_syntax_callback_t log_syntax_callback = NULL;
43
static void *log_syntax_callback_userdata = NULL;
44

45
static LogTarget log_target = LOG_TARGET_CONSOLE;
46
static int log_max_level = LOG_INFO;
47
static int log_target_max_level[_LOG_TARGET_SINGLE_MAX] = {
48
        [LOG_TARGET_CONSOLE] = INT_MAX,
49
        [LOG_TARGET_KMSG]    = INT_MAX,
50
        [LOG_TARGET_SYSLOG]  = INT_MAX,
51
        [LOG_TARGET_JOURNAL] = INT_MAX,
52
};
53
static int log_facility = LOG_DAEMON;
54
static bool ratelimit_kmsg = true;
55

56
static int console_fd = STDERR_FILENO;
57
static int console_fd_is_tty = -1; /* tri-state: -1 means don't know */
58
static int syslog_fd = -EBADF;
59
static int kmsg_fd = -EBADF;
60
static int journal_fd = -EBADF;
61

62
static bool syslog_is_stream = false;
63

64
static int show_color = -1; /* tristate */
65
static bool show_location = false;
66
static bool show_time = false;
67
static bool show_tid = false;
68

69
static bool upgrade_syslog_to_journal = false;
70
static bool always_reopen_console = false;
71
static bool open_when_needed = false;
72
static bool prohibit_ipc = false;
73

74
static thread_local const char *log_prefix = NULL;
75

76
#if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
77
bool _log_message_dummy = false; /* Always false */
78
#endif
79

80
/* An assert to use in logging functions that does not call recursively
81
 * into our logging functions (since that might lead to a loop). */
82
#define assert_raw(expr)                                                \
83
        do {                                                            \
84
                if (_unlikely_(!(expr))) {                              \
85
                        fputs(#expr "\n", stderr);                      \
86
                        abort();                                        \
87
                }                                                       \
88
        } while (false)
89

90
void log_prefix_swap(const char **prefix) {
1,132✔
91
        assert(prefix);
1,132✔
92

93
        SWAP_TWO(log_prefix, *prefix);
1,132✔
94
}
1,132✔
95

96
static void log_close_console(void) {
489,148✔
97
        /* See comment in log_close_journal() */
98
        (void) safe_close_above_stdio(TAKE_FD(console_fd));
489,148✔
99
        console_fd_is_tty = -1;
489,148✔
100
}
489,148✔
101

102
static int log_open_console(void) {
83,867✔
103

104
        if (!always_reopen_console) {
83,867✔
105
                console_fd = STDERR_FILENO;
73,263✔
106
                console_fd_is_tty = -1;
73,263✔
107
                return 0;
73,263✔
108
        }
109

110
        if (console_fd < 3) {
10,604✔
111
                int fd;
8,264✔
112

113
                fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
8,264✔
114
                if (fd < 0)
8,264✔
115
                        return fd;
116

117
                console_fd = fd_move_above_stdio(fd);
8,096✔
118
                console_fd_is_tty = true;
8,096✔
119
        }
120

121
        return 0;
122
}
123

124
static void log_close_kmsg(void) {
245,075✔
125
        /* See comment in log_close_journal() */
126
        (void) safe_close(TAKE_FD(kmsg_fd));
245,075✔
127
}
245,075✔
128

129
static int log_open_kmsg(void) {
23,944✔
130

131
        if (kmsg_fd >= 0)
23,944✔
132
                return 0;
133

134
        kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
20,040✔
135
        if (kmsg_fd < 0)
20,040✔
136
                return -errno;
8,874✔
137

138
        kmsg_fd = fd_move_above_stdio(kmsg_fd);
11,166✔
139
        return 0;
11,166✔
140
}
141

142
static void log_close_syslog(void) {
573,013✔
143
        /* See comment in log_close_journal() */
144
        (void) safe_close(TAKE_FD(syslog_fd));
573,013✔
145
}
573,013✔
146

147
static int create_log_socket(int type) {
220,634✔
148
        struct timeval tv;
220,634✔
149
        int fd;
220,634✔
150

151
        fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
220,634✔
152
        if (fd < 0)
220,634✔
153
                return -errno;
×
154

155
        fd = fd_move_above_stdio(fd);
220,634✔
156
        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
220,634✔
157

158
        /* We need a blocking fd here since we'd otherwise lose messages way too early. However, let's not hang forever
159
         * in the unlikely case of a deadlock. */
160
        if (getpid_cached() == 1)
220,634✔
161
                timeval_store(&tv, 10 * USEC_PER_MSEC);
306✔
162
        else
163
                timeval_store(&tv, 10 * USEC_PER_SEC);
220,328✔
164
        (void) setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
220,634✔
165

166
        return fd;
220,634✔
167
}
168

169
static int log_open_syslog(void) {
2✔
170
        int r;
2✔
171

172
        if (syslog_fd >= 0)
2✔
173
                return 0;
174

175
        syslog_fd = create_log_socket(SOCK_DGRAM);
2✔
176
        if (syslog_fd < 0) {
2✔
177
                r = syslog_fd;
×
178
                goto fail;
×
179
        }
180

181
        r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
2✔
182
        if (r < 0) {
2✔
183
                safe_close(syslog_fd);
×
184

185
                /* Some legacy syslog systems still use stream sockets. They really shouldn't. But what can
186
                 * we do... */
187
                syslog_fd = create_log_socket(SOCK_STREAM);
×
188
                if (syslog_fd < 0) {
×
189
                        r = syslog_fd;
×
190
                        goto fail;
×
191
                }
192

193
                r = connect_unix_path(syslog_fd, AT_FDCWD, "/dev/log");
×
194
                if (r < 0)
×
195
                        goto fail;
×
196

197
                syslog_is_stream = true;
×
198
        } else
199
                syslog_is_stream = false;
2✔
200

201
        return 0;
202

203
fail:
×
204
        log_close_syslog();
×
205
        return r;
×
206
}
207

208
static void log_close_journal(void) {
343,727✔
209
        /* If the journal FD is bad, safe_close will fail, and will try to log, which will fail, so we'll
210
         * try to close the journal FD, which is bad, so safe_close will fail... Whether we can close it
211
         * or not, invalidate it immediately so that we don't get in a recursive loop until we run out of
212
         * stack. */
213
        (void) safe_close(TAKE_FD(journal_fd));
343,727✔
214
}
343,727✔
215

216
static int log_open_journal(void) {
240,668✔
217
        int r;
240,668✔
218

219
        if (journal_fd >= 0)
240,668✔
220
                return 0;
221

222
        journal_fd = create_log_socket(SOCK_DGRAM);
220,632✔
223
        if (journal_fd < 0) {
220,632✔
224
                r = journal_fd;
×
225
                goto fail;
×
226
        }
227

228
        r = connect_unix_path(journal_fd, AT_FDCWD, "/run/systemd/journal/socket");
220,632✔
229
        if (r < 0)
220,632✔
230
                goto fail;
94✔
231

232
        return 0;
233

234
fail:
94✔
235
        log_close_journal();
94✔
236
        return r;
94✔
237
}
238

239
bool stderr_is_journal(void) {
312,024✔
240
        _cleanup_free_ char *w = NULL;
312,024✔
241
        const char *e;
312,024✔
242
        uint64_t dev, ino;
312,024✔
243
        struct stat st;
312,024✔
244

245
        e = getenv("JOURNAL_STREAM");
312,024✔
246
        if (!e)
312,024✔
247
                return false;
248

249
        if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
38,828✔
250
                return false;
251
        if (!e)
38,828✔
252
                return false;
253

254
        if (safe_atou64(w, &dev) < 0)
38,828✔
255
                return false;
256
        if (safe_atou64(e, &ino) < 0)
38,828✔
257
                return false;
258

259
        if (fstat(STDERR_FILENO, &st) < 0)
38,828✔
260
                return false;
261

262
        return st.st_dev == dev && st.st_ino == ino;
44,846✔
263
}
264

265
int log_open(void) {
327,940✔
266
        int r;
327,940✔
267

268
        /* Do not call from library code. */
269

270
        /* This function is often called in preparation for logging. Let's make sure we don't clobber errno,
271
         * so that a call to a logging function immediately following a log_open() call can still easily
272
         * reference an error that happened immediately before the log_open() call. */
273
        PROTECT_ERRNO;
327,940✔
274

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

280
        if (log_target == LOG_TARGET_NULL) {
327,940✔
281
                log_close_journal();
1✔
282
                log_close_syslog();
1✔
283
                log_close_console();
1✔
284
                return 0;
285
        }
286

287
        if (getpid_cached() == 1 ||
629,503✔
288
            stderr_is_journal() ||
301,564✔
289
            IN_SET(log_target,
278,851✔
290
                   LOG_TARGET_KMSG,
291
                   LOG_TARGET_JOURNAL,
292
                   LOG_TARGET_JOURNAL_OR_KMSG,
293
                   LOG_TARGET_SYSLOG,
294
                   LOG_TARGET_SYSLOG_OR_KMSG)) {
295

296
                if (!prohibit_ipc) {
255,410✔
297
                        if (IN_SET(log_target,
248,515✔
298
                                   LOG_TARGET_AUTO,
299
                                   LOG_TARGET_JOURNAL_OR_KMSG,
300
                                   LOG_TARGET_JOURNAL)) {
301

302
                                r = log_open_journal();
229,474✔
303
                                if (r >= 0) {
229,474✔
304
                                        log_close_syslog();
229,393✔
305
                                        log_close_console();
229,393✔
306
                                        return r;
307
                                }
308
                        }
309

310
                        if (IN_SET(log_target,
19,122✔
311
                                   LOG_TARGET_SYSLOG_OR_KMSG,
312
                                   LOG_TARGET_SYSLOG)) {
313

314
                                r = log_open_syslog();
2✔
315
                                if (r >= 0) {
2✔
316
                                        log_close_journal();
2✔
317
                                        log_close_console();
2✔
318
                                        return r;
319
                                }
320
                        }
321
                }
322

323
                if (IN_SET(log_target, LOG_TARGET_AUTO,
26,015✔
324
                                       LOG_TARGET_JOURNAL_OR_KMSG,
325
                                       LOG_TARGET_SYSLOG_OR_KMSG,
326
                                       LOG_TARGET_KMSG)) {
327
                        r = log_open_kmsg();
23,547✔
328
                        if (r >= 0) {
23,547✔
329
                                log_close_journal();
14,677✔
330
                                log_close_syslog();
14,677✔
331
                                log_close_console();
14,677✔
332
                                return r;
333
                        }
334
                }
335
        }
336

337
        log_close_journal();
83,867✔
338
        log_close_syslog();
83,867✔
339

340
        return log_open_console();
83,867✔
341
}
342

343
void log_set_target(LogTarget target) {
111,443✔
344
        assert(target >= 0);
111,443✔
345
        assert(target < _LOG_TARGET_MAX);
111,443✔
346

347
        if (upgrade_syslog_to_journal) {
111,443✔
348
                if (target == LOG_TARGET_SYSLOG)
381✔
349
                        target = LOG_TARGET_JOURNAL;
350
                else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
381✔
351
                        target = LOG_TARGET_JOURNAL_OR_KMSG;
×
352
        }
353

354
        log_target = target;
111,443✔
355
}
111,443✔
356

357
void log_set_target_and_open(LogTarget target) {
343✔
358
        log_set_target(target);
343✔
359
        log_open();
343✔
360
}
343✔
361

362
void log_close(void) {
245,075✔
363
        /* Do not call from library code. */
364

365
        log_close_journal();
245,075✔
366
        log_close_syslog();
245,075✔
367
        log_close_kmsg();
245,075✔
368
        log_close_console();
245,075✔
369
}
245,075✔
370

371
void log_forget_fds(void) {
12,955✔
372
        /* Do not call from library code. */
373

374
        console_fd = kmsg_fd = syslog_fd = journal_fd = -EBADF;
12,955✔
375
        console_fd_is_tty = -1;
12,955✔
376
}
12,955✔
377

378
int log_set_max_level(int level) {
1,179,061✔
379
        assert(level == LOG_NULL || log_level_is_valid(level));
1,179,061✔
380

381
        int old = log_max_level;
1,179,061✔
382
        log_max_level = level;
1,179,061✔
383

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

392
        /* Ensure that our own LOG_NULL define maps sanely to the log mask */
393
        assert_cc(LOG_UPTO(LOG_NULL) == 0);
1,179,061✔
394

395
        return old;
1,179,061✔
396
}
397

398
void log_set_facility(int facility) {
207✔
399
        log_facility = facility;
207✔
400
}
207✔
401

402
static bool check_console_fd_is_tty(void) {
238,816✔
403
        if (console_fd < 0)
238,816✔
404
                return false;
405

406
        if (console_fd_is_tty < 0)
238,816✔
407
                console_fd_is_tty = isatty_safe(console_fd);
14,615✔
408

409
        return console_fd_is_tty;
238,816✔
410
}
411

412
static int write_to_console(
369,025✔
413
                int level,
414
                int error,
415
                const char *file,
416
                int line,
417
                const char *func,
418
                const char *buffer) {
419

420
        static int dumb = -1;
369,025✔
421

422
        char location[256],
369,025✔
423
             header_time[FORMAT_TIMESTAMP_MAX],
424
             prefix[1 + DECIMAL_STR_MAX(int) + 2],
425
             tid_string[3 + DECIMAL_STR_MAX(pid_t) + 1];
426
        struct iovec iovec[11];
369,025✔
427
        const char *on = NULL, *off = NULL;
369,025✔
428
        size_t n = 0;
369,025✔
429

430
        if (console_fd < 0)
369,025✔
431
                return 0;
369,025✔
432

433
        if (dumb < 0)
368,242✔
434
                dumb = getenv_terminal_is_dumb();
15,427✔
435

436
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_CONSOLE])
368,242✔
437
                return 0;
438

439
        if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
238,816✔
440
                xsprintf(prefix, "<%i>", level);
373✔
441
                iovec[n++] = IOVEC_MAKE_STRING(prefix);
373✔
442
        }
443

444
        if (show_time &&
238,816✔
445
            format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
×
446
                iovec[n++] = IOVEC_MAKE_STRING(header_time);
×
447
                iovec[n++] = IOVEC_MAKE_STRING(" ");
×
448
        }
449

450
        if (show_tid) {
238,816✔
451
                xsprintf(tid_string, "(" PID_FMT ") ", gettid());
×
452
                iovec[n++] = IOVEC_MAKE_STRING(tid_string);
×
453
        }
454

455
        if (log_get_show_color())
238,816✔
456
                get_log_colors(LOG_PRI(level), &on, &off, NULL);
230,410✔
457

458
        if (show_location) {
238,816✔
459
                const char *lon = "", *loff = "";
×
460
                if (log_get_show_color()) {
×
461
                        lon = ansi_highlight_yellow4();
×
462
                        loff = ansi_normal();
×
463
                }
464

465
                (void) snprintf(location, sizeof location, "%s%s:%i%s: ", lon, file, line, loff);
×
466
                iovec[n++] = IOVEC_MAKE_STRING(location);
×
467
        }
468

469
        if (on)
238,816✔
470
                iovec[n++] = IOVEC_MAKE_STRING(on);
209,752✔
471
        if (log_prefix) {
238,816✔
472
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
9,905✔
473
                iovec[n++] = IOVEC_MAKE_STRING(": ");
9,905✔
474
        }
475
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
238,816✔
476
        if (off)
238,816✔
477
                iovec[n++] = IOVEC_MAKE_STRING(off);
209,752✔
478

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

486
        if (writev(console_fd, iovec, n) < 0) {
238,816✔
487

488
                if (errno == EIO && getpid_cached() == 1) {
×
489

490
                        /* If somebody tried to kick us from our console tty (via vhangup() or suchlike), try
491
                         * to reconnect. */
492

493
                        log_close_console();
×
494
                        (void) log_open_console();
×
495
                        if (console_fd < 0)
×
496
                                return 0;
497

498
                        if (writev(console_fd, iovec, n) < 0)
×
499
                                return -errno;
×
500
                } else
501
                        return -errno;
×
502
        }
503

504
        return 1;
505
}
506

507
static int write_to_syslog(
180✔
508
                int level,
509
                int error,
510
                const char *file,
511
                int line,
512
                const char *func,
513
                const char *buffer) {
514

515
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
180✔
516
             header_time[64],
517
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
518
        struct tm tm;
180✔
519
        int r;
180✔
520

521
        if (syslog_fd < 0)
180✔
522
                return 0;
180✔
523

524
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_SYSLOG])
180✔
525
                return 0;
526

527
        xsprintf(header_priority, "<%i>", level);
180✔
528

529
        r = localtime_or_gmtime_usec(now(CLOCK_REALTIME), /* utc= */ false, &tm);
180✔
530
        if (r < 0)
180✔
531
                return r;
532

533
        if (strftime(header_time, sizeof(header_time), "%h %e %T ", &tm) <= 0)
180✔
534
                return -EINVAL;
535

536
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
180✔
537

538
        struct iovec iovec[] = {
1,440✔
539
                IOVEC_MAKE_STRING(header_priority),
180✔
540
                IOVEC_MAKE_STRING(header_time),
180✔
541
                IOVEC_MAKE_STRING(program_invocation_short_name),
180✔
542
                IOVEC_MAKE_STRING(header_pid),
180✔
543
                IOVEC_MAKE_STRING(strempty(log_prefix)),
292✔
544
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
292✔
545
                IOVEC_MAKE_STRING(buffer),
180✔
546
        };
547
        const struct msghdr msghdr = {
180✔
548
                .msg_iov = iovec,
549
                .msg_iovlen = ELEMENTSOF(iovec),
550
        };
551

552
        /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
553
        if (syslog_is_stream)
180✔
554
                iovec[ELEMENTSOF(iovec) - 1].iov_len++;
×
555

556
        for (;;) {
180✔
557
                ssize_t n;
180✔
558

559
                n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
180✔
560
                if (n < 0)
180✔
561
                        return -errno;
×
562

563
                if (!syslog_is_stream)
180✔
564
                        break;
565

566
                if (iovec_inc_many(iovec, ELEMENTSOF(iovec), n))
×
567
                        break;
568
        }
569

570
        return 1;
571
}
572

573
static int write_to_kmsg(
341,600✔
574
                int level,
575
                int error,
576
                const char *file,
577
                int line,
578
                const char *func,
579
                const char *buffer) {
580

581
        /* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a
582
         * safety catch for the case where start indiscriminately logging in a loop. It will not catch cases
583
         * where we log excessively, but not in a tight loop.
584
         *
585
         * Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple
586
         * loggers.
587
         */
588
        static thread_local RateLimit ratelimit = { 5 * USEC_PER_SEC, 200 };
341,600✔
589

590
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
341,600✔
591
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
592

593
        if (kmsg_fd < 0)
341,600✔
594
                return 0;
341,600✔
595

596
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_KMSG])
82,594✔
597
                return 0;
598

599
        if (ratelimit_kmsg && !ratelimit_below(&ratelimit)) {
82,594✔
600
                if (ratelimit_num_dropped(&ratelimit) > 1)
×
601
                        return 0;
602

603
                buffer = "Too many messages being logged to kmsg, ignoring";
604
        }
605

606
        xsprintf(header_priority, "<%i>", level);
82,594✔
607
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
82,594✔
608

609
        const struct iovec iovec[] = {
660,752✔
610
                IOVEC_MAKE_STRING(header_priority),
82,594✔
611
                IOVEC_MAKE_STRING(program_invocation_short_name),
82,594✔
612
                IOVEC_MAKE_STRING(header_pid),
82,594✔
613
                IOVEC_MAKE_STRING(strempty(log_prefix)),
127,650✔
614
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
127,650✔
615
                IOVEC_MAKE_STRING(buffer),
82,594✔
616
                IOVEC_MAKE_STRING("\n"),
82,594✔
617
        };
618

619
        if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
82,594✔
620
                return -errno;
×
621

622
        return 1;
623
}
624

625
static int log_do_header(
7,044,059✔
626
                char *header,
627
                size_t size,
628
                int level,
629
                int error,
630
                const char *file, int line, const char *func,
631
                const char *object_field, const char *object,
632
                const char *extra_field, const char *extra) {
633
        int r;
7,044,059✔
634

635
        error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error);
7,044,059✔
636

637
        r = snprintf(header, size,
7,044,059✔
638
                     "PRIORITY=%i\n"
639
                     "SYSLOG_FACILITY=%i\n"
640
                     "TID=" PID_FMT "\n"
641
                     "%s%.256s%s"        /* CODE_FILE */
642
                     "%s%.*i%s"          /* CODE_LINE */
643
                     "%s%.256s%s"        /* CODE_FUNC */
644
                     "%s%.*i%s"          /* ERRNO */
645
                     "%s%.256s%s"        /* object */
646
                     "%s%.256s%s"        /* extra */
647
                     "SYSLOG_IDENTIFIER=%.256s\n",
648
                     LOG_PRI(level),
649
                     LOG_FAC(level),
7,044,059✔
650
                     gettid(),
651
                     isempty(file) ? "" : "CODE_FILE=",
7,044,059✔
652
                     isempty(file) ? "" : file,
7,044,059✔
653
                     isempty(file) ? "" : "\n",
7,044,059✔
654
                     line ? "CODE_LINE=" : "",
655
                     line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
656
                     line ? "\n" : "",
657
                     isempty(func) ? "" : "CODE_FUNC=",
7,044,059✔
658
                     isempty(func) ? "" : func,
7,044,059✔
659
                     isempty(func) ? "" : "\n",
7,044,059✔
660
                     error ? "ERRNO=" : "",
661
                     error ? 1 : 0, error,
662
                     error ? "\n" : "",
663
                     isempty(object) ? "" : ASSERT_PTR(object_field),
7,044,059✔
664
                     isempty(object) ? "" : object,
7,044,059✔
665
                     isempty(object) ? "" : "\n",
7,044,059✔
666
                     isempty(extra) ? "" : ASSERT_PTR(extra_field),
7,044,059✔
667
                     isempty(extra) ? "" : extra,
7,044,059✔
668
                     isempty(extra) ? "" : "\n",
7,044,059✔
669
                     program_invocation_short_name);
670
        assert_raw((size_t) r < size);
7,044,059✔
671

672
        return 0;
7,044,059✔
673
}
674

675
static void log_do_context(struct iovec *iovec, size_t iovec_len, size_t *n) {
7,044,059✔
676
        assert(iovec);
7,044,059✔
677
        assert(n);
7,044,059✔
678

679
        LIST_FOREACH(ll, c, log_context_head()) {
9,943,658✔
680
                STRV_FOREACH(s, c->fields) {
26,064,053✔
681
                        if (*n + 2 >= iovec_len)
22,982,605✔
682
                                return;
×
683

684
                        iovec[(*n)++] = IOVEC_MAKE_STRING(*s);
22,982,605✔
685
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
22,982,605✔
686
                }
687

688
                for (size_t i = 0; i < c->n_input_iovec; i++) {
3,108,722✔
689
                        if (*n + 2 >= iovec_len)
27,351✔
690
                                return;
77✔
691

692
                        iovec[(*n)++] = c->input_iovec[i];
27,274✔
693
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
27,274✔
694
                }
695

696
                if (c->key && c->value) {
3,081,371✔
697
                        if (*n + 3 >= iovec_len)
406,836✔
698
                                return;
181,772✔
699

700
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->key);
225,064✔
701
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->value);
225,064✔
702
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
225,064✔
703
                }
704
        }
705
}
706

707
static int write_to_journal(
7,347,098✔
708
                int level,
709
                int error,
710
                const char *file,
711
                int line,
712
                const char *func,
713
                const char *object_field,
714
                const char *object,
715
                const char *extra_field,
716
                const char *extra,
717
                const char *buffer) {
718

719
        char header[LINE_MAX];
7,347,098✔
720
        size_t n = 0, iovec_len;
7,347,098✔
721
        struct iovec *iovec;
7,347,098✔
722

723
        if (journal_fd < 0)
7,347,098✔
724
                return 0;
7,347,098✔
725

726
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL])
6,955,149✔
727
                return 0;
728

729
        iovec_len = MIN(6 + log_context_num_fields() * 3, IOVEC_MAX);
6,955,149✔
730
        iovec = newa(struct iovec, iovec_len);
6,955,149✔
731

732
        log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
6,955,149✔
733

734
        iovec[n++] = IOVEC_MAKE_STRING(header);
6,955,149✔
735
        iovec[n++] = IOVEC_MAKE_STRING("MESSAGE=");
6,955,149✔
736
        if (log_prefix) {
6,955,149✔
737
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
2,114,771✔
738
                iovec[n++] = IOVEC_MAKE_STRING(": ");
2,114,771✔
739
        }
740
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
6,955,149✔
741
        iovec[n++] = IOVEC_MAKE_STRING("\n");
6,955,149✔
742

743
        log_do_context(iovec, iovec_len, &n);
6,955,149✔
744

745
        const struct msghdr msghdr = {
6,955,149✔
746
                .msg_iov = iovec,
747
                .msg_iovlen = n,
748
        };
749

750
        if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0)
6,955,149✔
751
                return -errno;
861✔
752

753
        return 1;
754
}
755

756
int log_dispatch_internal(
7,391,761✔
757
                int level,
758
                int error,
759
                const char *file,
760
                int line,
761
                const char *func,
762
                const char *object_field,
763
                const char *object,
764
                const char *extra_field,
765
                const char *extra,
766
                char *buffer) {
767

768
        assert_raw(buffer);
7,391,761✔
769

770
        if (log_target == LOG_TARGET_NULL)
7,391,761✔
771
                return -ERRNO_VALUE(error);
37✔
772

773
        /* Patch in LOG_DAEMON facility if necessary */
774
        if (LOG_FAC(level) == 0)
7,391,724✔
775
                level |= log_facility;
7,377,642✔
776

777
        if (open_when_needed)
7,391,724✔
778
                (void) log_open();
193,980✔
779

780
        do {
7,445,401✔
781
                char *e;
7,445,401✔
782
                int k = 0;
7,445,401✔
783

784
                buffer += strspn(buffer, NEWLINE);
7,445,401✔
785

786
                if (buffer[0] == 0)
7,445,401✔
787
                        break;
788

789
                if ((e = strpbrk(buffer, NEWLINE)))
7,406,087✔
790
                        *(e++) = 0;
53,677✔
791

792
                if (IN_SET(log_target, LOG_TARGET_AUTO,
7,406,087✔
793
                                       LOG_TARGET_JOURNAL_OR_KMSG,
794
                                       LOG_TARGET_JOURNAL)) {
795

796
                        k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
7,347,098✔
797
                        if (k < 0 && k != -EAGAIN)
7,347,098✔
798
                                log_close_journal();
11✔
799
                }
800

801
                if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
7,406,087✔
802
                                       LOG_TARGET_SYSLOG)) {
803

804
                        k = write_to_syslog(level, error, file, line, func, buffer);
180✔
805
                        if (k < 0 && k != -EAGAIN)
180✔
806
                                log_close_syslog();
×
807
                }
808

809
                if (k <= 0 &&
7,406,087✔
810
                    IN_SET(log_target, LOG_TARGET_AUTO,
451,619✔
811
                                       LOG_TARGET_SYSLOG_OR_KMSG,
812
                                       LOG_TARGET_JOURNAL_OR_KMSG,
813
                                       LOG_TARGET_KMSG)) {
814

815
                        if (k < 0)
341,600✔
816
                                log_open_kmsg();
397✔
817

818
                        k = write_to_kmsg(level, error, file, line, func, buffer);
341,600✔
819
                        if (k < 0) {
341,600✔
820
                                log_close_kmsg();
×
821
                                (void) log_open_console();
×
822
                        }
823
                }
824

825
                if (k <= 0)
451,619✔
826
                        (void) write_to_console(level, error, file, line, func, buffer);
369,025✔
827

828
                buffer = e;
7,406,087✔
829
        } while (buffer);
7,406,087✔
830

831
        if (open_when_needed)
7,391,724✔
832
                log_close();
193,980✔
833

834
        return -ERRNO_VALUE(error);
7,391,724✔
835
}
836

837
int log_dump_internal(
9✔
838
                int level,
839
                int error,
840
                const char *file,
841
                int line,
842
                const char *func,
843
                char *buffer) {
844

845
        PROTECT_ERRNO;
9✔
846

847
        /* This modifies the buffer... */
848

849
        if (_likely_(LOG_PRI(level) > log_max_level))
9✔
850
                return -ERRNO_VALUE(error);
×
851

852
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
9✔
853
}
854

855
int log_internalv(
5,376,029✔
856
                int level,
857
                int error,
858
                const char *file,
859
                int line,
860
                const char *func,
861
                const char *format,
862
                va_list ap) {
863

864
        if (_likely_(LOG_PRI(level) > log_max_level))
5,376,029✔
865
                return -ERRNO_VALUE(error);
5,376,029✔
866

867
        /* Make sure that %m maps to the specified error (or "Success"). */
868
        char buffer[LINE_MAX];
5,376,029✔
869
        LOCAL_ERRNO(ERRNO_VALUE(error));
5,376,029✔
870

871
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
5,376,029✔
872

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

876
int log_internal(
5,374,119✔
877
                int level,
878
                int error,
879
                const char *file,
880
                int line,
881
                const char *func,
882
                const char *format, ...) {
883

884
        va_list ap;
5,374,119✔
885
        int r;
5,374,119✔
886

887
        va_start(ap, format);
5,374,119✔
888
        r = log_internalv(level, error, file, line, func, format, ap);
5,374,119✔
889
        va_end(ap);
5,374,119✔
890

891
        return r;
5,374,119✔
892
}
893

894
int log_object_internalv(
2,049,843✔
895
                int level,
896
                int error,
897
                const char *file,
898
                int line,
899
                const char *func,
900
                const char *object_field,
901
                const char *object,
902
                const char *extra_field,
903
                const char *extra,
904
                const char *format,
905
                va_list ap) {
906

907
        char *buffer, *b;
2,049,843✔
908

909
        if (_likely_(LOG_PRI(level) > log_max_level))
2,049,843✔
910
                return -ERRNO_VALUE(error);
2,049,843✔
911

912
        /* Make sure that %m maps to the specified error (or "Success"). */
913
        LOCAL_ERRNO(ERRNO_VALUE(error));
×
914

915
        LOG_SET_PREFIX(object);
4,003,662✔
916

917
        b = buffer = newa(char, LINE_MAX);
2,001,831✔
918
        (void) vsnprintf(b, LINE_MAX, format, ap);
2,001,831✔
919

920
        return log_dispatch_internal(level, error, file, line, func,
2,001,831✔
921
                                     object_field, object, extra_field, extra, buffer);
922
}
923

924
int log_object_internal(
2,042,023✔
925
                int level,
926
                int error,
927
                const char *file,
928
                int line,
929
                const char *func,
930
                const char *object_field,
931
                const char *object,
932
                const char *extra_field,
933
                const char *extra,
934
                const char *format, ...) {
935

936
        va_list ap;
2,042,023✔
937
        int r;
2,042,023✔
938

939
        va_start(ap, format);
2,042,023✔
940
        r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
2,042,023✔
941
        va_end(ap);
2,042,023✔
942

943
        return r;
2,042,023✔
944
}
945

946
int log_oom_internal(int level, const char *file, int line, const char *func) {
×
947
        return log_internal(level, ENOMEM, file, line, func, "Out of memory.");
×
948
}
949

950
int log_format_iovec(
85,327✔
951
                struct iovec *iovec,
952
                size_t iovec_len,
953
                size_t *n,
954
                bool newline_separator,
955
                int error,
956
                const char *format,
957
                va_list ap) {
958

959
        assert(iovec);
85,327✔
960
        assert(n);
85,327✔
961

962
        while (format && *n + 1 < iovec_len) {
512,419✔
963
                va_list aq;
427,092✔
964
                char *m;
427,092✔
965
                int r;
427,092✔
966

967
                /* We need to copy the va_list structure,
968
                 * since vasprintf() leaves it afterwards at
969
                 * an undefined location */
970

971
                errno = ERRNO_VALUE(error);
427,092✔
972

973
                va_copy(aq, ap);
427,092✔
974
                r = vasprintf(&m, format, aq);
427,092✔
975
                va_end(aq);
427,092✔
976
                if (r < 0)
427,092✔
977
                        return -EINVAL;
×
978

979
                /* Now, jump enough ahead, so that we point to
980
                 * the next format string */
981
                VA_FORMAT_ADVANCE(format, ap);
1,363,809✔
982

983
                iovec[(*n)++] = IOVEC_MAKE_STRING(m);
427,092✔
984
                if (newline_separator)
427,092✔
985
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
424,820✔
986

987
                format = va_arg(ap, char *);
427,092✔
988
        }
989
        return 0;
990
}
991

992
int log_struct_internal(
98,566✔
993
                int level,
994
                int error,
995
                const char *file,
996
                int line,
997
                const char *func,
998
                const char *format, ...) {
999

1000
        char buf[LINE_MAX];
98,566✔
1001
        bool found = false;
98,566✔
1002
        PROTECT_ERRNO;
98,566✔
1003
        va_list ap;
98,566✔
1004

1005
        if (_likely_(LOG_PRI(level) > log_max_level) ||
98,566✔
1006
            log_target == LOG_TARGET_NULL)
98,566✔
1007
                return -ERRNO_VALUE(error);
50✔
1008

1009
        if (LOG_FAC(level) == 0)
98,516✔
1010
                level |= log_facility;
98,516✔
1011

1012
        if (IN_SET(log_target,
98,516✔
1013
                   LOG_TARGET_AUTO,
1014
                   LOG_TARGET_JOURNAL_OR_KMSG,
1015
                   LOG_TARGET_JOURNAL)) {
1016

1017
                if (open_when_needed)
96,507✔
1018
                        log_open_journal();
11,194✔
1019

1020
                if (journal_fd >= 0) {
96,507✔
1021
                        char header[LINE_MAX];
85,022✔
1022
                        struct iovec *iovec;
85,022✔
1023
                        size_t n = 0, m, iovec_len;
85,022✔
1024
                        int r;
85,022✔
1025
                        bool fallback = false;
85,022✔
1026

1027
                        iovec_len = MIN(17 + log_context_num_fields() * 3, IOVEC_MAX);
85,022✔
1028
                        iovec = newa(struct iovec, iovec_len);
85,022✔
1029

1030
                        /* If the journal is available do structured logging.
1031
                         * Do not report the errno if it is synthetic. */
1032
                        log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
85,022✔
1033
                        iovec[n++] = IOVEC_MAKE_STRING(header);
85,022✔
1034

1035
                        va_start(ap, format);
85,022✔
1036
                        DISABLE_WARNING_FORMAT_NONLITERAL;
85,022✔
1037
                        r = log_format_iovec(iovec, iovec_len, &n, true, error, format, ap);
85,022✔
1038
                        REENABLE_WARNING;
85,022✔
1039
                        m = n;
85,022✔
1040
                        if (r < 0)
85,022✔
1041
                                fallback = true;
1042
                        else {
1043
                                log_do_context(iovec, iovec_len, &n);
85,022✔
1044

1045
                                const struct msghdr msghdr = {
85,022✔
1046
                                        .msg_iov = iovec,
1047
                                        .msg_iovlen = n,
1048
                                };
1049

1050
                                (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
85,022✔
1051
                        }
1052

1053
                        va_end(ap);
85,022✔
1054
                        for (size_t i = 1; i < m; i += 2)
509,837✔
1055
                                free(iovec[i].iov_base);
424,815✔
1056

1057
                        if (!fallback) {
85,022✔
1058
                                if (open_when_needed)
85,022✔
1059
                                        log_close();
11,181✔
1060

1061
                                return -ERRNO_VALUE(error);
85,022✔
1062
                        }
1063
                }
1064
        }
1065

1066
        /* Fallback if journal logging is not available or didn't work. */
1067

1068
        va_start(ap, format);
13,494✔
1069
        while (format) {
40,182✔
1070
                va_list aq;
40,182✔
1071

1072
                errno = ERRNO_VALUE(error);
40,182✔
1073

1074
                va_copy(aq, ap);
40,182✔
1075
                DISABLE_WARNING_FORMAT_NONLITERAL;
40,182✔
1076
                (void) vsnprintf(buf, sizeof buf, format, aq);
40,182✔
1077
                REENABLE_WARNING;
40,182✔
1078
                va_end(aq);
40,182✔
1079

1080
                if (startswith(buf, "MESSAGE=")) {
40,182✔
1081
                        found = true;
13,494✔
1082
                        break;
13,494✔
1083
                }
1084

1085
                VA_FORMAT_ADVANCE(format, ap);
71,827✔
1086

1087
                format = va_arg(ap, char *);
26,688✔
1088
        }
1089
        va_end(ap);
13,494✔
1090

1091
        if (!found) {
13,494✔
1092
                if (open_when_needed)
×
1093
                        log_close();
×
1094

1095
                return -ERRNO_VALUE(error);
×
1096
        }
1097

1098
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
13,494✔
1099
}
1100

1101
int log_struct_iovec_internal(
3,916✔
1102
                int level,
1103
                int error,
1104
                const char *file,
1105
                int line,
1106
                const char *func,
1107
                const struct iovec input_iovec[],
1108
                size_t n_input_iovec) {
1109

1110
        PROTECT_ERRNO;
3,916✔
1111

1112
        if (_likely_(LOG_PRI(level) > log_max_level) ||
3,916✔
1113
            log_target == LOG_TARGET_NULL)
3,916✔
1114
                return -ERRNO_VALUE(error);
×
1115

1116
        if (LOG_FAC(level) == 0)
3,916✔
1117
                level |= log_facility;
3,916✔
1118

1119
        if (IN_SET(log_target, LOG_TARGET_AUTO,
3,916✔
1120
                               LOG_TARGET_JOURNAL_OR_KMSG,
1121
                               LOG_TARGET_JOURNAL) &&
3,916✔
1122
            journal_fd >= 0) {
3,916✔
1123

1124
                char header[LINE_MAX];
3,888✔
1125
                struct iovec *iovec;
3,888✔
1126
                size_t n = 0, iovec_len;
3,888✔
1127

1128
                iovec_len = MIN(1 + n_input_iovec * 2 + log_context_num_fields() * 3, IOVEC_MAX);
3,888✔
1129
                iovec = newa(struct iovec, iovec_len);
3,888✔
1130

1131
                log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
3,888✔
1132

1133
                iovec[n++] = IOVEC_MAKE_STRING(header);
3,888✔
1134
                for (size_t i = 0; i < n_input_iovec; i++) {
31,046✔
1135
                        iovec[n++] = input_iovec[i];
27,158✔
1136
                        iovec[n++] = IOVEC_MAKE_STRING("\n");
27,158✔
1137
                }
1138

1139
                log_do_context(iovec, iovec_len, &n);
3,888✔
1140

1141
                const struct msghdr msghdr = {
3,888✔
1142
                        .msg_iov = iovec,
1143
                        .msg_iovlen = n,
1144
                };
1145

1146
                if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0)
3,888✔
1147
                        return -ERRNO_VALUE(error);
3,885✔
1148
        }
1149

1150
        for (size_t i = 0; i < n_input_iovec; i++)
124✔
1151
                if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) {
124✔
1152
                        char *m;
31✔
1153

1154
                        m = strndupa_safe((char*) input_iovec[i].iov_base + STRLEN("MESSAGE="),
31✔
1155
                                          input_iovec[i].iov_len - STRLEN("MESSAGE="));
1156

1157
                        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
31✔
1158
                }
1159

1160
        /* Couldn't find MESSAGE=. */
1161
        return -ERRNO_VALUE(error);
×
1162
}
1163

1164
int log_set_target_from_string(const char *e) {
13,611✔
1165
        LogTarget t;
13,611✔
1166

1167
        t = log_target_from_string(e);
13,611✔
1168
        if (t < 0)
13,611✔
1169
                return t;
1170

1171
        log_set_target(t);
13,611✔
1172
        return 0;
13,611✔
1173
}
1174

1175
int log_set_max_level_from_string(const char *e) {
33,398✔
1176
        int r;
89,240✔
1177

1178
        for (;;) {
89,240✔
1179
                _cleanup_free_ char *word = NULL, *prefix = NULL;
89,240✔
1180
                LogTarget target;
89,240✔
1181
                const char *colon;
89,240✔
1182

1183
                r = extract_first_word(&e, &word, ",", 0);
89,240✔
1184
                if (r < 0)
89,240✔
1185
                        return r;
1186
                if (r == 0)
89,240✔
1187
                        break;
1188

1189
                colon = strchr(word, ':');
55,842✔
1190
                if (!colon) {
55,842✔
1191
                        r = log_level_from_string(word);
33,398✔
1192
                        if (r < 0)
33,398✔
1193
                                return r;
1194

1195
                        log_set_max_level(r);
33,398✔
1196
                        continue;
33,398✔
1197
                }
1198

1199
                prefix = strndup(word, colon - word);
22,444✔
1200
                if (!prefix)
22,444✔
1201
                        return -ENOMEM;
1202

1203
                target = log_target_from_string(prefix);
22,444✔
1204
                if (target < 0)
22,444✔
1205
                        return target;
1206

1207
                if (target >= _LOG_TARGET_SINGLE_MAX)
22,444✔
1208
                        return -EINVAL;
1209

1210
                r = log_level_from_string(colon + 1);
22,444✔
1211
                if (r < 0)
22,444✔
1212
                        return r;
1213

1214
                log_target_max_level[target] = r;
22,444✔
1215
        }
1216

1217
        return 0;
33,398✔
1218
}
1219

1220
int log_max_levels_to_string(int level, char **ret) {
5,010✔
1221
        _cleanup_free_ char *s = NULL;
5,010✔
1222
        int r;
5,010✔
1223

1224
        assert(ret);
5,010✔
1225

1226
        r = log_level_to_string_alloc(level, &s);
5,010✔
1227
        if (r < 0)
5,010✔
1228
                return r;
1229

1230
        for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
25,050✔
1231
                _cleanup_free_ char *l = NULL;
5,010✔
1232

1233
                if (log_target_max_level[target] == INT_MAX)
20,040✔
1234
                        continue;
15,030✔
1235

1236
                r = log_level_to_string_alloc(log_target_max_level[target], &l);
5,010✔
1237
                if (r < 0)
5,010✔
1238
                        return r;
1239

1240
                r = strextendf_with_separator(&s, ",", "%s:%s", log_target_to_string(target), l);
5,010✔
1241
                if (r < 0)
5,010✔
1242
                        return r;
1243
        }
1244

1245
        *ret = TAKE_PTR(s);
5,010✔
1246
        return 0;
5,010✔
1247
}
1248

1249
static int log_set_ratelimit_kmsg_from_string(const char *e) {
9,584✔
1250
        int r;
9,584✔
1251

1252
        r = parse_boolean(e);
9,584✔
1253
        if (r < 0)
9,584✔
1254
                return r;
1255

1256
        ratelimit_kmsg = r;
9,584✔
1257
        return 0;
9,584✔
1258
}
1259

1260
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
339,271✔
1261

1262
        /*
1263
         * The systemd.log_xyz= settings are parsed by all tools, and
1264
         * so is "debug".
1265
         *
1266
         * However, "quiet" is only parsed by PID 1, and only turns of
1267
         * status output to /dev/console, but does not alter the log
1268
         * level.
1269
         */
1270

1271
        if (streq(key, "debug") && !value)
339,271✔
1272
                log_set_max_level(LOG_DEBUG);
×
1273

1274
        else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
339,271✔
1275

1276
                if (proc_cmdline_value_missing(key, value))
×
1277
                        return 0;
1278

1279
                if (log_set_target_from_string(value) < 0)
×
1280
                        log_warning("Failed to parse log target '%s', ignoring.", value);
×
1281

1282
        } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
339,271✔
1283

1284
                if (proc_cmdline_value_missing(key, value))
9,584✔
1285
                        return 0;
1286

1287
                if (log_set_max_level_from_string(value) < 0)
9,584✔
1288
                        log_warning("Failed to parse log level setting '%s', ignoring.", value);
×
1289

1290
        } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
329,687✔
1291

1292
                if (log_show_color_from_string(value ?: "1") < 0)
×
1293
                        log_warning("Failed to parse log color setting '%s', ignoring.", value);
×
1294

1295
        } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
329,687✔
1296

1297
                if (log_show_location_from_string(value ?: "1") < 0)
×
1298
                        log_warning("Failed to parse log location setting '%s', ignoring.", value);
×
1299

1300
        } else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
329,687✔
1301

1302
                if (log_show_tid_from_string(value ?: "1") < 0)
×
1303
                        log_warning("Failed to parse log tid setting '%s', ignoring.", value);
×
1304

1305
        } else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
329,687✔
1306

1307
                if (log_show_time_from_string(value ?: "1") < 0)
×
1308
                        log_warning("Failed to parse log time setting '%s', ignoring.", value);
×
1309

1310
        } else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
329,687✔
1311

1312
                if (log_set_ratelimit_kmsg_from_string(value ?: "1") < 0)
9,584✔
1313
                        log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", value);
×
1314
        }
1315

1316
        return 0;
1317
}
1318

1319
static bool should_parse_proc_cmdline(void) {
84,993✔
1320
        /* PID1 always reads the kernel command line. */
1321
        if (getpid_cached() == 1)
84,993✔
1322
                return true;
1323

1324
        /* Otherwise, parse the command line if invoked directly by systemd. */
1325
        return invoked_by_systemd();
84,740✔
1326
}
1327

1328
void log_parse_environment_variables(void) {
87,823✔
1329
        const char *e;
87,823✔
1330
        int r;
87,823✔
1331

1332
        e = getenv("SYSTEMD_LOG_TARGET");
87,823✔
1333
        if (e && log_set_target_from_string(e) < 0)
87,823✔
1334
                log_warning("Failed to parse log target '%s', ignoring.", e);
×
1335

1336
        e = getenv("SYSTEMD_LOG_LEVEL");
87,823✔
1337
        if (e) {
87,823✔
1338
                r = log_set_max_level_from_string(e);
9,943✔
1339
                if (r < 0)
9,943✔
1340
                        log_warning_errno(r, "Failed to parse log level '%s', ignoring: %m", e);
×
1341
        } else {
1342
                /* If no explicit log level is specified then let's see if this is a debug invocation, and if
1343
                 * so raise the log level to debug too. Note that this is not symmetric: just because
1344
                 * DEBUG_INVOCATION is explicitly set to 0 we won't lower the log level below debug. This
1345
                 * follows the logic that debug logging is an opt-in thing anyway, and if there's any reason
1346
                 * to enable it we should not disable it here automatically. */
1347
                r = getenv_bool("DEBUG_INVOCATION");
77,880✔
1348
                if (r < 0 && r != -ENXIO)
77,880✔
1349
                        log_warning_errno(r, "Failed to parse $DEBUG_INVOCATION value, ignoring: %m");
×
1350
                else if (r > 0)
77,880✔
1351
                        log_set_max_level(LOG_DEBUG);
×
1352
        }
1353

1354
        e = getenv("SYSTEMD_LOG_COLOR");
87,823✔
1355
        if (e && log_show_color_from_string(e) < 0)
87,823✔
1356
                log_warning("Failed to parse log color '%s', ignoring.", e);
×
1357

1358
        e = getenv("SYSTEMD_LOG_LOCATION");
87,823✔
1359
        if (e && log_show_location_from_string(e) < 0)
87,823✔
1360
                log_warning("Failed to parse log location '%s', ignoring.", e);
×
1361

1362
        e = getenv("SYSTEMD_LOG_TIME");
87,823✔
1363
        if (e && log_show_time_from_string(e) < 0)
87,823✔
1364
                log_warning("Failed to parse log time '%s', ignoring.", e);
×
1365

1366
        e = getenv("SYSTEMD_LOG_TID");
87,823✔
1367
        if (e && log_show_tid_from_string(e) < 0)
87,823✔
1368
                log_warning("Failed to parse log tid '%s', ignoring.", e);
×
1369

1370
        e = getenv("SYSTEMD_LOG_RATELIMIT_KMSG");
87,823✔
1371
        if (e && log_set_ratelimit_kmsg_from_string(e) < 0)
87,823✔
1372
                log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", e);
×
1373
}
87,823✔
1374

1375
void log_parse_environment(void) {
84,993✔
1376
        /* Do not call from library code. */
1377

1378
        if (should_parse_proc_cmdline())
84,993✔
1379
                (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
9,660✔
1380

1381
        log_parse_environment_variables();
84,993✔
1382
}
84,993✔
1383

1384
LogTarget log_get_target(void) {
27,443✔
1385
        return log_target;
27,443✔
1386
}
1387

1388
void log_settle_target(void) {
22,971✔
1389

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

1396
        LogTarget t = log_get_target();
22,971✔
1397

1398
        if (t != LOG_TARGET_AUTO)
22,971✔
1399
                return;
1400

1401
        t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
30,833✔
1402
                                                        : LOG_TARGET_CONSOLE;
20,460✔
1403
        log_set_target(t);
10,373✔
1404
}
1405

1406
int log_get_max_level(void) {
21,574,881✔
1407
        return log_max_level;
21,574,881✔
1408
}
1409

1410
int log_get_target_max_level(LogTarget target) {
16✔
1411
        assert(target >= 0);
16✔
1412
        assert(target < _LOG_TARGET_SINGLE_MAX);
16✔
1413
        return log_target_max_level[target];
16✔
1414
}
1415

1416
void log_show_color(bool b) {
72,862✔
1417
        show_color = b;
72,862✔
1418
}
72,862✔
1419

1420
bool log_get_show_color(void) {
290,363✔
1421
        return show_color > 0; /* Defaults to false. */
290,363✔
1422
}
1423

1424
void log_show_location(bool b) {
×
1425
        show_location = b;
×
1426
}
×
1427

1428
bool log_get_show_location(void) {
18✔
1429
        return show_location;
18✔
1430
}
1431

1432
void log_show_time(bool b) {
1✔
1433
        show_time = b;
1✔
1434
}
1✔
1435

1436
bool log_get_show_time(void) {
18✔
1437
        return show_time;
18✔
1438
}
1439

1440
void log_show_tid(bool b) {
1✔
1441
        show_tid = b;
1✔
1442
}
1✔
1443

1444
bool log_get_show_tid(void) {
×
1445
        return show_tid;
×
1446
}
1447

1448
int log_show_color_from_string(const char *e) {
×
1449
        int r;
×
1450

1451
        r = parse_boolean(e);
×
1452
        if (r < 0)
×
1453
                return r;
1454

1455
        log_show_color(r);
×
1456
        return 0;
×
1457
}
1458

1459
int log_show_location_from_string(const char *e) {
×
1460
        int r;
×
1461

1462
        r = parse_boolean(e);
×
1463
        if (r < 0)
×
1464
                return r;
1465

1466
        log_show_location(r);
×
1467
        return 0;
×
1468
}
1469

1470
int log_show_time_from_string(const char *e) {
×
1471
        int r;
×
1472

1473
        r = parse_boolean(e);
×
1474
        if (r < 0)
×
1475
                return r;
1476

1477
        log_show_time(r);
×
1478
        return 0;
×
1479
}
1480

1481
int log_show_tid_from_string(const char *e) {
×
1482
        int r;
×
1483

1484
        r = parse_boolean(e);
×
1485
        if (r < 0)
×
1486
                return r;
1487

1488
        log_show_tid(r);
×
1489
        return 0;
×
1490
}
1491

1492
bool log_on_console(void) {
112,995✔
1493
        if (IN_SET(log_target, LOG_TARGET_CONSOLE,
112,995✔
1494
                               LOG_TARGET_CONSOLE_PREFIXED))
1495
                return true;
1496

1497
        return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
151,332✔
1498
}
1499

1500
static const char *const log_target_table[_LOG_TARGET_MAX] = {
1501
        [LOG_TARGET_CONSOLE]          = "console",
1502
        [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1503
        [LOG_TARGET_KMSG]             = "kmsg",
1504
        [LOG_TARGET_JOURNAL]          = "journal",
1505
        [LOG_TARGET_JOURNAL_OR_KMSG]  = "journal-or-kmsg",
1506
        [LOG_TARGET_SYSLOG]           = "syslog",
1507
        [LOG_TARGET_SYSLOG_OR_KMSG]   = "syslog-or-kmsg",
1508
        [LOG_TARGET_AUTO]             = "auto",
1509
        [LOG_TARGET_NULL]             = "null",
1510
};
1511

1512
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
46,195✔
1513

1514
void log_received_signal(int level, const struct signalfd_siginfo *si) {
8,633✔
1515
        assert(si);
8,633✔
1516

1517
        if (si_code_from_process(si->ssi_code) && pid_is_valid(si->ssi_pid)) {
8,893✔
1518
                _cleanup_free_ char *p = NULL;
260✔
1519

1520
                (void) pid_get_comm(si->ssi_pid, &p);
260✔
1521

1522
                log_full(level,
260✔
1523
                         "Received SIG%s from PID %"PRIu32" (%s).",
1524
                         signal_to_string(si->ssi_signo),
1525
                         si->ssi_pid, strna(p));
1526
        } else
1527
                log_full(level,
8,373✔
1528
                         "Received SIG%s.",
1529
                         signal_to_string(si->ssi_signo));
1530
}
8,633✔
1531

1532
void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata) {
1,052✔
1533
        assert(!log_syntax_callback || !cb);
1,052✔
1534
        assert(!log_syntax_callback_userdata || !userdata);
1,052✔
1535

1536
        log_syntax_callback = cb;
1,052✔
1537
        log_syntax_callback_userdata = userdata;
1,052✔
1538
}
1,052✔
1539

1540
int log_syntax_internal(
32,645✔
1541
                const char *unit,
1542
                int level,
1543
                const char *config_file,
1544
                unsigned config_line,
1545
                int error,
1546
                const char *file,
1547
                int line,
1548
                const char *func,
1549
                const char *format, ...) {
1550

1551
        PROTECT_ERRNO;
32,645✔
1552

1553
        if (log_syntax_callback)
32,645✔
1554
                log_syntax_callback(unit, level, log_syntax_callback_userdata);
1,518✔
1555

1556
        if (_likely_(LOG_PRI(level) > log_max_level) ||
32,645✔
1557
            log_target == LOG_TARGET_NULL)
32,645✔
1558
                return -ERRNO_VALUE(error);
30✔
1559

1560
        char buffer[LINE_MAX];
32,615✔
1561
        va_list ap;
32,615✔
1562
        const char *unit_fmt = NULL;
32,615✔
1563

1564
        errno = ERRNO_VALUE(error);
32,615✔
1565

1566
        va_start(ap, format);
32,615✔
1567
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
32,615✔
1568
        va_end(ap);
32,615✔
1569

1570
        if (unit)
32,615✔
1571
                unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
24,506✔
1572

1573
        if (config_file) {
32,615✔
1574
                if (config_line > 0)
32,605✔
1575
                        return log_struct_internal(
25,789✔
1576
                                        level,
1577
                                        error,
1578
                                        file, line, func,
1579
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
25,789✔
1580
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
25,789✔
1581
                                        LOG_ITEM("CONFIG_LINE=%u", config_line),
25,789✔
1582
                                        LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
25,789✔
1583
                                        unit_fmt, unit,
1584
                                        NULL);
1585
                else
1586
                        return log_struct_internal(
6,816✔
1587
                                        level,
1588
                                        error,
1589
                                        file, line, func,
1590
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
6,816✔
1591
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
6,816✔
1592
                                        LOG_MESSAGE("%s: %s", config_file, buffer),
6,816✔
1593
                                        unit_fmt, unit,
1594
                                        NULL);
1595
        } else if (unit)
10✔
1596
                return log_struct_internal(
×
1597
                                level,
1598
                                error,
1599
                                file, line, func,
1600
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
×
1601
                                LOG_MESSAGE("%s: %s", unit, buffer),
×
1602
                                unit_fmt, unit,
1603
                                NULL);
1604
        else
1605
                return log_struct_internal(
10✔
1606
                                level,
1607
                                error,
1608
                                file, line, func,
1609
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
10✔
1610
                                LOG_MESSAGE("%s", buffer),
10✔
1611
                                NULL);
1612
}
1613

1614
int log_syntax_invalid_utf8_internal(
9✔
1615
                const char *unit,
1616
                int level,
1617
                const char *config_file,
1618
                unsigned config_line,
1619
                const char *file,
1620
                int line,
1621
                const char *func,
1622
                const char *rvalue) {
1623

1624
        PROTECT_ERRNO;
×
1625
        _cleanup_free_ char *p = NULL;
9✔
1626

1627
        if (rvalue)
9✔
1628
                p = utf8_escape_invalid(rvalue);
9✔
1629

1630
        return log_syntax_internal(unit, level, config_file, config_line,
9✔
1631
                                   SYNTHETIC_ERRNO(EINVAL), file, line, func,
1632
                                   "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1633
}
1634

1635
int log_syntax_parse_error_internal(
135✔
1636
                const char *unit,
1637
                const char *config_file,
1638
                unsigned config_line,
1639
                int error,
1640
                bool critical,
1641
                const char *file,
1642
                int line,
1643
                const char *func,
1644
                const char *lvalue,
1645
                const char *rvalue) {
1646

1647
        PROTECT_ERRNO;
×
1648
        _cleanup_free_ char *escaped = NULL;
135✔
1649

1650
        /* OOM is always handled as critical. */
1651
        if (ERRNO_VALUE(error) == ENOMEM)
135✔
1652
                return log_oom_internal(LOG_ERR, file, line, func);
×
1653

1654
        if (rvalue && !utf8_is_valid(rvalue)) {
270✔
1655
                escaped = utf8_escape_invalid(rvalue);
×
1656
                if (!escaped)
×
1657
                        rvalue = "(oom)";
1658
                else
1659
                        rvalue = " (escaped)";
×
1660
        }
1661

1662
        log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
675✔
1663
                            file, line, func,
1664
                            "Failed to parse %s=%s%s%s%s%s",
1665
                            strna(lvalue), strempty(escaped), strempty(rvalue),
1666
                            critical ? "" : ", ignoring",
1667
                            error == 0 ? "." : ": ",
1668
                            error == 0 ? "" : STRERROR(error));
135✔
1669

1670
        return critical ? -ERRNO_VALUE(error) : 0;
135✔
1671
}
1672

1673
void log_set_upgrade_syslog_to_journal(bool b) {
294✔
1674
        upgrade_syslog_to_journal = b;
294✔
1675

1676
        /* Make the change effective immediately */
1677
        if (b) {
294✔
1678
                if (log_target == LOG_TARGET_SYSLOG)
294✔
1679
                        log_target = LOG_TARGET_JOURNAL;
×
1680
                else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
294✔
1681
                        log_target = LOG_TARGET_JOURNAL_OR_KMSG;
×
1682
        }
1683
}
294✔
1684

1685
void log_set_always_reopen_console(bool b) {
13,154✔
1686
        always_reopen_console = b;
13,154✔
1687
}
13,154✔
1688

1689
void log_set_open_when_needed(bool b) {
35,623✔
1690
        open_when_needed = b;
35,623✔
1691
}
35,623✔
1692

1693
void log_set_prohibit_ipc(bool b) {
51,811✔
1694
        prohibit_ipc = b;
51,811✔
1695
}
51,811✔
1696

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

1701
        return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
×
1702
}
1703

1704
int log_dup_console(void) {
118✔
1705
        int copy;
118✔
1706

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

1710
        if (console_fd < 0 || console_fd >= 3)
118✔
1711
                return 0;
1712

1713
        copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);
1✔
1714
        if (copy < 0)
1✔
1715
                return -errno;
×
1716

1717
        console_fd = copy;
1✔
1718
        return 0;
1✔
1719
}
1720

1721
void log_setup(void) {
84,096✔
1722
        log_set_target(LOG_TARGET_AUTO);
84,096✔
1723
        log_parse_environment();
84,096✔
1724
        (void) log_open();
84,096✔
1725
        if (log_on_console() && show_color < 0)
84,096✔
1726
                log_show_color(true);
72,737✔
1727
}
84,096✔
1728

1729
const char* _log_set_prefix(const char *prefix, bool force) {
4,031,696✔
1730
        const char *old = log_prefix;
4,031,696✔
1731

1732
        if (prefix || force)
4,031,696✔
1733
                log_prefix = prefix;
4,029,719✔
1734

1735
        return old;
4,031,696✔
1736
}
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