chiark / gitweb /
journal: also consider audit fields with '-' valid
[elogind.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 <unistd.h>
25 #include <sys/socket.h>
26
27 #include "fileio.h"
28 #include "journald-server.h"
29 #include "journald-console.h"
30
31 static bool prefix_timestamp(void) {
32
33         static int cached_printk_time = -1;
34
35         if (_unlikely_(cached_printk_time < 0)) {
36                 _cleanup_free_ char *p = NULL;
37
38                 cached_printk_time =
39                         read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
40                         && parse_boolean(p) > 0;
41         }
42
43         return cached_printk_time;
44 }
45
46 void server_forward_console(
47                 Server *s,
48                 int priority,
49                 const char *identifier,
50                 const char *message,
51                 const struct ucred *ucred) {
52
53         struct iovec iovec[5];
54         char header_pid[16];
55         struct timespec ts;
56         char tbuf[4 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
57         int n = 0, fd;
58         _cleanup_free_ char *ident_buf = NULL;
59         const char *tty;
60
61         assert(s);
62         assert(message);
63
64         if (LOG_PRI(priority) > s->max_level_console)
65                 return;
66
67         /* First: timestamp */
68         if (prefix_timestamp()) {
69                 assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
70                 snprintf(tbuf, sizeof(tbuf), "[%5"PRI_TIME".%06ld] ",
71                          ts.tv_sec,
72                          ts.tv_nsec / 1000);
73                 IOVEC_SET_STRING(iovec[n++], tbuf);
74         }
75
76         /* Second: identifier and PID */
77         if (ucred) {
78                 if (!identifier) {
79                         get_process_comm(ucred->pid, &ident_buf);
80                         identifier = ident_buf;
81                 }
82
83                 snprintf(header_pid, sizeof(header_pid), "["PID_FMT"]: ", ucred->pid);
84                 char_array_0(header_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("Failed to open %s for logging: %m", tty);
104                 return;
105         }
106
107         if (writev(fd, iovec, n) < 0)
108                 log_debug("Failed to write to %s for logging: %m", tty);
109
110         safe_close(fd);
111 }