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