]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-console.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2011 Lennart Poettering
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.
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.
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/>.
22 #include <sys/socket.h>
25 #include "alloc-util.h"
28 #include "format-util.h"
30 #include "journald-console.h"
31 #include "journald-server.h"
32 #include "parse-util.h"
33 #include "process-util.h"
34 #include "stdio-util.h"
35 #include "terminal-util.h"
37 static bool prefix_timestamp(void) {
39 static int cached_printk_time
= -1;
41 if (_unlikely_(cached_printk_time
< 0)) {
42 _cleanup_free_
char *p
= NULL
;
45 read_one_line_file("/sys/module/printk/parameters/time", &p
) >= 0
46 && parse_boolean(p
) > 0;
49 return cached_printk_time
;
52 void server_forward_console(
55 const char *identifier
,
57 const struct ucred
*ucred
) {
59 struct iovec iovec
[5];
61 char tbuf
[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts
.tv_sec
) + DECIMAL_STR_MAX(ts
.tv_nsec
)-3 + 1];
62 char header_pid
[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t
)];
63 _cleanup_free_
char *ident_buf
= NULL
;
64 _cleanup_close_
int fd
= -1;
71 if (LOG_PRI(priority
) > s
->max_level_console
)
74 /* First: timestamp */
75 if (prefix_timestamp()) {
76 assert_se(clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0);
77 xsprintf(tbuf
, "[%5"PRI_TIME
".%06"PRI_NSEC
"] ",
79 (nsec_t
)ts
.tv_nsec
/ 1000);
81 iovec
[n
++] = IOVEC_MAKE_STRING(tbuf
);
84 /* Second: identifier and PID */
87 get_process_comm(ucred
->pid
, &ident_buf
);
88 identifier
= ident_buf
;
91 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
94 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
96 iovec
[n
++] = IOVEC_MAKE_STRING(header_pid
);
97 } else if (identifier
) {
98 iovec
[n
++] = IOVEC_MAKE_STRING(identifier
);
99 iovec
[n
++] = IOVEC_MAKE_STRING(": ");
102 /* Fourth: message */
103 iovec
[n
++] = IOVEC_MAKE_STRING(message
);
104 iovec
[n
++] = IOVEC_MAKE_STRING("\n");
106 tty
= s
->tty_path
?: "/dev/console";
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. */
113 fd
= open_terminal(tty
, O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
115 log_debug_errno(fd
, "Failed to open %s for logging: %m", tty
);
119 if (writev(fd
, iovec
, n
) < 0)
120 log_debug_errno(errno
, "Failed to write to %s for logging: %m", tty
);