Debian Codegraph @

src/journal/journald-config.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-event.h"
4
5 #include "conf-parser.h"
6 #include "creds-util.h"
7 #include "daemon-util.h"
8 #include "initrd-util.h"
9 #include "journald-audit.h"
10 #include "journald-config.h"
11 #include "journald-context.h"
12 #include "journald-kmsg.h"
13 #include "journald-manager.h"
14 #include "journald-socket.h"
15 #include "log.h"
16 #include "parse-util.h"
17 #include "proc-cmdline.h"
18 #include "socket-netlink.h"
19 #include "string-table.h"
20 #include "string-util.h"
21 #include "syslog-util.h"
22 #include "time-util.h"
23
24 #define DEFAULT_SYNC_INTERVAL_USEC (5*USEC_PER_MINUTE)
25 #define DEFAULT_RATE_LIMIT_INTERVAL (30*USEC_PER_SEC)
26 #define DEFAULT_RATE_LIMIT_BURST 10000
27 #define DEFAULT_MAX_FILE_USEC USEC_PER_MONTH
28
29 /* Pick a good default that is likely to fit into AF_UNIX and AF_INET SOCK_DGRAM datagrams, and even leaves some room
30 * for a bit of additional metadata. */
31 #define DEFAULT_LINE_MAX (48*1024)
32
33 void journal_config_done(JournalConfig *c) {
34 assert(c);
35
36 free(c->tty_path);
37 }
38
39 void journal_config_set_defaults(JournalConfig *c) {
40 assert(c);
41
42 journal_config_done(c);
43
44 *c = (JournalConfig) {
45 .storage = _STORAGE_INVALID,
46 .compress.enabled = -1,
47 .compress.threshold_bytes = UINT64_MAX,
48 .seal = -1,
49 .read_kmsg = -1,
50 .set_audit = _AUDIT_SET_MODE_INVALID,
51 .ratelimit_interval = DEFAULT_RATE_LIMIT_INTERVAL,
52 .ratelimit_burst = DEFAULT_RATE_LIMIT_BURST,
53 .forward_to_syslog = -1,
54 .forward_to_kmsg = -1,
55 .forward_to_console = -1,
56 .forward_to_wall = -1,
57 .max_level_store = -1,
58 .max_level_syslog = -1,
59 .max_level_kmsg = -1,
60 .max_level_console = -1,
61 .max_level_wall = -1,
62 .max_level_socket = -1,
63 .split_mode = _SPLIT_INVALID,
64 };
65
66 journal_reset_metrics(&c->system_storage_metrics);
67 journal_reset_metrics(&c->runtime_storage_metrics);
68 }
69
70 static void manager_merge_journal_compress_options(Manager *m) {
71 assert(m);
72
73 if (m->config_by_cmdline.compress.enabled >= 0)
74 m->config.compress = m->config_by_cmdline.compress;
75 else if (m->config_by_conf.compress.enabled >= 0)
76 m->config.compress = m->config_by_conf.compress;
77 else if (m->config_by_cred.compress.enabled >= 0)
78 m->config.compress = m->config_by_cred.compress;
79 else
80 m->config.compress = (JournalCompressOptions) {
81 .enabled = true,
82 .threshold_bytes = UINT64_MAX,
83 };
84 }
85
86 static void manager_merge_forward_to_socket(Manager *m) {
87 assert(m);
88
89 if (m->config_by_cmdline.forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC)
90 m->config.forward_to_socket = m->config_by_cmdline.forward_to_socket;
91 else if (m->config_by_conf.forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC)
92 m->config.forward_to_socket = m->config_by_conf.forward_to_socket;
93 else if (m->config_by_cred.forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC)
94 m->config.forward_to_socket = m->config_by_cred.forward_to_socket;
95 else
96 m->config.forward_to_socket = (SocketAddress) {};
97 }
98
99 #define MERGE_NON_NEGATIVE(name, default_value) \
100 m->config.name = \
101 m->config_by_cmdline.name >= 0 ? m->config_by_cmdline.name : \
102 m->config_by_conf.name >= 0 ? m->config_by_conf.name : \
103 m->config_by_cred.name >= 0 ? m->config_by_cred.name : \
104 default_value
105
106 #define MERGE_NON_ZERO(name, default_value) \
107 m->config.name = \
108 m->config_by_cmdline.name ?: \
109 m->config_by_conf.name ?: \
110 m->config_by_cred.name ?: \
111 default_value
112
113 void manager_merge_configs(Manager *m) {
114 assert(m);
115
116 /* From highest to lowest priority: cmdline, conf, cred */
117
118 journal_config_done(&m->config);
119
120 MERGE_NON_NEGATIVE(storage, JOURNAL_STORAGE_DEFAULT_VAL);
121
122 /* In the initrd, only 'none' and 'volatile' make sense. */
123 if (in_initrd() && IN_SET(m->config.storage, STORAGE_PERSISTENT, STORAGE_AUTO))
124 m->config.storage = STORAGE_VOLATILE;
125
126 manager_merge_journal_compress_options(m);
127 MERGE_NON_NEGATIVE(seal, true);
128 /* By default, /dev/kmsg is read only by the main namespace instance. */
129 MERGE_NON_NEGATIVE(read_kmsg, !m->namespace);
130 /* By default, kernel auditing is enabled by the main namespace instance, and not controlled by
131 * non-default namespace instances. */
132 MERGE_NON_NEGATIVE(set_audit, m->namespace ? AUDIT_KEEP : AUDIT_YES);
133 MERGE_NON_ZERO(sync_interval_usec, DEFAULT_SYNC_INTERVAL_USEC);
134
135 /* TODO: also merge them when comdline or credentials support to configure them. */
136 m->config.ratelimit_interval = m->config_by_conf.ratelimit_interval;
137 m->config.ratelimit_burst = m->config_by_conf.ratelimit_burst;
138 m->config.system_storage_metrics = m->config_by_conf.system_storage_metrics;
139 m->config.runtime_storage_metrics = m->config_by_conf.runtime_storage_metrics;
140
141 MERGE_NON_ZERO(max_retention_usec, 0);
142 MERGE_NON_ZERO(max_file_usec, DEFAULT_MAX_FILE_USEC);
143 MERGE_NON_NEGATIVE(forward_to_syslog, false);
144 MERGE_NON_NEGATIVE(forward_to_kmsg, false);
145 MERGE_NON_NEGATIVE(forward_to_console, false);
146 MERGE_NON_NEGATIVE(forward_to_wall, true);
147 manager_merge_forward_to_socket(m);
148
149 if (strdup_to(&m->config.tty_path,
150 m->config_by_cmdline.tty_path ?:
151 m->config_by_conf.tty_path ?:
152 m->config_by_cred.tty_path) < 0)
153 log_oom_debug();
154
155 MERGE_NON_NEGATIVE(max_level_store, LOG_DEBUG);
156 MERGE_NON_NEGATIVE(max_level_syslog, LOG_DEBUG);
157 MERGE_NON_NEGATIVE(max_level_kmsg, LOG_NOTICE);
158 MERGE_NON_NEGATIVE(max_level_console, LOG_INFO);
159 MERGE_NON_NEGATIVE(max_level_wall, LOG_EMERG);
160 MERGE_NON_NEGATIVE(max_level_socket, LOG_DEBUG);
161 MERGE_NON_NEGATIVE(split_mode, SPLIT_UID);
162 MERGE_NON_ZERO(line_max, DEFAULT_LINE_MAX);
163 }
164
165 static void manager_adjust_configs(Manager *m) {
166 assert(m);
167
168 if ((m->config.ratelimit_interval == 0) != (m->config.ratelimit_burst == 0)) { /* One set to 0 and the other not? */
169 log_debug("Setting both rate limit interval and burst from %s/%u to 0/0",
170 FORMAT_TIMESPAN(m->config.ratelimit_interval, USEC_PER_SEC),
171 m->config.ratelimit_burst);
172 m->config.ratelimit_interval = 0;
173 m->config.ratelimit_burst = 0;
174 }
175
176 /* copy metrics to manager */
177 m->system_storage.metrics = m->config.system_storage_metrics;
178 m->runtime_storage.metrics = m->config.runtime_storage_metrics;
179
180 if (m->config.forward_to_socket.sockaddr.sa.sa_family != AF_UNSPEC && m->namespace) {
181 log_debug("ForwardToSocket= is not supported in non-default namespace instance.");
182 m->config.forward_to_socket = (SocketAddress) {};
183 }
184 }
185
186 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
187 JournalConfig *c = ASSERT_PTR(data);
188 int r;
189
190 if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_syslog")) {
191
192 r = value ? parse_boolean(value) : true;
193 if (r < 0)
194 log_warning("Failed to parse forward to syslog switch \"%s\". Ignoring.", value);
195 else
196 c->forward_to_syslog = r;
197
198 } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_kmsg")) {
199
200 r = value ? parse_boolean(value) : true;
201 if (r < 0)
202 log_warning("Failed to parse forward to kmsg switch \"%s\". Ignoring.", value);
203 else
204 c->forward_to_kmsg = r;
205
206 } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_console")) {
207
208 r = value ? parse_boolean(value) : true;
209 if (r < 0)
210 log_warning("Failed to parse forward to console switch \"%s\". Ignoring.", value);
211 else
212 c->forward_to_console = r;
213
214 } else if (proc_cmdline_key_streq(key, "systemd.journald.forward_to_wall")) {
215
216 r = value ? parse_boolean(value) : true;
217 if (r < 0)
218 log_warning("Failed to parse forward to wall switch \"%s\". Ignoring.", value);
219 else
220 c->forward_to_wall = r;
221
222 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_console")) {
223
224 if (proc_cmdline_value_missing(key, value))
225 return 0;
226
227 r = log_level_from_string(value);
228 if (r < 0)
229 log_warning("Failed to parse max level console value \"%s\". Ignoring.", value);
230 else
231 c->max_level_console = r;
232
233 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_store")) {
234
235 if (proc_cmdline_value_missing(key, value))
236 return 0;
237
238 r = log_level_from_string(value);
239 if (r < 0)
240 log_warning("Failed to parse max level store value \"%s\". Ignoring.", value);
241 else
242 c->max_level_store = r;
243
244 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_syslog")) {
245
246 if (proc_cmdline_value_missing(key, value))
247 return 0;
248
249 r = log_level_from_string(value);
250 if (r < 0)
251 log_warning("Failed to parse max level syslog value \"%s\". Ignoring.", value);
252 else
253 c->max_level_syslog = r;
254
255 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_kmsg")) {
256
257 if (proc_cmdline_value_missing(key, value))
258 return 0;
259
260 r = log_level_from_string(value);
261 if (r < 0)
262 log_warning("Failed to parse max level kmsg value \"%s\". Ignoring.", value);
263 else
264 c->max_level_kmsg = r;
265
266 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_wall")) {
267
268 if (proc_cmdline_value_missing(key, value))
269 return 0;
270
271 r = log_level_from_string(value);
272 if (r < 0)
273 log_warning("Failed to parse max level wall value \"%s\". Ignoring.", value);
274 else
275 c->max_level_wall = r;
276
277 } else if (proc_cmdline_key_streq(key, "systemd.journald.max_level_socket")) {
278
279 if (proc_cmdline_value_missing(key, value))
280 return 0;
281
282 r = log_level_from_string(value);
283 if (r < 0)
284 log_warning("Failed to parse max level socket value \"%s\". Ignoring.", value);
285 else
286 c->max_level_socket = r;
287
288 } else if (startswith(key, "systemd.journald"))
289 log_warning("Unknown journald kernel command line option \"%s\". Ignoring.", key);
290
291 /* do not warn about state here, since probably systemd already did */
292 return 0;
293 }
294
295 static void manager_parse_config_file(Manager *m) {
296 const char *conf_file;
297
298 assert(m);
299
300 if (m->namespace)
301 conf_file = strjoina("systemd/journald@", m->namespace, ".conf");
302 else
303 conf_file = "systemd/journald.conf";
304
305 (void) config_parse_standard_file_with_dropins(
306 conf_file,
307 "Journal\0",
308 config_item_perf_lookup,
309 journald_gperf_lookup,
310 CONFIG_PARSE_WARN,
311 &m->config_by_conf);
312 }
313
314 static void manager_load_credentials(JournalConfig *c) {
315 _cleanup_free_ char *data = NULL;
316 int r;
317
318 assert(c);
319
320 r = read_credential("journal.forward_to_socket", (void**) &data, /* ret_size= */ NULL);
321 if (r < 0)
322 log_debug_errno(r, "Failed to read credential 'journal.forward_to_socket', ignoring: %m");
323 else {
324 r = socket_address_parse(&c->forward_to_socket, data);
325 if (r < 0)
326 log_debug_errno(r, "Failed to parse journal.forward_to_socket credential, ignoring: %s", data);
327 }
328
329 data = mfree(data);
330
331 r = read_credential("journal.storage", (void**) &data, /* ret_size= */ NULL);
332 if (r < 0)
333 log_debug_errno(r, "Failed to read credential journal.storage, ignoring: %m");
334 else {
335 r = storage_from_string(data);
336 if (r < 0)
337 log_debug_errno(r, "Failed to parse journal.storage credential, ignoring: %s", data);
338 else
339 c->storage = r;
340 }
341 }
342
343 void manager_load_config(Manager *m) {
344 int r;
345
346 assert(m);
347
348 manager_load_credentials(&m->config_by_cred);
349 manager_parse_config_file(m);
350
351 if (!m->namespace) {
352 /* Parse kernel command line, but only if we are not a namespace instance */
353 r = proc_cmdline_parse(parse_proc_cmdline_item, &m->config_by_cmdline, PROC_CMDLINE_STRIP_RD_PREFIX);
354 if (r < 0)
355 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
356 }
357
358 manager_merge_configs(m);
359 manager_adjust_configs(m);
360 }
361
362 static void manager_reload_config(Manager *m) {
363 assert(m);
364
365 journal_config_set_defaults(&m->config_by_conf);
366 manager_parse_config_file(m);
367 manager_merge_configs(m);
368 manager_adjust_configs(m);
369 }
370
371 int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
372 Manager *m = ASSERT_PTR(userdata);
373
374 (void) notify_reloading();
375
376 _cleanup_(journal_config_done) JournalConfig old = TAKE_STRUCT(m->config);
377
378 manager_reload_config(m);
379
380 (void) manager_reopen_dev_kmsg(m, old.read_kmsg);
381 manager_reset_kernel_audit(m, old.set_audit);
382 manager_reload_forward_socket(m, &old.forward_to_socket);
383 manager_refresh_client_contexts_on_reload(m, old.ratelimit_interval, old.ratelimit_burst);
384 manager_reopen_journals(m, &old);
385
386 log_info("Config file reloaded.");
387 (void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE);
388
389 return 0;
390 }
391
392 static const char* const storage_table[_STORAGE_MAX] = {
393 [STORAGE_AUTO] = "auto",
394 [STORAGE_VOLATILE] = "volatile",
395 [STORAGE_PERSISTENT] = "persistent",
396 [STORAGE_NONE] = "none"
397 };
398
399 DEFINE_STRING_TABLE_LOOKUP(storage, Storage);
400 DEFINE_CONFIG_PARSE_ENUM(config_parse_storage, storage, Storage);
401
402 static const char* const split_mode_table[_SPLIT_MAX] = {
403 [SPLIT_LOGIN] = "login",
404 [SPLIT_UID] = "uid",
405 [SPLIT_NONE] = "none",
406 };
407
408 DEFINE_STRING_TABLE_LOOKUP(split_mode, SplitMode);
409 DEFINE_CONFIG_PARSE_ENUM(config_parse_split_mode, split_mode, SplitMode);
410
411 static const char* const audit_set_mode_table[_AUDIT_SET_MODE_MAX] = {
412 [AUDIT_NO] = "no",
413 [AUDIT_YES] = "yes",
414 [AUDIT_KEEP] = "keep",
415 };
416
417 DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING_WITH_BOOLEAN(audit_set_mode, AuditSetMode, AUDIT_YES);
418 /* For backward compatibility, an empty string has special meaning and equals to 'keep'. */
419 DEFINE_CONFIG_PARSE_ENUM_WITH_DEFAULT(config_parse_audit_set_mode, audit_set_mode, AuditSetMode, AUDIT_KEEP);
420
421 int config_parse_line_max(
422 const char *unit,
423 const char *filename,
424 unsigned line,
425 const char *section,
426 unsigned section_line,
427 const char *lvalue,
428 int ltype,
429 const char *rvalue,
430 void *data,
431 void *userdata) {
432
433 size_t *sz = ASSERT_PTR(data);
434 int r;
435
436 if (isempty(rvalue)) {
437 /* Empty assignment means default */
438 *sz = DEFAULT_LINE_MAX;
439 return 0;
440 }
441
442 uint64_t v;
443 r = parse_size(rvalue, 1024, &v);
444 if (r < 0)
445 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
446
447 if (v < 79) {
448 /* Why specify 79 here as minimum line length? Simply, because the most common traditional
449 * terminal size is 80ch, and it might make sense to break one character before the natural
450 * line break would occur on that. */
451 log_syntax(unit, LOG_WARNING, filename, line, 0, "LineMax= too small, clamping to 79: %s", rvalue);
452 *sz = 79;
453 } else if (v > (uint64_t) (SSIZE_MAX-1)) {
454 /* So, why specify SSIZE_MAX-1 here? Because that's one below the largest size value read()
455 * can return, and we need one extra byte for the trailing NUL byte. Of course IRL such large
456 * memory allocations will fail anyway, hence this limit is mostly theoretical anyway, as we'll
457 * fail much earlier anyway. */
458 log_syntax(unit, LOG_WARNING, filename, line, 0, "LineMax= too large, clamping to %" PRIu64 ": %s", (uint64_t) (SSIZE_MAX-1), rvalue);
459 *sz = SSIZE_MAX-1;
460 } else
461 *sz = (size_t) v;
462
463 return 0;
464 }
465
466 int config_parse_compress(
467 const char *unit,
468 const char *filename,
469 unsigned line,
470 const char *section,
471 unsigned section_line,
472 const char *lvalue,
473 int ltype,
474 const char *rvalue,
475 void *data,
476 void *userdata) {
477
478 JournalCompressOptions *compress = ASSERT_PTR(data);
479 int r;
480
481 if (isempty(rvalue)) {
482 compress->enabled = -1;
483 compress->threshold_bytes = UINT64_MAX;
484 return 0;
485 }
486
487 if (streq(rvalue, "1")) {
488 log_syntax(unit, LOG_WARNING, filename, line, 0,
489 "Compress= ambiguously specified as 1, enabling compression with default threshold.");
490 compress->enabled = true;
491 compress->threshold_bytes = UINT64_MAX;
492 return 0;
493 }
494
495 if (streq(rvalue, "0")) {
496 log_syntax(unit, LOG_WARNING, filename, line, 0,
497 "Compress= ambiguously specified as 0, disabling compression.");
498 compress->enabled = false;
499 compress->threshold_bytes = UINT64_MAX;
500 return 0;
501 }
502
503 r = parse_boolean(rvalue);
504 if (r >= 0) {
505 compress->enabled = r;
506 compress->threshold_bytes = UINT64_MAX;
507 return 0;
508 }
509
510 r = parse_size(rvalue, 1024, &compress->threshold_bytes);
511 if (r < 0)
512 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
513
514 compress->enabled = true;
515 return 0;
516 }
517
518 int config_parse_forward_to_socket(
519 const char *unit,
520 const char *filename,
521 unsigned line,
522 const char *section,
523 unsigned section_line,
524 const char *lvalue,
525 int ltype,
526 const char *rvalue,
527 void *data,
528 void *userdata) {
529
530 SocketAddress *addr = ASSERT_PTR(data);
531 int r;
532
533 if (isempty(rvalue)) {
534 *addr = (SocketAddress) {};
535 return 0;
536 }
537
538 r = socket_address_parse(addr, rvalue);
539 if (r < 0)
540 return log_syntax_parse_error(unit, filename, line, r, lvalue, rvalue);
541
542 return 0;
543 }