]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/journald-console.c
shared: add process-util.[ch]
[thirdparty/systemd.git] / src / journal / journald-console.c
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 <time.h>
23 #include <fcntl.h>
24 #include <sys/socket.h>
25
26 #include "fileio.h"
27 #include "journald-server.h"
28 #include "journald-console.h"
29 #include "formats-util.h"
30 #include "process-util.h"
31
32 static bool prefix_timestamp(void) {
33
34 static int cached_printk_time = -1;
35
36 if (_unlikely_(cached_printk_time < 0)) {
37 _cleanup_free_ char *p = NULL;
38
39 cached_printk_time =
40 read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
41 && parse_boolean(p) > 0;
42 }
43
44 return cached_printk_time;
45 }
46
47 void server_forward_console(
48 Server *s,
49 int priority,
50 const char *identifier,
51 const char *message,
52 const struct ucred *ucred) {
53
54 struct iovec iovec[5];
55 struct timespec ts;
56 char tbuf[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
57 char header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t)];
58 int n = 0, fd;
59 _cleanup_free_ char *ident_buf = NULL;
60 const char *tty;
61
62 assert(s);
63 assert(message);
64
65 if (LOG_PRI(priority) > s->max_level_console)
66 return;
67
68 /* First: timestamp */
69 if (prefix_timestamp()) {
70 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
71 xsprintf(tbuf, "[%5"PRI_TIME".%06ld] ",
72 ts.tv_sec,
73 ts.tv_nsec / 1000);
74 IOVEC_SET_STRING(iovec[n++], tbuf);
75 }
76
77 /* Second: identifier and PID */
78 if (ucred) {
79 if (!identifier) {
80 get_process_comm(ucred->pid, &ident_buf);
81 identifier = ident_buf;
82 }
83
84 xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
85
86 if (identifier)
87 IOVEC_SET_STRING(iovec[n++], identifier);
88
89 IOVEC_SET_STRING(iovec[n++], header_pid);
90 } else if (identifier) {
91 IOVEC_SET_STRING(iovec[n++], identifier);
92 IOVEC_SET_STRING(iovec[n++], ": ");
93 }
94
95 /* Fourth: message */
96 IOVEC_SET_STRING(iovec[n++], message);
97 IOVEC_SET_STRING(iovec[n++], "\n");
98
99 tty = s->tty_path ? s->tty_path : "/dev/console";
100
101 fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
102 if (fd < 0) {
103 log_debug_errno(errno, "Failed to open %s for logging: %m", tty);
104 return;
105 }
106
107 if (writev(fd, iovec, n) < 0)
108 log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
109
110 safe_close(fd);
111 }