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

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

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

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

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

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

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

71
static bool syslog_is_stream = false;
72

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

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

83
static thread_local const char *log_prefix = NULL;
84

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

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

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

105
static int log_open_console(void) {
94,738✔
106

107
        if (!always_reopen_console) {
94,738✔
108
                console_fd = STDERR_FILENO;
70,014✔
109
                console_fd_is_tty = -1;
70,014✔
110
                return 0;
70,014✔
111
        }
112

113
        if (console_fd < 3) {
24,724✔
114
                int fd;
22,451✔
115

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

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

124
        return 0;
125
}
126

127
static void log_close_kmsg(void) {
182,442✔
128
        /* See comment in log_close_journal() */
129
        (void) safe_close(TAKE_FD(kmsg_fd));
182,442✔
130
}
182,442✔
131

132
static int log_open_kmsg(void) {
22,254✔
133

134
        if (kmsg_fd >= 0)
22,254✔
135
                return 0;
136

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

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

145
static void log_close_syslog(void) {
440,310✔
146
        /* See comment in log_close_journal() */
147
        (void) safe_close(TAKE_FD(syslog_fd));
440,310✔
148
}
440,310✔
149

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

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

158
        fd = fd_move_above_stdio(fd);
153,303✔
159
        (void) fd_inc_sndbuf(fd, SNDBUF_SIZE);
153,303✔
160

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

169
        return fd;
153,303✔
170
}
171

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

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

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

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

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

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

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

204
        return 0;
205

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

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

219
static int log_open_journal(void) {
160,926✔
220
        int r;
160,926✔
221

222
        if (journal_fd >= 0)
160,926✔
223
                return 0;
224

225
        journal_fd = create_log_socket(SOCK_DGRAM);
153,301✔
226
        if (journal_fd < 0) {
153,301✔
227
                r = journal_fd;
×
UNCOV
228
                goto fail;
×
229
        }
230

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

235
        return 0;
236

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

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

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

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

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

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

265
        return st.st_dev == dev && st.st_ino == ino;
17,101✔
266
}
267

268
int log_open(void) {
257,870✔
269
        int r;
257,870✔
270

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

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

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

283
        if (log_target == LOG_TARGET_NULL) {
257,870✔
284
                log_close_journal();
1✔
285
                log_close_syslog();
1✔
286
                log_close_console();
1✔
287
                return 0;
288
        }
289

290
        if (getpid_cached() == 1 ||
504,470✔
291
            stderr_is_journal() ||
246,601✔
292
            IN_SET(log_target,
234,219✔
293
                   LOG_TARGET_KMSG,
294
                   LOG_TARGET_JOURNAL,
295
                   LOG_TARGET_JOURNAL_OR_KMSG,
296
                   LOG_TARGET_SYSLOG,
297
                   LOG_TARGET_SYSLOG_OR_KMSG)) {
298

299
                if (!prohibit_ipc) {
177,193✔
300
                        if (IN_SET(log_target,
173,059✔
301
                                   LOG_TARGET_AUTO,
302
                                   LOG_TARGET_JOURNAL_OR_KMSG,
303
                                   LOG_TARGET_JOURNAL)) {
304

305
                                r = log_open_journal();
153,513✔
306
                                if (r >= 0) {
153,513✔
307
                                        log_close_syslog();
153,473✔
308
                                        log_close_console();
153,473✔
309
                                        return r;
310
                                }
311
                        }
312

313
                        if (IN_SET(log_target,
19,586✔
314
                                   LOG_TARGET_SYSLOG_OR_KMSG,
315
                                   LOG_TARGET_SYSLOG)) {
316

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

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

340
        log_close_journal();
94,737✔
341
        log_close_syslog();
94,737✔
342

343
        return log_open_console();
94,737✔
344
}
345

346
void log_set_target(LogTarget target) {
106,405✔
347
        assert(target >= 0);
106,405✔
348
        assert(target < _LOG_TARGET_MAX);
106,405✔
349

350
        if (upgrade_syslog_to_journal) {
106,405✔
351
                if (target == LOG_TARGET_SYSLOG)
309✔
352
                        target = LOG_TARGET_JOURNAL;
353
                else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
309✔
UNCOV
354
                        target = LOG_TARGET_JOURNAL_OR_KMSG;
×
355
        }
356

357
        log_target = target;
106,405✔
358
}
106,405✔
359

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

365
void log_close(void) {
182,442✔
366
        /* Do not call from library code. */
367

368
        log_close_journal();
182,442✔
369
        log_close_syslog();
182,442✔
370
        log_close_kmsg();
182,442✔
371
        log_close_console();
182,442✔
372
}
182,442✔
373

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

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

381
int log_set_max_level(int level) {
690,920✔
382
        assert(level == LOG_NULL || log_level_is_valid(level));
690,920✔
383

384
        int old = log_max_level;
690,920✔
385
        log_max_level = level;
690,920✔
386

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

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

398
        return old;
690,920✔
399
}
400

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

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

409
        if (console_fd_is_tty < 0)
160,249✔
410
                console_fd_is_tty = isatty_safe(console_fd);
11,315✔
411

412
        return console_fd_is_tty;
160,249✔
413
}
414

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

423
        static int dumb = -1;
248,583✔
424

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

433
        if (console_fd < 0)
248,583✔
434
                return 0;
248,583✔
435

436
        if (dumb < 0)
235,584✔
437
                dumb = getenv_terminal_is_dumb();
12,246✔
438

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

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

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

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

458
        if (log_get_show_color())
160,249✔
459
                get_log_colors(LOG_PRI(level), &on, &off, NULL);
147,160✔
460

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

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

472
        if (on)
160,249✔
473
                iovec[n++] = IOVEC_MAKE_STRING(on);
135,291✔
474
        if (log_prefix) {
160,249✔
475
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
7,118✔
476
                iovec[n++] = IOVEC_MAKE_STRING(": ");
7,118✔
477
        }
478
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
160,249✔
479
        if (off)
160,249✔
480
                iovec[n++] = IOVEC_MAKE_STRING(off);
135,291✔
481

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

489
        if (writev(console_fd, iovec, n) < 0) {
160,249✔
490

491
                if (errno == EIO && getpid_cached() == 1) {
84✔
492

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

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

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

507
        return 1;
508
}
509

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

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

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

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

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

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

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

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

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

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

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

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

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

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

573
        return 1;
574
}
575

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

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

593
        char header_priority[2 + DECIMAL_STR_MAX(int) + 1],
217,645✔
594
             header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1];
595

596
        if (kmsg_fd < 0)
217,645✔
597
                return 0;
217,645✔
598

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

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

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

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

612
        const struct iovec iovec[] = {
333,024✔
613
                IOVEC_MAKE_STRING(header_priority),
41,628✔
614
                IOVEC_MAKE_STRING(program_invocation_short_name),
41,628✔
615
                IOVEC_MAKE_STRING(header_pid),
41,628✔
616
                IOVEC_MAKE_STRING(strempty(log_prefix)),
63,608✔
617
                IOVEC_MAKE_STRING(log_prefix ? ": " : ""),
63,608✔
618
                IOVEC_MAKE_STRING(buffer),
41,628✔
619
                IOVEC_MAKE_STRING("\n"),
41,628✔
620
        };
621

622
        if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
41,628✔
UNCOV
623
                return -errno;
×
624

625
        return 1;
626
}
627

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

638
        error = IS_SYNTHETIC_ERRNO(error) ? 0 : ERRNO_VALUE(error);
4,121,454✔
639

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

675
        return 0;
4,121,454✔
676
}
677

678
static void log_do_context(struct iovec *iovec, size_t iovec_len, size_t *n) {
4,121,454✔
679
        assert(iovec);
4,121,454✔
680
        assert(n);
4,121,454✔
681

682
        LIST_FOREACH(ll, c, log_context_head()) {
6,097,922✔
683
                STRV_FOREACH(s, c->fields) {
18,478,802✔
684
                        if (*n + 2 >= iovec_len)
16,374,946✔
UNCOV
685
                                return;
×
686

687
                        iovec[(*n)++] = IOVEC_MAKE_STRING(*s);
16,374,946✔
688
                        iovec[(*n)++] = IOVEC_MAKE_STRING("\n");
16,374,946✔
689
                }
690

691
                for (size_t i = 0; i < c->n_input_iovec; i++) {
2,107,302✔
692
                        if (*n + 2 >= iovec_len)
3,446✔
UNCOV
693
                                return;
×
694

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

699
                if (c->key && c->value) {
2,103,856✔
700
                        if (*n + 3 >= iovec_len)
278,744✔
701
                                return;
127,388✔
702

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

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

722
        char header[LINE_MAX];
4,337,425✔
723
        size_t n = 0, iovec_len;
4,337,425✔
724
        struct iovec *iovec;
4,337,425✔
725

726
        if (journal_fd < 0)
4,337,425✔
727
                return 0;
4,337,425✔
728

729
        if (LOG_PRI(level) > log_target_max_level[LOG_TARGET_JOURNAL])
4,077,399✔
730
                return 0;
731

732
        iovec_len = MIN(6 + log_context_num_fields() * 3, IOVEC_MAX);
4,077,399✔
733
        iovec = newa(struct iovec, iovec_len);
4,077,399✔
734

735
        log_do_header(header, sizeof(header), level, error, file, line, func, object_field, object, extra_field, extra);
4,077,399✔
736

737
        iovec[n++] = IOVEC_MAKE_STRING(header);
4,077,399✔
738
        iovec[n++] = IOVEC_MAKE_STRING("MESSAGE=");
4,077,399✔
739
        if (log_prefix) {
4,077,399✔
740
                iovec[n++] = IOVEC_MAKE_STRING(log_prefix);
1,771,738✔
741
                iovec[n++] = IOVEC_MAKE_STRING(": ");
1,771,738✔
742
        }
743
        iovec[n++] = IOVEC_MAKE_STRING(buffer);
4,077,399✔
744
        iovec[n++] = IOVEC_MAKE_STRING("\n");
4,077,399✔
745

746
        log_do_context(iovec, iovec_len, &n);
4,077,399✔
747

748
        const struct msghdr msghdr = {
4,077,399✔
749
                .msg_iov = iovec,
750
                .msg_iovlen = n,
751
        };
752

753
        if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) < 0)
4,077,399✔
754
                return -errno;
210✔
755

756
        return 1;
757
}
758

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

771
        assert_raw(buffer);
4,358,295✔
772

773
        if (log_target == LOG_TARGET_NULL)
4,358,295✔
774
                return -ERRNO_VALUE(error);
10✔
775

776
        /* Patch in LOG_DAEMON facility if necessary */
777
        if (LOG_FAC(level) == 0)
4,358,285✔
778
                level |= log_facility;
4,349,725✔
779

780
        if (open_when_needed)
4,358,285✔
781
                (void) log_open();
143,275✔
782

783
        do {
4,411,579✔
784
                char *e;
4,411,579✔
785
                int k = 0;
4,411,579✔
786

787
                buffer += strspn(buffer, NEWLINE);
4,411,579✔
788

789
                if (buffer[0] == 0)
4,411,579✔
790
                        break;
791

792
                if ((e = strpbrk(buffer, NEWLINE)))
4,367,580✔
793
                        *(e++) = 0;
53,294✔
794

795
                if (IN_SET(log_target, LOG_TARGET_AUTO,
4,367,580✔
796
                                       LOG_TARGET_JOURNAL_OR_KMSG,
797
                                       LOG_TARGET_JOURNAL)) {
798

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

804
                if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
4,367,580✔
805
                                       LOG_TARGET_SYSLOG)) {
806

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

812
                if (k <= 0 &&
4,367,580✔
813
                    IN_SET(log_target, LOG_TARGET_AUTO,
290,211✔
814
                                       LOG_TARGET_SYSLOG_OR_KMSG,
815
                                       LOG_TARGET_JOURNAL_OR_KMSG,
816
                                       LOG_TARGET_KMSG)) {
817

818
                        if (k < 0)
217,645✔
819
                                log_open_kmsg();
204✔
820

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

828
                if (k <= 0)
290,211✔
829
                        (void) write_to_console(level, error, file, line, func, buffer);
248,583✔
830

831
                buffer = e;
4,367,580✔
832
        } while (buffer);
4,367,580✔
833

834
        if (open_when_needed)
4,358,285✔
835
                log_close();
143,275✔
836

837
        return -ERRNO_VALUE(error);
4,358,285✔
838
}
839

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

848
        PROTECT_ERRNO;
6✔
849

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

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

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

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

867
        if (_likely_(LOG_PRI(level) > log_max_level))
2,657,687✔
868
                return -ERRNO_VALUE(error);
2,657,687✔
869

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

874
        (void) vsnprintf(buffer, sizeof buffer, format, ap);
2,657,463✔
875

876
        return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buffer);
2,657,463✔
877
}
878

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

887
        va_list ap;
2,657,017✔
888
        int r;
2,657,017✔
889

890
        va_start(ap, format);
2,657,017✔
891
        r = log_internalv(level, error, file, line, func, format, ap);
2,657,017✔
892
        va_end(ap);
2,657,017✔
893

894
        return r;
2,657,017✔
895
}
896

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

910
        char *buffer, *b;
1,742,633✔
911

912
        if (_likely_(LOG_PRI(level) > log_max_level))
1,742,633✔
913
                return -ERRNO_VALUE(error);
1,742,633✔
914

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

918
        LOG_SET_PREFIX(object);
3,384,896✔
919

920
        b = buffer = newa(char, LINE_MAX);
1,692,448✔
921
        (void) vsnprintf(b, LINE_MAX, format, ap);
1,692,448✔
922

923
        return log_dispatch_internal(level, error, file, line, func,
1,692,448✔
924
                                     object_field, object, extra_field, extra, buffer);
925
}
926

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

939
        va_list ap;
1,738,346✔
940
        int r;
1,738,346✔
941

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

946
        return r;
1,738,346✔
947
}
948

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

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

962
        static const char nl = '\n';
42,773✔
963

964
        while (format && *n + 1 < iovec_len) {
247,159✔
965
                va_list aq;
204,386✔
966
                char *m;
204,386✔
967
                int r;
204,386✔
968

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

973
                errno = ERRNO_VALUE(error);
204,386✔
974

975
                va_copy(aq, ap);
204,386✔
976
                r = vasprintf(&m, format, aq);
204,386✔
977
                va_end(aq);
204,386✔
978
                if (r < 0)
204,386✔
UNCOV
979
                        return -EINVAL;
×
980

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

985
                iovec[(*n)++] = IOVEC_MAKE_STRING(m);
204,386✔
986
                if (newline_separator)
204,386✔
987
                        iovec[(*n)++] = IOVEC_MAKE((char *)&nl, 1);
202,918✔
988

989
                format = va_arg(ap, char *);
204,386✔
990
        }
991
        return 0;
992
}
993

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

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

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

1011
        if (LOG_FAC(level) == 0)
50,593✔
1012
                level |= log_facility;
50,593✔
1013

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

1019
                if (open_when_needed)
48,380✔
1020
                        log_open_journal();
7,413✔
1021

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

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

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

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

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

1052
                                (void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
42,621✔
1053
                        }
1054

1055
                        va_end(ap);
42,621✔
1056
                        for (size_t i = 1; i < m; i += 2)
245,539✔
1057
                                free(iovec[i].iov_base);
202,918✔
1058

1059
                        if (!fallback) {
42,621✔
1060
                                if (open_when_needed)
42,621✔
1061
                                        log_close();
7,400✔
1062

1063
                                return -ERRNO_VALUE(error);
42,621✔
1064
                        }
1065
                }
1066
        }
1067

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

1070
        va_start(ap, format);
7,972✔
1071
        while (format) {
22,113✔
1072
                va_list aq;
22,113✔
1073

1074
                errno = ERRNO_VALUE(error);
22,113✔
1075

1076
                va_copy(aq, ap);
22,113✔
1077
                DISABLE_WARNING_FORMAT_NONLITERAL;
22,113✔
1078
                (void) vsnprintf(buf, sizeof buf, format, aq);
22,113✔
1079
                REENABLE_WARNING;
22,113✔
1080
                va_end(aq);
22,113✔
1081

1082
                if (startswith(buf, "MESSAGE=")) {
22,113✔
1083
                        found = true;
7,972✔
1084
                        break;
7,972✔
1085
                }
1086

1087
                VA_FORMAT_ADVANCE(format, ap);
38,297✔
1088

1089
                format = va_arg(ap, char *);
14,141✔
1090
        }
1091
        va_end(ap);
7,972✔
1092

1093
        if (!found) {
7,972✔
1094
                if (open_when_needed)
×
UNCOV
1095
                        log_close();
×
1096

UNCOV
1097
                return -ERRNO_VALUE(error);
×
1098
        }
1099

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

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

1112
        PROTECT_ERRNO;
1,462✔
1113

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

1118
        if (LOG_FAC(level) == 0)
1,462✔
1119
                level |= log_facility;
1,462✔
1120

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

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

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

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

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

1141
                log_do_context(iovec, iovec_len, &n);
1,434✔
1142

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

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

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

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

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

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

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

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

1173
        log_set_target(t);
12,106✔
1174
        return 0;
12,106✔
1175
}
1176

1177
int log_set_max_level_from_string(const char *e) {
24,559✔
1178
        int r;
67,612✔
1179

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

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

1191
                colon = strchr(word, ':');
43,053✔
1192
                if (!colon) {
43,053✔
1193
                        r = log_level_from_string(word);
24,559✔
1194
                        if (r < 0)
24,559✔
1195
                                return r;
1196

1197
                        log_set_max_level(r);
24,559✔
1198
                        continue;
24,559✔
1199
                }
1200

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

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

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

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

1216
                log_target_max_level[target] = r;
18,494✔
1217
        }
1218

1219
        return 0;
24,559✔
1220
}
1221

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

1226
        assert(ret);
2,209✔
1227

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

1232
        for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
11,045✔
1233
                _cleanup_free_ char *l = NULL;
2,209✔
1234

1235
                if (log_target_max_level[target] == INT_MAX)
8,836✔
1236
                        continue;
6,627✔
1237

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

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

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

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

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

1258
        ratelimit_kmsg = r;
6,999✔
1259
        return 0;
6,999✔
1260
}
1261

1262
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
250,301✔
1263

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

1273
        if (streq(key, "debug") && !value)
250,301✔
UNCOV
1274
                log_set_max_level(LOG_DEBUG);
×
1275

1276
        else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
250,301✔
1277

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

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

1284
        } else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
250,301✔
1285

1286
                if (proc_cmdline_value_missing(key, value))
6,999✔
1287
                        return 0;
1288

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

1292
        } else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
243,302✔
1293

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

1297
        } else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
243,302✔
1298

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

1302
        } else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
243,302✔
1303

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

1307
        } else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
243,302✔
1308

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

1312
        } else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
243,302✔
1313

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

1318
        return 0;
1319
}
1320

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

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

1330
void log_parse_environment_variables(void) {
91,570✔
1331
        const char *e;
91,570✔
1332
        int r;
91,570✔
1333

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

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

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

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

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

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

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

1377
void log_parse_environment(void) {
89,705✔
1378
        /* Do not call from library code. */
1379

1380
        if (should_parse_proc_cmdline())
89,705✔
1381
                (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
7,072✔
1382

1383
        log_parse_environment_variables();
89,705✔
1384
}
89,705✔
1385

1386
LogTarget log_get_target(void) {
16,044✔
1387
        return log_target;
16,044✔
1388
}
1389

1390
void log_settle_target(void) {
14,121✔
1391

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

1398
        LogTarget t = log_get_target();
14,121✔
1399

1400
        if (t != LOG_TARGET_AUTO)
14,121✔
1401
                return;
1402

1403
        t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
9,598✔
1404
                                                        : LOG_TARGET_CONSOLE;
6,355✔
1405
        log_set_target(t);
3,243✔
1406
}
1407

1408
int log_get_max_level(void) {
12,780,044✔
1409
        return log_max_level;
12,780,044✔
1410
}
1411

1412
void log_show_color(bool b) {
81,236✔
1413
        show_color = b;
81,236✔
1414
}
81,236✔
1415

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1493
        return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
128,075✔
1494
}
1495

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

1508
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
35,132✔
1509

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

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

1516
                (void) pid_get_comm(si->ssi_pid, &p);
3,642✔
1517

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

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

1532
        log_syntax_callback = cb;
856✔
1533
        log_syntax_callback_userdata = userdata;
856✔
1534
}
856✔
1535

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

1547
        PROTECT_ERRNO;
11,531✔
1548

1549
        if (log_syntax_callback)
11,531✔
1550
                log_syntax_callback(unit, level, log_syntax_callback_userdata);
846✔
1551

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

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

1560
        errno = ERRNO_VALUE(error);
11,501✔
1561

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

1566
        if (unit)
11,501✔
1567
                unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
4,883✔
1568

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

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

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

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

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

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

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

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

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

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

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

1669
void log_set_upgrade_syslog_to_journal(bool b) {
233✔
1670
        upgrade_syslog_to_journal = b;
233✔
1671

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

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

1685
void log_set_open_when_needed(bool b) {
18,166✔
1686
        open_when_needed = b;
18,166✔
1687
}
18,166✔
1688

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

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

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

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

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

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

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

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

1717
void log_setup(void) {
88,902✔
1718
        log_set_target(LOG_TARGET_AUTO);
88,902✔
1719
        log_parse_environment();
88,902✔
1720
        (void) log_open();
88,902✔
1721
        if (log_on_console() && show_color < 0)
88,902✔
1722
                log_show_color(true);
81,133✔
1723
}
88,902✔
1724

1725
const char* _log_set_prefix(const char *prefix, bool force) {
3,408,486✔
1726
        const char *old = log_prefix;
3,408,486✔
1727

1728
        if (prefix || force)
3,408,486✔
1729
                log_prefix = prefix;
3,406,646✔
1730

1731
        return old;
3,408,486✔
1732
}
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