]>
Commit | Line | Data |
---|---|---|
3b7124a8 LP |
1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
2 | ||
3 | /*** | |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2011 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU Lesser General Public License as published by | |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | Lesser General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU Lesser General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
ad79565d | 22 | #include <time.h> |
3b7124a8 | 23 | #include <fcntl.h> |
4871690d | 24 | #include <sys/socket.h> |
3b7124a8 | 25 | |
6bedfcbb | 26 | #include "fd-util.h" |
ad79565d | 27 | #include "fileio.h" |
6482f626 | 28 | #include "formats-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" |
288a74cc | 33 | #include "terminal-util.h" |
3b7124a8 | 34 | |
ad79565d UTL |
35 | static bool prefix_timestamp(void) { |
36 | ||
37 | static int cached_printk_time = -1; | |
38 | ||
39 | if (_unlikely_(cached_printk_time < 0)) { | |
40 | _cleanup_free_ char *p = NULL; | |
41 | ||
42 | cached_printk_time = | |
43 | read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0 | |
44 | && parse_boolean(p) > 0; | |
45 | } | |
46 | ||
47 | return cached_printk_time; | |
48 | } | |
49 | ||
3b7124a8 LP |
50 | void server_forward_console( |
51 | Server *s, | |
52 | int priority, | |
53 | const char *identifier, | |
54 | const char *message, | |
3b3154df | 55 | const struct ucred *ucred) { |
3b7124a8 | 56 | |
ad79565d | 57 | struct iovec iovec[5]; |
ad79565d | 58 | struct timespec ts; |
5ffa8c81 ZJS |
59 | char tbuf[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1]; |
60 | char header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t)]; | |
3b7124a8 | 61 | int n = 0, fd; |
fb472900 | 62 | _cleanup_free_ char *ident_buf = NULL; |
3b7124a8 LP |
63 | const char *tty; |
64 | ||
65 | assert(s); | |
66 | assert(message); | |
67 | ||
68 | if (LOG_PRI(priority) > s->max_level_console) | |
69 | return; | |
70 | ||
ad79565d UTL |
71 | /* First: timestamp */ |
72 | if (prefix_timestamp()) { | |
73 | assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0); | |
5ffa8c81 | 74 | xsprintf(tbuf, "[%5"PRI_TIME".%06ld] ", |
de0671ee ZJS |
75 | ts.tv_sec, |
76 | ts.tv_nsec / 1000); | |
ad79565d UTL |
77 | IOVEC_SET_STRING(iovec[n++], tbuf); |
78 | } | |
79 | ||
80 | /* Second: identifier and PID */ | |
3b7124a8 LP |
81 | if (ucred) { |
82 | if (!identifier) { | |
83 | get_process_comm(ucred->pid, &ident_buf); | |
84 | identifier = ident_buf; | |
85 | } | |
86 | ||
5ffa8c81 | 87 | xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid); |
3b7124a8 LP |
88 | |
89 | if (identifier) | |
90 | IOVEC_SET_STRING(iovec[n++], identifier); | |
91 | ||
92 | IOVEC_SET_STRING(iovec[n++], header_pid); | |
93 | } else if (identifier) { | |
94 | IOVEC_SET_STRING(iovec[n++], identifier); | |
95 | IOVEC_SET_STRING(iovec[n++], ": "); | |
96 | } | |
97 | ||
ad79565d | 98 | /* Fourth: message */ |
3b7124a8 LP |
99 | IOVEC_SET_STRING(iovec[n++], message); |
100 | IOVEC_SET_STRING(iovec[n++], "\n"); | |
101 | ||
102 | tty = s->tty_path ? s->tty_path : "/dev/console"; | |
103 | ||
104 | fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC); | |
105 | if (fd < 0) { | |
56f64d95 | 106 | log_debug_errno(errno, "Failed to open %s for logging: %m", tty); |
fb472900 | 107 | return; |
3b7124a8 LP |
108 | } |
109 | ||
110 | if (writev(fd, iovec, n) < 0) | |
56f64d95 | 111 | log_debug_errno(errno, "Failed to write to %s for logging: %m", tty); |
3b7124a8 | 112 | |
03e334a1 | 113 | safe_close(fd); |
3b7124a8 | 114 | } |