HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/basic/log.c
Git blob: 17ea26d8a410ee2bdf145253a920ce9273ae8b41
Lines 1001–1751 of 1,751
const char *func,
const char *format, ...) {
char buf[LINE_MAX];
bool found = false;
PROTECT_ERRNO;
va_list ap;
if (_likely_(LOG_PRI(level) > log_max_level) ||
log_target == LOG_TARGET_NULL)
return -ERRNO_VALUE(error);
if (LOG_FAC(level) == 0)
level |= log_facility;
if (IN_SET(log_target,
LOG_TARGET_AUTO,
LOG_TARGET_JOURNAL_OR_KMSG,
LOG_TARGET_JOURNAL)) {
if (open_when_needed)
log_open_journal();
if (journal_fd >= 0) {
char header[LINE_MAX];
struct iovec *iovec;
size_t n = 0, m, iovec_len;
int r;
bool fallback = false;
if (!MUL_SAFE(&iovec_len, log_context_num_fields(), 3) ||
!ADD_SAFE(&iovec_len, iovec_len, 17))
iovec_len = IOVEC_MAX;
else
iovec_len = MIN(iovec_len, IOVEC_MAX);
iovec = newa(struct iovec, iovec_len);
/* If the journal is available do structured logging.
* Do not report the errno if it is synthetic. */
log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
iovec[n++] = IOVEC_MAKE_STRING(header);
va_start(ap, format);
DISABLE_WARNING_FORMAT_NONLITERAL;
r = log_format_iovec(iovec, iovec_len, &n, true, error, format, ap);
REENABLE_WARNING;
m = n;
if (r < 0)
fallback = true;
else {
log_do_context(iovec, iovec_len, &n);
const struct msghdr msghdr = {
.msg_iov = iovec,
.msg_iovlen = n,
};
(void) sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL);
}
va_end(ap);
for (size_t i = 1; i < m; i += 2)
free(iovec[i].iov_base);
if (!fallback) {
if (open_when_needed)
log_close();
return -ERRNO_VALUE(error);
}
}
}
/* Fallback if journal logging is not available or didn't work. */
va_start(ap, format);
while (format) {
va_list aq;
errno = ERRNO_VALUE(error);
va_copy(aq, ap);
DISABLE_WARNING_FORMAT_NONLITERAL;
(void) vsnprintf(buf, sizeof buf, format, aq);
REENABLE_WARNING;
va_end(aq);
if (startswith(buf, "MESSAGE=")) {
found = true;
break;
}
VA_FORMAT_ADVANCE(format, ap);
format = va_arg(ap, char *);
}
va_end(ap);
if (!found) {
if (open_when_needed)
log_close();
return -ERRNO_VALUE(error);
}
return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, buf + 8);
}
int log_struct_iovec_internal(
int level,
int error,
const char *file,
int line,
const char *func,
const struct iovec input_iovec[],
size_t n_input_iovec) {
PROTECT_ERRNO;
if (_likely_(LOG_PRI(level) > log_max_level) ||
log_target == LOG_TARGET_NULL)
return -ERRNO_VALUE(error);
if (LOG_FAC(level) == 0)
level |= log_facility;
if (IN_SET(log_target, LOG_TARGET_AUTO,
LOG_TARGET_JOURNAL_OR_KMSG,
LOG_TARGET_JOURNAL) &&
journal_fd >= 0) {
char header[LINE_MAX];
struct iovec *iovec;
size_t n = 0, iovec_len, n_context_iovec, n_input_iovec_expanded;
if (!MUL_SAFE(&n_input_iovec_expanded, n_input_iovec, 2) ||
!MUL_SAFE(&n_context_iovec, log_context_num_fields(), 3) ||
!ADD_SAFE(&iovec_len, 1, n_input_iovec_expanded) ||
!ADD_SAFE(&iovec_len, iovec_len, n_context_iovec))
iovec_len = IOVEC_MAX;
else
iovec_len = MIN(iovec_len, IOVEC_MAX);
iovec = newa(struct iovec, iovec_len);
log_do_header(header, sizeof(header), level, error, file, line, func, NULL, NULL, NULL, NULL);
iovec[n++] = IOVEC_MAKE_STRING(header);
for (size_t i = 0; i < n_input_iovec && n + 2 <= iovec_len; i++) {
iovec[n++] = input_iovec[i];
iovec[n++] = IOVEC_MAKE_STRING("\n");
}
log_do_context(iovec, iovec_len, &n);
const struct msghdr msghdr = {
.msg_iov = iovec,
.msg_iovlen = n,
};
if (sendmsg(journal_fd, &msghdr, MSG_NOSIGNAL) >= 0)
return -ERRNO_VALUE(error);
}
for (size_t i = 0; i < n_input_iovec; i++)
if (memory_startswith(input_iovec[i].iov_base, input_iovec[i].iov_len, "MESSAGE=")) {
char *m;
m = strndupa_safe((char*) input_iovec[i].iov_base + STRLEN("MESSAGE="),
input_iovec[i].iov_len - STRLEN("MESSAGE="));
return log_dispatch_internal(level, error, file, line, func, NULL, NULL, NULL, NULL, m);
}
/* Couldn't find MESSAGE=. */
return -ERRNO_VALUE(error);
}
int log_set_target_from_string(const char *e) {
LogTarget t;
t = log_target_from_string(e);
if (t < 0)
return t;
log_set_target(t);
return 0;
}
int log_set_max_level_from_string(const char *e) {
int r;
for (;;) {
_cleanup_free_ char *word = NULL, *prefix = NULL;
LogTarget target;
const char *colon;
r = extract_first_word(&e, &word, ",", 0);
if (r < 0)
return r;
if (r == 0)
break;
colon = strchr(word, ':');
if (!colon) {
r = log_level_from_string(word);
if (r < 0)
return r;
log_set_max_level(r);
continue;
}
prefix = strndup(word, colon - word);
if (!prefix)
return -ENOMEM;
target = log_target_from_string(prefix);
if (target < 0)
return target;
if (target >= _LOG_TARGET_SINGLE_MAX)
return -EINVAL;
r = log_level_from_string(colon + 1);
if (r < 0)
return r;
log_target_max_level[target] = r;
}
return 0;
}
int log_max_levels_to_string(int level, char **ret) {
_cleanup_free_ char *s = NULL;
int r;
assert(ret);
r = log_level_to_string_alloc(level, &s);
if (r < 0)
return r;
for (LogTarget target = 0; target < _LOG_TARGET_SINGLE_MAX; target++) {
_cleanup_free_ char *l = NULL;
if (log_target_max_level[target] == INT_MAX)
continue;
r = log_level_to_string_alloc(log_target_max_level[target], &l);
if (r < 0)
return r;
r = strextendf_with_separator(&s, ",", "%s:%s", log_target_to_string(target), l);
if (r < 0)
return r;
}
*ret = TAKE_PTR(s);
return 0;
}
static int log_set_ratelimit_kmsg_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
ratelimit_kmsg = r;
return 0;
}
static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
/*
* 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
* status output to /dev/console, but does not alter the log
* level.
*/
if (streq(key, "debug") && !value)
log_set_max_level(LOG_DEBUG);
else if (proc_cmdline_key_streq(key, "systemd.log_target")) {
if (proc_cmdline_value_missing(key, value))
return 0;
if (log_set_target_from_string(value) < 0)
log_warning("Failed to parse log target '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_level")) {
if (proc_cmdline_value_missing(key, value))
return 0;
if (log_set_max_level_from_string(value) < 0)
log_warning("Failed to parse log level setting '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_color")) {
if (log_show_color_from_string(value ?: "1") < 0)
log_warning("Failed to parse log color setting '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_location")) {
if (log_show_location_from_string(value ?: "1") < 0)
log_warning("Failed to parse log location setting '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_tid")) {
if (log_show_tid_from_string(value ?: "1") < 0)
log_warning("Failed to parse log tid setting '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_time")) {
if (log_show_time_from_string(value ?: "1") < 0)
log_warning("Failed to parse log time setting '%s', ignoring.", value);
} else if (proc_cmdline_key_streq(key, "systemd.log_ratelimit_kmsg")) {
if (log_set_ratelimit_kmsg_from_string(value ?: "1") < 0)
log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", value);
}
return 0;
}
static bool should_parse_proc_cmdline(void) {
/* PID1 always reads the kernel command line. */
if (getpid_cached() == 1)
return true;
/* Otherwise, parse the command line if invoked directly by systemd. */
return invoked_by_systemd();
}
void log_parse_environment_variables(void) {
const char *e;
int r;
e = getenv("SYSTEMD_LOG_TARGET");
if (e && log_set_target_from_string(e) < 0)
log_warning("Failed to parse log target '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_LEVEL");
if (e) {
r = log_set_max_level_from_string(e);
if (r < 0)
log_warning_errno(r, "Failed to parse log level '%s', ignoring: %m", e);
} else {
/* If no explicit log level is specified then let's see if this is a debug invocation, and if
* so raise the log level to debug too. Note that this is not symmetric: just because
* DEBUG_INVOCATION is explicitly set to 0 we won't lower the log level below debug. This
* follows the logic that debug logging is an opt-in thing anyway, and if there's any reason
* to enable it we should not disable it here automatically. */
r = getenv_bool("DEBUG_INVOCATION");
if (r < 0 && r != -ENXIO)
log_warning_errno(r, "Failed to parse $DEBUG_INVOCATION value, ignoring: %m");
else if (r > 0)
log_set_max_level(LOG_DEBUG);
}
e = getenv("SYSTEMD_LOG_COLOR");
if (e && log_show_color_from_string(e) < 0)
log_warning("Failed to parse log color '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_LOCATION");
if (e && log_show_location_from_string(e) < 0)
log_warning("Failed to parse log location '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_TIME");
if (e && log_show_time_from_string(e) < 0)
log_warning("Failed to parse log time '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_TID");
if (e && log_show_tid_from_string(e) < 0)
log_warning("Failed to parse log tid '%s', ignoring.", e);
e = getenv("SYSTEMD_LOG_RATELIMIT_KMSG");
if (e && log_set_ratelimit_kmsg_from_string(e) < 0)
log_warning("Failed to parse log ratelimit kmsg boolean '%s', ignoring.", e);
}
void log_parse_environment(void) {
/* Do not call from library code. */
if (should_parse_proc_cmdline())
(void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX);
log_parse_environment_variables();
}
LogTarget log_get_target(void) {
return log_target;
}
void log_settle_target(void) {
/* If we're using LOG_TARGET_AUTO and opening the log again on every single log call, we'll check if
* stderr is attached to the journal every single log call. However, if we then close all file
* descriptors later, that will stop working because stderr will be closed as well. To avoid that
* problem, this function is used to permanently change the log target depending on whether stderr is
* connected to the journal or not. */
LogTarget t = log_get_target();
if (t != LOG_TARGET_AUTO)
return;
t = getpid_cached() == 1 || stderr_is_journal() ? (prohibit_ipc ? LOG_TARGET_KMSG : LOG_TARGET_JOURNAL_OR_KMSG)
: LOG_TARGET_CONSOLE;
log_set_target(t);
}
int log_get_max_level(void) {
return log_max_level;
}
int log_get_target_max_level(LogTarget target) {
assert(target >= 0);
assert(target < _LOG_TARGET_SINGLE_MAX);
return log_target_max_level[target];
}
void log_show_color(bool b) {
show_color = b;
}
bool log_get_show_color(void) {
return show_color > 0; /* Defaults to false. */
}
void log_show_location(bool b) {
show_location = b;
}
bool log_get_show_location(void) {
return show_location;
}
void log_show_time(bool b) {
show_time = b;
}
bool log_get_show_time(void) {
return show_time;
}
void log_show_tid(bool b) {
show_tid = b;
}
bool log_get_show_tid(void) {
return show_tid;
}
int log_show_color_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
log_show_color(r);
return 0;
}
int log_show_location_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
log_show_location(r);
return 0;
}
int log_show_time_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
log_show_time(r);
return 0;
}
int log_show_tid_from_string(const char *e) {
int r;
r = parse_boolean(e);
if (r < 0)
return r;
log_show_tid(r);
return 0;
}
bool log_on_console(void) {
if (IN_SET(log_target, LOG_TARGET_CONSOLE,
LOG_TARGET_CONSOLE_PREFIXED))
return true;
return syslog_fd < 0 && kmsg_fd < 0 && journal_fd < 0;
}
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_NULL] = "null",
};
DEFINE_STRING_TABLE_LOOKUP(log_target, LogTarget);
void log_received_signal(int level, const struct signalfd_siginfo *si) {
assert(si);
if (si_code_from_process(si->ssi_code) && pid_is_valid(si->ssi_pid)) {
_cleanup_free_ char *p = NULL;
(void) pid_get_comm(si->ssi_pid, &p);
log_full(level,
"Received SIG%s from PID %"PRIu32" (%s).",
signal_to_string(si->ssi_signo),
si->ssi_pid, strna(p));
} else
log_full(level,
"Received SIG%s.",
signal_to_string(si->ssi_signo));
}
void set_log_syntax_callback(log_syntax_callback_t cb, void *userdata) {
assert(!log_syntax_callback || !cb);
assert(!log_syntax_callback_userdata || !userdata);
log_syntax_callback = cb;
log_syntax_callback_userdata = userdata;
}
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, ...) {
PROTECT_ERRNO;
if (log_syntax_callback)
log_syntax_callback(unit, level, log_syntax_callback_userdata);
if (_likely_(LOG_PRI(level) > log_max_level) ||
log_target == LOG_TARGET_NULL)
return -ERRNO_VALUE(error);
char buffer[LINE_MAX];
va_list ap;
const char *unit_fmt = NULL;
errno = ERRNO_VALUE(error);
va_start(ap, format);
(void) vsnprintf(buffer, sizeof buffer, format, ap);
va_end(ap);
if (unit)
unit_fmt = getpid_cached() == 1 ? "UNIT=%s" : "USER_UNIT=%s";
if (config_file) {
if (config_line > 0)
return log_struct_internal(
level,
error,
file, line, func,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_ITEM("CONFIG_FILE=%s", config_file),
LOG_ITEM("CONFIG_LINE=%u", config_line),
LOG_MESSAGE("%s:%u: %s", config_file, config_line, buffer),
unit_fmt, unit,
NULL);
else
return log_struct_internal(
level,
error,
file, line, func,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_ITEM("CONFIG_FILE=%s", config_file),
LOG_MESSAGE("%s: %s", config_file, buffer),
unit_fmt, unit,
NULL);
} else if (unit)
return log_struct_internal(
level,
error,
file, line, func,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_MESSAGE("%s: %s", unit, buffer),
unit_fmt, unit,
NULL);
else
return log_struct_internal(
level,
error,
file, line, func,
LOG_MESSAGE_ID(SD_MESSAGE_INVALID_CONFIGURATION_STR),
LOG_MESSAGE("%s", buffer),
NULL);
}
int log_syntax_invalid_utf8_internal(
const char *unit,
int level,
const char *config_file,
unsigned config_line,
const char *file,
int line,
const char *func,
const char *rvalue) {
PROTECT_ERRNO;
_cleanup_free_ char *p = NULL;
if (rvalue)
p = utf8_escape_invalid(rvalue);
return log_syntax_internal(unit, level, config_file, config_line,
SYNTHETIC_ERRNO(EINVAL), file, line, func,
"String is not UTF-8 clean, ignoring assignment: %s", strna(p));
}
int log_syntax_parse_error_internal(
const char *unit,
const char *config_file,
unsigned config_line,
int error,
bool critical,
const char *file,
int line,
const char *func,
const char *lvalue,
const char *rvalue) {
PROTECT_ERRNO;
_cleanup_free_ char *escaped = NULL;
/* OOM is always handled as critical. */
if (ERRNO_VALUE(error) == ENOMEM)
return log_oom_internal(LOG_ERR, file, line, func);
if (rvalue && !utf8_is_valid(rvalue)) {
escaped = utf8_escape_invalid(rvalue);
if (!escaped)
rvalue = "(oom)";
else
rvalue = " (escaped)";
}
log_syntax_internal(unit, critical ? LOG_ERR : LOG_WARNING, config_file, config_line, error,
file, line, func,
"Failed to parse %s=%s%s%s%s%s",
strna(lvalue), strempty(escaped), strempty(rvalue),
critical ? "" : ", ignoring",
error == 0 ? "." : ": ",
error == 0 ? "" : STRERROR(error));
return critical ? -ERRNO_VALUE(error) : 0;
}
void log_set_upgrade_syslog_to_journal(bool b) {
upgrade_syslog_to_journal = b;
/* Make the change effective immediately */
if (b) {
if (log_target == LOG_TARGET_SYSLOG)
log_target = LOG_TARGET_JOURNAL;
else if (log_target == LOG_TARGET_SYSLOG_OR_KMSG)
log_target = LOG_TARGET_JOURNAL_OR_KMSG;
}
}
void log_set_always_reopen_console(bool b) {
always_reopen_console = b;
}
void log_set_open_when_needed(bool b) {
open_when_needed = b;
}
void log_set_prohibit_ipc(bool b) {
prohibit_ipc = b;
}
int log_emergency_level(void) {
/* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
* then the system of the whole system is obviously affected. */
return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
}
int log_dup_console(void) {
int copy;
/* Duplicate the fd we use for fd logging if it's < 3 and use the copy from now on. This call is useful
* whenever we want to continue logging through the original fd, but want to rearrange stderr. */
if (console_fd < 0 || console_fd >= 3)
return 0;
copy = fcntl(console_fd, F_DUPFD_CLOEXEC, 3);
if (copy < 0)
return -errno;
console_fd = copy;
return 0;
}
void log_setup(void) {
log_set_target(LOG_TARGET_AUTO);
log_parse_environment();
(void) log_open();
if (log_on_console() && show_color < 0)
log_show_color(true);
}
const char* _log_set_prefix(const char *prefix, bool force) {
const char *old = log_prefix;
if (prefix || force)
log_prefix = prefix;
return old;
}