log.c revision afc5dbf37fd2399d37976388d9dd9ab470ecf446
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <printf.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <unistd.h>
#include "sd-messages.h"
#include "fd-util.h"
#include "formats-util.h"
#include "io-util.h"
#include "log.h"
#include "macro.h"
#include "missing.h"
#include "parse-util.h"
#include "proc-cmdline.h"
#include "process-util.h"
#include "signal-util.h"
#include "socket-util.h"
#include "string-table.h"
#include "string-util.h"
#include "syslog-util.h"
#include "terminal-util.h"
#include "util.h"
static int log_max_level = LOG_INFO;
static int log_facility = LOG_DAEMON;
static int console_fd = STDERR_FILENO;
static int syslog_fd = -1;
static int kmsg_fd = -1;
static int journal_fd = -1;
static bool syslog_is_stream = false;
static bool show_color = false;
static bool show_location = false;
static bool upgrade_syslog_to_journal = false;
/* Akin to glibc's __abort_msg; which is private and we hence cannot
* use here. */
static char *log_abort_msg = NULL;
void log_close_console(void) {
if (console_fd < 0)
return;
if (getpid() == 1) {
if (console_fd >= 3)
console_fd = -1;
}
}
static int log_open_console(void) {
if (console_fd >= 0)
return 0;
if (getpid() == 1) {
if (console_fd < 0)
return console_fd;
} else
return 0;
}
void log_close_kmsg(void) {
}
static int log_open_kmsg(void) {
if (kmsg_fd >= 0)
return 0;
if (kmsg_fd < 0)
return -errno;
return 0;
}
void log_close_syslog(void) {
}
static int create_log_socket(int type) {
int fd;
if (fd < 0)
return -errno;
/* We need a blocking fd here since we'd otherwise lose
messages way too early. However, let's not hang forever in the
unlikely case of a deadlock. */
if (getpid() == 1)
else
return fd;
}
static int log_open_syslog(void) {
static const union sockaddr_union sa = {
};
int r;
if (syslog_fd >= 0)
return 0;
if (syslog_fd < 0) {
r = syslog_fd;
goto fail;
}
if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
/* Some legacy syslog systems still use stream
* sockets. They really shouldn't. But what can we
* do... */
if (syslog_fd < 0) {
r = syslog_fd;
goto fail;
}
if (connect(syslog_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
r = -errno;
goto fail;
}
syslog_is_stream = true;
} else
syslog_is_stream = false;
return 0;
fail:
return r;
}
void log_close_journal(void) {
}
static int log_open_journal(void) {
static const union sockaddr_union sa = {
};
int r;
if (journal_fd >= 0)
return 0;
if (journal_fd < 0) {
r = journal_fd;
goto fail;
}
if (connect(journal_fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + strlen(sa.un.sun_path)) < 0) {
r = -errno;
goto fail;
}
return 0;
fail:
return r;
}
int log_open(void) {
int r;
/* If we don't use the console we close it here, to not get
* killed by SAK. If we don't use syslog we close it here so
* that we are not confused by somebody deleting the socket in
* because there is no reason to close it. */
if (log_target == LOG_TARGET_NULL) {
return 0;
}
getpid() == 1 ||
isatty(STDERR_FILENO) <= 0) {
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL) {
r = log_open_journal();
if (r >= 0) {
return r;
}
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
r = log_open_syslog();
if (r >= 0) {
return r;
}
}
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_SAFE ||
log_target == LOG_TARGET_KMSG) {
r = log_open_kmsg();
if (r >= 0) {
return r;
}
}
}
return log_open_console();
}
if (upgrade_syslog_to_journal) {
if (target == LOG_TARGET_SYSLOG)
else if (target == LOG_TARGET_SYSLOG_OR_KMSG)
}
log_target = target;
}
void log_close(void) {
}
void log_forget_fds(void) {
}
void log_set_max_level(int level) {
}
void log_set_facility(int facility) {
}
static int write_to_console(
int level,
int error,
const char *file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *buffer) {
unsigned n = 0;
bool highlight;
if (console_fd < 0)
return 0;
if (log_target == LOG_TARGET_CONSOLE_PREFIXED) {
}
if (show_location) {
}
if (highlight)
if (highlight)
/* If somebody tried to kick us from our
* console tty (via vhangup() or suchlike),
* try to reconnect */
if (console_fd < 0)
return 0;
return -errno;
} else
return -errno;
}
return 1;
}
static int write_to_syslog(
int level,
int error,
const char *file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *buffer) {
header_time[64],
};
time_t t;
if (syslog_fd < 0)
return 0;
if (!tm)
return -EINVAL;
return -EINVAL;
/* When using syslog via SOCK_STREAM separate the messages by NUL chars */
if (syslog_is_stream)
for (;;) {
ssize_t n;
if (n < 0)
return -errno;
if (!syslog_is_stream ||
break;
}
return 1;
}
static int write_to_kmsg(
int level,
int error,
const char*file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *buffer) {
if (kmsg_fd < 0)
return 0;
return -errno;
return 1;
}
static int log_do_header(
char *header,
int level,
int error,
const char *object_field, const char *object) {
"PRIORITY=%i\n"
"SYSLOG_FACILITY=%i\n"
"%s%s%s"
"%s%.*i%s"
"%s%s%s"
"%s%.*i%s"
"%s%s%s"
"SYSLOG_IDENTIFIER=%s\n",
return 0;
}
static int write_to_journal(
int level,
int error,
const char*file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *buffer) {
if (journal_fd < 0)
return 0;
return -errno;
return 1;
}
static int log_dispatch(
int level,
int error,
const char *file,
int line,
const char *func,
const char *object_field,
const char *object,
char *buffer) {
if (log_target == LOG_TARGET_NULL)
return -error;
/* Patch in LOG_DAEMON facility if necessary */
if ((level & LOG_FACMASK) == 0)
if (error < 0)
do {
char *e;
int k = 0;
if (buffer[0] == 0)
break;
*(e++) = 0;
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL) {
if (k < 0) {
if (k != -EAGAIN)
}
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
if (k < 0) {
if (k != -EAGAIN)
}
}
if (k <= 0 &&
(log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_SAFE ||
log_target == LOG_TARGET_KMSG)) {
if (k < 0) {
}
}
if (k <= 0)
buffer = e;
} while (buffer);
return -error;
}
int log_dump_internal(
int level,
int error,
const char *file,
int line,
const char *func,
char *buffer) {
/* This modifies the buffer... */
if (error < 0)
return -error;
}
int log_internalv(
int level,
int error,
const char*file,
int line,
const char *func,
const char *format,
if (error < 0)
return -error;
/* Make sure that %m maps to the specified error */
if (error != 0)
}
int log_internal(
int level,
int error,
const char*file,
int line,
const char *func,
const char *format, ...) {
int r;
return r;
}
int log_object_internalv(
int level,
int error,
const char*file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *format,
char *buffer, *b;
size_t l;
if (error < 0)
return -error;
/* Make sure that %m maps to the specified error */
if (error != 0)
/* Prepend the object name before the message */
if (object) {
size_t n;
l = n + 2 + LINE_MAX;
} else {
l = LINE_MAX;
}
}
int log_object_internal(
int level,
int error,
const char*file,
int line,
const char *func,
const char *object_field,
const char *object,
const char *format, ...) {
int r;
return r;
}
static void log_assert(
int level,
const char *text,
const char *file,
int line,
const char *func,
const char *format) {
return;
}
log_assert(LOG_CRIT, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
abort();
}
noreturn void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
log_assert(LOG_CRIT, text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
abort();
}
log_assert(LOG_DEBUG, text, file, line, func, "Assertion '%s' failed at %s:%u, function %s(). Ignoring.");
}
return -ENOMEM;
}
int log_struct_internal(
int level,
int error,
const char *file,
int line,
const char *func,
const char *format, ...) {
bool found = false;
if (error < 0)
return -error;
if (log_target == LOG_TARGET_NULL)
return -error;
if ((level & LOG_FACMASK) == 0)
if ((log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_JOURNAL) &&
journal_fd >= 0) {
unsigned n = 0, i;
};
static const char nl = '\n';
bool fallback = false;
/* If the journal is available do structured logging */
char *m;
/* We need to copy the va_list structure,
* since vasprintf() leaves it afterwards at
* an undefined location */
if (error != 0)
fallback = true;
goto finish;
}
/* Now, jump enough ahead, so that we point to
* the next format string */
IOVEC_SET_STRING(iovec[n++], m);
n++;
}
mh.msg_iovlen = n;
for (i = 1; i < n; i += 2)
if (!fallback)
return -error;
}
/* Fallback if journal logging is not available or didn't work. */
while (format) {
if (error != 0)
found = true;
break;
}
}
if (!found)
return -error;
}
int log_set_target_from_string(const char *e) {
LogTarget t;
t = log_target_from_string(e);
if (t < 0)
return -EINVAL;
log_set_target(t);
return 0;
}
int log_set_max_level_from_string(const char *e) {
int t;
t = log_level_from_string(e);
if (t < 0)
return -EINVAL;
return 0;
}
/*
* The systemd.log_xyz= settings are parsed by all tools, and
* so is "debug".
*
* However, "quiet" is only parsed by PID 1, and only turns of
* level.
*/
if (log_set_target_from_string(value) < 0)
if (log_set_max_level_from_string(value) < 0)
if (log_show_color_from_string(value) < 0)
if (log_show_location_from_string(value) < 0)
}
return 0;
}
void log_parse_environment(void) {
const char *e;
if (get_ctty_devnr(0, NULL) < 0)
/* Only try to read the command line in daemons.
We assume that anything that has a controlling
tty is user stuff. */
e = secure_getenv("SYSTEMD_LOG_TARGET");
if (e && log_set_target_from_string(e) < 0)
log_warning("Failed to parse log target '%s'. Ignoring.", e);
e = secure_getenv("SYSTEMD_LOG_LEVEL");
if (e && log_set_max_level_from_string(e) < 0)
log_warning("Failed to parse log level '%s'. Ignoring.", e);
e = secure_getenv("SYSTEMD_LOG_COLOR");
if (e && log_show_color_from_string(e) < 0)
log_warning("Failed to parse bool '%s'. Ignoring.", e);
e = secure_getenv("SYSTEMD_LOG_LOCATION");
if (e && log_show_location_from_string(e) < 0)
log_warning("Failed to parse bool '%s'. Ignoring.", e);
}
LogTarget log_get_target(void) {
return log_target;
}
int log_get_max_level(void) {
return log_max_level;
}
void log_show_color(bool b) {
show_color = b;
}
bool log_get_show_color(void) {
return show_color;
}
void log_show_location(bool b) {
show_location = b;
}
bool log_get_show_location(void) {
return show_location;
}
int log_show_color_from_string(const char *e) {
int t;
t = parse_boolean(e);
if (t < 0)
return t;
log_show_color(t);
return 0;
}
int log_show_location_from_string(const char *e) {
int t;
t = parse_boolean(e);
if (t < 0)
return t;
return 0;
}
bool log_on_console(void) {
if (log_target == LOG_TARGET_CONSOLE ||
return true;
}
static const char *const log_target_table[_LOG_TARGET_MAX] = {
[LOG_TARGET_CONSOLE] = "console",
[LOG_TARGET_CONSOLE_PREFIXED] = "console-prefixed",
[LOG_TARGET_KMSG] = "kmsg",
[LOG_TARGET_JOURNAL] = "journal",
[LOG_TARGET_JOURNAL_OR_KMSG] = "journal-or-kmsg",
[LOG_TARGET_SYSLOG] = "syslog",
[LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
[LOG_TARGET_AUTO] = "auto",
[LOG_TARGET_SAFE] = "safe",
[LOG_TARGET_NULL] = "null"
};
_cleanup_free_ char *p = NULL;
} else
"Received SIG%s.",
}
void log_set_upgrade_syslog_to_journal(bool b) {
}
int log_syntax_internal(
const char *unit,
int level,
const char *config_file,
unsigned config_line,
int error,
const char *file,
int line,
const char *func,
const char *format, ...) {
int r;
if (error < 0)
return -error;
if (log_target == LOG_TARGET_NULL)
return -error;
if (error != 0)
if (unit)
r = log_struct_internal(
"CONFIG_FILE=%s", config_file,
"CONFIG_LINE=%u", config_line,
NULL);
else
r = log_struct_internal(
"CONFIG_FILE=%s", config_file,
"CONFIG_LINE=%u", config_line,
NULL);
return r;
}