]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-console.c
io-util: add new IOVEC_INIT/IOVEC_MAKE macros
[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)];
fb472900 62 _cleanup_free_ char *ident_buf = NULL;
e6a7ec4b 63 _cleanup_close_ int fd = -1;
3b7124a8 64 const char *tty;
e6a7ec4b 65 int n = 0;
3b7124a8
LP
66
67 assert(s);
68 assert(message);
69
70 if (LOG_PRI(priority) > s->max_level_console)
71 return;
72
ad79565d
UTL
73 /* First: timestamp */
74 if (prefix_timestamp()) {
75 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
b123d975 76 xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ",
de0671ee 77 ts.tv_sec,
b123d975 78 (nsec_t)ts.tv_nsec / 1000);
e6a7ec4b
LP
79
80 iovec[n++] = IOVEC_MAKE_STRING(tbuf);
ad79565d
UTL
81 }
82
83 /* Second: identifier and PID */
3b7124a8
LP
84 if (ucred) {
85 if (!identifier) {
86 get_process_comm(ucred->pid, &ident_buf);
87 identifier = ident_buf;
88 }
89
5ffa8c81 90 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
3b7124a8
LP
91
92 if (identifier)
e6a7ec4b 93 iovec[n++] = IOVEC_MAKE_STRING(identifier);
3b7124a8 94
e6a7ec4b 95 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
3b7124a8 96 } else if (identifier) {
e6a7ec4b
LP
97 iovec[n++] = IOVEC_MAKE_STRING(identifier);
98 iovec[n++] = IOVEC_MAKE_STRING(": ");
3b7124a8
LP
99 }
100
ad79565d 101 /* Fourth: message */
e6a7ec4b
LP
102 iovec[n++] = IOVEC_MAKE_STRING(message);
103 iovec[n++] = IOVEC_MAKE_STRING("\n");
3b7124a8 104
e6a7ec4b 105 tty = s->tty_path ?: "/dev/console";
3b7124a8 106
8ae2c630
LP
107 /* Before you ask: yes, on purpose we open/close the console for each log line we write individually. This is a
108 * good strategy to avoid journald getting killed by the kernel's SAK concept (it doesn't fix this entirely,
109 * but minimizes the time window the kernel might end up killing journald due to SAK). It also makes things
110 * easier for us so that we don't have to recover from hangups and suchlike triggered on the console. */
111
3b7124a8
LP
112 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
113 if (fd < 0) {
709f6e46 114 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
fb472900 115 return;
3b7124a8
LP
116 }
117
118 if (writev(fd, iovec, n) < 0)
56f64d95 119 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
3b7124a8 120}