]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-console.c
Merge pull request #5960 from keszybz/journald-memleak
[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 int n = 0, fd;
63 _cleanup_free_ char *ident_buf = NULL;
64 const char *tty;
65
66 assert(s);
67 assert(message);
68
69 if (LOG_PRI(priority) > s->max_level_console)
70 return;
71
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"] ",
76 ts.tv_sec,
77 (nsec_t)ts.tv_nsec / 1000);
78 IOVEC_SET_STRING(iovec[n++], tbuf);
79 }
80
81 /* Second: identifier and PID */
82 if (ucred) {
83 if (!identifier) {
84 get_process_comm(ucred->pid, &ident_buf);
85 identifier = ident_buf;
86 }
87
88 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
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
99 /* Fourth: message */
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
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
110 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
111 if (fd < 0) {
112 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
113 return;
114 }
115
116 if (writev(fd, iovec, n) < 0)
117 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
118
119 safe_close(fd);
120 }