]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-console.c
io-util: add new IOVEC_INIT/IOVEC_MAKE macros
[thirdparty/systemd.git] / src / journal / journald-console.c
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>
21 #include <sys/socket.h>
22 #include <time.h>
23
24 #include "alloc-util.h"
25 #include "fd-util.h"
26 #include "fileio.h"
27 #include "format-util.h"
28 #include "io-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"
35
36 static 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
51 void server_forward_console(
52 Server *s,
53 int priority,
54 const char *identifier,
55 const char *message,
56 const struct ucred *ucred) {
57
58 struct iovec iovec[5];
59 struct timespec ts;
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)];
62 _cleanup_free_ char *ident_buf = NULL;
63 _cleanup_close_ int fd = -1;
64 const char *tty;
65 int n = 0;
66
67 assert(s);
68 assert(message);
69
70 if (LOG_PRI(priority) > s->max_level_console)
71 return;
72
73 /* First: timestamp */
74 if (prefix_timestamp()) {
75 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
76 xsprintf(tbuf, "[%5"PRI_TIME".%06"PRI_NSEC"] ",
77 ts.tv_sec,
78 (nsec_t)ts.tv_nsec / 1000);
79
80 iovec[n++] = IOVEC_MAKE_STRING(tbuf);
81 }
82
83 /* Second: identifier and PID */
84 if (ucred) {
85 if (!identifier) {
86 get_process_comm(ucred->pid, &ident_buf);
87 identifier = ident_buf;
88 }
89
90 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
91
92 if (identifier)
93 iovec[n++] = IOVEC_MAKE_STRING(identifier);
94
95 iovec[n++] = IOVEC_MAKE_STRING(header_pid);
96 } else if (identifier) {
97 iovec[n++] = IOVEC_MAKE_STRING(identifier);
98 iovec[n++] = IOVEC_MAKE_STRING(": ");
99 }
100
101 /* Fourth: message */
102 iovec[n++] = IOVEC_MAKE_STRING(message);
103 iovec[n++] = IOVEC_MAKE_STRING("\n");
104
105 tty = s->tty_path ?: "/dev/console";
106
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
112 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
113 if (fd < 0) {
114 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
115 return;
116 }
117
118 if (writev(fd, iovec, n) < 0)
119 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
120 }