log.c revision 3eff4208ffecedd778fec260f0d4b18e94dab443
/*-*- 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 <stddef.h>
#include "log.h"
#include "util.h"
#include "macro.h"
#include "socket-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;
/* 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) {
if (kmsg_fd < 0)
return;
kmsg_fd = -1;
}
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) {
if (syslog_fd < 0)
return;
syslog_fd = -1;
}
static int create_log_socket(int type) {
int fd;
* and if the buffers are full we just drop the messages */
if (fd < 0)
return -errno;
return fd;
}
static int log_open_syslog(void) {
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;
log_debug("Successfully opened syslog for logging.");
return 0;
fail:
return r;
}
void log_close_journal(void) {
if (journal_fd < 0)
return;
journal_fd = -1;
}
static int log_open_journal(void) {
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;
}
log_debug("Successfully opened journal 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 ||
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_KMSG) {
r = log_open_kmsg();
if (r >= 0) {
return r;
}
}
}
return log_open_console();
}
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,
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;
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,
const char*file,
int line,
const char *func,
const char *buffer) {
if (kmsg_fd < 0)
return 0;
return -errno;
return 1;
}
static int write_to_journal(
int level,
const char*file,
int line,
const char *func,
const char *buffer) {
if (journal_fd < 0)
return 0;
"PRIORITY=%i\n"
"SYSLOG_FACILITY=%i\n"
"CODE_FILE=%s\n"
"CODE_LINE=%i\n"
"CODE_FUNCTION=%s\n"
"MESSAGE=",
file,
line,
func);
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;
/* Patch in LOG_DAEMON facility if necessary */
if ((level & LOG_FACMASK) == 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)
} else if (k > 0)
r++;
}
if (log_target == LOG_TARGET_SYSLOG_OR_KMSG ||
log_target == LOG_TARGET_SYSLOG) {
if (k < 0) {
if (k != -EAGAIN)
} else if (k > 0)
r++;
}
if (k <= 0 &&
(log_target == LOG_TARGET_AUTO ||
log_target == LOG_TARGET_KMSG)) {
if (k < 0) {
} else if (k > 0)
r++;
}
if (k <= 0) {
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_metav(
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;
}
int log_meta(
int level,
const char*file,
int line,
const char *func,
const char *format, ...) {
int r;
return r;
}
_noreturn_ static void log_assert(const char *text, const char *file, int line, const char *func, const char *format) {
abort();
}
}
_noreturn_ void log_assert_failed_unreachable(const char *text, const char *file, int line, const char *func) {
log_assert(text, file, line, func, "Code should not be reached '%s' at %s:%u, function %s(). Aborting.");
}
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 t;
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;
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;
}
static const char *const log_target_table[] = {
[LOG_TARGET_CONSOLE] = "console",
[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_NULL] = "null"
};