journald-console.c revision 6bedfcbb2970e06a4d3280c8fb62083d252ede73
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering/***
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering This file is part of systemd.
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering Copyright 2011 Lennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering systemd is free software; you can redistribute it and/or modify it
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering under the terms of the GNU Lesser General Public License as published by
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering (at your option) any later version.
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering systemd is distributed in the hope that it will be useful, but
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering Lesser General Public License for more details.
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering You should have received a copy of the GNU Lesser General Public License
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering***/
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <time.h>
a0f29c767a3bb3d621c658fa5b87063e1f44e24aLennart Poettering#include <fcntl.h>
a0f29c767a3bb3d621c658fa5b87063e1f44e24aLennart Poettering#include <sys/socket.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
a0f29c767a3bb3d621c658fa5b87063e1f44e24aLennart Poettering#include "fd-util.h"
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering#include "fileio.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering#include "formats-util.h"
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering#include "journald-console.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering#include "journald-server.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering#include "parse-util.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering#include "process-util.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering#include "terminal-util.h"
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poetteringstatic bool prefix_timestamp(void) {
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering static int cached_printk_time = -1;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani if (_unlikely_(cached_printk_time < 0)) {
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering _cleanup_free_ char *p = NULL;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering cached_printk_time =
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering read_one_line_file("/sys/module/printk/parameters/time", &p) >= 0
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani && parse_boolean(p) > 0;
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani }
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani return cached_printk_time;
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani}
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poetteringvoid server_forward_console(
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering Server *s,
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani int priority,
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering const char *identifier,
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering const char *message,
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering const struct ucred *ucred) {
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering struct iovec iovec[5];
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering struct timespec ts;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering char tbuf[sizeof("[] ")-1 + DECIMAL_STR_MAX(ts.tv_sec) + DECIMAL_STR_MAX(ts.tv_nsec)-3 + 1];
2e3c585472540d69a1f4f2023bc5ed67bad8bdb2Susant Sahani char header_pid[sizeof("[]: ")-1 + DECIMAL_STR_MAX(pid_t)];
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering int n = 0, fd;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering _cleanup_free_ char *ident_buf = NULL;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering const char *tty;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering assert(s);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering assert(message);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering if (LOG_PRI(priority) > s->max_level_console)
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering return;
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering /* First: timestamp */
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering if (prefix_timestamp()) {
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering assert_se(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering xsprintf(tbuf, "[%5"PRI_TIME".%06ld] ",
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering ts.tv_sec,
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering ts.tv_nsec / 1000);
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering IOVEC_SET_STRING(iovec[n++], tbuf);
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering }
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering /* Second: identifier and PID */
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering if (ucred) {
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering if (!identifier) {
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering get_process_comm(ucred->pid, &ident_buf);
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering identifier = ident_buf;
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering }
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering xsprintf(header_pid, "["PID_FMT"]: ", ucred->pid);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering if (identifier)
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering IOVEC_SET_STRING(iovec[n++], identifier);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
12ca818ffddb77eb6a0fabe369a5bcbf6994ff8bLennart Poettering IOVEC_SET_STRING(iovec[n++], header_pid);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering } else if (identifier) {
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering IOVEC_SET_STRING(iovec[n++], identifier);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering IOVEC_SET_STRING(iovec[n++], ": ");
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering }
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering
84e51726a38e133ca42d2f30f0668d3921b210cbLennart Poettering /* Fourth: message */
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering IOVEC_SET_STRING(iovec[n++], message);
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering IOVEC_SET_STRING(iovec[n++], "\n");
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering tty = s->tty_path ? s->tty_path : "/dev/console";
be795898c0d2fa21e3389b89793dcd45c47f8b9fJosh Triplett
be795898c0d2fa21e3389b89793dcd45c47f8b9fJosh Triplett fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
be795898c0d2fa21e3389b89793dcd45c47f8b9fJosh Triplett if (fd < 0) {
be795898c0d2fa21e3389b89793dcd45c47f8b9fJosh Triplett log_debug_errno(errno, "Failed to open %s for logging: %m", tty);
be795898c0d2fa21e3389b89793dcd45c47f8b9fJosh Triplett return;
874ff7bf4d6fe693542209f127d23cd89adc499bLennart Poettering }
if (writev(fd, iovec, n) < 0)
log_debug_errno(errno, "Failed to write to %s for logging: %m", tty);
safe_close(fd);
}