]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-console.c
2 This file is part of systemd.
4 Copyright 2011 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/socket.h>
24 #include "alloc-util.h"
27 #include "format-util.h"
29 #include "journald-console.h"
30 #include "journald-server.h"
31 #include "parse-util.h"
32 #include "process-util.h"
33 #include "stdio-util.h"
34 #include "terminal-util.h"
36 static bool prefix_timestamp(void) {
38 static int cached_printk_time
= -1;
40 if (_unlikely_(cached_printk_time
< 0)) {
41 _cleanup_free_
char *p
= NULL
;
44 read_one_line_file("/sys/module/printk/parameters/time", &p
) >= 0
45 && parse_boolean(p
) > 0;
48 return cached_printk_time
;
51 void server_forward_console(
54 const char *identifier
,
56 const struct ucred
*ucred
) {
58 struct iovec iovec
[5];
60 char tbuf
[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts
.tv_sec
) + DECIMAL_STR_MAX(ts
.tv_nsec
)-3 + 1];
61 char header_pid
[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t
)];
63 _cleanup_free_
char *ident_buf
= NULL
;
69 if (LOG_PRI(priority
) > s
->max_level_console
)
72 /* First: timestamp */
73 if (prefix_timestamp()) {
74 assert_se(clock_gettime(CLOCK_MONOTONIC
, &ts
) == 0);
75 xsprintf(tbuf
, "[%5"PRI_TIME
".%06"PRI_NSEC
"] ",
77 (nsec_t
)ts
.tv_nsec
/ 1000);
78 IOVEC_SET_STRING(iovec
[n
++], tbuf
);
81 /* Second: identifier and PID */
84 get_process_comm(ucred
->pid
, &ident_buf
);
85 identifier
= ident_buf
;
88 xsprintf(header_pid
, "["PID_FMT
"]: ", ucred
->pid
);
91 IOVEC_SET_STRING(iovec
[n
++], identifier
);
93 IOVEC_SET_STRING(iovec
[n
++], header_pid
);
94 } else if (identifier
) {
95 IOVEC_SET_STRING(iovec
[n
++], identifier
);
96 IOVEC_SET_STRING(iovec
[n
++], ": ");
100 IOVEC_SET_STRING(iovec
[n
++], message
);
101 IOVEC_SET_STRING(iovec
[n
++], "\n");
103 tty
= s
->tty_path
? s
->tty_path
: "/dev/console";
105 /* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a
106 * good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely,
107 * but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things
108 * easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */
110 fd
= open_terminal(tty
, O_WRONLY
|O_NOCTTY
|O_CLOEXEC
);
112 log_debug_errno(fd
, "Failed to open %s for logging: %m", tty
);
116 if (writev(fd
, iovec
, n
) < 0)
117 log_debug_errno(errno
, "Failed to write to %s for logging: %m", tty
);