journald-console.c revision 4871690d9e32608bbd9b18505b5326c2079c9690
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers/***
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers This file is part of systemd.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers Copyright 2011 Lennart Poettering
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers systemd is free software; you can redistribute it and/or modify it
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers under the terms of the GNU Lesser General Public License as published by
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers the Free Software Foundation; either version 2.1 of the License, or
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers (at your option) any later version.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers systemd is distributed in the hope that it will be useful, but
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers WITHOUT ANY WARRANTY; without even the implied warranty of
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers Lesser General Public License for more details.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers You should have received a copy of the GNU Lesser General Public License
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers along with systemd; If not, see <http://www.gnu.org/licenses/>.
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers***/
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <fcntl.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <unistd.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include <sys/socket.h>
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include "journald.h"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers#include "journald-console.h"
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sieversvoid server_forward_console(
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers Server *s,
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers int priority,
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers const char *identifier,
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers const char *message,
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers struct ucred *ucred) {
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers struct iovec iovec[4];
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers char header_pid[16];
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers int n = 0, fd;
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers char *ident_buf = NULL;
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers const char *tty;
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers assert(s);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers assert(message);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers if (LOG_PRI(priority) > s->max_level_console)
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers return;
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers /* First: identifier and PID */
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers if (ucred) {
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers if (!identifier) {
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers get_process_comm(ucred->pid, &ident_buf);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers identifier = ident_buf;
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner }
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) ucred->pid);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers char_array_0(header_pid);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers if (identifier)
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers IOVEC_SET_STRING(iovec[n++], identifier);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers IOVEC_SET_STRING(iovec[n++], header_pid);
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers } else if (identifier) {
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner IOVEC_SET_STRING(iovec[n++], identifier);
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner IOVEC_SET_STRING(iovec[n++], ": ");
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner }
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner /* Third: message */
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner IOVEC_SET_STRING(iovec[n++], message);
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner IOVEC_SET_STRING(iovec[n++], "\n");
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner tty = s->tty_path ? s->tty_path : "/dev/console";
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner fd = open_terminal(tty, O_WRONLY|O_NOCTTY|O_CLOEXEC);
8f0e73f250f4a397ea07d29a339bd7e64d077612Dave Reisner if (fd < 0) {
5ba2dc259f3cdd8fddef68cfd28380a32534e49aKay Sievers log_debug("Failed to open %s for logging: %s", tty, strerror(errno));
goto finish;
}
if (writev(fd, iovec, n) < 0)
log_debug("Failed to write to %s for logging: %s", tty, strerror(errno));
close_nointr_nofail(fd);
finish:
free(ident_buf);
}