Debian Codegraph @

src/journal/journalctl.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <locale.h>
4
5 #include "sd-journal.h"
6 #include "sd-varlink.h"
7
8 #include "build.h"
9 #include "dissect-image.h"
10 #include "extract-word.h"
11 #include "format-table.h"
12 #include "glob-util.h"
13 #include "help-util.h"
14 #include "id128-print.h"
15 #include "image-policy.h"
16 #include "journalctl.h"
17 #include "journalctl-authenticate.h"
18 #include "journalctl-catalog.h"
19 #include "journalctl-misc.h"
20 #include "journalctl-show.h"
21 #include "journalctl-varlink.h"
22 #include "journalctl-varlink-server.h"
23 #include "log.h"
24 #include "loop-util.h"
25 #include "main-func.h"
26 #include "mount-util.h"
27 #include "mountpoint-util.h"
28 #include "options.h"
29 #include "output-mode.h"
30 #include "pager.h"
31 #include "parse-argument.h"
32 #include "parse-util.h"
33 #include "pcre2-util.h"
34 #include "runtime-scope.h"
35 #include "set.h"
36 #include "static-destruct.h"
37 #include "string-table.h"
38 #include "string-util.h"
39 #include "strv.h"
40 #include "syslog-util.h"
41 #include "time-util.h"
42 #include "varlink-io.systemd.JournalAccess.h"
43 #include "varlink-util.h"
44
45 #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE)
46
47 enum {
48 /* Special values for arg_lines */
49 ARG_LINES_DEFAULT = -2,
50 ARG_LINES_ALL = -1,
51 };
52
53 JournalctlAction arg_action = ACTION_SHOW;
54 OutputMode arg_output = OUTPUT_SHORT;
55 sd_json_format_flags_t arg_json_format_flags = SD_JSON_FORMAT_OFF;
56 PagerFlags arg_pager_flags = 0;
57 bool arg_utc = false;
58 bool arg_follow = false;
59 bool arg_full = true;
60 bool arg_all = false;
61 int arg_lines = ARG_LINES_DEFAULT;
62 bool arg_lines_oldest = false;
63 bool arg_no_tail = false;
64 bool arg_truncate_newline = false;
65 bool arg_quiet = false;
66 bool arg_merge = false;
67 int arg_boot = -1; /* tristate */
68 sd_id128_t arg_boot_id = {};
69 int arg_boot_offset = 0;
70 bool arg_dmesg = false;
71 bool arg_no_hostname = false;
72 char *arg_cursor = NULL;
73 char *arg_cursor_file = NULL;
74 char *arg_after_cursor = NULL;
75 bool arg_show_cursor = false;
76 char *arg_directory = NULL;
77 char **arg_file = NULL;
78 bool arg_file_stdin = false;
79 int arg_priorities = 0;
80 Set *arg_facilities = NULL;
81 char *arg_verify_key = NULL;
82 #if HAVE_GCRYPT
83 usec_t arg_interval = DEFAULT_FSS_INTERVAL_USEC;
84 bool arg_force = false;
85 #endif
86 usec_t arg_since = 0;
87 usec_t arg_until = 0;
88 bool arg_since_set = false;
89 bool arg_until_set = false;
90 char **arg_syslog_identifier = NULL;
91 char **arg_exclude_identifier = NULL;
92 char **arg_system_units = NULL;
93 char **arg_user_units = NULL;
94 bool arg_invocation = false;
95 sd_id128_t arg_invocation_id = SD_ID128_NULL;
96 int arg_invocation_offset = 0;
97 char *arg_field = NULL;
98 bool arg_catalog = false;
99 bool arg_reverse = false;
100 int arg_journal_type = 0;
101 int arg_journal_additional_open_flags = 0;
102 int arg_namespace_flags = 0;
103 char *arg_root = NULL;
104 char *arg_image = NULL;
105 char *arg_machine = NULL;
106 char *arg_namespace = NULL;
107 uint64_t arg_vacuum_size = 0;
108 uint64_t arg_vacuum_n_files = 0;
109 usec_t arg_vacuum_time = 0;
110 Set *arg_output_fields = NULL;
111 char *arg_pattern = NULL;
112 pcre2_code *arg_compiled_pattern = NULL;
113 PatternCompileCase arg_case = PATTERN_COMPILE_CASE_AUTO;
114 ImagePolicy *arg_image_policy = NULL;
115 bool arg_synchronize_on_exit = false;
116
117 static bool arg_varlink = false;
118 RuntimeScope arg_varlink_runtime_scope = _RUNTIME_SCOPE_INVALID;
119
120 STATIC_DESTRUCTOR_REGISTER(arg_cursor, freep);
121 STATIC_DESTRUCTOR_REGISTER(arg_cursor_file, freep);
122 STATIC_DESTRUCTOR_REGISTER(arg_after_cursor, freep);
123 STATIC_DESTRUCTOR_REGISTER(arg_directory, freep);
124 STATIC_DESTRUCTOR_REGISTER(arg_file, strv_freep);
125 STATIC_DESTRUCTOR_REGISTER(arg_facilities, set_freep);
126 STATIC_DESTRUCTOR_REGISTER(arg_verify_key, erase_and_freep);
127 STATIC_DESTRUCTOR_REGISTER(arg_syslog_identifier, strv_freep);
128 STATIC_DESTRUCTOR_REGISTER(arg_exclude_identifier, strv_freep);
129 STATIC_DESTRUCTOR_REGISTER(arg_system_units, strv_freep);
130 STATIC_DESTRUCTOR_REGISTER(arg_user_units, strv_freep);
131 STATIC_DESTRUCTOR_REGISTER(arg_field, freep);
132 STATIC_DESTRUCTOR_REGISTER(arg_root, freep);
133 STATIC_DESTRUCTOR_REGISTER(arg_image, freep);
134 STATIC_DESTRUCTOR_REGISTER(arg_machine, freep);
135 STATIC_DESTRUCTOR_REGISTER(arg_namespace, freep);
136 STATIC_DESTRUCTOR_REGISTER(arg_output_fields, set_freep);
137 STATIC_DESTRUCTOR_REGISTER(arg_pattern, freep);
138 STATIC_DESTRUCTOR_REGISTER(arg_compiled_pattern, pcre2_code_freep);
139 STATIC_DESTRUCTOR_REGISTER(arg_image_policy, image_policy_freep);
140
141 static int parse_id_descriptor(const char *x, sd_id128_t *ret_id, int *ret_offset) {
142 sd_id128_t id = SD_ID128_NULL;
143 int off = 0, r;
144
145 assert(x);
146 assert(ret_id);
147 assert(ret_offset);
148
149 if (streq(x, "all")) {
150 *ret_id = SD_ID128_NULL;
151 *ret_offset = 0;
152 return 0;
153 }
154
155 if (strlen(x) >= SD_ID128_STRING_MAX - 1) {
156 char *t;
157
158 t = strndupa_safe(x, SD_ID128_STRING_MAX - 1);
159 r = sd_id128_from_string(t, &id);
160 if (r >= 0)
161 x += SD_ID128_STRING_MAX - 1;
162
163 if (!IN_SET(*x, 0, '-', '+'))
164 return -EINVAL;
165
166 if (*x != 0) {
167 r = safe_atoi(x, &off);
168 if (r < 0)
169 return r;
170 }
171 } else {
172 r = safe_atoi(x, &off);
173 if (r < 0)
174 return r;
175 }
176
177 *ret_id = id;
178 *ret_offset = off;
179 return 1;
180 }
181
182 static int parse_lines(const char *arg, bool graceful) {
183 const char *l;
184 int n, r;
185
186 assert(arg || graceful);
187
188 if (!arg)
189 goto default_noarg;
190
191 if (streq(arg, "all")) {
192 arg_lines = ARG_LINES_ALL;
193 return 1;
194 }
195
196 l = startswith(arg, "+");
197
198 r = safe_atoi(l ?: arg, &n);
199 if (r < 0 || n < 0) {
200 if (graceful)
201 goto default_noarg;
202
203 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to parse --lines='%s'.", arg);
204 }
205
206 arg_lines = n;
207 arg_lines_oldest = l;
208
209 return 1;
210
211 default_noarg:
212 arg_lines = 10;
213 arg_lines_oldest = false;
214 return 0;
215 }
216
217 static int help_facilities(void) {
218 if (!arg_quiet)
219 puts("Available facilities:");
220
221 for (int i = 0; i < LOG_NFACILITIES; i++) {
222 _cleanup_free_ char *t = NULL;
223
224 if (log_facility_unshifted_to_string_alloc(i, &t) < 0)
225 return log_oom();
226 puts(t);
227 }
228
229 return 0;
230 }
231
232 static int help(void) {
233 static const char *const groups[] = {
234 "Source Options",
235 "Filtering Options",
236 "Output Control Options",
237 "Pager Control Options",
238 "Forward Secure Sealing (FSS) Options",
239 "Commands",
240 };
241
242 Table *tables[ELEMENTSOF(groups)] = {};
243 CLEANUP_ELEMENTS(tables, table_unref_array_clear);
244 int r;
245
246 pager_open(arg_pager_flags);
247
248 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
249 r = option_parser_get_help_table_full("journalctl", groups[i], &tables[i]);
250 if (r < 0)
251 return r;
252 }
253
254 assert_cc(ELEMENTSOF(tables) == 6);
255 (void) table_sync_column_widths(0, tables[0], tables[1], tables[2],
256 tables[3], tables[4], tables[5]);
257
258 help_cmdline("[OPTIONS…] [MATCHES…]");
259 help_abstract("Query the journal.");
260
261 for (size_t i = 0; i < ELEMENTSOF(groups); i++) {
262 help_section(groups[i]);
263 r = table_print_or_warn(tables[i]);
264 if (r < 0)
265 return r;
266 }
267
268 help_man_page_reference("journalctl", "1");
269 return 0;
270 }
271
272 static int vl_server(void) {
273 _cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL;
274 int r;
275
276 r = varlink_server_new(&varlink_server, /* flags= */ 0, /* userdata= */ NULL);
277 if (r < 0)
278 return log_error_errno(r, "Failed to allocate Varlink server: %m");
279
280 r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_JournalAccess);
281 if (r < 0)
282 return log_error_errno(r, "Failed to add Varlink interface: %m");
283
284 r = sd_varlink_server_bind_method(varlink_server, "io.systemd.JournalAccess.GetEntries", vl_method_get_entries);
285 if (r < 0)
286 return log_error_errno(r, "Failed to bind Varlink method: %m");
287
288 r = sd_varlink_server_loop_auto(varlink_server);
289 if (r < 0)
290 return log_error_errno(r, "Failed to run Varlink event loop: %m");
291
292 return 0;
293 }
294
295 static int parse_argv(int argc, char *argv[], char ***remaining_args) {
296 int r;
297
298 assert(argc >= 0);
299 assert(argv);
300 assert(remaining_args);
301
302 r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT);
303 if (r < 0)
304 return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m");
305 if (r > 0) {
306 arg_varlink = true;
307
308 OptionParser opts = { argc, argv, .namespace = "journalctl-varlink" };
309
310 FOREACH_OPTION_OR_RETURN(c, &opts)
311 switch (c) {
312
313 OPTION_NAMESPACE("journalctl-varlink"): {}
314
315 OPTION_COMMON_SYSTEM:
316 arg_varlink_runtime_scope = RUNTIME_SCOPE_SYSTEM;
317 break;
318
319 OPTION_COMMON_USER:
320 arg_varlink_runtime_scope = RUNTIME_SCOPE_USER;
321 break;
322 }
323
324 if (arg_varlink_runtime_scope < 0)
325 return log_error_errno(arg_varlink_runtime_scope, "Cannot run in Varlink mode with no runtime scope specified.");
326
327 if (option_parser_get_n_args(&opts) > 0)
328 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No arguments expected in Varlink mode.");
329
330 *remaining_args = NULL;
331 return 1;
332 }
333
334 OptionParser opts = { argc, argv, .namespace = "journalctl" };
335
336 FOREACH_OPTION_OR_RETURN(c, &opts)
337 switch (c) {
338
339 OPTION_NAMESPACE("journalctl"): {}
340
341 OPTION_GROUP("Source Options"): {}
342
343 OPTION_LONG("system", NULL, "Show the system journal"):
344 arg_journal_type |= SD_JOURNAL_SYSTEM;
345 break;
346
347 OPTION_LONG("user", NULL, "Show the user journal for the current user"):
348 arg_journal_type |= SD_JOURNAL_CURRENT_USER;
349 break;
350
351 OPTION_COMMON_MACHINE:
352 r = free_and_strdup_warn(&arg_machine, opts.arg);
353 if (r < 0)
354 return r;
355 break;
356
357 OPTION('m', "merge", NULL, "Show entries from all available journals"):
358 arg_merge = true;
359 break;
360
361 OPTION('D', "directory", "PATH", "Show journal files from directory"):
362 r = free_and_strdup_warn(&arg_directory, opts.arg);
363 if (r < 0)
364 return r;
365 break;
366
367 OPTION('i', "file", "PATH", "Show journal file"):
368 if (streq(opts.arg, "-"))
369 /* An undocumented feature: we can read journal files from STDIN. We don't document
370 * this though, since after all we only support this for mmap-able, seekable files, and
371 * not for example pipes which are probably the primary use case for reading things from
372 * STDIN. To avoid confusion we hence don't document this feature. */
373 arg_file_stdin = true;
374 else {
375 r = glob_extend(&arg_file, opts.arg, GLOB_NOCHECK);
376 if (r < 0)
377 return log_error_errno(r, "Failed to add paths: %m");
378 }
379 break;
380
381 OPTION_LONG("root", "PATH", "Operate on an alternate filesystem root"):
382 r = parse_path_argument(opts.arg, /* suppress_root= */ true, &arg_root);
383 if (r < 0)
384 return r;
385 break;
386
387 OPTION_LONG("image", "PATH", "Operate on disk image as filesystem root"):
388 r = parse_path_argument(opts.arg, /* suppress_root= */ false, &arg_image);
389 if (r < 0)
390 return r;
391 break;
392
393 OPTION_LONG("image-policy", "POLICY", "Specify disk image dissection policy"):
394 r = parse_image_policy_argument(opts.arg, &arg_image_policy);
395 if (r < 0)
396 return r;
397 break;
398
399 OPTION_LONG("namespace", "NAMESPACE",
400 "Show journal data from specified journal namespace"):
401 if (streq(opts.arg, "*")) {
402 arg_namespace_flags = SD_JOURNAL_ALL_NAMESPACES;
403 arg_namespace = mfree(arg_namespace);
404 } else if (startswith(opts.arg, "+")) {
405 arg_namespace_flags = SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE;
406 r = free_and_strdup_warn(&arg_namespace, opts.arg + 1);
407 if (r < 0)
408 return r;
409 } else if (isempty(opts.arg)) {
410 arg_namespace_flags = 0;
411 arg_namespace = mfree(arg_namespace);
412 } else {
413 arg_namespace_flags = 0;
414 r = free_and_strdup_warn(&arg_namespace, opts.arg);
415 if (r < 0)
416 return r;
417 }
418 break;
419
420 OPTION_GROUP("Filtering Options"): {}
421
422 OPTION('S', "since", "DATE", "Show entries not older than the specified date"):
423 r = parse_timestamp(opts.arg, &arg_since);
424 if (r < 0)
425 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
426 "Failed to parse timestamp: %s", opts.arg);
427 arg_since_set = true;
428 break;
429
430 OPTION('U', "until", "DATE", "Show entries not newer than the specified date"):
431 r = parse_timestamp(opts.arg, &arg_until);
432 if (r < 0)
433 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
434 "Failed to parse timestamp: %s", opts.arg);
435 arg_until_set = true;
436 break;
437
438 OPTION('c', "cursor", "CURSOR", "Show entries starting at the specified cursor"):
439 r = free_and_strdup_warn(&arg_cursor, opts.arg);
440 if (r < 0)
441 return r;
442 break;
443
444 OPTION_LONG("after-cursor", "CURSOR", "Show entries after the specified cursor"):
445 r = free_and_strdup_warn(&arg_after_cursor, opts.arg);
446 if (r < 0)
447 return r;
448 break;
449
450 OPTION_LONG("cursor-file", "FILE", "Show entries after cursor in FILE and update FILE"):
451 r = free_and_strdup_warn(&arg_cursor_file, opts.arg);
452 if (r < 0)
453 return r;
454 break;
455
456 OPTION_LONG("this-boot", NULL, /* help= */ NULL):
457 arg_boot = true;
458 arg_boot_id = SD_ID128_NULL;
459 arg_boot_offset = 0;
460 break;
461
462 OPTION_FULL(OPTION_OPTIONAL_ARG, 'b', "boot", "ID",
463 "Show current boot or the specified boot"):
464 arg_boot = true;
465 arg_boot_id = SD_ID128_NULL;
466 arg_boot_offset = 0;
467
468 if (opts.arg) {
469 r = parse_id_descriptor(opts.arg, &arg_boot_id, &arg_boot_offset);
470 if (r < 0)
471 return log_error_errno(r, "Failed to parse boot descriptor '%s'", opts.arg);
472
473 arg_boot = r;
474
475 } else {
476 /* Hmm, no argument? Maybe the next word on the command line is supposed to
477 * be the argument? Let's see if there is one and is parsable as a boot
478 * descriptor… */
479 char *peek = option_parser_peek_next_arg(&opts);
480 if (peek) {
481 r = parse_id_descriptor(peek, &arg_boot_id, &arg_boot_offset);
482 if (r >= 0) {
483 arg_boot = r;
484 (void) option_parser_consume_next_arg(&opts);
485 }
486 }
487 }
488 break;
489
490 OPTION('u', "unit", "UNIT", "Show logs from the specified unit"):
491 r = strv_extend(&arg_system_units, opts.arg);
492 if (r < 0)
493 return log_oom();
494 break;
495
496 OPTION_LONG("user-unit", "UNIT", "Show logs from the specified user unit"):
497 r = strv_extend(&arg_user_units, opts.arg);
498 if (r < 0)
499 return log_oom();
500 break;
501
502 OPTION_LONG("invocation", "ID", "Show logs from the matching invocation ID"):
503 r = parse_id_descriptor(opts.arg, &arg_invocation_id, &arg_invocation_offset);
504 if (r < 0)
505 return log_error_errno(r, "Failed to parse invocation descriptor: %s", opts.arg);
506 arg_invocation = r;
507 break;
508
509 OPTION_SHORT('I', NULL, "Show logs from the latest invocation of unit"):
510 /* Equivalent to --invocation=0 */
511 arg_invocation = true;
512 arg_invocation_id = SD_ID128_NULL;
513 arg_invocation_offset = 0;
514 break;
515
516 OPTION('t', "identifier", "ID", "Show entries with the specified syslog identifier"):
517 r = strv_extend(&arg_syslog_identifier, opts.arg);
518 if (r < 0)
519 return log_oom();
520 break;
521
522 OPTION('T', "exclude-identifier", "ID",
523 "Hide entries with the specified syslog identifier"):
524 r = strv_extend(&arg_exclude_identifier, opts.arg);
525 if (r < 0)
526 return log_oom();
527 break;
528
529 OPTION('p', "priority", "RANGE", "Show entries within the specified priority range"): {
530
531 const char *dots = strstr(opts.arg, "..");
532 if (dots) {
533 /* a range */
534 _cleanup_free_ char *a = strndup(opts.arg, dots - opts.arg);
535 if (!a)
536 return log_oom();
537
538 int from = log_level_from_string(a),
539 to = log_level_from_string(dots + 2);
540
541 if (from < 0 || to < 0)
542 return log_error_errno(from < 0 ? from : to,
543 "Failed to parse log level range %s", opts.arg);
544
545 arg_priorities = 0;
546 if (from < to)
547 for (int i = from; i <= to; i++)
548 arg_priorities |= 1 << i;
549 else
550 for (int i = to; i <= from; i++)
551 arg_priorities |= 1 << i;
552
553 } else {
554 int p = log_level_from_string(opts.arg);
555 if (p < 0)
556 return log_error_errno(p, "Unknown log level %s", opts.arg);
557
558 arg_priorities = 0;
559 for (int i = 0; i <= p; i++)
560 arg_priorities |= 1 << i;
561 }
562
563 break;
564 }
565
566 OPTION_LONG("facility", "FACILITY…", "Show entries with the specified facilities"):
567 for (const char *p = opts.arg;;) {
568 _cleanup_free_ char *fac = NULL;
569 int num;
570
571 r = extract_first_word(&p, &fac, ",", 0);
572 if (r < 0)
573 return log_error_errno(r, "Failed to parse facilities: %s", opts.arg);
574 if (r == 0)
575 break;
576
577 if (streq(fac, "help")) {
578 help_facilities();
579 return 0;
580 }
581
582 num = log_facility_unshifted_from_string(fac);
583 if (num < 0)
584 return log_error_errno(num, "Bad --facility= argument \"%s\".", fac);
585
586 if (set_ensure_put(&arg_facilities, NULL, INT_TO_PTR(num)) < 0)
587 return log_oom();
588 }
589
590 break;
591
592 OPTION('g', "grep", "PATTERN", "Show entries with MESSAGE matching PATTERN"):
593 r = free_and_strdup_warn(&arg_pattern, opts.arg);
594 if (r < 0)
595 return r;
596 break;
597
598 OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "case-sensitive", "BOOL",
599 "Force case sensitive or insensitive matching"):
600 if (opts.arg) {
601 r = parse_boolean(opts.arg);
602 if (r < 0)
603 return log_error_errno(r, "Bad --case-sensitive= argument \"%s\": %m", opts.arg);
604 arg_case = r ? PATTERN_COMPILE_CASE_SENSITIVE : PATTERN_COMPILE_CASE_INSENSITIVE;
605 } else
606 arg_case = PATTERN_COMPILE_CASE_SENSITIVE;
607
608 break;
609
610 OPTION('k', "dmesg", NULL, "Show kernel message log from the current boot"):
611 arg_dmesg = true;
612 break;
613
614 OPTION_GROUP("Output Control Options"): {}
615
616 OPTION('o', "output", "STRING",
617 "Change journal output mode (short, short-precise, short-iso, short-iso-precise, "
618 "short-full, short-monotonic, short-unix, verbose, export, json, json-pretty, "
619 "json-sse, json-seq, cat, with-unit)"):
620 if (streq(opts.arg, "help"))
621 return DUMP_STRING_TABLE(output_mode, OutputMode, _OUTPUT_MODE_MAX);
622
623 arg_output = output_mode_from_string(opts.arg);
624 if (arg_output < 0)
625 return log_error_errno(arg_output, "Unknown output format '%s'.", opts.arg);
626
627 if (IN_SET(arg_output, OUTPUT_EXPORT, OUTPUT_JSON, OUTPUT_JSON_PRETTY, OUTPUT_JSON_SSE, OUTPUT_JSON_SEQ, OUTPUT_CAT))
628 arg_quiet = true;
629
630 if (OUTPUT_MODE_IS_JSON(arg_output))
631 arg_json_format_flags = output_mode_to_json_format_flags(arg_output) | SD_JSON_FORMAT_COLOR_AUTO;
632 else
633 arg_json_format_flags = SD_JSON_FORMAT_OFF;
634
635 break;
636
637 OPTION_LONG("output-fields", "LIST", "Select fields to print in verbose/export/json modes"): {
638 _cleanup_strv_free_ char **v = NULL;
639
640 v = strv_split(opts.arg, ",");
641 if (!v)
642 return log_oom();
643
644 r = set_put_strdupv(&arg_output_fields, v);
645 if (r < 0)
646 return log_oom();
647
648 break;
649 }
650
651 OPTION_FULL(OPTION_OPTIONAL_ARG, 'n', "lines", "[+]INTEGER",
652 "Number of journal entries to show"): {
653 const char *p = opts.arg ?: option_parser_peek_next_arg(&opts);
654
655 r = parse_lines(p, /* graceful= */ !opts.arg);
656 if (r < 0)
657 return r;
658 if (r > 0 && !opts.arg)
659 (void) option_parser_consume_next_arg(&opts);
660
661 break;
662 }
663
664 OPTION('r', "reverse", NULL, "Show the newest entries first"):
665 arg_reverse = true;
666 break;
667
668 OPTION_LONG("show-cursor", NULL, "Print the cursor after all the entries"):
669 arg_show_cursor = true;
670 break;
671
672 OPTION_LONG("utc", NULL, "Express time in Coordinated Universal Time (UTC)"):
673 arg_utc = true;
674 break;
675
676 OPTION('x', "catalog", NULL, "Add message explanations where available"):
677 arg_catalog = true;
678 break;
679
680 OPTION('W', "no-hostname", NULL, "Suppress output of hostname field"):
681 arg_no_hostname = true;
682 break;
683
684 OPTION('l', "full", NULL, /* help= */ NULL):
685 arg_full = true;
686 break;
687
688 OPTION_LONG("no-full", NULL, "Ellipsize fields"):
689 arg_full = false;
690 break;
691
692 OPTION('a', "all", NULL, "Show all fields, including long and unprintable"):
693 arg_all = true;
694 break;
695
696 OPTION('f', "follow", NULL, "Follow the journal"):
697 arg_follow = true;
698 break;
699
700 OPTION_LONG("no-tail", NULL, "Show all lines, even in follow mode"):
701 arg_no_tail = true;
702 break;
703
704 OPTION_LONG("truncate-newline", NULL, "Truncate entries by first newline character"):
705 arg_truncate_newline = true;
706 break;
707
708 OPTION('q', "quiet", NULL, "Do not show info messages and privilege warning"):
709 arg_quiet = true;
710 break;
711
712 OPTION_LONG("synchronize-on-exit", "BOOL",
713 "Wait for Journal synchronization before exiting"):
714 r = parse_boolean_argument("--synchronize-on-exit", opts.arg, &arg_synchronize_on_exit);
715 if (r < 0)
716 return r;
717 break;
718
719 OPTION_GROUP("Pager Control Options"): {}
720
721 OPTION_COMMON_NO_PAGER:
722 arg_pager_flags |= PAGER_DISABLE;
723 break;
724
725 OPTION('e', "pager-end", NULL, "Immediately jump to the end in the pager"):
726 arg_pager_flags |= PAGER_JUMP_TO_END;
727 break;
728
729 OPTION_GROUP("Forward Secure Sealing (FSS) Options"): {}
730
731 OPTION_LONG("interval", "TIME", "Time interval for changing the FSS sealing key"):
732 #if HAVE_GCRYPT
733 r = parse_sec(opts.arg, &arg_interval);
734 if (r < 0 || arg_interval <= 0)
735 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
736 "Failed to parse sealing key change interval: %s", opts.arg);
737 break;
738 #else
739 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
740 "Compiled without forward-secure sealing support.");
741 #endif
742
743 OPTION_LONG("verify-key", "KEY", "Specify FSS verification key"):
744 #if HAVE_GCRYPT
745 erase_and_free(arg_verify_key);
746 arg_verify_key = strdup(opts.arg);
747 if (!arg_verify_key)
748 return log_oom();
749
750 /* Use memset not explicit_bzero() or similar so this doesn't look confusing
751 * in ps or htop output. We need to cast away the const to do this. */
752 memset((char*) opts.arg, 'x', strlen(opts.arg));
753
754 arg_action = ACTION_VERIFY;
755 arg_merge = false;
756 break;
757 #else
758 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
759 "Compiled without forward-secure sealing support.");
760 #endif
761
762 OPTION_LONG("force", NULL, "Override of the FSS key pair with --setup-keys"):
763 #if HAVE_GCRYPT
764 arg_force = true;
765 break;
766 #else
767 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
768 "Compiled without forward-secure sealing support.");
769 #endif
770
771 OPTION_GROUP("Commands"): {}
772
773 OPTION_COMMON_HELP:
774 return help();
775
776 OPTION_COMMON_VERSION:
777 return version();
778
779 OPTION('N', "fields", NULL, "List all field names currently used"):
780 arg_action = ACTION_LIST_FIELD_NAMES;
781 break;
782
783 OPTION('F', "field", "FIELD", "List all values that a specified field takes"):
784 arg_action = ACTION_LIST_FIELDS;
785 r = free_and_strdup_warn(&arg_field, opts.arg);
786 if (r < 0)
787 return r;
788 break;
789
790 OPTION_LONG("list-boots", NULL, "Show terse information about recorded boots"):
791 arg_action = ACTION_LIST_BOOTS;
792 break;
793
794 OPTION_LONG("list-invocations", NULL, "Show invocation IDs of specified unit"):
795 arg_action = ACTION_LIST_INVOCATIONS;
796 break;
797
798 OPTION_LONG("list-namespaces", NULL, "Show list of journal namespaces"):
799 arg_action = ACTION_LIST_NAMESPACES;
800 break;
801
802 OPTION_LONG("disk-usage", NULL, "Show total disk usage of all journal files"):
803 arg_action = ACTION_DISK_USAGE;
804 break;
805
806 OPTION_LONG("vacuum-size", "BYTES", "Reduce disk usage below specified size"):
807 r = parse_size(opts.arg, 1024, &arg_vacuum_size);
808 if (r < 0)
809 return log_error_errno(r, "Failed to parse vacuum size: %s", opts.arg);
810
811 arg_action = arg_action == ACTION_ROTATE ? ACTION_ROTATE_AND_VACUUM : ACTION_VACUUM;
812 break;
813
814 OPTION_LONG("vacuum-files", "INT", "Leave only the specified number of journal files"):
815 r = safe_atou64(opts.arg, &arg_vacuum_n_files);
816 if (r < 0)
817 return log_error_errno(r, "Failed to parse vacuum files: %s", opts.arg);
818
819 arg_action = arg_action == ACTION_ROTATE ? ACTION_ROTATE_AND_VACUUM : ACTION_VACUUM;
820 break;
821
822 OPTION_LONG("vacuum-time", "TIME", "Remove journal files older than specified time"):
823 r = parse_sec(opts.arg, &arg_vacuum_time);
824 if (r < 0)
825 return log_error_errno(r, "Failed to parse vacuum time: %s", opts.arg);
826
827 arg_action = arg_action == ACTION_ROTATE ? ACTION_ROTATE_AND_VACUUM : ACTION_VACUUM;
828 break;
829
830 OPTION_LONG("verify", NULL, "Verify journal file consistency"):
831 arg_action = ACTION_VERIFY;
832 break;
833
834 OPTION_LONG("sync", NULL, "Synchronize unwritten journal messages to disk"):
835 arg_action = ACTION_SYNC;
836 break;
837
838 OPTION_LONG("relinquish-var", NULL, "Stop logging to disk, log to temporary file system"):
839 arg_action = ACTION_RELINQUISH_VAR;
840 break;
841
842 OPTION_LONG("smart-relinquish-var", NULL,
843 "Similar, but NOP if log directory is on root mount"):
844 arg_action = ACTION_SMART_RELINQUISH_VAR;
845 break;
846
847 OPTION_LONG("flush", NULL, "Flush all journal data from /run into /var"):
848 arg_action = ACTION_FLUSH;
849 break;
850
851 OPTION_LONG("rotate", NULL, "Request immediate rotation of the journal files"):
852 arg_action = arg_action == ACTION_VACUUM ? ACTION_ROTATE_AND_VACUUM : ACTION_ROTATE;
853 break;
854
855 OPTION_LONG("header", NULL, "Show journal header information"):
856 arg_action = ACTION_PRINT_HEADER;
857 break;
858
859 OPTION_LONG("list-catalog", NULL, "Show all message IDs in the catalog"):
860 arg_action = ACTION_LIST_CATALOG;
861 break;
862
863 OPTION_LONG("dump-catalog", NULL, "Show entries in the message catalog"):
864 arg_action = ACTION_DUMP_CATALOG;
865 break;
866
867 OPTION_LONG("update-catalog", NULL, "Update the message catalog database"):
868 arg_action = ACTION_UPDATE_CATALOG;
869 break;
870
871 OPTION_LONG("setup-keys", NULL, "Generate a new FSS key pair"):
872 #if HAVE_GCRYPT
873 arg_action = ACTION_SETUP_KEYS;
874 break;
875 #else
876 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
877 "Compiled without forward-secure sealing support.");
878 #endif
879
880 OPTION_LONG("new-id128", NULL, /* help= */ NULL):
881 arg_action = ACTION_NEW_ID128;
882 break;
883 }
884
885 char **args = option_parser_get_args(&opts);
886 size_t n_args = option_parser_get_n_args(&opts);
887
888 if (arg_no_tail)
889 arg_lines = ARG_LINES_ALL;
890
891 if (arg_lines == ARG_LINES_DEFAULT) {
892 if (arg_follow && !arg_since_set)
893 arg_lines = 10;
894 else if (FLAGS_SET(arg_pager_flags, PAGER_JUMP_TO_END))
895 arg_lines = 1000;
896 }
897
898 if (arg_boot < 0)
899 /* Show the current boot if -f/--follow, -k/--dmesg, or -e/--pager-end is specified unless
900 * -m/--merge is specified. */
901 arg_boot = !arg_merge && (arg_follow || arg_dmesg || FLAGS_SET(arg_pager_flags, PAGER_JUMP_TO_END));
902 if (!arg_boot) {
903 /* Clear the boot ID and offset if -b/--boot is unspecified for safety. */
904 arg_boot_id = SD_ID128_NULL;
905 arg_boot_offset = 0;
906 }
907
908 if (!!arg_directory + !!arg_file + arg_file_stdin + !!arg_machine + !!arg_root + !!arg_image > 1)
909 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
910 "Please specify at most one of -D/--directory=, --file=, -M/--machine=, --root=, --image=.");
911
912 if (arg_since_set && arg_until_set && arg_since > arg_until)
913 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
914 "--since= must be before --until=.");
915
916 if (!!arg_cursor + !!arg_after_cursor + !!arg_cursor_file + !!arg_since_set > 1)
917 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
918 "Please specify only one of --since=, --cursor=, --cursor-file=, and --after-cursor=.");
919
920 if (arg_follow && arg_reverse)
921 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
922 "Please specify either --reverse or --follow, not both.");
923
924 if (arg_action == ACTION_SHOW && arg_lines >= 0 && arg_lines_oldest && (arg_reverse || arg_follow))
925 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
926 "--lines=+N is unsupported when --reverse or --follow is specified.");
927
928 if (!IN_SET(arg_action, ACTION_SHOW, ACTION_DUMP_CATALOG, ACTION_LIST_CATALOG) && n_args > 0)
929 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
930 "Extraneous arguments starting with '%s'",
931 args[0]);
932
933 if ((arg_boot || arg_action == ACTION_LIST_BOOTS) && arg_merge)
934 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
935 "Using --boot or --list-boots with --merge is not supported.");
936
937 if (!strv_isempty(arg_system_units) && arg_journal_type == SD_JOURNAL_CURRENT_USER) {
938 /* Specifying --user and --unit= at the same time makes no sense (as the former excludes the user
939 * journal, but the latter excludes the system journal, thus resulting in empty output). Let's be nice
940 * to users, and automatically turn --unit= into --user-unit= if combined with --user. */
941 r = strv_extend_strv(&arg_user_units, arg_system_units, true);
942 if (r < 0)
943 return r;
944
945 arg_system_units = strv_free(arg_system_units);
946 }
947
948 if (arg_pattern) {
949 r = pattern_compile_and_log(arg_pattern, arg_case, &arg_compiled_pattern);
950 if (r < 0)
951 return r;
952
953 /* When --grep is used along with --lines without '+', i.e. when we start from the end of the
954 * journal, we don't know how many lines we can print. So we search backwards and count until
955 * enough lines have been printed or we hit the head.
956 * An exception is that --follow might set arg_lines, so let's not imply --reverse
957 * if that is specified. */
958 if (arg_lines_needs_seek_end() && !arg_follow)
959 arg_reverse = true;
960 }
961
962 if (!arg_follow)
963 arg_journal_additional_open_flags = SD_JOURNAL_ASSUME_IMMUTABLE;
964
965 args = strv_copy(args);
966 if (!args)
967 return log_oom();
968
969 *remaining_args = args;
970 return 1;
971 }
972
973 static int run(int argc, char *argv[]) {
974 _cleanup_(loop_device_unrefp) LoopDevice *loop_device = NULL;
975 _cleanup_(umount_and_freep) char *mounted_dir = NULL;
976 _cleanup_strv_free_ char **args = NULL;
977 int r;
978
979 setlocale(LC_ALL, "");
980 log_setup();
981
982 r = parse_argv(argc, argv, &args);
983 if (r <= 0)
984 return r;
985
986 if (arg_varlink)
987 return vl_server();
988
989 if (arg_image) {
990 assert(!arg_root);
991
992 r = mount_image_privately_interactively(
993 arg_image,
994 arg_image_policy,
995 DISSECT_IMAGE_GENERIC_ROOT |
996 DISSECT_IMAGE_REQUIRE_ROOT |
997 DISSECT_IMAGE_VALIDATE_OS |
998 DISSECT_IMAGE_RELAX_VAR_CHECK |
999 (arg_action == ACTION_UPDATE_CATALOG ? DISSECT_IMAGE_FSCK|DISSECT_IMAGE_GROWFS : DISSECT_IMAGE_READ_ONLY) |
1000 DISSECT_IMAGE_ALLOW_USERSPACE_VERITY,
1001 &mounted_dir,
1002 /* ret_dir_fd= */ NULL,
1003 &loop_device);
1004 if (r < 0)
1005 return r;
1006
1007 arg_root = strdup(mounted_dir);
1008 if (!arg_root)
1009 return log_oom();
1010 }
1011
1012 switch (arg_action) {
1013
1014 case ACTION_SHOW:
1015 return action_show(args);
1016
1017 case ACTION_NEW_ID128:
1018 return id128_print_new(ID128_PRINT_PRETTY);
1019
1020 case ACTION_SETUP_KEYS:
1021 return action_setup_keys();
1022
1023 case ACTION_LIST_CATALOG:
1024 case ACTION_DUMP_CATALOG:
1025 return action_list_catalog(args);
1026
1027 case ACTION_UPDATE_CATALOG:
1028 return action_update_catalog();
1029
1030 case ACTION_PRINT_HEADER:
1031 return action_print_header();
1032
1033 case ACTION_VERIFY:
1034 return action_verify();
1035
1036 case ACTION_DISK_USAGE:
1037 return action_disk_usage();
1038
1039 case ACTION_LIST_BOOTS:
1040 return action_list_boots();
1041
1042 case ACTION_LIST_FIELDS:
1043 return action_list_fields();
1044
1045 case ACTION_LIST_FIELD_NAMES:
1046 return action_list_field_names();
1047
1048 case ACTION_LIST_INVOCATIONS:
1049 return action_list_invocations();
1050
1051 case ACTION_LIST_NAMESPACES:
1052 return action_list_namespaces();
1053
1054 case ACTION_FLUSH:
1055 return action_flush_to_var();
1056
1057 case ACTION_SMART_RELINQUISH_VAR: {
1058 int root_mnt_id, log_mnt_id;
1059
1060 /* Try to be smart about relinquishing access to /var/log/journal/ during shutdown:
1061 * if it's on the same mount as the root file system there's no point in
1062 * relinquishing access and we can leave journald write to it until the very last
1063 * moment. */
1064
1065 r = path_get_mnt_id("/", &root_mnt_id);
1066 if (r < 0)
1067 log_debug_errno(r, "Failed to get root mount ID, ignoring: %m");
1068 else {
1069 r = path_get_mnt_id("/var/log/journal/", &log_mnt_id);
1070 if (r < 0)
1071 log_debug_errno(r, "Failed to get journal directory mount ID, ignoring: %m");
1072 else if (root_mnt_id == log_mnt_id) {
1073 log_debug("/var/log/journal/ is on root file system, not relinquishing access to /var.");
1074 return 0;
1075 } else
1076 log_debug("/var/log/journal/ is not on the root file system, relinquishing access to it.");
1077 }
1078
1079 _fallthrough_;
1080 }
1081
1082 case ACTION_RELINQUISH_VAR:
1083 return action_relinquish_var();
1084
1085 case ACTION_SYNC:
1086 return action_sync();
1087
1088 case ACTION_ROTATE:
1089 return action_rotate();
1090
1091 case ACTION_VACUUM:
1092 return action_vacuum();
1093
1094 case ACTION_ROTATE_AND_VACUUM:
1095 return action_rotate_and_vacuum();
1096
1097 default:
1098 assert_not_reached();
1099 }
1100 }
1101
1102 DEFINE_MAIN_FUNCTION(run);