log.c revision 543295ad369793bdac510c6c3bf4afae8f1cdab5
/*-*- 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 <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <printf.h>
#include "log.h"
#include "util.h"
#include "missing.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) {
int r;
union sockaddr_union sa = {
};
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) {
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;
}
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();
}
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 *object_name,
const char *object,
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 *object_name,
const char *object,
const char *buffer) {
};
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,
const char*file,
int line,
const char *func,
const char *object_name,
const char *object,
const char *buffer) {
if (kmsg_fd < 0)
return 0;
return -errno;
return 1;
}
int level,
const char *object_name, const char *object) {
"PRIORITY=%i\n"
"SYSLOG_FACILITY=%i\n"
"%s%.*s%s"
"%s%.*i%s"
"%s%.*s%s"
"%s%.*s%s"
"SYSLOG_IDENTIFIER=%s\n",
return 0;
}
static int write_to_journal(
int level,
const char*file,
int line,
const char *func,
const char *object_name,
const char *object,
const char *buffer) {
if (journal_fd < 0)
return 0;
return -errno;
return 1;
}
static int log_dispatch(
int level,
const char*file,
int line,
const char *func,
const char *object_name,
const char *object,
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_SAFE ||
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) {
/* This modifies the buffer... */
return 0;
}
int log_metav(
int level,
const char*file,
int line,
const char *func,
const char *format,
return 0;
}
int log_meta(
int level,
const char*file,
int line,
const char *func,
const char *format, ...) {
int r;
return r;
}
int log_metav_object(
int level,
const char*file,
int line,
const char *func,
const char *object_name,
const char *object,
const char *format,
return 0;
}
int log_meta_object(
int level,
const char*file,
int line,
const char *func,
const char *object_name,
const char *object,
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.");
}
return -ENOMEM;
}
int log_struct_internal(
int level,
const char *file,
int line,
const char *func,
const char *format, ...) {
int r;
return 0;
if (log_target == LOG_TARGET_NULL)
return 0;
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';
/* If the journal is available do structured logging */
char *buf;
/* We need to copy the va_list structure,
* since vasprintf() leaves it afterwards at
* an undefined location */
r = -ENOMEM;
goto finish;
}
/* Now, jump enough ahead, so that we point to
* the next format string */
n++;
}
mh.msg_iovlen = n;
r = -errno;
else
r = 1;
for (i = 1; i < n; i += 2)
} else {
bool found = false;
/* Fallback if journal logging is not available */
while (format) {
found = true;
break;
}
}
if (found)
else
r = -EINVAL;
}
return r;
}
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;
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;
}
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;
}
bool log_on_console(void) {
if (log_target == LOG_TARGET_CONSOLE)
return true;
}
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_SAFE] = "safe",
[LOG_TARGET_NULL] = "null"
};