]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-console.c
Merge pull request #5990 from keszybz/logind
[thirdparty/systemd.git] / src / journal / journald-console.c
CommitLineData
3b7124a8
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2011 Lennart Poettering
5
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.
10
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.
15
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/>.
18***/
19
20#include <fcntl.h>
4871690d 21#include <sys/socket.h>
cf0fbc49 22#include <time.h>
3b7124a8 23
b5efdb8a 24#include "alloc-util.h"
6bedfcbb 25#include "fd-util.h"
ad79565d 26#include "fileio.h"
f97b34a6 27#include "format-util.h"
afc5dbf3 28#include "io-util.h"
6bedfcbb
LP
29#include "journald-console.h"
30#include "journald-server.h"
31#include "parse-util.h"
0b452006 32#include "process-util.h"
15a5e950 33#include "stdio-util.h"
288a74cc 34#include "terminal-util.h"
3b7124a8 35
ad79565d
UTL
36static bool prefix_timestamp(void) {
37
38 static int cached_printk_time = -1;
39
40 if (_unlikely_(cached_printk_time < 0)) {
41 _cleanup_free_ char *p = NULL;
42
43 cached_printk_time =
44 read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
45 && parse_boolean(p) > 0;
46 }
47
48 return cached_printk_time;
49}
50
3b7124a8
LP
51void server_forward_console(
52 Server *s,
53 int priority,
54 const char *identifier,
55 const char *message,
3b3154df 56 const struct ucred *ucred) {
3b7124a8 57
ad79565d 58 struct iovec iovec[5];
ad79565d 59 struct timespec ts;
5ffa8c81
ZJS
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)];
3b7124a8 62 int n = 0, fd;
fb472900 63 _cleanup_free_ char *ident_buf = NULL;
3b7124a8
LP
64 const char *tty;
65
66 assert(s);
67 assert(message);
68
69 if (LOG_PRI(priority) > s->max_level_console)
70 return;
71
ad79565d
UTL
72 /* First: timestamp */
73 if (prefix_timestamp()) {
74 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
b123d975 75 xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ",
de0671ee 76 ts.tv_sec,
b123d975 77 (nsec_t)ts.tv_nsec / 1000);
ad79565d
UTL
78 IOVEC_SET_STRING(iovec[n++], tbuf);
79 }
80
81 /* Second: identifier and PID */
3b7124a8
LP
82 if (ucred) {
83 if (!identifier) {
84 get_process_comm(ucred->pid, &ident_buf);
85 identifier = ident_buf;
86 }
87
5ffa8c81 88 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
3b7124a8
LP
89
90 if (identifier)
91 IOVEC_SET_STRING(iovec[n++], identifier);
92
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++], ": ");
97 }
98
ad79565d 99 /* Fourth: message */
3b7124a8
LP
100 IOVEC_SET_STRING(iovec[n++], message);
101 IOVEC_SET_STRING(iovec[n++], "\n");
102
103 tty = s->tty_path ? s->tty_path : "/dev/console";
104
8ae2c630
LP
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. */
109
3b7124a8
LP
110 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
111 if (fd < 0) {
709f6e46 112 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
fb472900 113 return;
3b7124a8
LP
114 }
115
116 if (writev(fd, iovec, n) < 0)
56f64d95 117 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
3b7124a8 118
03e334a1 119 safe_close(fd);
3b7124a8 120}