| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <threads.h> |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "env-util.h" |
| 7 |
#include "iovec-util.h" |
| 8 |
#include "log.h" |
| 9 |
#include "log-context.h" |
| 10 |
#include "string-util.h" |
| 11 |
#include "strv.h" |
| 12 |
|
| 13 |
static int saved_log_context_enabled = -1; |
| 14 |
static thread_local LIST_HEAD(LogContext, _log_context) = NULL; |
| 15 |
static thread_local size_t _log_context_num_fields = 0; |
| 16 |
|
| 17 |
bool log_context_enabled(void) { |
| 18 |
int r; |
| 19 |
|
| 20 |
if (log_get_max_level() == LOG_DEBUG) |
| 21 |
return true; |
| 22 |
|
| 23 |
if (saved_log_context_enabled >= 0) |
| 24 |
return saved_log_context_enabled; |
| 25 |
|
| 26 |
r = secure_getenv_bool("SYSTEMD_ENABLE_LOG_CONTEXT"); |
| 27 |
if (r < 0 && r != -ENXIO) |
| 28 |
log_debug_errno(r, "Failed to parse $SYSTEMD_ENABLE_LOG_CONTEXT, ignoring: %m"); |
| 29 |
|
| 30 |
saved_log_context_enabled = r > 0; |
| 31 |
|
| 32 |
return saved_log_context_enabled; |
| 33 |
} |
| 34 |
|
| 35 |
static LogContext* log_context_attach(LogContext *c) { |
| 36 |
assert(c); |
| 37 |
|
| 38 |
_log_context_num_fields += strv_length(c->fields); |
| 39 |
_log_context_num_fields += c->n_input_iovec; |
| 40 |
_log_context_num_fields += !!c->key; |
| 41 |
|
| 42 |
return LIST_PREPEND(ll, _log_context, c); |
| 43 |
} |
| 44 |
|
| 45 |
static LogContext* log_context_detach(LogContext *c) { |
| 46 |
if (!c) |
| 47 |
return NULL; |
| 48 |
|
| 49 |
assert(_log_context_num_fields >= strv_length(c->fields) + c->n_input_iovec +!!c->key); |
| 50 |
_log_context_num_fields -= strv_length(c->fields); |
| 51 |
_log_context_num_fields -= c->n_input_iovec; |
| 52 |
_log_context_num_fields -= !!c->key; |
| 53 |
|
| 54 |
LIST_REMOVE(ll, _log_context, c); |
| 55 |
return NULL; |
| 56 |
} |
| 57 |
|
| 58 |
LogContext* log_context_new(const char *key, const char *value) { |
| 59 |
assert(key); |
| 60 |
assert(endswith(key, "=")); |
| 61 |
|
| 62 |
if (!value) |
| 63 |
return NULL; |
| 64 |
|
| 65 |
LIST_FOREACH(ll, i, _log_context) |
| 66 |
if (i->key == key && i->value == value) |
| 67 |
return log_context_ref(i); |
| 68 |
|
| 69 |
LogContext *c = new(LogContext, 1); |
| 70 |
if (!c) |
| 71 |
return NULL; |
| 72 |
|
| 73 |
*c = (LogContext) { |
| 74 |
.n_ref = 1, |
| 75 |
.key = (char *) key, |
| 76 |
.value = (char *) value, |
| 77 |
}; |
| 78 |
|
| 79 |
return log_context_attach(c); |
| 80 |
} |
| 81 |
|
| 82 |
LogContext* log_context_new_strv(char **fields, bool owned) { |
| 83 |
if (!fields) |
| 84 |
return NULL; |
| 85 |
|
| 86 |
LIST_FOREACH(ll, i, _log_context) |
| 87 |
if (i->fields == fields) { |
| 88 |
assert(!owned); |
| 89 |
return log_context_ref(i); |
| 90 |
} |
| 91 |
|
| 92 |
LogContext *c = new(LogContext, 1); |
| 93 |
if (!c) |
| 94 |
return NULL; |
| 95 |
|
| 96 |
*c = (LogContext) { |
| 97 |
.n_ref = 1, |
| 98 |
.fields = fields, |
| 99 |
.owned = owned, |
| 100 |
}; |
| 101 |
|
| 102 |
return log_context_attach(c); |
| 103 |
} |
| 104 |
|
| 105 |
LogContext* log_context_new_iov(struct iovec *input_iovec, size_t n_input_iovec, bool owned) { |
| 106 |
if (!input_iovec || n_input_iovec == 0) |
| 107 |
return NULL; |
| 108 |
|
| 109 |
LIST_FOREACH(ll, i, _log_context) |
| 110 |
if (i->input_iovec == input_iovec && i->n_input_iovec == n_input_iovec) { |
| 111 |
assert(!owned); |
| 112 |
return log_context_ref(i); |
| 113 |
} |
| 114 |
|
| 115 |
LogContext *c = new(LogContext, 1); |
| 116 |
if (!c) |
| 117 |
return NULL; |
| 118 |
|
| 119 |
*c = (LogContext) { |
| 120 |
.n_ref = 1, |
| 121 |
.input_iovec = input_iovec, |
| 122 |
.n_input_iovec = n_input_iovec, |
| 123 |
.owned = owned, |
| 124 |
}; |
| 125 |
|
| 126 |
return log_context_attach(c); |
| 127 |
} |
| 128 |
|
| 129 |
static LogContext* log_context_free(LogContext *c) { |
| 130 |
if (!c) |
| 131 |
return NULL; |
| 132 |
|
| 133 |
log_context_detach(c); |
| 134 |
|
| 135 |
if (c->owned) { |
| 136 |
strv_free(c->fields); |
| 137 |
iovec_array_free(c->input_iovec, c->n_input_iovec); |
| 138 |
free(c->key); |
| 139 |
free(c->value); |
| 140 |
} |
| 141 |
|
| 142 |
return mfree(c); |
| 143 |
} |
| 144 |
|
| 145 |
DEFINE_TRIVIAL_REF_UNREF_FUNC(LogContext, log_context, log_context_free); |
| 146 |
|
| 147 |
LogContext* log_context_new_strv_consume(char **fields) { |
| 148 |
LogContext *c = log_context_new_strv(fields, /* owned= */ true); |
| 149 |
if (!c) |
| 150 |
strv_free(fields); |
| 151 |
|
| 152 |
return c; |
| 153 |
} |
| 154 |
|
| 155 |
LogContext* log_context_new_iov_consume(struct iovec *input_iovec, size_t n_input_iovec) { |
| 156 |
LogContext *c = log_context_new_iov(input_iovec, n_input_iovec, /* owned= */ true); |
| 157 |
if (!c) |
| 158 |
iovec_array_free(input_iovec, n_input_iovec); |
| 159 |
|
| 160 |
return c; |
| 161 |
} |
| 162 |
|
| 163 |
LogContext* log_context_head(void) { |
| 164 |
return _log_context; |
| 165 |
} |
| 166 |
|
| 167 |
size_t log_context_num_contexts(void) { |
| 168 |
size_t n = 0; |
| 169 |
|
| 170 |
LIST_FOREACH(ll, c, _log_context) |
| 171 |
n++; |
| 172 |
|
| 173 |
return n; |
| 174 |
} |
| 175 |
|
| 176 |
size_t log_context_num_fields(void) { |
| 177 |
return _log_context_num_fields; |
| 178 |
} |
| 179 |
|
| 180 |
void log_context_swap(LogContext **log_context, size_t *num_fields) { |
| 181 |
assert(log_context); |
| 182 |
assert(num_fields); |
| 183 |
|
| 184 |
SWAP_TWO(_log_context, *log_context); |
| 185 |
SWAP_TWO(_log_context_num_fields, *num_fields); |
| 186 |
} |
| 187 |
|
| 188 |
void _reset_log_level(int *saved_log_level) { |
| 189 |
assert(saved_log_level); |
| 190 |
|
| 191 |
log_set_max_level(*saved_log_level); |
| 192 |
} |