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

systemd / systemd / 14554080340

19 Apr 2025 11:46AM UTC coverage: 72.101% (-0.03%) from 72.13%
14554080340

push

github

web-flow
Add two new paragraphs to coding style about header files (#37188)

296880 of 411754 relevant lines covered (72.1%)

687547.52 hits per line

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

84.98
/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 "list.h"
28
#include "log.h"
29
#include "log-context.h"
30
#include "macro.h"
31
#include "missing_syscall.h"
32
#include "parse-util.h"
33
#include "proc-cmdline.h"
34
#include "process-util.h"
35
#include "ratelimit.h"
36
#include "signal-util.h"
37
#include "socket-util.h"
38
#include "stdio-util.h"
39
#include "string-table.h"
40
#include "string-util.h"
41
#include "strv.h"
42
#include "syslog-util.h"
43
#include "terminal-util.h"
44
#include "time-util.h"
45
#include "utf8.h"
46

47
#define SNDBUF_SIZE (8*1024*1024)
48
#define IOVEC_MAX 256U
49

50
static log_syntax_callback_t log_syntax_callback = NULL;
51
static void *log_syntax_callback_userdata = NULL;
52

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

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

70
static bool syslog_is_stream = false;
71

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

77
static bool upgrade_syslog_to_journal = false;
78
static bool always_reopen_console = false;
79
static bool open_when_needed = false;
80
static bool prohibit_ipc = false;
81

82
static thread_local const char *log_prefix = NULL;
83

84
#if LOG_MESSAGE_VERIFICATION || defined(__COVERITY__)
85
bool _log_message_dummy = false; /* Always false */
86
#endif
87

88
/* An assert to use in logging functions that does not call recursively
89
 * into our logging functions (since that might lead to a loop). */
90
#define assert_raw(expr)                                                \
91
        do {                                                            \
92
                if (_unlikely_(!(expr))) {                              \
93
                        fputs(#expr "\n", stderr);                      \
94
                        abort();                                        \
95
                }                                                       \
96
        } while (false)
97

98
static void log_close_console(void) {
343,342✔
99
        /* See comment in log_close_journal() */
100
        (void) safe_close_above_stdio(TAKE_FD(console_fd));
343,342✔
101
        console_fd_is_tty = -1;
343,342✔
102
}
343,342✔
103

104
static int log_open_console(void) {
93,939✔
105

106
        if (!always_reopen_console) {
93,939✔
107
                console_fd = STDERR_FILENO;
68,988✔
108
                console_fd_is_tty = -1;
68,988✔
109
                return 0;
68,988✔
110
        }
111

112
        if (console_fd < 3) {
24,951✔
113
                int fd;
22,762✔
114

115
                fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
22,762✔
116
                if (fd < 0)
22,762✔
117
                        return fd;
118

119
                console_fd = fd_move_above_stdio(fd);
14,724✔
120
                console_fd_is_tty = true;
14,724✔
121
        }
122

123
        return 0;
124
}
125

126
static void log_close_kmsg(void) {
181,553✔
127
        /* See comment in log_close_journal() */
128
        (void) safe_close(TAKE_FD(kmsg_fd));
181,553✔
129
}
181,553✔
130

131
static int log_open_kmsg(void) {
22,493✔
132

133
        if (kmsg_fd >= 0)
22,493✔
134
                return 0;
135

136
        kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
20,753✔
137
        if (kmsg_fd < 0)
20,753✔
138
                return -errno;
12,738✔
139

140
        kmsg_fd = fd_move_above_stdio(kmsg_fd);
8,015✔
141
        return 0;
8,015✔
142
}
143

144
static void log_close_syslog(void) {
437,277✔
145
        /* See comment in log_close_journal() */
146
        (void) safe_close(TAKE_FD(syslog_fd));
437,277✔
147
}
437,277✔
148

149
static int create_log_socket(int type) {
152,299✔
150
        struct timeval tv;
152,299✔
151
        int fd;
152,299✔
152

153
        fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
152,299✔
154
        if (fd < 0)
152,299✔
155
                return -errno;
×
156

157
        fd = fd_move_above_stdio(fd);
152,299✔
158
        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
152,299✔
159

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

168
        return fd;
152,299✔
169
}
170

171
static int log_open_syslog(void) {
2✔
172
        int r;
2✔
173

174
        if (syslog_fd >= 0)
2✔
175
                return 0;
176

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

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

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

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

199
                syslog_is_stream = true;
×
200
        } else
201
                syslog_is_stream = false;
2✔
202

203
        return 0;
204

205
fail:
×
206
        log_close_syslog();
×
207
        return r;
×
208
}
209

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

218
static int log_open_journal(void) {
159,579✔
219
        int r;
159,579✔
220

221
        if (journal_fd >= 0)
159,579✔
222
                return 0;
223

224
        journal_fd = create_log_socket(SOCK_DGRAM);
152,297✔
225
        if (journal_fd < 0) {
152,297✔
226
                r = journal_fd;
×
227
                goto fail;
×
228
        }
229

230
        r = connect_unix_path(journal_fd, AT_FDCWD, "/run/systemd/journal/socket");
152,297✔
231
        if (r < 0)
152,297✔
232
                goto fail;
53✔
233

234
        return 0;
235

236
fail:
53✔
237
        log_close_journal();
53✔
238
        return r;
53✔
239
}
240

241
bool stderr_is_journal(void) {
248,203✔
242
        _cleanup_free_ char *w = NULL;
248,203✔
243
        const char *e;
248,203✔
244
        uint64_t dev, ino;
248,203✔
245
        struct stat st;
248,203✔
246

247
        e = getenv("JOURNAL_STREAM");
248,203✔
248
        if (!e)
248,203✔
249
                return false;
250

251
        if (extract_first_word(&e, &w, ":", EXTRACT_DONT_COALESCE_SEPARATORS) <= 0)
16,189✔
252
                return false;
253
        if (!e)
16,189✔
254
                return false;
255

256
        if (safe_atou64(w, &dev) < 0)
16,189✔
257
                return false;
258
        if (safe_atou64(e, &ino) < 0)
16,189✔
259
                return false;
260

261
        if (fstat(STDERR_FILENO, &st) < 0)
16,189✔
262
                return false;
263

264
        return st.st_dev == dev && st.st_ino == ino;
16,981✔
265
}
266

267
int log_open(void) {
255,726✔
268
        int r;
255,726✔
269

270
        /* Do not call from library code. */
271

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

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

282
        if (log_target == LOG_TARGET_NULL) {
255,726✔
283
                log_close_journal();
1✔
284
                log_close_syslog();
1✔
285
                log_close_console();
1✔
286
                return 0;
287
        }
288

289
        if (getpid_cached() == 1 ||
500,643✔
290
            stderr_is_journal() ||
244,918✔
291
            IN_SET(log_target,
232,598✔
292
                   LOG_TARGET_KMSG,
293
                   LOG_TARGET_JOURNAL,
294
                   LOG_TARGET_JOURNAL_OR_KMSG,
295
                   LOG_TARGET_SYSLOG,
296
                   LOG_TARGET_SYSLOG_OR_KMSG)) {
297

298
                if (!prohibit_ipc) {
176,110✔
299
                        if (IN_SET(log_target,
172,097✔
300
                                   LOG_TARGET_AUTO,
301
                                   LOG_TARGET_JOURNAL_OR_KMSG,
302
                                   LOG_TARGET_JOURNAL)) {
303

304
                                r = log_open_journal();
152,203✔
305
                                if (r >= 0) {
152,203✔
306
                                        log_close_syslog();
152,163✔
307
                                        log_close_console();
152,163✔
308
                                        return r;
309
                                }
310
                        }
311

312
                        if (IN_SET(log_target,
19,934✔
313
                                   LOG_TARGET_SYSLOG_OR_KMSG,
314
                                   LOG_TARGET_SYSLOG)) {
315

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

325
                if (IN_SET(log_target, LOG_TARGET_AUTO,
23,945✔
326
                                       LOG_TARGET_JOURNAL_OR_KMSG,
327
                                       LOG_TARGET_SYSLOG_OR_KMSG,
328
                                       LOG_TARGET_KMSG)) {
329
                        r = log_open_kmsg();
22,360✔
330
                        if (r >= 0) {
22,360✔
331
                                log_close_journal();
9,622✔
332
                                log_close_syslog();
9,622✔
333
                                log_close_console();
9,622✔
334
                                return r;
335
                        }
336
                }
337
        }
338

339
        log_close_journal();
93,938✔
340
        log_close_syslog();
93,938✔
341

342
        return log_open_console();
93,938✔
343
}
344

345
void log_set_target(LogTarget target) {
105,195✔
346
        assert(target >= 0);
105,195✔
347
        assert(target < _LOG_TARGET_MAX);
105,195✔
348

349
        if (upgrade_syslog_to_journal) {
105,195✔
350
                if (target == LOG_TARGET_SYSLOG)
308✔
351
                        target = LOG_TARGET_JOURNAL;
352
                else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
308✔
353
                        target = LOG_TARGET_JOURNAL_OR_KMSG;
×
354
        }
355

356
        log_target = target;
105,195✔
357
}
105,195✔
358

359
void log_set_target_and_open(LogTarget target) {
279✔
360
        log_set_target(target);
279✔
361
        log_open();
279✔
362
}
279✔
363

364
void log_close(void) {
181,553✔
365
        /* Do not call from library code. */
366

367
        log_close_journal();
181,553✔
368
        log_close_syslog();
181,553✔
369
        log_close_kmsg();
181,553✔
370
        log_close_console();
181,553✔
371
}
181,553✔
372

373
void log_forget_fds(void) {
11,455✔
374
        /* Do not call from library code. */
375

376
        console_fd = kmsg_fd = syslog_fd = journal_fd = -EBADF;
11,455✔
377
        console_fd_is_tty = -1;
11,455✔
378
}
11,455✔
379

380
int log_set_max_level(int level) {
139,940✔
381
        assert(level == LOG_NULL || log_level_is_valid(level));
139,940✔
382

383
        int old = log_max_level;
139,940✔
384
        log_max_level = level;
139,940✔
385

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

394
        /* Ensure that our own LOG_NULL define maps sanely to the log mask */
395
        assert_cc(LOG_UPTO(LOG_NULL) == 0);
139,940✔
396

397
        return old;
139,940✔
398
}
399

400
void log_set_facility(int facility) {
244✔
401
        log_facility = facility;
244✔
402
}
244✔
403

404
static bool check_console_fd_is_tty(void) {
160,353✔
405
        if (console_fd < 0)
160,353✔
406
                return false;
407

408
        if (console_fd_is_tty < 0)
160,353✔
409
                console_fd_is_tty = isatty_safe(console_fd);
11,379✔
410

411
        return console_fd_is_tty;
160,353✔
412
}
413

414
static int write_to_console(
252,989✔
415
                int level,
416
                int error,
417
                const char *file,
418
                int line,
419
                const char *func,
420
                const char *buffer) {
421

422
        static int dumb = -1;
252,989✔
423

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

432
        if (console_fd < 0)
252,989✔
433
                return 0;
252,989✔
434

435
        if (dumb < 0)
235,430✔
436
                dumb = getenv_terminal_is_dumb();
12,309✔
437

438
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_CONSOLE])
235,430✔
439
                return 0;
440

441
        if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
160,353✔
442
                xsprintf(prefix, "<%i>", level);
446✔
443
                iovec[n++] = IOVEC_MAKE_STRING(prefix);
446✔
444
        }
445

446
        if (show_time &&
160,353✔
447
            format_timestamp(header_time, sizeof(header_time), now(CLOCK_REALTIME))) {
×
448
                iovec[n++] = IOVEC_MAKE_STRING(header_time);
×
449
                iovec[n++] = IOVEC_MAKE_STRING(" ");
×
450
        }
451

452
        if (show_tid) {
160,353✔
453
                xsprintf(tid_string, "(" PID_FMT ") ", gettid());
×
454
                iovec[n++] = IOVEC_MAKE_STRING(tid_string);
×
455
        }
456

457
        if (log_get_show_color())
160,353✔
458
                get_log_colors(LOG_PRI(level), &on, &off, NULL);
146,271✔
459

460
        if (show_location) {
160,353✔
461
                const char *lon = "", *loff = "";
×
462
                if (log_get_show_color()) {
×
463
                        lon = ansi_highlight_yellow4();
×
464
                        loff = ansi_normal();
×
465
                }
466

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

471
        if (on)
160,353✔
472
                iovec[n++] = IOVEC_MAKE_STRING(on);
134,246✔
473
        if (log_prefix) {
160,353✔
474
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
6,956✔
475
                iovec[n++] = IOVEC_MAKE_STRING(": ");
6,956✔
476
        }
477
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
160,353✔
478
        if (off)
160,353✔
479
                iovec[n++] = IOVEC_MAKE_STRING(off);
134,246✔
480

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

488
        if (writev(console_fd, iovec, n) < 0) {
160,353✔
489

490
                if (errno == EIO && getpid_cached() == 1) {
82✔
491

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

495
                        log_close_console();
1✔
496
                        (void) log_open_console();
1✔
497
                        if (console_fd < 0)
1✔
498
                                return 0;
499

500
                        if (writev(console_fd, iovec, n) < 0)
1✔
501
                                return -errno;
×
502
                } else
503
                        return -errno;
81✔
504
        }
505

506
        return 1;
507
}
508

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

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

523
        if (syslog_fd < 0)
180✔
524
                return 0;
180✔
525

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

529
        xsprintf(header_priority, "<%i>", level);
180✔
530

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

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

538
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
180✔
539

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

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

558
        for (;;) {
180✔
559
                ssize_t n;
180✔
560

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

565
                if (!syslog_is_stream)
180✔
566
                        break;
567

568
                if (iovec_increment(iovec, ELEMENTSOF(iovec), n))
×
569
                        break;
570
        }
571

572
        return 1;
573
}
574

575
static int write_to_kmsg(
221,483✔
576
                int level,
577
                int error,
578
                const char *file,
579
                int line,
580
                const char *func,
581
                const char *buffer) {
582

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

592
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
221,483✔
593
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
594

595
        if (kmsg_fd < 0)
221,483✔
596
                return 0;
221,483✔
597

598
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_KMSG])
41,674✔
599
                return 0;
600

601
        if (ratelimit_kmsg && !ratelimit_below(&ratelimit)) {
41,674✔
602
                if (ratelimit_num_dropped(&ratelimit) > 1)
×
603
                        return 0;
604

605
                buffer = "Too many messages being logged to kmsg, ignoring";
606
        }
607

608
        xsprintf(header_priority, "<%i>", level);
41,674✔
609
        xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());
41,674✔
610

611
        const struct iovec iovec[] = {
208,370✔
612
                IOVEC_MAKE_STRING(header_priority),
41,674✔
613
                IOVEC_MAKE_STRING(program_invocation_short_name),
41,674✔
614
                IOVEC_MAKE_STRING(header_pid),
41,674✔
615
                IOVEC_MAKE_STRING(strempty(log_prefix)),
41,674✔
616
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
41,674✔
617
                IOVEC_MAKE_STRING(buffer),
41,674✔
618
                IOVEC_MAKE_STRING("\n"),
41,674✔
619
        };
620

621
        if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
41,674✔
622
                return -errno;
×
623

624
        return 1;
625
}
626

627
static int log_do_header(
4,024,935✔
628
                char *header,
629
                size_t size,
630
                int level,
631
                int error,
632
                const char *file, int line, const char *func,
633
                const char *object_field, const char *object,
634
                const char *extra_field, const char *extra) {
635
        int r;
4,024,935✔
636

637
        error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error);
4,024,935✔
638

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

674
        return 0;
4,024,935✔
675
}
676

677
static void log_do_context(struct iovec *iovec, size_t iovec_len, size_t *n) {
4,024,935✔
678
        assert(iovec);
4,024,935✔
679
        assert(n);
4,024,935✔
680

681
        LIST_FOREACH(ll, c, log_context_head()) {
6,052,301✔
682
                STRV_FOREACH(s, c->fields) {
17,701,401✔
683
                        if (*n + 2 >= iovec_len)
15,668,505✔
684
                                return;
×
685

686
                        iovec[(*n)++] = IOVEC_MAKE_STRING(*s);
15,668,505✔
687
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
15,668,505✔
688
                }
689

690
                for (size_t i = 0; i < c->n_input_iovec; i++) {
2,035,952✔
691
                        if (*n + 2 >= iovec_len)
3,056✔
692
                                return;
×
693

694
                        iovec[(*n)++] = c->input_iovec[i];
3,056✔
695
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
3,056✔
696
                }
697

698
                if (c->key && c->value) {
2,032,896✔
699
                        if (*n + 3 >= iovec_len)
276,649✔
700
                                return;
5,530✔
701

702
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->key);
271,119✔
703
                        iovec[(*n)++] = IOVEC_MAKE_STRING(c->value);
271,119✔
704
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
271,119✔
705
                }
706
        }
707
}
708

709
static int write_to_journal(
4,244,473✔
710
                int level,
711
                int error,
712
                const char *file,
713
                int line,
714
                const char *func,
715
                const char *object_field,
716
                const char *object,
717
                const char *extra_field,
718
                const char *extra,
719
                const char *buffer) {
720

721
        char header[LINE_MAX];
4,244,473✔
722
        size_t n = 0, iovec_len;
4,244,473✔
723
        struct iovec *iovec;
4,244,473✔
724

725
        if (journal_fd < 0)
4,244,473✔
726
                return 0;
4,244,473✔
727

728
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL])
3,981,315✔
729
                return 0;
730

731
        iovec_len = MIN(6 + log_context_num_fields() * 3, IOVEC_MAX);
3,981,315✔
732
        iovec = newa(struct iovec, iovec_len);
3,981,315✔
733

734
        log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
3,981,315✔
735

736
        iovec[n++] = IOVEC_MAKE_STRING(header);
3,981,315✔
737
        iovec[n++] = IOVEC_MAKE_STRING("MESSAGE=");
3,981,315✔
738
        if (log_prefix) {
3,981,315✔
739
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
1,576,209✔
740
                iovec[n++] = IOVEC_MAKE_STRING(": ");
1,576,209✔
741
        }
742
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
3,981,315✔
743
        iovec[n++] = IOVEC_MAKE_STRING("\n");
3,981,315✔
744

745
        log_do_context(iovec, iovec_len, &n);
3,981,315✔
746

747
        const struct msghdr msghdr = {
3,981,315✔
748
                .msg_iov = iovec,
749
                .msg_iovlen = n,
750
        };
751

752
        if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0)
3,981,315✔
753
                return -errno;
146✔
754

755
        return 1;
756
}
757

758
int log_dispatch_internal(
4,266,801✔
759
                int level,
760
                int error,
761
                const char *file,
762
                int line,
763
                const char *func,
764
                const char *object_field,
765
                const char *object,
766
                const char *extra_field,
767
                const char *extra,
768
                char *buffer) {
769

770
        assert_raw(buffer);
4,266,801✔
771

772
        if (log_target == LOG_TARGET_NULL)
4,266,801✔
773
                return -ERRNO_VALUE(error);
10✔
774

775
        /* Patch in LOG_DAEMON facility if necessary */
776
        if (LOG_FAC(level) == 0)
4,266,791✔
777
                level |= log_facility;
4,258,169✔
778

779
        if (open_when_needed)
4,266,791✔
780
                (void) log_open();
142,749✔
781

782
        do {
4,320,114✔
783
                char *e;
4,320,114✔
784
                int k = 0;
4,320,114✔
785

786
                buffer += strspn(buffer, NEWLINE);
4,320,114✔
787

788
                if (buffer[0] == 0)
4,320,114✔
789
                        break;
790

791
                if ((e = strpbrk(buffer, NEWLINE)))
4,276,012✔
792
                        *(e++) = 0;
53,323✔
793

794
                if (IN_SET(log_target, LOG_TARGET_AUTO,
4,276,012✔
795
                                       LOG_TARGET_JOURNAL_OR_KMSG,
796
                                       LOG_TARGET_JOURNAL)) {
797

798
                        k = write_to_journal(level, error, file, line, func, object_field, object, extra_field, extra, buffer);
4,244,473✔
799
                        if (k < 0 && k != -EAGAIN)
4,244,473✔
800
                                log_close_journal();
3✔
801
                }
802

803
                if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
4,276,012✔
804
                                       LOG_TARGET_SYSLOG)) {
805

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

811
                if (k <= 0 &&
4,276,012✔
812
                    IN_SET(log_target, LOG_TARGET_AUTO,
294,663✔
813
                                       LOG_TARGET_SYSLOG_OR_KMSG,
814
                                       LOG_TARGET_JOURNAL_OR_KMSG,
815
                                       LOG_TARGET_KMSG)) {
816

817
                        if (k < 0)
221,483✔
818
                                log_open_kmsg();
133✔
819

820
                        k = write_to_kmsg(level, error, file, line, func, buffer);
221,483✔
821
                        if (k < 0) {
221,483✔
822
                                log_close_kmsg();
×
823
                                (void) log_open_console();
×
824
                        }
825
                }
826

827
                if (k <= 0)
294,663✔
828
                        (void) write_to_console(level, error, file, line, func, buffer);
252,989✔
829

830
                buffer = e;
4,276,012✔
831
        } while (buffer);
4,276,012✔
832

833
        if (open_when_needed)
4,266,791✔
834
                log_close();
142,749✔
835

836
        return -ERRNO_VALUE(error);
4,266,791✔
837
}
838

839
int log_dump_internal(
6✔
840
                int level,
841
                int error,
842
                const char *file,
843
                int line,
844
                const char *func,
845
                char *buffer) {
846

847
        PROTECT_ERRNO;
6✔
848

849
        /* This modifies the buffer... */
850

851
        if (_likely_(LOG_PRI(level) > log_max_level))
6✔
852
                return -ERRNO_VALUE(error);
×
853

854
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
6✔
855
}
856

857
int log_internalv(
2,630,868✔
858
                int level,
859
                int error,
860
                const char *file,
861
                int line,
862
                const char *func,
863
                const char *format,
864
                va_list ap) {
865

866
        if (_likely_(LOG_PRI(level) > log_max_level))
2,630,868✔
867
                return -ERRNO_VALUE(error);
2,630,868✔
868

869
        /* Make sure that %m maps to the specified error (or "Success"). */
870
        char buffer[LINE_MAX];
2,630,644✔
871
        LOCAL_ERRNO(ERRNO_VALUE(error));
2,630,644✔
872

873
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
2,630,644✔
874

875
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
2,630,644✔
876
}
877

878
int log_internal(
2,630,198✔
879
                int level,
880
                int error,
881
                const char *file,
882
                int line,
883
                const char *func,
884
                const char *format, ...) {
885

886
        va_list ap;
2,630,198✔
887
        int r;
2,630,198✔
888

889
        va_start(ap, format);
2,630,198✔
890
        r = log_internalv(level, error, file, line, func, format, ap);
2,630,198✔
891
        va_end(ap);
2,630,198✔
892

893
        return r;
2,630,198✔
894
}
895

896
int log_object_internalv(
1,669,115✔
897
                int level,
898
                int error,
899
                const char *file,
900
                int line,
901
                const char *func,
902
                const char *object_field,
903
                const char *object,
904
                const char *extra_field,
905
                const char *extra,
906
                const char *format,
907
                va_list ap) {
908

909
        char *buffer, *b;
1,669,115✔
910

911
        if (_likely_(LOG_PRI(level) > log_max_level))
1,669,115✔
912
                return -ERRNO_VALUE(error);
1,669,115✔
913

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

917
        LOG_SET_PREFIX(object);
3,255,568✔
918

919
        b = buffer = newa(char, LINE_MAX);
1,627,784✔
920
        (void) vsnprintf(b, LINE_MAX, format, ap);
1,627,784✔
921

922
        return log_dispatch_internal(level, error, file, line, func,
1,627,784✔
923
                                     object_field, object, extra_field, extra, buffer);
924
}
925

926
int log_object_internal(
1,664,829✔
927
                int level,
928
                int error,
929
                const char *file,
930
                int line,
931
                const char *func,
932
                const char *object_field,
933
                const char *object,
934
                const char *extra_field,
935
                const char *extra,
936
                const char *format, ...) {
937

938
        va_list ap;
1,664,829✔
939
        int r;
1,664,829✔
940

941
        va_start(ap, format);
1,664,829✔
942
        r = log_object_internalv(level, error, file, line, func, object_field, object, extra_field, extra, format, ap);
1,664,829✔
943
        va_end(ap);
1,664,829✔
944

945
        return r;
1,664,829✔
946
}
947

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

952
int log_format_iovec(
42,348✔
953
                struct iovec *iovec,
954
                size_t iovec_len,
955
                size_t *n,
956
                bool newline_separator,
957
                int error,
958
                const char *format,
959
                va_list ap) {
960

961
        static const char nl = '\n';
42,348✔
962

963
        while (format && *n + 1 < iovec_len) {
259,287✔
964
                va_list aq;
216,939✔
965
                char *m;
216,939✔
966
                int r;
216,939✔
967

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

972
                errno = ERRNO_VALUE(error);
216,939✔
973

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

980
                /* Now, jump enough ahead, so that we point to
981
                 * the next format string */
982
                VA_FORMAT_ADVANCE(format, ap);
710,594✔
983

984
                iovec[(*n)++] = IOVEC_MAKE_STRING(m);
216,939✔
985
                if (newline_separator)
216,939✔
986
                        iovec[(*n)++] = IOVEC_MAKE((char *)&nl, 1);
215,473✔
987

988
                format = va_arg(ap, char *);
216,939✔
989
        }
990
        return 0;
991
}
992

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

1001
        char buf[LINE_MAX];
50,208✔
1002
        bool found = false;
50,208✔
1003
        PROTECT_ERRNO;
50,208✔
1004
        va_list ap;
50,208✔
1005

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

1010
        if (LOG_FAC(level) == 0)
50,158✔
1011
                level |= log_facility;
50,158✔
1012

1013
        if (IN_SET(log_target,
50,158✔
1014
                   LOG_TARGET_AUTO,
1015
                   LOG_TARGET_JOURNAL_OR_KMSG,
1016
                   LOG_TARGET_JOURNAL)) {
1017

1018
                if (open_when_needed)
47,938✔
1019
                        log_open_journal();
7,377✔
1020

1021
                if (journal_fd >= 0) {
47,938✔
1022
                        char header[LINE_MAX];
42,197✔
1023
                        struct iovec *iovec;
42,197✔
1024
                        size_t n = 0, m, iovec_len;
42,197✔
1025
                        int r;
42,197✔
1026
                        bool fallback = false;
42,197✔
1027

1028
                        iovec_len = MIN(17 + log_context_num_fields() * 3, IOVEC_MAX);
42,197✔
1029
                        iovec = newa(struct iovec, iovec_len);
42,197✔
1030

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

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

1046
                                const struct msghdr msghdr = {
42,197✔
1047
                                        .msg_iov = iovec,
1048
                                        .msg_iovlen = n,
1049
                                };
1050

1051
                                (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
42,197✔
1052
                        }
1053

1054
                        va_end(ap);
42,197✔
1055
                        for (size_t i = 1; i < m; i += 2)
257,670✔
1056
                                free(iovec[i].iov_base);
215,473✔
1057

1058
                        if (!fallback) {
42,197✔
1059
                                if (open_when_needed)
42,197✔
1060
                                        log_close();
7,364✔
1061

1062
                                return -ERRNO_VALUE(error);
42,197✔
1063
                        }
1064
                }
1065
        }
1066

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

1069
        va_start(ap, format);
7,961✔
1070
        while (format) {
25,771✔
1071
                va_list aq;
25,771✔
1072

1073
                errno = ERRNO_VALUE(error);
25,771✔
1074

1075
                va_copy(aq, ap);
25,771✔
1076
                DISABLE_WARNING_FORMAT_NONLITERAL;
25,771✔
1077
                (void) vsnprintf(buf, sizeof buf, format, aq);
25,771✔
1078
                REENABLE_WARNING;
25,771✔
1079
                va_end(aq);
25,771✔
1080

1081
                if (startswith(buf, "MESSAGE=")) {
25,771✔
1082
                        found = true;
7,961✔
1083
                        break;
7,961✔
1084
                }
1085

1086
                VA_FORMAT_ADVANCE(format, ap);
52,979✔
1087

1088
                format = va_arg(ap, char *);
17,810✔
1089
        }
1090
        va_end(ap);
7,961✔
1091

1092
        if (!found) {
7,961✔
1093
                if (open_when_needed)
×
1094
                        log_close();
×
1095

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

1099
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
7,961✔
1100
}
1101

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

1111
        PROTECT_ERRNO;
1,452✔
1112

1113
        if (_likely_(LOG_PRI(level) > log_max_level) ||
1,452✔
1114
            log_target == LOG_TARGET_NULL)
1,452✔
1115
                return -ERRNO_VALUE(error);
×
1116

1117
        if (LOG_FAC(level) == 0)
1,452✔
1118
                level |= log_facility;
1,452✔
1119

1120
        if (IN_SET(log_target, LOG_TARGET_AUTO,
1,452✔
1121
                               LOG_TARGET_JOURNAL_OR_KMSG,
1122
                               LOG_TARGET_JOURNAL) &&
1,452✔
1123
            journal_fd >= 0) {
1,452✔
1124

1125
                char header[LINE_MAX];
1,424✔
1126
                struct iovec *iovec;
1,424✔
1127
                size_t n = 0, iovec_len;
1,424✔
1128

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

1132
                log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
1,424✔
1133

1134
                iovec[n++] = IOVEC_MAKE_STRING(header);
1,424✔
1135
                for (size_t i = 0; i < n_input_iovec; i++) {
11,342✔
1136
                        iovec[n++] = input_iovec[i];
9,918✔
1137
                        iovec[n++] = IOVEC_MAKE_STRING("\n");
9,918✔
1138
                }
1139

1140
                log_do_context(iovec, iovec_len, &n);
1,424✔
1141

1142
                const struct msghdr msghdr = {
1,424✔
1143
                        .msg_iov = iovec,
1144
                        .msg_iovlen = n,
1145
                };
1146

1147
                if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0)
1,424✔
1148
                        return -ERRNO_VALUE(error);
1,424✔
1149
        }
1150

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

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

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

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

1165
int log_set_target_from_string(const char *e) {
12,066✔
1166
        LogTarget t;
12,066✔
1167

1168
        t = log_target_from_string(e);
12,066✔
1169
        if (t < 0)
12,066✔
1170
                return t;
1171

1172
        log_set_target(t);
12,066✔
1173
        return 0;
12,066✔
1174
}
1175

1176
int log_set_max_level_from_string(const char *e) {
24,520✔
1177
        int r;
67,474✔
1178

1179
        for (;;) {
67,474✔
1180
                _cleanup_free_ char *word = NULL, *prefix = NULL;
67,474✔
1181
                LogTarget target;
67,474✔
1182
                const char *colon;
67,474✔
1183

1184
                r = extract_first_word(&e, &word, ",", 0);
67,474✔
1185
                if (r < 0)
67,474✔
1186
                        return r;
1187
                if (r == 0)
67,474✔
1188
                        break;
1189

1190
                colon = strchr(word, ':');
42,954✔
1191
                if (!colon) {
42,954✔
1192
                        r = log_level_from_string(word);
24,520✔
1193
                        if (r < 0)
24,520✔
1194
                                return r;
1195

1196
                        log_set_max_level(r);
24,520✔
1197
                        continue;
24,520✔
1198
                }
1199

1200
                prefix = strndup(word, colon - word);
18,434✔
1201
                if (!prefix)
18,434✔
1202
                        return -ENOMEM;
1203

1204
                target = log_target_from_string(prefix);
18,434✔
1205
                if (target < 0)
18,434✔
1206
                        return target;
1207

1208
                if (target >= _LOG_TARGET_SINGLE_MAX)
18,434✔
1209
                        return -EINVAL;
1210

1211
                r = log_level_from_string(colon + 1);
18,434✔
1212
                if (r < 0)
18,434✔
1213
                        return r;
1214

1215
                log_target_max_level[target] = r;
18,434✔
1216
        }
1217

1218
        return 0;
24,520✔
1219
}
1220

1221
int log_max_levels_to_string(int level, char **ret) {
2,199✔
1222
        _cleanup_free_ char *s = NULL;
2,199✔
1223
        int r;
2,199✔
1224

1225
        assert(ret);
2,199✔
1226

1227
        r = log_level_to_string_alloc(level, &s);
2,199✔
1228
        if (r < 0)
2,199✔
1229
                return r;
1230

1231
        for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
10,995✔
1232
                _cleanup_free_ char *l = NULL;
2,199✔
1233

1234
                if (log_target_max_level[target] == INT_MAX)
8,796✔
1235
                        continue;
6,597✔
1236

1237
                r = log_level_to_string_alloc(log_target_max_level[target], &l);
2,199✔
1238
                if (r < 0)
2,199✔
1239
                        return r;
1240

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

1246
        *ret = TAKE_PTR(s);
2,199✔
1247
        return 0;
2,199✔
1248
}
1249

1250
static int log_set_ratelimit_kmsg_from_string(const char *e) {
6,978✔
1251
        int r;
6,978✔
1252

1253
        r = parse_boolean(e);
6,978✔
1254
        if (r < 0)
6,978✔
1255
                return r;
1256

1257
        ratelimit_kmsg = r;
6,978✔
1258
        return 0;
6,978✔
1259
}
1260

1261
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
249,833✔
1262

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

1272
        if (streq(key, "debug") && !value)
249,833✔
1273
                log_set_max_level(LOG_DEBUG);
×
1274

1275
        else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
249,833✔
1276

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

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

1283
        } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
249,833✔
1284

1285
                if (proc_cmdline_value_missing(key, value))
6,978✔
1286
                        return 0;
1287

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

1291
        } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
242,855✔
1292

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

1296
        } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
242,855✔
1297

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

1301
        } else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
242,855✔
1302

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

1306
        } else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
242,855✔
1307

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

1311
        } else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
242,855✔
1312

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

1317
        return 0;
1318
}
1319

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

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

1329
void log_parse_environment_variables(void) {
90,508✔
1330
        const char *e;
90,508✔
1331
        int r;
90,508✔
1332

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

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

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

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

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

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

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

1376
void log_parse_environment(void) {
88,631✔
1377
        /* Do not call from library code. */
1378

1379
        if (should_parse_proc_cmdline())
88,631✔
1380
                (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
7,051✔
1381

1382
        log_parse_environment_variables();
88,631✔
1383
}
88,631✔
1384

1385
LogTarget log_get_target(void) {
15,946✔
1386
        return log_target;
15,946✔
1387
}
1388

1389
void log_settle_target(void) {
14,038✔
1390

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

1397
        LogTarget t = log_get_target();
14,038✔
1398

1399
        if (t != LOG_TARGET_AUTO)
14,038✔
1400
                return;
1401

1402
        t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
6,371✔
1403
                                                        : LOG_TARGET_CONSOLE;
6,263✔
1404
        log_set_target(t);
3,197✔
1405
}
1406

1407
int log_get_max_level(void) {
12,611,772✔
1408
        return log_max_level;
12,611,772✔
1409
}
1410

1411
void log_show_color(bool b) {
80,173✔
1412
        show_color = b;
80,173✔
1413
}
80,173✔
1414

1415
bool log_get_show_color(void) {
187,939✔
1416
        return show_color > 0; /* Defaults to false. */
187,939✔
1417
}
1418

1419
void log_show_location(bool b) {
×
1420
        show_location = b;
×
1421
}
×
1422

1423
bool log_get_show_location(void) {
14✔
1424
        return show_location;
14✔
1425
}
1426

1427
void log_show_time(bool b) {
1✔
1428
        show_time = b;
1✔
1429
}
1✔
1430

1431
bool log_get_show_time(void) {
14✔
1432
        return show_time;
14✔
1433
}
1434

1435
void log_show_tid(bool b) {
1✔
1436
        show_tid = b;
1✔
1437
}
1✔
1438

1439
bool log_get_show_tid(void) {
×
1440
        return show_tid;
×
1441
}
1442

1443
int log_show_color_from_string(const char *e) {
×
1444
        int r;
×
1445

1446
        r = parse_boolean(e);
×
1447
        if (r < 0)
×
1448
                return r;
1449

1450
        log_show_color(r);
×
1451
        return 0;
×
1452
}
1453

1454
int log_show_location_from_string(const char *e) {
×
1455
        int r;
×
1456

1457
        r = parse_boolean(e);
×
1458
        if (r < 0)
×
1459
                return r;
1460

1461
        log_show_location(r);
×
1462
        return 0;
×
1463
}
1464

1465
int log_show_time_from_string(const char *e) {
×
1466
        int r;
×
1467

1468
        r = parse_boolean(e);
×
1469
        if (r < 0)
×
1470
                return r;
1471

1472
        log_show_time(r);
×
1473
        return 0;
×
1474
}
1475

1476
int log_show_tid_from_string(const char *e) {
×
1477
        int r;
×
1478

1479
        r = parse_boolean(e);
×
1480
        if (r < 0)
×
1481
                return r;
1482

1483
        log_show_tid(r);
×
1484
        return 0;
×
1485
}
1486

1487
bool log_on_console(void) {
104,105✔
1488
        if (IN_SET(log_target, LOG_TARGET_CONSOLE,
104,105✔
1489
                               LOG_TARGET_CONSOLE_PREFIXED))
1490
                return true;
1491

1492
        return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
126,682✔
1493
}
1494

1495
static const char *const log_target_table[_LOG_TARGET_MAX] = {
1496
        [LOG_TARGET_CONSOLE]          = "console",
1497
        [LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
1498
        [LOG_TARGET_KMSG]             = "kmsg",
1499
        [LOG_TARGET_JOURNAL]          = "journal",
1500
        [LOG_TARGET_JOURNAL_OR_KMSG]  = "journal-or-kmsg",
1501
        [LOG_TARGET_SYSLOG]           = "syslog",
1502
        [LOG_TARGET_SYSLOG_OR_KMSG]   = "syslog-or-kmsg",
1503
        [LOG_TARGET_AUTO]             = "auto",
1504
        [LOG_TARGET_NULL]             = "null",
1505
};
1506

1507
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
35,012✔
1508

1509
void log_received_signal(int level, const struct signalfd_siginfo *si) {
3,592✔
1510
        assert(si);
3,592✔
1511

1512
        if (pid_is_valid(si->ssi_pid)) {
3,592✔
1513
                _cleanup_free_ char *p = NULL;
3,592✔
1514

1515
                (void) pid_get_comm(si->ssi_pid, &p);
3,592✔
1516

1517
                log_full(level,
4,299✔
1518
                         "Received SIG%s from PID %"PRIu32" (%s).",
1519
                         signal_to_string(si->ssi_signo),
1520
                         si->ssi_pid, strna(p));
1521
        } else
1522
                log_full(level,
×
1523
                         "Received SIG%s.",
1524
                         signal_to_string(si->ssi_signo));
1525
}
3,592✔
1526

1527
void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata) {
852✔
1528
        assert(!log_syntax_callback || !cb);
852✔
1529
        assert(!log_syntax_callback_userdata || !userdata);
852✔
1530

1531
        log_syntax_callback = cb;
852✔
1532
        log_syntax_callback_userdata = userdata;
852✔
1533
}
852✔
1534

1535
int log_syntax_internal(
11,438✔
1536
                const char *unit,
1537
                int level,
1538
                const char *config_file,
1539
                unsigned config_line,
1540
                int error,
1541
                const char *file,
1542
                int line,
1543
                const char *func,
1544
                const char *format, ...) {
1545

1546
        PROTECT_ERRNO;
11,438✔
1547

1548
        if (log_syntax_callback)
11,438✔
1549
                log_syntax_callback(unit, level, log_syntax_callback_userdata);
846✔
1550

1551
        if (_likely_(LOG_PRI(level) > log_max_level) ||
11,438✔
1552
            log_target == LOG_TARGET_NULL)
11,438✔
1553
                return -ERRNO_VALUE(error);
30✔
1554

1555
        char buffer[LINE_MAX];
11,408✔
1556
        va_list ap;
11,408✔
1557
        const char *unit_fmt = NULL;
11,408✔
1558

1559
        errno = ERRNO_VALUE(error);
11,408✔
1560

1561
        va_start(ap, format);
11,408✔
1562
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
11,408✔
1563
        va_end(ap);
11,408✔
1564

1565
        if (unit)
11,408✔
1566
                unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
4,790✔
1567

1568
        if (config_file) {
11,408✔
1569
                if (config_line > 0)
11,398✔
1570
                        return log_struct_internal(
5,877✔
1571
                                        level,
1572
                                        error,
1573
                                        file, line, func,
1574
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
5,877✔
1575
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
5,877✔
1576
                                        LOG_ITEM("CONFIG_LINE=%u", config_line),
5,877✔
1577
                                        LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
5,877✔
1578
                                        unit_fmt, unit,
1579
                                        NULL);
1580
                else
1581
                        return log_struct_internal(
5,521✔
1582
                                        level,
1583
                                        error,
1584
                                        file, line, func,
1585
                                        LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
5,521✔
1586
                                        LOG_ITEM("CONFIG_FILE=%s", config_file),
5,521✔
1587
                                        LOG_MESSAGE("%s: %s", config_file, buffer),
5,521✔
1588
                                        unit_fmt, unit,
1589
                                        NULL);
1590
        } else if (unit)
10✔
1591
                return log_struct_internal(
×
1592
                                level,
1593
                                error,
1594
                                file, line, func,
1595
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
×
1596
                                LOG_MESSAGE("%s: %s", unit, buffer),
×
1597
                                unit_fmt, unit,
1598
                                NULL);
1599
        else
1600
                return log_struct_internal(
10✔
1601
                                level,
1602
                                error,
1603
                                file, line, func,
1604
                                LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
10✔
1605
                                LOG_MESSAGE("%s", buffer),
10✔
1606
                                NULL);
1607
}
1608

1609
int log_syntax_invalid_utf8_internal(
1✔
1610
                const char *unit,
1611
                int level,
1612
                const char *config_file,
1613
                unsigned config_line,
1614
                const char *file,
1615
                int line,
1616
                const char *func,
1617
                const char *rvalue) {
1618

1619
        PROTECT_ERRNO;
×
1620
        _cleanup_free_ char *p = NULL;
1✔
1621

1622
        if (rvalue)
1✔
1623
                p = utf8_escape_invalid(rvalue);
1✔
1624

1625
        return log_syntax_internal(unit, level, config_file, config_line,
1✔
1626
                                   SYNTHETIC_ERRNO(EINVAL), file, line, func,
1627
                                   "String is not UTF-8 clean, ignoring assignment: %s", strna(p));
1628
}
1629

1630
int log_syntax_parse_error_internal(
110✔
1631
                const char *unit,
1632
                const char *config_file,
1633
                unsigned config_line,
1634
                int error,
1635
                bool critical,
1636
                const char *file,
1637
                int line,
1638
                const char *func,
1639
                const char *lvalue,
1640
                const char *rvalue) {
1641

1642
        PROTECT_ERRNO;
×
1643
        _cleanup_free_ char *escaped = NULL;
110✔
1644

1645
        /* OOM is always handled as critical. */
1646
        if (ERRNO_VALUE(error) == ENOMEM)
110✔
1647
                return log_oom_internal(LOG_ERR, file, line, func);
×
1648

1649
        if (rvalue && !utf8_is_valid(rvalue)) {
220✔
1650
                escaped = utf8_escape_invalid(rvalue);
×
1651
                if (!escaped)
×
1652
                        rvalue = "(oom)";
1653
                else
1654
                        rvalue = " (escaped)";
×
1655
        }
1656

1657
        log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
550✔
1658
                            file, line, func,
1659
                            "Failed to parse %s=%s%s%s%s%s",
1660
                            strna(lvalue), strempty(escaped), strempty(rvalue),
1661
                            critical ? "" : ", ignoring",
1662
                            error == 0 ? "." : ": ",
1663
                            error == 0 ? "" : STRERROR(error));
110✔
1664

1665
        return critical ? -ERRNO_VALUE(error) : 0;
110✔
1666
}
1667

1668
void log_set_upgrade_syslog_to_journal(bool b) {
232✔
1669
        upgrade_syslog_to_journal = b;
232✔
1670

1671
        /* Make the change effective immediately */
1672
        if (b) {
232✔
1673
                if (log_target == LOG_TARGET_SYSLOG)
232✔
1674
                        log_target = LOG_TARGET_JOURNAL;
×
1675
                else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
232✔
1676
                        log_target = LOG_TARGET_JOURNAL_OR_KMSG;
×
1677
        }
1678
}
232✔
1679

1680
void log_set_always_reopen_console(bool b) {
11,688✔
1681
        always_reopen_console = b;
11,688✔
1682
}
11,688✔
1683

1684
void log_set_open_when_needed(bool b) {
17,988✔
1685
        open_when_needed = b;
17,988✔
1686
}
17,988✔
1687

1688
void log_set_prohibit_ipc(bool b) {
33,400✔
1689
        prohibit_ipc = b;
33,400✔
1690
}
33,400✔
1691

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

1696
        return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
×
1697
}
1698

1699
int log_dup_console(void) {
98✔
1700
        int copy;
98✔
1701

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

1705
        if (console_fd < 0 || console_fd >= 3)
98✔
1706
                return 0;
1707

1708
        copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);
1✔
1709
        if (copy < 0)
1✔
1710
                return -errno;
×
1711

1712
        console_fd = copy;
1✔
1713
        return 0;
1✔
1714
}
1715

1716
void log_setup(void) {
87,835✔
1717
        log_set_target(LOG_TARGET_AUTO);
87,835✔
1718
        log_parse_environment();
87,835✔
1719
        (void) log_open();
87,835✔
1720
        if (log_on_console() && show_color < 0)
87,835✔
1721
                log_show_color(true);
80,070✔
1722
}
87,835✔
1723

1724
const char* _log_set_prefix(const char *prefix, bool force) {
3,255,954✔
1725
        const char *old = log_prefix;
3,255,954✔
1726

1727
        if (prefix || force)
3,255,954✔
1728
                log_prefix = prefix;
3,254,099✔
1729

1730
        return old;
3,255,954✔
1731
}
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