Debian Codegraph @

src/journal/journald-console.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <sys/socket.h>
5 #include <time.h>
6
7 #include "alloc-util.h"
8 #include "fd-util.h"
9 #include "fileio.h"
10 #include "format-util.h"
11 #include "iovec-util.h"
12 #include "journald-console.h"
13 #include "journald-manager.h"
14 #include "log.h"
15 #include "process-util.h"
16 #include "stdio-util.h"
17 #include "terminal-util.h"
18 #include "time-util.h"
19
20 static bool prefix_timestamp(void) {
21
22 static int cached_printk_time = -1;
23
24 if (_unlikely_(cached_printk_time < 0))
25 cached_printk_time = read_boolean_file("/sys/module/printk/parameters/time") > 0;
26
27 return cached_printk_time;
28 }
29
30 void manager_forward_console(
31 Manager *m,
32 int priority,
33 const char *identifier,
34 const char *message,
35 const struct ucred *ucred) {
36
37 struct iovec iovec[7];
38 struct timespec ts;
39 char tbuf[STRLEN("[] ") + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
40 char header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t)];
41 _cleanup_free_ char *ident_buf = NULL;
42 _cleanup_close_ int fd = -EBADF;
43 const char *tty, *color_on = "", *color_off = "";
44 int n = 0;
45
46 assert(m);
47 assert(message);
48
49 if (LOG_PRI(priority) > m->config.max_level_console)
50 return;
51
52 /* First: timestamp */
53 if (prefix_timestamp()) {
54 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
55 xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ",
56 ts.tv_sec,
57 (nsec_t)ts.tv_nsec / 1000);
58
59 iovec[n++] = IOVEC_MAKE_STRING(tbuf);
60 }
61
62 /* Second: identifier and PID */
63 if (ucred) {
64 if (!identifier) {
65 (void) pid_get_comm(ucred->pid, &ident_buf);
66 identifier = ident_buf;
67 }
68
69 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
70
71 if (identifier)
72 iovec[n++] = IOVEC_MAKE_STRING(identifier);
73
74 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
75 } else if (identifier) {
76 iovec[n++] = IOVEC_MAKE_STRING(identifier);
77 iovec[n++] = IOVEC_MAKE_STRING(": ");
78 }
79
80 get_log_colors(LOG_PRI(priority), &color_on, &color_off, NULL);
81
82 /* Fourth: message */
83 iovec[n++] = IOVEC_MAKE_STRING(color_on);
84 iovec[n++] = IOVEC_MAKE_STRING(message);
85 iovec[n++] = IOVEC_MAKE_STRING(color_off);
86 iovec[n++] = IOVEC_MAKE_STRING("\n");
87
88 tty = m->config.tty_path ?: "/dev/console";
89
90 /* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a
91 * good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely,
92 * but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things
93 * easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */
94
95 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
96 if (fd < 0) {
97 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
98 return;
99 }
100
101 if (writev(fd, iovec, n) < 0)
102 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
103 }