log.c revision b8d0ffc21f9c237cdeef49b1873b47df1a0a1543
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2010 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdarg.h>
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek#include <stdio.h>
b5efdb8af40ea759a1ea584c1bc44ecc81dd00ceLennart Poettering#include <errno.h>
4ad7f2761da661853dcc29d542efb4727abb1101Nick Owens#include <unistd.h>
7263f72499e962b3fd54cdb7c79d49ca72121edeZbigniew Jędrzejewski-Szmek#include <fcntl.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <sys/socket.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include <sys/un.h>
e4e73a632524c382139034d4271f53b6089ab4cbLennart Poettering#include <stddef.h>
8730bccfc59fe507bd3e0a3abcf411b497ac4f0eLennart Poettering#include <printf.h>
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering
07630cea1f3a845c09309f197ac7c4f11edd3b62Lennart Poettering#include "log.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "util.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "missing.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "macro.h"
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#include "socket-util.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering#define SNDBUF_SIZE (8*1024*1024)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic LogTarget log_target = LOG_TARGET_CONSOLE;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int log_max_level = LOG_INFO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int log_facility = LOG_DAEMON;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int console_fd = STDERR_FILENO;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int syslog_fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int kmsg_fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int journal_fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool syslog_is_stream = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool show_color = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic bool show_location = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen/* Akin to glibc's __abort_msg; which is private and we hence cannot
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering * use here. */
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poetteringstatic char *log_abort_msg = NULL;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersenvoid log_close_console(void) {
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (console_fd < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (getpid() == 1) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (console_fd >= 3)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering close_nointr_nofail(console_fd);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering console_fd = -1;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering }
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering}
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poetteringstatic int log_open_console(void) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (console_fd >= 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return 0;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (getpid() == 1) {
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering console_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC);
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering if (console_fd < 0)
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering return console_fd;
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering } else
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering console_fd = STDERR_FILENO;
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen
36d9205d669bcdcb04fa730d1f3549a9fc9a9001Tom Gundersen return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringvoid log_close_kmsg(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (kmsg_fd < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering close_nointr_nofail(kmsg_fd);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering kmsg_fd = -1;
dc477e7385e8ab29efb8fadb72ec994077a105c6Lennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringstatic int log_open_kmsg(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (kmsg_fd >= 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering kmsg_fd = open("/dev/kmsg", O_WRONLY|O_NOCTTY|O_CLOEXEC);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (kmsg_fd < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return -errno;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringvoid log_close_syslog(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (syslog_fd < 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering close_nointr_nofail(syslog_fd);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering syslog_fd = -1;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int create_log_socket(int type) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int fd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering struct timeval tv;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd = socket(AF_UNIX, type|SOCK_CLOEXEC, 0);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (fd < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering fd_inc_sndbuf(fd, SNDBUF_SIZE);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* We need a blocking fd here since we'd otherwise lose
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering messages way too early. However, let's not hang forever in the
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering unlikely case of a deadlock. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (getpid() == 1)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering timeval_store(&tv, 10 * USEC_PER_MSEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering else
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering timeval_store(&tv, 10 * USEC_PER_SEC);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering return fd;
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering}
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int log_open_syslog(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering union sockaddr_union sa = {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .un.sun_family = AF_UNIX,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering .un.sun_path = "/dev/log",
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering };
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (syslog_fd >= 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
1b4f6e79ec51a57003896a0b605fba427b4a98d2Lennart Poettering syslog_fd = create_log_socket(SOCK_DGRAM);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (syslog_fd < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = syslog_fd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering close_nointr_nofail(syslog_fd);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Some legacy syslog systems still use stream
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * sockets. They really shouldn't. But what can we
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * do... */
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering syslog_fd = create_log_socket(SOCK_STREAM);
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (syslog_fd < 0) {
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering r = syslog_fd;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering goto fail;
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering }
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
28b9b7640603f88cb49f95609331fa5072715f15Lennart Poettering r = -errno;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering syslog_is_stream = true;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering } else
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering syslog_is_stream = false;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringfail:
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_close_syslog();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringvoid log_close_journal(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (journal_fd < 0)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
105e151299dc1208855380be2b22d0db2d66ebc6Lennart Poettering close_nointr_nofail(journal_fd);
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering journal_fd = -1;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic int log_open_journal(void) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering union sockaddr_union sa = {
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering .un.sun_family = AF_UNIX,
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering .un.sun_path = "/run/systemd/journal/socket",
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering };
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (journal_fd >= 0)
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering journal_fd = create_log_socket(SOCK_DGRAM);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (journal_fd < 0) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering r = journal_fd;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering goto fail;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = -errno;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering goto fail;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return 0;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringfail:
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_close_journal();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering}
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poetteringint log_open(void) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering int r;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* If we don't use the console we close it here, to not get
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen * killed by SAK. If we don't use syslog we close it here so
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering * that we are not confused by somebody deleting the socket in
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering * the fs. If we don't use /dev/kmsg we still keep it open,
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * because there is no reason to close it. */
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (log_target == LOG_TARGET_NULL) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen log_close_journal();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_close_syslog();
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering log_close_console();
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen return 0;
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen }
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if ((log_target != LOG_TARGET_AUTO && log_target != LOG_TARGET_SAFE) ||
58db254ade4fb2ef77de68f28c4f13814819f6a1Lennart Poettering getpid() == 1 ||
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering isatty(STDERR_FILENO) <= 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (log_target == LOG_TARGET_AUTO ||
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_target == LOG_TARGET_JOURNAL) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering r = log_open_journal();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r >= 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_close_syslog();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_close_console();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering return r;
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering }
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen log_target == LOG_TARGET_SYSLOG) {
5d27351f8546530cf779847b0b04b0172c09f9d0Tom Gundersen r = log_open_syslog();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r >= 0) {
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_close_journal();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering log_close_console();
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering return r;
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering }
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering if (log_target == LOG_TARGET_AUTO ||
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_target == LOG_TARGET_SAFE ||
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
65b200e70d01cb7c513114b602c9b5cf41348f77Lennart Poettering log_target == LOG_TARGET_KMSG) {
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering r = log_open_kmsg();
801ad6a6a9cd8fbd58b9f9c27f20dbb3c87d47ddLennart Poettering if (r >= 0) {
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_close_journal();
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_close_syslog();
547973dea7abd6c124ff6c79fe2bbe322a7314aeLennart Poettering log_close_console();
0936416a1cba187dc76903dae73246f4cbddf312Lennart Poettering return r;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering }
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_close_journal();
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen log_close_syslog();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen return log_open_console();
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen}
b826ab586c9e0a9c0d438a75c28cf3a8ab485929Tom Gundersen
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringvoid log_set_target(LogTarget target) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert(target >= 0);
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt assert(target < _LOG_TARGET_MAX);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_target = target;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringvoid log_close(void) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_close_journal();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_close_syslog();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_close_kmsg();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_close_console();
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringvoid log_forget_fds(void) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering console_fd = kmsg_fd = syslog_fd = journal_fd = -1;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringvoid log_set_max_level(int level) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering assert((level & LOG_PRIMASK) == level);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_max_level = level;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidtvoid log_set_facility(int facility) {
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt log_facility = facility;
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt}
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poetteringstatic int write_to_console(
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen int level,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char*file,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int line,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *func,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering const char *object_name,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering const char *object,
6af47493de0ef2b66d4c3fbcdd4a2e12fec4bfbaLennart Poettering const char *buffer) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char location[64];
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen struct iovec iovec[5] = {};
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering unsigned n = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering bool highlight;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (console_fd < 0)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return 0;
d23a27a964748967e1ad20e86de869a753af555bTom Gundersen
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering highlight = LOG_PRI(level) <= LOG_ERR && show_color;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (show_location) {
c52a97b896c914e17ba5be73c0e806455fd9ad4dLennart Poettering snprintf(location, sizeof(location), "(%s:%u) ", file, line);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char_array_0(location);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering IOVEC_SET_STRING(iovec[n++], location);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (highlight)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_RED_ON);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering IOVEC_SET_STRING(iovec[n++], buffer);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (highlight)
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(iovec[n++], ANSI_HIGHLIGHT_OFF);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering IOVEC_SET_STRING(iovec[n++], "\n");
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (writev(console_fd, iovec, n) < 0) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (errno == EIO && getpid() == 1) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
ee3d6aff9bd73c1b23e29d1fa1fa6f7a1ef0533bLennart Poettering /* If somebody tried to kick us from our
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering * console tty (via vhangup() or suchlike),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * try to reconnect */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering log_close_console();
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering log_open_console();
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (console_fd < 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return 0;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering if (writev(console_fd, iovec, n) < 0)
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -errno;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering } else
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering return -errno;
8bf52d3d17d364438191077d0750b8b80b5dc53aLennart Poettering }
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic int write_to_syslog(
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int level,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char*file,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int line,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *func,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *object_name,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *object,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering const char *buffer) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering char header_priority[16], header_time[64], header_pid[16];
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct iovec iovec[5] = {};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct msghdr msghdr = {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .msg_iov = iovec,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering .msg_iovlen = ELEMENTSOF(iovec),
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering };
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering time_t t;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering struct tm *tm;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (syslog_fd < 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering snprintf(header_priority, sizeof(header_priority), "<%i>", level);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering char_array_0(header_priority);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek t = (time_t) (now(CLOCK_REALTIME) / USEC_PER_SEC);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek tm = localtime(&t);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (!tm)
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering return -EINVAL;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek if (strftime(header_time, sizeof(header_time), "%h %e %T ", tm) <= 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering return -EINVAL;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering char_array_0(header_pid);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering IOVEC_SET_STRING(iovec[0], header_priority);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iovec[1], header_time);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering IOVEC_SET_STRING(iovec[2], program_invocation_short_name);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering IOVEC_SET_STRING(iovec[3], header_pid);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iovec[4], buffer);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek /* When using syslog via SOCK_STREAM separate the messages by NUL chars */
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (syslog_is_stream)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering iovec[4].iov_len++;
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering for (;;) {
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek ssize_t n;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek n = sendmsg(syslog_fd, &msghdr, MSG_NOSIGNAL);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (n < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return -errno;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (!syslog_is_stream ||
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen (size_t) n >= IOVEC_TOTAL_SIZE(iovec, ELEMENTSOF(iovec)))
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering break;
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering IOVEC_INCREMENT(iovec, ELEMENTSOF(iovec), n);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering }
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 1;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic int write_to_kmsg(
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int level,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char*file,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int line,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *func,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen const char *object_name,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen const char *object,
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen const char *buffer) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char header_priority[16], header_pid[16];
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen struct iovec iovec[5] = {};
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen if (kmsg_fd < 0)
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return 0;
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen snprintf(header_priority, sizeof(header_priority), "<%i>", level);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char_array_0(header_priority);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek snprintf(header_pid, sizeof(header_pid), "[%lu]: ", (unsigned long) getpid());
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek char_array_0(header_pid);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iovec[0], header_priority);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering IOVEC_SET_STRING(iovec[1], program_invocation_short_name);
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek IOVEC_SET_STRING(iovec[2], header_pid);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering IOVEC_SET_STRING(iovec[3], buffer);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering IOVEC_SET_STRING(iovec[4], "\n");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (writev(kmsg_fd, iovec, ELEMENTSOF(iovec)) < 0)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return -errno;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return 1;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic int log_do_header(char *header, size_t size,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int level,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *file, int line, const char *func,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *object_name, const char *object) {
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering snprintf(header, size,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "PRIORITY=%i\n"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "SYSLOG_FACILITY=%i\n"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "%s%.*s%s"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "%s%.*i%s"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "%s%.*s%s"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "%s%.*s%s"
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering "SYSLOG_IDENTIFIER=%s\n",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering LOG_PRI(level),
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering LOG_FAC(level),
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering file ? "CODE_FILE=" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering file ? LINE_MAX : 0, file, /* %.0s means no output */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering file ? "\n" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering line ? "CODE_LINE=" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering line ? 1 : 0, line, /* %.0d means no output too, special case for 0 */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering line ? "\n" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering func ? "CODE_FUNCTION=" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering func ? LINE_MAX : 0, func,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering func ? "\n" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering object ? object_name : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering object ? LINE_MAX : 0, object, /* %.0s means no output */
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering object ? "\n" : "",
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering program_invocation_short_name);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering header[size - 1] = '\0';
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering return 0;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering}
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringstatic int write_to_journal(
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int level,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char*file,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int line,
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering const char *func,
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering const char *object_name,
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering const char *object,
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering const char *buffer) {
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering char header[LINE_MAX];
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering struct iovec iovec[4] = {};
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering struct msghdr mh = {};
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (journal_fd < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return 0;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering log_do_header(header, sizeof(header), level,
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering file, line, func, object_name, object);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering IOVEC_SET_STRING(iovec[0], header);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering IOVEC_SET_STRING(iovec[1], "MESSAGE=");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering IOVEC_SET_STRING(iovec[2], buffer);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering IOVEC_SET_STRING(iovec[3], "\n");
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering mh.msg_iov = iovec;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering mh.msg_iovlen = ELEMENTSOF(iovec);
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return -errno;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering return 1;
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering}
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poetteringstatic int log_dispatch(
78c6a153c47f8d597c827bdcaf8c4e42ac87f738Lennart Poettering int level,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char*file,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering int line,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *func,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *object_name,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *object,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering char *buffer) {
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering int r = 0;
4d247a6cd3f69acbc5a09e8ac7e4fbb50eaa3228Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (log_target == LOG_TARGET_NULL)
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return 0;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* Patch in LOG_DAEMON facility if necessary */
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering if ((level & LOG_FACMASK) == 0)
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering level = log_facility | LOG_PRI(level);
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering
fd0b4602f6332c3f1660eb208c8f5c719709a009Lennart Poettering do {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char *e;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering int k = 0;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering buffer += strspn(buffer, NEWLINE);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (buffer[0] == 0)
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering break;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if ((e = strpbrk(buffer, NEWLINE)))
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering *(e++) = 0;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (log_target == LOG_TARGET_AUTO ||
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering log_target == LOG_TARGET_JOURNAL) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering k = write_to_journal(level, file, line, func,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering object_name, object, buffer);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (k < 0) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (k != -EAGAIN)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_close_journal();
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek log_open_kmsg();
0f84a72e3c0f58d71cff2121e6df1611eaf9c9eaDavid Herrmann } else if (k > 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r++;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering log_target == LOG_TARGET_SYSLOG) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering k = write_to_syslog(level, file, line, func,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering object_name, object, buffer);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (k < 0) {
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (k != -EAGAIN)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_close_syslog();
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_open_kmsg();
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering } else if (k > 0)
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering r++;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering }
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering if (k <= 0 &&
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering (log_target == LOG_TARGET_AUTO ||
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_target == LOG_TARGET_SAFE ||
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering log_target == LOG_TARGET_KMSG)) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek k = write_to_kmsg(level, file, line, func,
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek object_name, object, buffer);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (k < 0) {
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_close_kmsg();
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_open_console();
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek } else if (k > 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r++;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (k <= 0) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek k = write_to_console(level, file, line, func,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek object_name, object, buffer);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (k < 0)
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return k;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen buffer = e;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen } while (buffer);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen return r;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen}
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poetteringint log_dump_internal(
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering int level,
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering const char*file,
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen int line,
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen const char *func,
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering char *buffer) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering PROTECT_ERRNO;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek /* This modifies the buffer... */
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (_likely_(LOG_PRI(level) > log_max_level))
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return log_dispatch(level, file, line, func, NULL, NULL, buffer);
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn}
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykrynint log_metav(
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int level,
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn const char*file,
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn int line,
03664a62914782dbd8f069bbcf8a0c8ca1df7010Lukas Nykryn const char *func,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek const char *format,
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek va_list ap) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek PROTECT_ERRNO;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek char buffer[LINE_MAX];
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen if (_likely_(LOG_PRI(level) > log_max_level))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return 0;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen vsnprintf(buffer, sizeof(buffer), format, ap);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen char_array_0(buffer);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen return log_dispatch(level, file, line, func, NULL, NULL, buffer);
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen}
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersenint log_meta(
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int level,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen const char*file,
5d45a8808431987c370706d365fb0cc95cf03d52Tom Gundersen int line,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering const char *func,
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering const char *format, ...) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering int r;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering va_list ap;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek va_start(ap, format);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = log_metav(level, file, line, func, format, ap);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek va_end(ap);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return r;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint log_metav_object(
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int level,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const char*file,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int line,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const char *func,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const char *object_name,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const char *object,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek const char *format,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek va_list ap) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek PROTECT_ERRNO;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char buffer[LINE_MAX];
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (_likely_(LOG_PRI(level) > log_max_level))
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return 0;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek vsnprintf(buffer, sizeof(buffer), format, ap);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek char_array_0(buffer);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek return log_dispatch(level, file, line, func,
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek object_name, object, buffer);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek}
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmekint log_meta_object(
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen int level,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char*file,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen int line,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *func,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *object_name,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *object,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen const char *format, ...) {
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen int r;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen va_list ap;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen va_start(ap, format);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen r = log_metav_object(level, file, line, func,
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen object_name, object, format, ap);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen va_end(ap);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return r;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#pragma GCC diagnostic push
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen#pragma GCC diagnostic ignored "-Wformat-nonliteral"
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenstatic void log_assert(int level, const char *text, const char *file, int line, const char *func, const char *format) {
cb57dd41595adddb08095298bb1ed258c8ea4877Tom Gundersen static char buffer[LINE_MAX];
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering if (_likely_(LOG_PRI(level) > log_max_level))
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen return;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen snprintf(buffer, sizeof(buffer), format, text, file, line, func);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen char_array_0(buffer);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_abort_msg = buffer;
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_dispatch(level, file, line, func, NULL, NULL, buffer);
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
2c1fb4f71206bf970d493294208c5d7597194856Lennart Poettering#pragma GCC diagnostic pop
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersennoreturn void log_assert_failed(const char *text, const char *file, int line, const char *func) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen abort();
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersennoreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen abort();
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen}
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersen
50f1e641a93cacfc693b0c3d300bee5df0c8c460Tom Gundersenvoid log_assert_failed_return(const char *text, const char *file, int line, const char *func) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering PROTECT_ERRNO;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint log_oom_internal(const char *file, int line, const char *func) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_meta(LOG_ERR, file, line, func, "Out of memory.");
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return -ENOMEM;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering}
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint log_struct_internal(
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int level,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering const char *file,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int line,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering const char *func,
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering const char *format, ...) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering PROTECT_ERRNO;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering va_list ap;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering int r;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (_likely_(LOG_PRI(level) > log_max_level))
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if (log_target == LOG_TARGET_NULL)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering return 0;
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if ((level & LOG_FACMASK) == 0)
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering level = log_facility | LOG_PRI(level);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering if ((log_target == LOG_TARGET_AUTO ||
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_target == LOG_TARGET_JOURNAL_OR_KMSG ||
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering log_target == LOG_TARGET_JOURNAL) &&
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering journal_fd >= 0) {
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering char header[LINE_MAX];
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering struct iovec iovec[17] = {};
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering unsigned n = 0, i;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering struct msghdr mh = {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek .msg_iov = iovec,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering };
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering static const char nl = '\n';
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* If the journal is available do structured logging */
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering log_do_header(header, sizeof(header), level,
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering file, line, func, NULL, NULL);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering IOVEC_SET_STRING(iovec[n++], header);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_start(ap, format);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering while (format && n + 1 < ELEMENTSOF(iovec)) {
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering char *buf;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering va_list aq;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* We need to copy the va_list structure,
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * since vasprintf() leaves it afterwards at
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering * an undefined location */
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering va_copy(aq, ap);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering if (vasprintf(&buf, format, aq) < 0) {
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering va_end(aq);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering r = -ENOMEM;
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering goto finish;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering }
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering va_end(aq);
9c92ce6d67f88beb31dd6555d12ae3f632218a39Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering /* Now, jump enough ahead, so that we point to
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering * the next format string */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering VA_FORMAT_ADVANCE(format, ap);
8ac4e9e1e54397f6d1745c2a7a806132418c7da2Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering IOVEC_SET_STRING(iovec[n++], buf);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering iovec[n].iov_base = (char*) &nl;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering iovec[n].iov_len = 1;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering n++;
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering format = va_arg(ap, char *);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering mh.msg_iovlen = n;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering if (sendmsg(journal_fd, &mh, MSG_NOSIGNAL) < 0)
9de3e3294065e8697ff10130b53f274319cdcf6fZbigniew Jędrzejewski-Szmek r = -errno;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek else
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poettering r = 1;
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering finish:
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek va_end(ap);
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek for (i = 1; i < n; i += 2)
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek free(iovec[i].iov_base);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek } else {
2e276efc7b0398a3086629a52970bdd4ab7252f9Zbigniew Jędrzejewski-Szmek char buf[LINE_MAX];
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering bool found = false;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering /* Fallback if journal logging is not available */
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_start(ap, format);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering while (format) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering va_list aq;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_copy(aq, ap);
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering vsnprintf(buf, sizeof(buf), format, aq);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering va_end(aq);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering char_array_0(buf);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (startswith(buf, "MESSAGE=")) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek found = true;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering break;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek VA_FORMAT_ADVANCE(format, ap);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering format = va_arg(ap, char *);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering va_end(ap);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering if (found)
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = log_dispatch(level, file, line, func,
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering NULL, NULL, buf + 8);
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering else
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering r = -EINVAL;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering }
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering return r;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poetteringint log_set_target_from_string(const char *e) {
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering LogTarget t;
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek t = log_target_from_string(e);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek if (t < 0)
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return -EINVAL;
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek log_set_target(t);
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek return 0;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering}
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmek
946c70944ebdf428ffeb9991a7449edbd4011461Zbigniew Jędrzejewski-Szmekint log_set_max_level_from_string(const char *e) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int t;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek t = log_level_from_string(e);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek if (t < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return t;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_set_max_level(t);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return 0;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poetteringvoid log_parse_environment(void) {
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek _cleanup_free_ char *line = NULL;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek const char *e;
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek int r;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek r = proc_cmdline(&line);
0dae31d468b1a0e22d98921f7b0dbd92fd217167Zbigniew Jędrzejewski-Szmek if (r < 0)
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_warning("Failed to read /proc/cmdline. Ignoring: %s", strerror(-r));
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen else if (r > 0) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen char *w, *state;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering size_t l;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen FOREACH_WORD_QUOTED(w, l, line, state) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (l == 5 && startswith(w, "debug")) {
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen log_set_max_level(LOG_DEBUG);
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen break;
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen }
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen e = secure_getenv("SYSTEMD_LOG_TARGET");
abf126a355e2f2b62b6c51ab3bb37895d1e3eee7Tom Gundersen if (e && log_set_target_from_string(e) < 0)
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek log_warning("Failed to parse log target %s. Ignoring.", e);
549c1a2564b56f2bb38f1203d59c747ea15817f3Tom Gundersen
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek e = secure_getenv("SYSTEMD_LOG_LEVEL");
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (e && log_set_max_level_from_string(e) < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_warning("Failed to parse log level %s. Ignoring.", e);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering e = secure_getenv("SYSTEMD_LOG_COLOR");
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering if (e && log_show_color_from_string(e) < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_warning("Failed to parse bool %s. Ignoring.", e);
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering e = secure_getenv("SYSTEMD_LOG_LOCATION");
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering if (e && log_show_location_from_string(e) < 0)
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering log_warning("Failed to parse bool %s. Ignoring.", e);
42cc2eebb01056beb7acd3ecfe8e533558237f84Lennart Poettering}
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-SzmekLogTarget log_get_target(void) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek return log_target;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek}
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersenint log_get_max_level(void) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek return log_max_level;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering}
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poetteringvoid log_show_color(bool b) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek show_color = b;
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering}
f91dc2400dc33e9a0745ecaaef7489af116dca38Lennart Poettering
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmekvoid log_show_location(bool b) {
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek show_location = b;
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek}
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poetteringint log_show_color_from_string(const char *e) {
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek int t;
ff3d6560bead6879a2fed1bf99bfe8273b3723f1Zbigniew Jędrzejewski-Szmek
2d4c5cbc0ed3ccb09dc086a040088b454c22c644Lennart Poettering t = parse_boolean(e);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (t < 0)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return t;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek log_show_color(t);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
1bf968f36393666f2c57953b1748e6219c027deeTom Gundersenint log_show_location_from_string(const char *e) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek int t;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek t = parse_boolean(e);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen if (t < 0)
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return t;
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen log_show_location(t);
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return 0;
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen}
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersenbool log_on_console(void) {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek if (log_target == LOG_TARGET_CONSOLE)
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek return true;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek}
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmekstatic const char *const log_target_table[] = {
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_CONSOLE] = "console",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_KMSG] = "kmsg",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_JOURNAL] = "journal",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen [LOG_TARGET_SYSLOG] = "syslog",
7c6423e19136a7b7b6ef3fe06b94822e582dda27Tom Gundersen [LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_AUTO] = "auto",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_SAFE] = "safe",
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek [LOG_TARGET_NULL] = "null"
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek};
7b50eb2efa122200e39646c19a29abab302f7d24Lennart Poettering
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-SzmekDEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
151226ab4bf276d60d51864330a99f886b923697Zbigniew Jędrzejewski-Szmek