log.c revision 35b8ca3aaf8cb044ad76675dfcad89e000dd4a5c
/*-*- 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 General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include "log.h"
#include "util.h"
#include "macro.h"
static int log_max_level = LOG_INFO;
static int console_fd = STDERR_FILENO;
static int syslog_fd = -1;
static int kmsg_fd = -1;
static bool syslog_is_stream = false;
static bool show_color = false;
static bool show_location = 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) {
return console_fd;
}
} else
return 0;
}
void log_close_kmsg(void) {
if (kmsg_fd < 0)
return;
kmsg_fd = -1;
}
static int log_open_kmsg(void) {
if (kmsg_fd >= 0)
return 0;
return -errno;
}
return 0;
}
void log_close_syslog(void) {
if (syslog_fd < 0)
return;
syslog_fd = -1;
}
static int create_log_socket(int type) {
int fd;
return -errno;
/* Make sure we don't block for more than 5s when talking to
* syslog */
return -errno;
}
return fd;
}
static int log_open_syslog(void) {
union {
struct sockaddr_un un;
} sa;
int r;
if (syslog_fd >= 0)
return 0;
r = -errno;
goto fail;
}
/* Some legacy syslog systems still use stream
* sockets. They really shouldn't. But what can we
* do... */
r = -errno;
goto fail;
}
r = -errno;
goto fail;
}
syslog_is_stream = true;
} else
syslog_is_stream = false;
log_debug("Successfully opened syslog for logging.");
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;
}
if (log_target != LOG_TARGET_AUTO ||
getpid() == 1 ||
isatty(STDERR_FILENO) <= 0) {
if (log_target == LOG_TARGET_AUTO ||
if ((r = log_open_syslog()) >= 0) {
return r;
}
if (log_target == LOG_TARGET_AUTO ||
if ((r = log_open_kmsg()) >= 0) {
return r;
}
}
return log_open_console();
}
log_target = target;
}
void log_set_max_level(int level) {
}
static int write_to_console(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
char location[64];
unsigned n = 0;
bool highlight;
if (console_fd < 0)
return 0;
if (show_location)
if (highlight)
if (highlight)
return -errno;
return 1;
}
static int write_to_syslog(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
time_t t;
if (syslog_fd < 0)
return 0;
snprintf(header_priority, sizeof(header_priority), "<%i>", LOG_MAKEPRI(LOG_DAEMON, LOG_PRI(level)));
return -EINVAL;
return -EINVAL;
/* When using syslog via SOCK_STREAM separate the messages by NUL chars */
if (syslog_is_stream)
for (;;) {
ssize_t n;
return -errno;
if (!syslog_is_stream ||
break;
}
return 1;
}
static int write_to_kmsg(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
if (kmsg_fd < 0)
return 0;
return -errno;
return 1;
}
static int log_dispatch(
int level,
const char*file,
int line,
const char *func,
char *buffer) {
int r = 0;
if (log_target == LOG_TARGET_NULL)
return 0;
do {
char *e;
int k = 0;
if (buffer[0] == 0)
break;
*(e++) = 0;
if (log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_SYSLOG) {
} else if (k > 0)
r++;
}
if (k <= 0 &&
(log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_KMSG)) {
} else if (k > 0)
r++;
}
if (k <= 0 &&
return k;
buffer = e;
} while (buffer);
return r;
}
int log_dump_internal(
int level,
const char*file,
int line,
const char *func,
char *buffer) {
int saved_errno, r;
/* This modifies the buffer... */
return 0;
saved_errno = errno;
errno = saved_errno;
return r;
}
int log_meta(
int level,
const char*file,
int line,
const char *func,
const char *format, ...) {
int saved_errno, r;
return 0;
saved_errno = errno;
errno = saved_errno;
return r;
}
void log_assert(
const char*file,
int line,
const char *func,
const char *format, ...) {
int saved_errno = errno;
abort();
/* If the user chose to ignore this SIGABRT, we are happy to go on, as if nothing happened. */
errno = saved_errno;
}
int log_set_target_from_string(const char *e) {
LogTarget t;
if ((t = log_target_from_string(e)) < 0)
return -EINVAL;
log_set_target(t);
return 0;
}
int log_set_max_level_from_string(const char *e) {
int t;
if ((t = log_level_from_string(e)) < 0)
return -EINVAL;
return 0;
}
void log_parse_environment(void) {
const char *e;
if ((e = getenv("SYSTEMD_LOG_TARGET")))
if (log_set_target_from_string(e) < 0)
log_warning("Failed to parse log target %s. Ignoring.", e);
if ((e = getenv("SYSTEMD_LOG_LEVEL")))
if (log_set_max_level_from_string(e) < 0)
log_warning("Failed to parse log level %s. Ignoring.", e);
if ((e = getenv("SYSTEMD_LOG_COLOR")))
if (log_show_color_from_string(e) < 0)
log_warning("Failed to parse bool %s. Ignoring.", e);
if ((e = getenv("SYSTEMD_LOG_LOCATION")))
if (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;
}
void log_show_location(bool b) {
show_location = b;
}
int log_show_color_from_string(const char *e) {
int t;
if ((t = parse_boolean(e)) < 0)
return -EINVAL;
log_show_color(t);
return 0;
}
int log_show_location_from_string(const char *e) {
int t;
if ((t = parse_boolean(e)) < 0)
return -EINVAL;
return 0;
}
static const char *const log_target_table[] = {
[LOG_TARGET_CONSOLE] = "console",
[LOG_TARGET_SYSLOG] = "syslog",
[LOG_TARGET_KMSG] = "kmsg",
[LOG_TARGET_SYSLOG_OR_KMSG] = "syslog-or-kmsg",
[LOG_TARGET_NULL] = "null",
[LOG_TARGET_AUTO] = "auto"
};