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