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