]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 MS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2011 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <fcntl.h> | |
663996b3 | 22 | #include <sys/socket.h> |
db2df898 | 23 | #include <time.h> |
663996b3 | 24 | |
db2df898 MP |
25 | #include "alloc-util.h" |
26 | #include "fd-util.h" | |
60f067b4 | 27 | #include "fileio.h" |
2897b343 | 28 | #include "format-util.h" |
db2df898 MP |
29 | #include "io-util.h" |
30 | #include "journald-console.h" | |
31 | #include "journald-server.h" | |
32 | #include "parse-util.h" | |
e3bff60a | 33 | #include "process-util.h" |
db2df898 | 34 | #include "stdio-util.h" |
e3bff60a | 35 | #include "terminal-util.h" |
663996b3 | 36 | |
60f067b4 JS |
37 | static bool prefix_timestamp(void) { |
38 | ||
39 | static int cached_printk_time = -1; | |
40 | ||
41 | if (_unlikely_(cached_printk_time < 0)) { | |
42 | _cleanup_free_ char *p = NULL; | |
43 | ||
44 | cached_printk_time = | |
45 | read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0 | |
46 | && parse_boolean(p) > 0; | |
47 | } | |
48 | ||
49 | return cached_printk_time; | |
50 | } | |
51 | ||
663996b3 MS |
52 | void server_forward_console( |
53 | Server *s, | |
54 | int priority, | |
55 | const char *identifier, | |
56 | const char *message, | |
f47781d8 | 57 | const struct ucred *ucred) { |
663996b3 | 58 | |
60f067b4 | 59 | struct iovec iovec[5]; |
60f067b4 | 60 | struct timespec ts; |
52ad194e MB |
61 | char tbuf[STRLEN("[] ") + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1]; |
62 | char header_pid[STRLEN("[]: ") + DECIMAL_STR_MAX(pid_t)]; | |
60f067b4 | 63 | _cleanup_free_ char *ident_buf = NULL; |
f5e65279 | 64 | _cleanup_close_ int fd = -1; |
663996b3 | 65 | const char *tty; |
f5e65279 | 66 | int n = 0; |
663996b3 MS |
67 | |
68 | assert(s); | |
69 | assert(message); | |
70 | ||
71 | if (LOG_PRI(priority) > s->max_level_console) | |
72 | return; | |
73 | ||
60f067b4 JS |
74 | /* First: timestamp */ |
75 | if (prefix_timestamp()) { | |
76 | assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0); | |
81c58355 | 77 | xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ", |
60f067b4 | 78 | ts.tv_sec, |
81c58355 | 79 | (nsec_t)ts.tv_nsec / 1000); |
f5e65279 MB |
80 | |
81 | iovec[n++] = IOVEC_MAKE_STRING(tbuf); | |
60f067b4 JS |
82 | } |
83 | ||
84 | /* Second: identifier and PID */ | |
663996b3 MS |
85 | if (ucred) { |
86 | if (!identifier) { | |
87 | get_process_comm(ucred->pid, &ident_buf); | |
88 | identifier = ident_buf; | |
89 | } | |
90 | ||
e735f4d4 | 91 | xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); |
663996b3 MS |
92 | |
93 | if (identifier) | |
f5e65279 | 94 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
663996b3 | 95 | |
f5e65279 | 96 | iovec[n++] = IOVEC_MAKE_STRING(header_pid); |
663996b3 | 97 | } else if (identifier) { |
f5e65279 MB |
98 | iovec[n++] = IOVEC_MAKE_STRING(identifier); |
99 | iovec[n++] = IOVEC_MAKE_STRING(": "); | |
663996b3 MS |
100 | } |
101 | ||
60f067b4 | 102 | /* Fourth: message */ |
f5e65279 MB |
103 | iovec[n++] = IOVEC_MAKE_STRING(message); |
104 | iovec[n++] = IOVEC_MAKE_STRING("\n"); | |
663996b3 | 105 | |
f5e65279 | 106 | tty = s->tty_path ?: "/dev/console"; |
663996b3 | 107 | |
8a584da2 MP |
108 | /* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a |
109 | * good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely, | |
110 | * but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things | |
111 | * easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */ | |
112 | ||
663996b3 MS |
113 | fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC); |
114 | if (fd < 0) { | |
db2df898 | 115 | log_debug_errno(fd, "Failed to open %s for logging: %m", tty); |
60f067b4 | 116 | return; |
663996b3 MS |
117 | } |
118 | ||
119 | if (writev(fd, iovec, n) < 0) | |
f47781d8 | 120 | log_debug_errno(errno, "Failed to write to %s for logging: %m", tty); |
663996b3 | 121 | } |