]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/journald-console.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / journal / journald-console.c
CommitLineData
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
22#include <fcntl.h>
4871690d 23#include <sys/socket.h>
cf0fbc49 24#include <time.h>
3b7124a8 25
b5efdb8a 26#include "alloc-util.h"
6bedfcbb 27#include "fd-util.h"
ad79565d 28#include "fileio.h"
6482f626 29#include "formats-util.h"
afc5dbf3 30#include "io-util.h"
6bedfcbb
LP
31#include "journald-console.h"
32#include "journald-server.h"
33#include "parse-util.h"
0b452006 34#include "process-util.h"
15a5e950 35#include "stdio-util.h"
288a74cc 36#include "terminal-util.h"
3b7124a8 37
ad79565d
UTL
38static bool prefix_timestamp(void) {
39
40 static int cached_printk_time = -1;
41
42 if (_unlikely_(cached_printk_time < 0)) {
43 _cleanup_free_ char *p = NULL;
44
45 cached_printk_time =
46 read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
47 && parse_boolean(p) > 0;
48 }
49
50 return cached_printk_time;
51}
52
3b7124a8
LP
53void server_forward_console(
54 Server *s,
55 int priority,
56 const char *identifier,
57 const char *message,
3b3154df 58 const struct ucred *ucred) {
3b7124a8 59
ad79565d 60 struct iovec iovec[5];
ad79565d 61 struct timespec ts;
5ffa8c81
ZJS
62 char tbuf[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
63 char header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t)];
3b7124a8 64 int n = 0, fd;
fb472900 65 _cleanup_free_ char *ident_buf = NULL;
3b7124a8
LP
66 const char *tty;
67
68 assert(s);
69 assert(message);
70
71 if (LOG_PRI(priority) > s->max_level_console)
72 return;
73
ad79565d
UTL
74 /* First: timestamp */
75 if (prefix_timestamp()) {
76 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
5ffa8c81 77 xsprintf(tbuf, "[%5"PRI_TIME".%06ld] ",
de0671ee
ZJS
78 ts.tv_sec,
79 ts.tv_nsec / 1000);
ad79565d
UTL
80 IOVEC_SET_STRING(iovec[n++], tbuf);
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)
93 IOVEC_SET_STRING(iovec[n++], identifier);
94
95 IOVEC_SET_STRING(iovec[n++], header_pid);
96 } else if (identifier) {
97 IOVEC_SET_STRING(iovec[n++], identifier);
98 IOVEC_SET_STRING(iovec[n++], ": ");
99 }
100
ad79565d 101 /* Fourth: message */
3b7124a8
LP
102 IOVEC_SET_STRING(iovec[n++], message);
103 IOVEC_SET_STRING(iovec[n++], "\n");
104
105 tty = s->tty_path ? s->tty_path : "/dev/console";
106
107 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
108 if (fd < 0) {
709f6e46 109 log_debug_errno(fd, "Failed to open %s for logging: %m", tty);
fb472900 110 return;
3b7124a8
LP
111 }
112
113 if (writev(fd, iovec, n) < 0)
56f64d95 114 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
3b7124a8 115
03e334a1 116 safe_close(fd);
3b7124a8 117}