40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen/***
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen This file is part of systemd.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Copyright 2014 Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen systemd is free software; you can redistribute it and/or modify it
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen under the terms of the GNU Lesser General Public License as published by
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen the Free Software Foundation; either version 2.1 of the License, or
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen (at your option) any later version.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen systemd is distributed in the hope that it will be useful, but
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen WITHOUT ANY WARRANTY; without even the implied warranty of
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Lesser General Public License for more details.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen You should have received a copy of the GNU Lesser General Public License
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen along with systemd; If not, see <http://www.gnu.org/licenses/>.
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen***/
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include "alloc-util.h"
6482f6269c87d2249e52e889a63adbdd50f2d691Ronny Chevalier#include "formats-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "journald-server.h"
cf0fbc49e67b55f8d346fc94de28c90113505297Thomas Hindoe Paaboel Andersen#include "journald-wall.h"
0b452006de98294d1690f045f6ea2f7f6630ec3bRonny Chevalier#include "process-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "string-util.h"
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "utmp-wtmp.h"
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensenvoid server_forward_wall(
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen Server *s,
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen int priority,
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen const char *identifier,
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen const char *message,
3b3154df7e2773332bb814e167187367a0ccae4aLennart Poettering const struct ucred *ucred) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen _cleanup_free_ char *ident_buf = NULL, *l_buf = NULL;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen const char *l;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen int r;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen assert(s);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen assert(message);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (LOG_PRI(priority) > s->max_level_wall)
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (ucred) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (!identifier) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen get_process_comm(ucred->pid, &ident_buf);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen identifier = ident_buf;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen }
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (asprintf(&l_buf, "%s["PID_FMT"]: %s", strempty(identifier), ucred->pid, message) < 0) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_oom();
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen }
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen l = l_buf;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen } else if (identifier) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen l = l_buf = strjoin(identifier, ": ", message, NULL);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (!l_buf) {
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen log_oom();
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen return;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen }
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen } else
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen l = message;
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen
99f710dde855f7ecb699ddac6ad77923c1f6bc85Daniel Mack r = utmp_wall(l, "systemd-journald", NULL, NULL, NULL);
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen if (r < 0)
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt log_debug_errno(r, "Failed to send wall message: %m");
40b71e89bae4e51768db4dc50ec64c1e9c96eec4Sebastian Thorarensen}