journald-wall.c revision 99f710dde855f7ecb699ddac6ad77923c1f6bc85
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2014 Sebastian Thorarensen
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "utmp-wtmp.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journald-server.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "journald-wall.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include "formats-util.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "process-util.h"
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poetteringvoid server_forward_wall(
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering Server *s,
96aad8d15a324d0e956a4e5653a11a67b209b41aLennart Poettering int priority,
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering const char *identifier,
23c80348e656a4e6fd9ba8f17523a65b6fa349a0Kay Sievers const char *message,
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering const struct ucred *ucred) {
c01ff965b48bb9693dcd77cbc748b5d8676766b0Lennart Poettering
25300b5a1fcf54674a69d0f4ab08925be00b0227Lennart Poettering _cleanup_free_ char *ident_buf = NULL, *l_buf = NULL;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering const char *l;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering int r;
003dffde2c1b93afbc9aff24b277276f65424406Lennart Poettering
4cee5eede280b7fd48c18a1942616c4ac896a554Lennart Poettering assert(s);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert(message);
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering if (LOG_PRI(priority) > s->max_level_wall)
15a5e95075a7f6007dd97b2a165c8ed16fe683dfLennart Poettering return;
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering
3ffd4af22052963e7a29431721ee204e634bea75Lennart Poettering if (ucred) {
b1d4f8e154bf61b5de1b27461ef8e9c8c5e838a1Lennart Poettering if (!identifier) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering get_process_comm(ucred->pid, &ident_buf);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering identifier = ident_buf;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (asprintf(&l_buf, "%s["PID_FMT"]: %s", strempty(identifier), ucred->pid, message) < 0) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering log_oom();
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering l = l_buf;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering } else if (identifier) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering l = l_buf = strjoin(identifier, ": ", message, NULL);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (!l_buf) {
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering log_oom();
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering return;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering }
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering } else
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering l = message;
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering r = utmp_wall(l, "systemd-journald", NULL, NULL, NULL);
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering if (r < 0)
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering log_debug_errno(r, "Failed to send wall message: %m");
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering}
160e3793adf2da2bd9ae3fe6b8881bb937e6e71bLennart Poettering