1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2011 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
34 #include <sys/inotify.h>
39 #include "sd-journal.h"
42 #include "bus-error.h"
48 #include "hostname-util.h"
50 #include "journal-def.h"
51 #include "journal-internal.h"
52 #include "journal-qrcode.h"
53 #include "journal-vacuum.h"
54 #include "journal-verify.h"
56 #include "logs-show.h"
59 #include "path-util.h"
63 #include "terminal-util.h"
64 #include "unit-name.h"
65 #include "user-util.h"
67 #define DEFAULT_FSS_INTERVAL_USEC (15*USEC_PER_MINUTE)
70 /* Special values for arg_lines */
71 ARG_LINES_DEFAULT
= -2,
75 static OutputMode arg_output
= OUTPUT_SHORT
;
76 static bool arg_utc
= false;
77 static bool arg_pager_end
= false;
78 static bool arg_follow
= false;
79 static bool arg_full
= true;
80 static bool arg_all
= false;
81 static bool arg_no_pager
= false;
82 static int arg_lines
= ARG_LINES_DEFAULT
;
83 static bool arg_no_tail
= false;
84 static bool arg_quiet
= false;
85 static bool arg_merge
= false;
86 static bool arg_boot
= false;
87 static sd_id128_t arg_boot_id
= {};
88 static int arg_boot_offset
= 0;
89 static bool arg_dmesg
= false;
90 static const char *arg_cursor
= NULL
;
91 static const char *arg_after_cursor
= NULL
;
92 static bool arg_show_cursor
= false;
93 static const char *arg_directory
= NULL
;
94 static char **arg_file
= NULL
;
95 static int arg_priorities
= 0xFF;
96 static const char *arg_verify_key
= NULL
;
98 static usec_t arg_interval
= DEFAULT_FSS_INTERVAL_USEC
;
99 static bool arg_force
= false;
101 static usec_t arg_since
, arg_until
;
102 static bool arg_since_set
= false, arg_until_set
= false;
103 static char **arg_syslog_identifier
= NULL
;
104 static char **arg_system_units
= NULL
;
105 static char **arg_user_units
= NULL
;
106 static const char *arg_field
= NULL
;
107 static bool arg_catalog
= false;
108 static bool arg_reverse
= false;
109 static int arg_journal_type
= 0;
110 static char *arg_root
= NULL
;
111 static const char *arg_machine
= NULL
;
112 static uint64_t arg_vacuum_size
= 0;
113 static uint64_t arg_vacuum_n_files
= 0;
114 static usec_t arg_vacuum_time
= 0;
125 ACTION_UPDATE_CATALOG
,
130 } arg_action
= ACTION_SHOW
;
132 typedef struct BootId
{
136 LIST_FIELDS(struct BootId
, boot_list
);
139 static void pager_open_if_enabled(void) {
144 pager_open(arg_pager_end
);
147 static char *format_timestamp_maybe_utc(char *buf
, size_t l
, usec_t t
) {
150 return format_timestamp_utc(buf
, l
, t
);
152 return format_timestamp(buf
, l
, t
);
155 static int parse_boot_descriptor(const char *x
, sd_id128_t
*boot_id
, int *offset
) {
156 sd_id128_t id
= SD_ID128_NULL
;
159 if (strlen(x
) >= 32) {
163 r
= sd_id128_from_string(t
, &id
);
167 if (*x
!= '-' && *x
!= '+' && *x
!= 0)
171 r
= safe_atoi(x
, &off
);
176 r
= safe_atoi(x
, &off
);
190 static void help(void) {
192 pager_open_if_enabled();
194 printf("%s [OPTIONS...] [MATCHES...]\n\n"
195 "Query the journal.\n\n"
197 " --system Show the system journal\n"
198 " --user Show the user journal for the current user\n"
199 " -M --machine=CONTAINER Operate on local container\n"
200 " -S --since=DATE Show entries not older than the specified date\n"
201 " -U --until=DATE Show entries not newer than the specified date\n"
202 " -c --cursor=CURSOR Show entries starting at the specified cursor\n"
203 " --after-cursor=CURSOR Show entries after the specified cursor\n"
204 " --show-cursor Print the cursor after all the entries\n"
205 " -b --boot[=ID] Show current boot or the specified boot\n"
206 " --list-boots Show terse information about recorded boots\n"
207 " -k --dmesg Show kernel message log from the current boot\n"
208 " -u --unit=UNIT Show logs from the specified unit\n"
209 " --user-unit=UNIT Show logs from the specified user unit\n"
210 " -t --identifier=STRING Show entries with the specified syslog identifier\n"
211 " -p --priority=RANGE Show entries with the specified priority\n"
212 " -e --pager-end Immediately jump to the end in the pager\n"
213 " -f --follow Follow the journal\n"
214 " -n --lines[=INTEGER] Number of journal entries to show\n"
215 " --no-tail Show all lines, even in follow mode\n"
216 " -r --reverse Show the newest entries first\n"
217 " -o --output=STRING Change journal output mode (short, short-iso,\n"
218 " short-precise, short-monotonic, verbose,\n"
219 " export, json, json-pretty, json-sse, cat)\n"
220 " --utc Express time in Coordinated Universal Time (UTC)\n"
221 " -x --catalog Add message explanations where available\n"
222 " --no-full Ellipsize fields\n"
223 " -a --all Show all fields, including long and unprintable\n"
224 " -q --quiet Do not show info messages and privilege warning\n"
225 " --no-pager Do not pipe output into a pager\n"
226 " -m --merge Show entries from all available journals\n"
227 " -D --directory=PATH Show journal files from directory\n"
228 " --file=PATH Show journal file\n"
229 " --root=ROOT Operate on catalog files underneath the root ROOT\n"
231 " --interval=TIME Time interval for changing the FSS sealing key\n"
232 " --verify-key=KEY Specify FSS verification key\n"
233 " --force Override of the FSS key pair with --setup-keys\n"
236 " -h --help Show this help text\n"
237 " --version Show package version\n"
238 " -F --field=FIELD List all values that a specified field takes\n"
239 " --new-id128 Generate a new 128-bit ID\n"
240 " --disk-usage Show total disk usage of all journal files\n"
241 " --vacuum-size=BYTES Reduce disk usage below specified size\n"
242 " --vacuum-files=INT Leave only the specified number of journal files\n"
243 " --vacuum-time=TIME Remove journal files older than specified time\n"
244 " --flush Flush all journal data from /run into /var\n"
245 " --rotate Request immediate rotation of the journal files\n"
246 " --header Show journal header information\n"
247 " --list-catalog Show all message IDs in the catalog\n"
248 " --dump-catalog Show entries in the message catalog\n"
249 " --update-catalog Update the message catalog database\n"
251 " --setup-keys Generate a new FSS key pair\n"
252 " --verify Verify journal file consistency\n"
254 , program_invocation_short_name
);
257 static int parse_argv(int argc
, char *argv
[]) {
291 static const struct option options
[] = {
292 { "help", no_argument
, NULL
, 'h' },
293 { "version" , no_argument
, NULL
, ARG_VERSION
},
294 { "no-pager", no_argument
, NULL
, ARG_NO_PAGER
},
295 { "pager-end", no_argument
, NULL
, 'e' },
296 { "follow", no_argument
, NULL
, 'f' },
297 { "force", no_argument
, NULL
, ARG_FORCE
},
298 { "output", required_argument
, NULL
, 'o' },
299 { "all", no_argument
, NULL
, 'a' },
300 { "full", no_argument
, NULL
, 'l' },
301 { "no-full", no_argument
, NULL
, ARG_NO_FULL
},
302 { "lines", optional_argument
, NULL
, 'n' },
303 { "no-tail", no_argument
, NULL
, ARG_NO_TAIL
},
304 { "new-id128", no_argument
, NULL
, ARG_NEW_ID128
},
305 { "quiet", no_argument
, NULL
, 'q' },
306 { "merge", no_argument
, NULL
, 'm' },
307 { "boot", optional_argument
, NULL
, 'b' },
308 { "list-boots", no_argument
, NULL
, ARG_LIST_BOOTS
},
309 { "this-boot", optional_argument
, NULL
, 'b' }, /* deprecated */
310 { "dmesg", no_argument
, NULL
, 'k' },
311 { "system", no_argument
, NULL
, ARG_SYSTEM
},
312 { "user", no_argument
, NULL
, ARG_USER
},
313 { "directory", required_argument
, NULL
, 'D' },
314 { "file", required_argument
, NULL
, ARG_FILE
},
315 { "root", required_argument
, NULL
, ARG_ROOT
},
316 { "header", no_argument
, NULL
, ARG_HEADER
},
317 { "identifier", required_argument
, NULL
, 't' },
318 { "priority", required_argument
, NULL
, 'p' },
319 { "setup-keys", no_argument
, NULL
, ARG_SETUP_KEYS
},
320 { "interval", required_argument
, NULL
, ARG_INTERVAL
},
321 { "verify", no_argument
, NULL
, ARG_VERIFY
},
322 { "verify-key", required_argument
, NULL
, ARG_VERIFY_KEY
},
323 { "disk-usage", no_argument
, NULL
, ARG_DISK_USAGE
},
324 { "cursor", required_argument
, NULL
, 'c' },
325 { "after-cursor", required_argument
, NULL
, ARG_AFTER_CURSOR
},
326 { "show-cursor", no_argument
, NULL
, ARG_SHOW_CURSOR
},
327 { "since", required_argument
, NULL
, 'S' },
328 { "until", required_argument
, NULL
, 'U' },
329 { "unit", required_argument
, NULL
, 'u' },
330 { "user-unit", required_argument
, NULL
, ARG_USER_UNIT
},
331 { "field", required_argument
, NULL
, 'F' },
332 { "catalog", no_argument
, NULL
, 'x' },
333 { "list-catalog", no_argument
, NULL
, ARG_LIST_CATALOG
},
334 { "dump-catalog", no_argument
, NULL
, ARG_DUMP_CATALOG
},
335 { "update-catalog", no_argument
, NULL
, ARG_UPDATE_CATALOG
},
336 { "reverse", no_argument
, NULL
, 'r' },
337 { "machine", required_argument
, NULL
, 'M' },
338 { "utc", no_argument
, NULL
, ARG_UTC
},
339 { "flush", no_argument
, NULL
, ARG_FLUSH
},
340 { "rotate", no_argument
, NULL
, ARG_ROTATE
},
341 { "vacuum-size", required_argument
, NULL
, ARG_VACUUM_SIZE
},
342 { "vacuum-files", required_argument
, NULL
, ARG_VACUUM_FILES
},
343 { "vacuum-time", required_argument
, NULL
, ARG_VACUUM_TIME
},
352 while ((c
= getopt_long(argc
, argv
, "hefo:aln::qmb::kD:p:c:S:U:t:u:F:xrM:", options
, NULL
)) >= 0)
368 arg_pager_end
= true;
370 if (arg_lines
== ARG_LINES_DEFAULT
)
380 arg_output
= output_mode_from_string(optarg
);
381 if (arg_output
< 0) {
382 log_error("Unknown output format '%s'.", optarg
);
386 if (arg_output
== OUTPUT_EXPORT
||
387 arg_output
== OUTPUT_JSON
||
388 arg_output
== OUTPUT_JSON_PRETTY
||
389 arg_output
== OUTPUT_JSON_SSE
||
390 arg_output
== OUTPUT_CAT
)
409 if (streq(optarg
, "all"))
410 arg_lines
= ARG_LINES_ALL
;
412 r
= safe_atoi(optarg
, &arg_lines
);
413 if (r
< 0 || arg_lines
< 0) {
414 log_error("Failed to parse lines '%s'", optarg
);
421 /* Hmm, no argument? Maybe the next
422 * word on the command line is
423 * supposed to be the argument? Let's
424 * see if there is one, and is
428 if (streq(argv
[optind
], "all")) {
429 arg_lines
= ARG_LINES_ALL
;
431 } else if (safe_atoi(argv
[optind
], &n
) >= 0 && n
>= 0) {
445 arg_action
= ACTION_NEW_ID128
;
460 r
= parse_boot_descriptor(optarg
, &arg_boot_id
, &arg_boot_offset
);
462 log_error("Failed to parse boot descriptor '%s'", optarg
);
467 /* Hmm, no argument? Maybe the next
468 * word on the command line is
469 * supposed to be the argument? Let's
470 * see if there is one and is parsable
471 * as a boot descriptor... */
474 parse_boot_descriptor(argv
[optind
], &arg_boot_id
, &arg_boot_offset
) >= 0)
481 arg_action
= ACTION_LIST_BOOTS
;
485 arg_boot
= arg_dmesg
= true;
489 arg_journal_type
|= SD_JOURNAL_SYSTEM
;
493 arg_journal_type
|= SD_JOURNAL_CURRENT_USER
;
497 arg_machine
= optarg
;
501 arg_directory
= optarg
;
505 r
= glob_extend(&arg_file
, optarg
);
507 return log_error_errno(r
, "Failed to add paths: %m");
511 r
= parse_path_argument_and_warn(optarg
, true, &arg_root
);
520 case ARG_AFTER_CURSOR
:
521 arg_after_cursor
= optarg
;
524 case ARG_SHOW_CURSOR
:
525 arg_show_cursor
= true;
529 arg_action
= ACTION_PRINT_HEADER
;
533 arg_action
= ACTION_VERIFY
;
537 arg_action
= ACTION_DISK_USAGE
;
540 case ARG_VACUUM_SIZE
:
541 r
= parse_size(optarg
, 1024, &arg_vacuum_size
);
543 log_error("Failed to parse vacuum size: %s", optarg
);
547 arg_action
= ACTION_VACUUM
;
550 case ARG_VACUUM_FILES
:
551 r
= safe_atou64(optarg
, &arg_vacuum_n_files
);
553 log_error("Failed to parse vacuum files: %s", optarg
);
557 arg_action
= ACTION_VACUUM
;
560 case ARG_VACUUM_TIME
:
561 r
= parse_sec(optarg
, &arg_vacuum_time
);
563 log_error("Failed to parse vacuum time: %s", optarg
);
567 arg_action
= ACTION_VACUUM
;
576 arg_action
= ACTION_SETUP_KEYS
;
581 arg_action
= ACTION_VERIFY
;
582 arg_verify_key
= optarg
;
587 r
= parse_sec(optarg
, &arg_interval
);
588 if (r
< 0 || arg_interval
<= 0) {
589 log_error("Failed to parse sealing key change interval: %s", optarg
);
598 log_error("Forward-secure sealing not available.");
605 dots
= strstr(optarg
, "..");
611 a
= strndup(optarg
, dots
- optarg
);
615 from
= log_level_from_string(a
);
616 to
= log_level_from_string(dots
+ 2);
619 if (from
< 0 || to
< 0) {
620 log_error("Failed to parse log level range %s", optarg
);
627 for (i
= from
; i
<= to
; i
++)
628 arg_priorities
|= 1 << i
;
630 for (i
= to
; i
<= from
; i
++)
631 arg_priorities
|= 1 << i
;
637 p
= log_level_from_string(optarg
);
639 log_error("Unknown log level %s", optarg
);
645 for (i
= 0; i
<= p
; i
++)
646 arg_priorities
|= 1 << i
;
653 r
= parse_timestamp(optarg
, &arg_since
);
655 log_error("Failed to parse timestamp: %s", optarg
);
658 arg_since_set
= true;
662 r
= parse_timestamp(optarg
, &arg_until
);
664 log_error("Failed to parse timestamp: %s", optarg
);
667 arg_until_set
= true;
671 r
= strv_extend(&arg_syslog_identifier
, optarg
);
677 r
= strv_extend(&arg_system_units
, optarg
);
683 r
= strv_extend(&arg_user_units
, optarg
);
696 case ARG_LIST_CATALOG
:
697 arg_action
= ACTION_LIST_CATALOG
;
700 case ARG_DUMP_CATALOG
:
701 arg_action
= ACTION_DUMP_CATALOG
;
704 case ARG_UPDATE_CATALOG
:
705 arg_action
= ACTION_UPDATE_CATALOG
;
717 arg_action
= ACTION_FLUSH
;
721 arg_action
= ACTION_ROTATE
;
728 assert_not_reached("Unhandled option");
731 if (arg_follow
&& !arg_no_tail
&& !arg_since
&& arg_lines
== ARG_LINES_DEFAULT
)
734 if (!!arg_directory
+ !!arg_file
+ !!arg_machine
> 1) {
735 log_error("Please specify either -D/--directory= or --file= or -M/--machine=, not more than one.");
739 if (arg_since_set
&& arg_until_set
&& arg_since
> arg_until
) {
740 log_error("--since= must be before --until=.");
744 if (!!arg_cursor
+ !!arg_after_cursor
+ !!arg_since_set
> 1) {
745 log_error("Please specify only one of --since=, --cursor=, and --after-cursor.");
749 if (arg_follow
&& arg_reverse
) {
750 log_error("Please specify either --reverse= or --follow=, not both.");
754 if (!IN_SET(arg_action
, ACTION_SHOW
, ACTION_DUMP_CATALOG
, ACTION_LIST_CATALOG
) && optind
< argc
) {
755 log_error("Extraneous arguments starting with '%s'", argv
[optind
]);
759 if ((arg_boot
|| arg_action
== ACTION_LIST_BOOTS
) && (arg_file
|| arg_directory
|| arg_merge
)) {
760 log_error("Using --boot or --list-boots with --file, --directory or --merge is not supported.");
767 static int generate_new_id128(void) {
772 r
= sd_id128_randomize(&id
);
774 return log_error_errno(r
, "Failed to generate ID: %m");
776 printf("As string:\n"
777 SD_ID128_FORMAT_STR
"\n\n"
779 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x\n\n"
781 "#define MESSAGE_XYZ SD_ID128_MAKE(",
782 SD_ID128_FORMAT_VAL(id
),
783 SD_ID128_FORMAT_VAL(id
));
784 for (i
= 0; i
< 16; i
++)
785 printf("%02x%s", id
.bytes
[i
], i
!= 15 ? "," : "");
786 fputs(")\n\n", stdout
);
788 printf("As Python constant:\n"
790 ">>> MESSAGE_XYZ = uuid.UUID('" SD_ID128_FORMAT_STR
"')\n",
791 SD_ID128_FORMAT_VAL(id
));
796 static int add_matches(sd_journal
*j
, char **args
) {
798 bool have_term
= false;
802 STRV_FOREACH(i
, args
) {
805 if (streq(*i
, "+")) {
808 r
= sd_journal_add_disjunction(j
);
811 } else if (path_is_absolute(*i
)) {
812 _cleanup_free_
char *p
, *t
= NULL
, *t2
= NULL
;
814 _cleanup_free_
char *interpreter
= NULL
;
817 p
= canonicalize_file_name(*i
);
820 if (lstat(path
, &st
) < 0)
821 return log_error_errno(errno
, "Couldn't stat file: %m");
823 if (S_ISREG(st
.st_mode
) && (0111 & st
.st_mode
)) {
824 if (executable_is_script(path
, &interpreter
) > 0) {
825 _cleanup_free_
char *comm
;
827 comm
= strndup(basename(path
), 15);
831 t
= strappend("_COMM=", comm
);
833 /* Append _EXE only if the interpreter is not a link.
834 Otherwise, it might be outdated often. */
835 if (lstat(interpreter
, &st
) == 0 &&
836 !S_ISLNK(st
.st_mode
)) {
837 t2
= strappend("_EXE=", interpreter
);
842 t
= strappend("_EXE=", path
);
843 } else if (S_ISCHR(st
.st_mode
))
844 (void) asprintf(&t
, "_KERNEL_DEVICE=c%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
845 else if (S_ISBLK(st
.st_mode
))
846 (void) asprintf(&t
, "_KERNEL_DEVICE=b%u:%u", major(st
.st_rdev
), minor(st
.st_rdev
));
848 log_error("File is neither a device node, nor regular file, nor executable: %s", *i
);
855 r
= sd_journal_add_match(j
, t
, 0);
857 r
= sd_journal_add_match(j
, t2
, 0);
861 r
= sd_journal_add_match(j
, *i
, 0);
866 return log_error_errno(r
, "Failed to add match '%s': %m", *i
);
869 if (!strv_isempty(args
) && !have_term
) {
870 log_error("\"+\" can only be used between terms");
877 static void boot_id_free_all(BootId
*l
) {
881 LIST_REMOVE(boot_list
, l
, i
);
886 static int discover_next_boot(
890 bool read_realtime
) {
893 char match
[9+32+1] = "_BOOT_ID=";
894 _cleanup_free_ BootId
*next_boot
= NULL
;
899 /* We expect the journal to be on the last position of a boot
900 * (in relation to the direction we are going), so that the next
901 * invocation of sd_journal_next/previous will be from a different
902 * boot. We then collect any information we desire and then jump
903 * to the last location of the new boot by using a _BOOT_ID match
904 * coming from the other journal direction. */
906 /* Make sure we aren't restricted by any _BOOT_ID matches, so that
907 * we can actually advance to a *different* boot. */
908 sd_journal_flush_matches(j
);
911 r
= sd_journal_previous(j
);
913 r
= sd_journal_next(j
);
917 return 0; /* End of journal, yay. */
919 next_boot
= new0(BootId
, 1);
923 r
= sd_journal_get_monotonic_usec(j
, NULL
, &next_boot
->id
);
928 r
= sd_journal_get_realtime_usec(j
, &next_boot
->first
);
933 /* Now seek to the last occurrence of this boot ID. */
934 sd_id128_to_string(next_boot
->id
, match
+ 9);
935 r
= sd_journal_add_match(j
, match
, sizeof(match
) - 1);
940 r
= sd_journal_seek_head(j
);
942 r
= sd_journal_seek_tail(j
);
947 r
= sd_journal_next(j
);
949 r
= sd_journal_previous(j
);
953 return -ENODATA
; /* This shouldn't happen. We just came from this very boot ID. */
956 r
= sd_journal_get_realtime_usec(j
, &next_boot
->last
);
967 static int get_boots(
970 BootId
*query_ref_boot
,
971 int ref_boot_offset
) {
975 BootId
*head
= NULL
, *tail
= NULL
;
976 const bool advance_older
= query_ref_boot
&& ref_boot_offset
<= 0;
980 /* Adjust for the asymmetry that offset 0 is
981 * the last (and current) boot, while 1 is considered the
982 * (chronological) first boot in the journal. */
983 skip_once
= query_ref_boot
&& sd_id128_is_null(query_ref_boot
->id
) && ref_boot_offset
< 0;
985 /* Advance to the earliest/latest occurrence of our reference
986 * boot ID (taking our lookup direction into account), so that
987 * discover_next_boot() can do its job.
988 * If no reference is given, the journal head/tail will do,
989 * they're "virtual" boots after all. */
990 if (query_ref_boot
&& !sd_id128_is_null(query_ref_boot
->id
)) {
991 char match
[9+32+1] = "_BOOT_ID=";
993 sd_journal_flush_matches(j
);
995 sd_id128_to_string(query_ref_boot
->id
, match
+ 9);
996 r
= sd_journal_add_match(j
, match
, sizeof(match
) - 1);
1001 r
= sd_journal_seek_head(j
);
1003 r
= sd_journal_seek_tail(j
);
1008 r
= sd_journal_next(j
);
1010 r
= sd_journal_previous(j
);
1015 else if (ref_boot_offset
== 0) {
1021 r
= sd_journal_seek_tail(j
);
1023 r
= sd_journal_seek_head(j
);
1027 /* No sd_journal_next/previous here. */
1031 _cleanup_free_ BootId
*current
= NULL
;
1033 r
= discover_next_boot(j
, ¤t
, advance_older
, !query_ref_boot
);
1035 boot_id_free_all(head
);
1042 if (query_ref_boot
) {
1044 ref_boot_offset
+= advance_older
? 1 : -1;
1047 if (ref_boot_offset
== 0) {
1049 query_ref_boot
->id
= current
->id
;
1053 LIST_INSERT_AFTER(boot_list
, head
, tail
, current
);
1064 sd_journal_flush_matches(j
);
1069 static int list_boots(sd_journal
*j
) {
1071 BootId
*id
, *all_ids
;
1075 count
= get_boots(j
, &all_ids
, NULL
, 0);
1077 return log_error_errno(count
, "Failed to determine boots: %m");
1081 pager_open_if_enabled();
1083 /* numbers are one less, but we need an extra char for the sign */
1084 w
= DECIMAL_STR_WIDTH(count
- 1) + 1;
1087 LIST_FOREACH(boot_list
, id
, all_ids
) {
1088 char a
[FORMAT_TIMESTAMP_MAX
], b
[FORMAT_TIMESTAMP_MAX
];
1090 printf("% *i " SD_ID128_FORMAT_STR
" %s—%s\n",
1092 SD_ID128_FORMAT_VAL(id
->id
),
1093 format_timestamp_maybe_utc(a
, sizeof(a
), id
->first
),
1094 format_timestamp_maybe_utc(b
, sizeof(b
), id
->last
));
1098 boot_id_free_all(all_ids
);
1103 static int add_boot(sd_journal
*j
) {
1104 char match
[9+32+1] = "_BOOT_ID=";
1106 BootId ref_boot_id
= {};
1113 if (arg_boot_offset
== 0 && sd_id128_equal(arg_boot_id
, SD_ID128_NULL
))
1114 return add_match_this_boot(j
, arg_machine
);
1116 ref_boot_id
.id
= arg_boot_id
;
1117 r
= get_boots(j
, NULL
, &ref_boot_id
, arg_boot_offset
);
1120 const char *reason
= (r
== 0) ? "No such boot ID in journal" : strerror(-r
);
1122 if (sd_id128_is_null(arg_boot_id
))
1123 log_error("Failed to look up boot %+i: %s", arg_boot_offset
, reason
);
1125 log_error("Failed to look up boot ID "SD_ID128_FORMAT_STR
"%+i: %s",
1126 SD_ID128_FORMAT_VAL(arg_boot_id
), arg_boot_offset
, reason
);
1128 return r
== 0 ? -ENODATA
: r
;
1131 sd_id128_to_string(ref_boot_id
.id
, match
+ 9);
1133 r
= sd_journal_add_match(j
, match
, sizeof(match
) - 1);
1135 return log_error_errno(r
, "Failed to add match: %m");
1137 r
= sd_journal_add_conjunction(j
);
1139 return log_error_errno(r
, "Failed to add conjunction: %m");
1144 static int add_dmesg(sd_journal
*j
) {
1151 r
= sd_journal_add_match(j
, "_TRANSPORT=kernel", strlen("_TRANSPORT=kernel"));
1153 return log_error_errno(r
, "Failed to add match: %m");
1155 r
= sd_journal_add_conjunction(j
);
1157 return log_error_errno(r
, "Failed to add conjunction: %m");
1162 static int get_possible_units(
1168 _cleanup_set_free_free_ Set
*found
;
1172 found
= set_new(&string_hash_ops
);
1176 NULSTR_FOREACH(field
, fields
) {
1180 r
= sd_journal_query_unique(j
, field
);
1184 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, size
) {
1185 char **pattern
, *eq
;
1187 _cleanup_free_
char *u
= NULL
;
1189 eq
= memchr(data
, '=', size
);
1191 prefix
= eq
- (char*) data
+ 1;
1195 u
= strndup((char*) data
+ prefix
, size
- prefix
);
1199 STRV_FOREACH(pattern
, patterns
)
1200 if (fnmatch(*pattern
, u
, FNM_NOESCAPE
) == 0) {
1201 log_debug("Matched %s with pattern %s=%s", u
, field
, *pattern
);
1203 r
= set_consume(found
, u
);
1205 if (r
< 0 && r
!= -EEXIST
)
1218 /* This list is supposed to return the superset of unit names
1219 * possibly matched by rules added with add_matches_for_unit... */
1220 #define SYSTEM_UNITS \
1224 "OBJECT_SYSTEMD_UNIT\0" \
1227 /* ... and add_matches_for_user_unit */
1228 #define USER_UNITS \
1229 "_SYSTEMD_USER_UNIT\0" \
1231 "COREDUMP_USER_UNIT\0" \
1232 "OBJECT_SYSTEMD_USER_UNIT\0"
1234 static int add_units(sd_journal
*j
) {
1235 _cleanup_strv_free_
char **patterns
= NULL
;
1241 STRV_FOREACH(i
, arg_system_units
) {
1242 _cleanup_free_
char *u
= NULL
;
1244 r
= unit_name_mangle(*i
, UNIT_NAME_GLOB
, &u
);
1248 if (string_is_glob(u
)) {
1249 r
= strv_push(&patterns
, u
);
1254 r
= add_matches_for_unit(j
, u
);
1257 r
= sd_journal_add_disjunction(j
);
1264 if (!strv_isempty(patterns
)) {
1265 _cleanup_set_free_free_ Set
*units
= NULL
;
1269 r
= get_possible_units(j
, SYSTEM_UNITS
, patterns
, &units
);
1273 SET_FOREACH(u
, units
, it
) {
1274 r
= add_matches_for_unit(j
, u
);
1277 r
= sd_journal_add_disjunction(j
);
1284 patterns
= strv_free(patterns
);
1286 STRV_FOREACH(i
, arg_user_units
) {
1287 _cleanup_free_
char *u
= NULL
;
1289 r
= unit_name_mangle(*i
, UNIT_NAME_GLOB
, &u
);
1293 if (string_is_glob(u
)) {
1294 r
= strv_push(&patterns
, u
);
1299 r
= add_matches_for_user_unit(j
, u
, getuid());
1302 r
= sd_journal_add_disjunction(j
);
1309 if (!strv_isempty(patterns
)) {
1310 _cleanup_set_free_free_ Set
*units
= NULL
;
1314 r
= get_possible_units(j
, USER_UNITS
, patterns
, &units
);
1318 SET_FOREACH(u
, units
, it
) {
1319 r
= add_matches_for_user_unit(j
, u
, getuid());
1322 r
= sd_journal_add_disjunction(j
);
1329 /* Complain if the user request matches but nothing whatsoever was
1330 * found, since otherwise everything would be matched. */
1331 if (!(strv_isempty(arg_system_units
) && strv_isempty(arg_user_units
)) && count
== 0)
1334 r
= sd_journal_add_conjunction(j
);
1341 static int add_priorities(sd_journal
*j
) {
1342 char match
[] = "PRIORITY=0";
1346 if (arg_priorities
== 0xFF)
1349 for (i
= LOG_EMERG
; i
<= LOG_DEBUG
; i
++)
1350 if (arg_priorities
& (1 << i
)) {
1351 match
[sizeof(match
)-2] = '0' + i
;
1353 r
= sd_journal_add_match(j
, match
, strlen(match
));
1355 return log_error_errno(r
, "Failed to add match: %m");
1358 r
= sd_journal_add_conjunction(j
);
1360 return log_error_errno(r
, "Failed to add conjunction: %m");
1366 static int add_syslog_identifier(sd_journal
*j
) {
1372 STRV_FOREACH(i
, arg_syslog_identifier
) {
1375 u
= strjoina("SYSLOG_IDENTIFIER=", *i
);
1376 r
= sd_journal_add_match(j
, u
, 0);
1379 r
= sd_journal_add_disjunction(j
);
1384 r
= sd_journal_add_conjunction(j
);
1391 static int setup_keys(void) {
1393 size_t mpk_size
, seed_size
, state_size
, i
;
1394 uint8_t *mpk
, *seed
, *state
;
1396 sd_id128_t machine
, boot
;
1397 char *p
= NULL
, *k
= NULL
;
1402 r
= stat("/var/log/journal", &st
);
1403 if (r
< 0 && errno
!= ENOENT
&& errno
!= ENOTDIR
)
1404 return log_error_errno(errno
, "stat(\"%s\") failed: %m", "/var/log/journal");
1406 if (r
< 0 || !S_ISDIR(st
.st_mode
)) {
1407 log_error("%s is not a directory, must be using persistent logging for FSS.",
1408 "/var/log/journal");
1409 return r
< 0 ? -errno
: -ENOTDIR
;
1412 r
= sd_id128_get_machine(&machine
);
1414 return log_error_errno(r
, "Failed to get machine ID: %m");
1416 r
= sd_id128_get_boot(&boot
);
1418 return log_error_errno(r
, "Failed to get boot ID: %m");
1420 if (asprintf(&p
, "/var/log/journal/" SD_ID128_FORMAT_STR
"/fss",
1421 SD_ID128_FORMAT_VAL(machine
)) < 0)
1426 if (r
< 0 && errno
!= ENOENT
) {
1427 r
= log_error_errno(errno
, "unlink(\"%s\") failed: %m", p
);
1430 } else if (access(p
, F_OK
) >= 0) {
1431 log_error("Sealing key file %s exists already. Use --force to recreate.", p
);
1436 if (asprintf(&k
, "/var/log/journal/" SD_ID128_FORMAT_STR
"/fss.tmp.XXXXXX",
1437 SD_ID128_FORMAT_VAL(machine
)) < 0) {
1442 mpk_size
= FSPRG_mskinbytes(FSPRG_RECOMMENDED_SECPAR
);
1443 mpk
= alloca(mpk_size
);
1445 seed_size
= FSPRG_RECOMMENDED_SEEDLEN
;
1446 seed
= alloca(seed_size
);
1448 state_size
= FSPRG_stateinbytes(FSPRG_RECOMMENDED_SECPAR
);
1449 state
= alloca(state_size
);
1451 fd
= open("/dev/random", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1453 r
= log_error_errno(errno
, "Failed to open /dev/random: %m");
1457 log_info("Generating seed...");
1458 r
= loop_read_exact(fd
, seed
, seed_size
, true);
1460 log_error_errno(r
, "Failed to read random seed: %m");
1464 log_info("Generating key pair...");
1465 FSPRG_GenMK(NULL
, mpk
, seed
, seed_size
, FSPRG_RECOMMENDED_SECPAR
);
1467 log_info("Generating sealing key...");
1468 FSPRG_GenState0(state
, mpk
, seed
, seed_size
);
1470 assert(arg_interval
> 0);
1472 n
= now(CLOCK_REALTIME
);
1476 fd
= mkostemp_safe(k
, O_WRONLY
|O_CLOEXEC
);
1478 r
= log_error_errno(errno
, "Failed to open %s: %m", k
);
1482 /* Enable secure remove, exclusion from dump, synchronous
1483 * writing and in-place updating */
1484 r
= chattr_fd(fd
, FS_SECRM_FL
|FS_NODUMP_FL
|FS_SYNC_FL
|FS_NOCOW_FL
, FS_SECRM_FL
|FS_NODUMP_FL
|FS_SYNC_FL
|FS_NOCOW_FL
);
1486 log_warning_errno(errno
, "Failed to set file attributes: %m");
1489 memcpy(h
.signature
, "KSHHRHLP", 8);
1490 h
.machine_id
= machine
;
1492 h
.header_size
= htole64(sizeof(h
));
1493 h
.start_usec
= htole64(n
* arg_interval
);
1494 h
.interval_usec
= htole64(arg_interval
);
1495 h
.fsprg_secpar
= htole16(FSPRG_RECOMMENDED_SECPAR
);
1496 h
.fsprg_state_size
= htole64(state_size
);
1498 r
= loop_write(fd
, &h
, sizeof(h
), false);
1500 log_error_errno(r
, "Failed to write header: %m");
1504 r
= loop_write(fd
, state
, state_size
, false);
1506 log_error_errno(r
, "Failed to write state: %m");
1510 if (link(k
, p
) < 0) {
1511 r
= log_error_errno(errno
, "Failed to link file: %m");
1518 "The new key pair has been generated. The " ANSI_HIGHLIGHT
"secret sealing key" ANSI_NORMAL
" has been written to\n"
1519 "the following local file. This key file is automatically updated when the\n"
1520 "sealing key is advanced. It should not be used on multiple hosts.\n"
1524 "Please write down the following " ANSI_HIGHLIGHT
"secret verification key" ANSI_NORMAL
". It should be stored\n"
1525 "at a safe location and should not be saved locally on disk.\n"
1526 "\n\t" ANSI_HIGHLIGHT_RED
, p
);
1529 for (i
= 0; i
< seed_size
; i
++) {
1530 if (i
> 0 && i
% 3 == 0)
1532 printf("%02x", ((uint8_t*) seed
)[i
]);
1535 printf("/%llx-%llx\n", (unsigned long long) n
, (unsigned long long) arg_interval
);
1538 char tsb
[FORMAT_TIMESPAN_MAX
], *hn
;
1542 "The sealing key is automatically changed every %s.\n",
1543 format_timespan(tsb
, sizeof(tsb
), arg_interval
, 0));
1545 hn
= gethostname_malloc();
1548 hostname_cleanup(hn
);
1549 fprintf(stderr
, "\nThe keys have been generated for host %s/" SD_ID128_FORMAT_STR
".\n", hn
, SD_ID128_FORMAT_VAL(machine
));
1551 fprintf(stderr
, "\nThe keys have been generated for host " SD_ID128_FORMAT_STR
".\n", SD_ID128_FORMAT_VAL(machine
));
1553 #ifdef HAVE_QRENCODE
1554 /* If this is not an UTF-8 system don't print any QR codes */
1555 if (is_locale_utf8()) {
1556 fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr
);
1557 print_qr_code(stderr
, seed
, seed_size
, n
, arg_interval
, hn
, machine
);
1577 log_error("Forward-secure sealing not available.");
1582 static int verify(sd_journal
*j
) {
1589 log_show_color(true);
1591 ORDERED_HASHMAP_FOREACH(f
, j
->files
, i
) {
1593 usec_t first
= 0, validated
= 0, last
= 0;
1596 if (!arg_verify_key
&& JOURNAL_HEADER_SEALED(f
->header
))
1597 log_notice("Journal file %s has sealing enabled but verification key has not been passed using --verify-key=.", f
->path
);
1600 k
= journal_file_verify(f
, arg_verify_key
, &first
, &validated
, &last
, true);
1602 /* If the key was invalid give up right-away. */
1605 log_warning_errno(k
, "FAIL: %s (%m)", f
->path
);
1608 char a
[FORMAT_TIMESTAMP_MAX
], b
[FORMAT_TIMESTAMP_MAX
], c
[FORMAT_TIMESPAN_MAX
];
1609 log_info("PASS: %s", f
->path
);
1611 if (arg_verify_key
&& JOURNAL_HEADER_SEALED(f
->header
)) {
1612 if (validated
> 0) {
1613 log_info("=> Validated from %s to %s, final %s entries not sealed.",
1614 format_timestamp_maybe_utc(a
, sizeof(a
), first
),
1615 format_timestamp_maybe_utc(b
, sizeof(b
), validated
),
1616 format_timespan(c
, sizeof(c
), last
> validated
? last
- validated
: 0, 0));
1617 } else if (last
> 0)
1618 log_info("=> No sealing yet, %s of entries not sealed.",
1619 format_timespan(c
, sizeof(c
), last
- first
, 0));
1621 log_info("=> No sealing yet, no entries in file.");
1629 static int access_check_var_log_journal(sd_journal
*j
) {
1631 _cleanup_strv_free_
char **g
= NULL
;
1641 /* If we are root, we should have access, don't warn. */
1645 /* If we are in the 'systemd-journal' group, we should have
1647 r
= in_group("systemd-journal");
1649 return log_error_errno(r
, "Failed to check if we are in the 'systemd-journal' group: %m");
1654 if (laccess("/run/log/journal", F_OK
) >= 0)
1655 dir
= "/run/log/journal";
1657 dir
= "/var/log/journal";
1659 /* If we are in any of the groups listed in the journal ACLs,
1660 * then all is good, too. Let's enumerate all groups from the
1661 * default ACL of the directory, which generally should allow
1662 * access to most journal files too. */
1663 r
= acl_search_groups(dir
, &g
);
1665 return log_error_errno(r
, "Failed to search journal ACL: %m");
1669 /* Print a pretty list, if there were ACLs set. */
1670 if (!strv_isempty(g
)) {
1671 _cleanup_free_
char *s
= NULL
;
1673 /* Thre are groups in the ACL, let's list them */
1674 r
= strv_extend(&g
, "systemd-journal");
1681 s
= strv_join(g
, "', '");
1685 log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
1686 " Users in groups '%s' can see all messages.\n"
1687 " Pass -q to turn off this notice.", s
);
1692 /* If no ACLs were found, print a short version of the message. */
1693 log_notice("Hint: You are currently not seeing messages from other users and the system.\n"
1694 " Users in the 'systemd-journal' group can see all messages. Pass -q to\n"
1695 " turn off this notice.");
1700 static int access_check(sd_journal
*j
) {
1707 if (set_isempty(j
->errors
)) {
1708 if (ordered_hashmap_isempty(j
->files
))
1709 log_notice("No journal files were found.");
1714 if (set_contains(j
->errors
, INT_TO_PTR(-EACCES
))) {
1715 (void) access_check_var_log_journal(j
);
1717 if (ordered_hashmap_isempty(j
->files
))
1718 r
= log_error_errno(EACCES
, "No journal files were opened due to insufficient permissions.");
1721 SET_FOREACH(code
, j
->errors
, it
) {
1724 err
= -PTR_TO_INT(code
);
1730 log_warning_errno(err
, "Error was encountered while opening journal files: %m");
1738 static int flush_to_var(void) {
1739 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1740 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
1741 _cleanup_close_
int watch_fd
= -1;
1745 if (access("/run/systemd/journal/flushed", F_OK
) >= 0)
1748 /* OK, let's actually do the full logic, send SIGUSR1 to the
1749 * daemon and set up inotify to wait for the flushed file to appear */
1750 r
= bus_connect_system_systemd(&bus
);
1752 return log_error_errno(r
, "Failed to get D-Bus connection: %m");
1754 r
= sd_bus_call_method(
1756 "org.freedesktop.systemd1",
1757 "/org/freedesktop/systemd1",
1758 "org.freedesktop.systemd1.Manager",
1762 "ssi", "systemd-journald.service", "main", SIGUSR1
);
1764 log_error("Failed to kill journal service: %s", bus_error_message(&error
, r
));
1768 mkdir_p("/run/systemd/journal", 0755);
1770 watch_fd
= inotify_init1(IN_NONBLOCK
|IN_CLOEXEC
);
1772 return log_error_errno(errno
, "Failed to create inotify watch: %m");
1774 r
= inotify_add_watch(watch_fd
, "/run/systemd/journal", IN_CREATE
|IN_DONT_FOLLOW
|IN_ONLYDIR
);
1776 return log_error_errno(errno
, "Failed to watch journal directory: %m");
1779 if (access("/run/systemd/journal/flushed", F_OK
) >= 0)
1782 if (errno
!= ENOENT
)
1783 return log_error_errno(errno
, "Failed to check for existence of /run/systemd/journal/flushed: %m");
1785 r
= fd_wait_for_event(watch_fd
, POLLIN
, USEC_INFINITY
);
1787 return log_error_errno(r
, "Failed to wait for event: %m");
1789 r
= flush_fd(watch_fd
);
1791 return log_error_errno(r
, "Failed to flush inotify events: %m");
1797 static int rotate(void) {
1798 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1799 _cleanup_bus_flush_close_unref_ sd_bus
*bus
= NULL
;
1802 r
= bus_connect_system_systemd(&bus
);
1804 return log_error_errno(r
, "Failed to get D-Bus connection: %m");
1806 r
= sd_bus_call_method(
1808 "org.freedesktop.systemd1",
1809 "/org/freedesktop/systemd1",
1810 "org.freedesktop.systemd1.Manager",
1814 "ssi", "systemd-journald.service", "main", SIGUSR2
);
1816 return log_error_errno(r
, "Failed to kill journal service: %s", bus_error_message(&error
, r
));
1821 int main(int argc
, char *argv
[]) {
1823 _cleanup_journal_close_ sd_journal
*j
= NULL
;
1824 bool need_seek
= false;
1825 sd_id128_t previous_boot_id
;
1826 bool previous_boot_id_valid
= false, first_line
= true;
1828 bool ellipsized
= false;
1830 setlocale(LC_ALL
, "");
1831 log_parse_environment();
1834 r
= parse_argv(argc
, argv
);
1838 signal(SIGWINCH
, columns_lines_cache_reset
);
1841 /* Increase max number of open files to 16K if we can, we
1842 * might needs this when browsing journal files, which might
1843 * be split up into many files. */
1844 setrlimit_closest(RLIMIT_NOFILE
, &RLIMIT_MAKE_CONST(16384));
1846 if (arg_action
== ACTION_NEW_ID128
) {
1847 r
= generate_new_id128();
1851 if (arg_action
== ACTION_FLUSH
) {
1856 if (arg_action
== ACTION_ROTATE
) {
1861 if (arg_action
== ACTION_SETUP_KEYS
) {
1866 if (arg_action
== ACTION_UPDATE_CATALOG
||
1867 arg_action
== ACTION_LIST_CATALOG
||
1868 arg_action
== ACTION_DUMP_CATALOG
) {
1870 _cleanup_free_
char *database
;
1872 database
= path_join(arg_root
, CATALOG_DATABASE
, NULL
);
1878 if (arg_action
== ACTION_UPDATE_CATALOG
) {
1879 r
= catalog_update(database
, arg_root
, catalog_file_dirs
);
1881 log_error_errno(r
, "Failed to list catalog: %m");
1883 bool oneline
= arg_action
== ACTION_LIST_CATALOG
;
1885 pager_open_if_enabled();
1887 r
= catalog_list_items(stdout
, database
,
1888 oneline
, argv
+ optind
);
1890 r
= catalog_list(stdout
, database
, oneline
);
1892 log_error_errno(r
, "Failed to list catalog: %m");
1899 r
= sd_journal_open_directory(&j
, arg_directory
, arg_journal_type
);
1901 r
= sd_journal_open_files(&j
, (const char**) arg_file
, 0);
1902 else if (arg_machine
)
1903 r
= sd_journal_open_container(&j
, arg_machine
, 0);
1905 r
= sd_journal_open(&j
, !arg_merge
*SD_JOURNAL_LOCAL_ONLY
+ arg_journal_type
);
1907 log_error_errno(r
, "Failed to open %s: %m",
1908 arg_directory
? arg_directory
: arg_file
? "files" : "journal");
1912 r
= access_check(j
);
1916 if (arg_action
== ACTION_VERIFY
) {
1921 if (arg_action
== ACTION_PRINT_HEADER
) {
1922 journal_print_header(j
);
1927 if (arg_action
== ACTION_DISK_USAGE
) {
1929 char sbytes
[FORMAT_BYTES_MAX
];
1931 r
= sd_journal_get_usage(j
, &bytes
);
1935 printf("Archived and active journals take up %s on disk.\n",
1936 format_bytes(sbytes
, sizeof(sbytes
), bytes
));
1940 if (arg_action
== ACTION_VACUUM
) {
1944 HASHMAP_FOREACH(d
, j
->directories_by_path
, i
) {
1950 q
= journal_directory_vacuum(d
->path
, arg_vacuum_size
, arg_vacuum_n_files
, arg_vacuum_time
, NULL
, true);
1952 log_error_errno(q
, "Failed to vacuum %s: %m", d
->path
);
1960 if (arg_action
== ACTION_LIST_BOOTS
) {
1965 /* add_boot() must be called first!
1966 * It may need to seek the journal to find parent boot IDs. */
1977 log_error_errno(r
, "Failed to add filter for units: %m");
1981 r
= add_syslog_identifier(j
);
1983 log_error_errno(r
, "Failed to add filter for syslog identifiers: %m");
1987 r
= add_priorities(j
);
1991 r
= add_matches(j
, argv
+ optind
);
1995 if (_unlikely_(log_get_max_level() >= LOG_DEBUG
)) {
1996 _cleanup_free_
char *filter
;
1998 filter
= journal_make_match_string(j
);
2002 log_debug("Journal filter: %s", filter
);
2009 r
= sd_journal_set_data_threshold(j
, 0);
2011 log_error_errno(r
, "Failed to unset data size threshold: %m");
2015 r
= sd_journal_query_unique(j
, arg_field
);
2017 log_error_errno(r
, "Failed to query unique data objects: %m");
2021 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, size
) {
2024 if (arg_lines
>= 0 && n_shown
>= arg_lines
)
2027 eq
= memchr(data
, '=', size
);
2029 printf("%.*s\n", (int) (size
- ((const uint8_t*) eq
- (const uint8_t*) data
+ 1)), (const char*) eq
+ 1);
2031 printf("%.*s\n", (int) size
, (const char*) data
);
2040 /* Opening the fd now means the first sd_journal_wait() will actually wait */
2042 r
= sd_journal_get_fd(j
);
2044 log_error_errno(r
, "Failed to get journal fd: %m");
2049 if (arg_cursor
|| arg_after_cursor
) {
2050 r
= sd_journal_seek_cursor(j
, arg_cursor
?: arg_after_cursor
);
2052 log_error_errno(r
, "Failed to seek to cursor: %m");
2057 r
= sd_journal_next_skip(j
, 1 + !!arg_after_cursor
);
2059 r
= sd_journal_previous_skip(j
, 1 + !!arg_after_cursor
);
2061 if (arg_after_cursor
&& r
< 2) {
2062 /* We couldn't find the next entry after the cursor. */
2069 } else if (arg_since_set
&& !arg_reverse
) {
2070 r
= sd_journal_seek_realtime_usec(j
, arg_since
);
2072 log_error_errno(r
, "Failed to seek to date: %m");
2075 r
= sd_journal_next(j
);
2077 } else if (arg_until_set
&& arg_reverse
) {
2078 r
= sd_journal_seek_realtime_usec(j
, arg_until
);
2080 log_error_errno(r
, "Failed to seek to date: %m");
2083 r
= sd_journal_previous(j
);
2085 } else if (arg_lines
>= 0) {
2086 r
= sd_journal_seek_tail(j
);
2088 log_error_errno(r
, "Failed to seek to tail: %m");
2092 r
= sd_journal_previous_skip(j
, arg_lines
);
2094 } else if (arg_reverse
) {
2095 r
= sd_journal_seek_tail(j
);
2097 log_error_errno(r
, "Failed to seek to tail: %m");
2101 r
= sd_journal_previous(j
);
2104 r
= sd_journal_seek_head(j
);
2106 log_error_errno(r
, "Failed to seek to head: %m");
2110 r
= sd_journal_next(j
);
2114 log_error_errno(r
, "Failed to iterate through journal: %m");
2121 printf("-- No entries --\n");
2127 pager_open_if_enabled();
2131 char start_buf
[FORMAT_TIMESTAMP_MAX
], end_buf
[FORMAT_TIMESTAMP_MAX
];
2133 r
= sd_journal_get_cutoff_realtime_usec(j
, &start
, &end
);
2135 log_error_errno(r
, "Failed to get cutoff: %m");
2141 printf("-- Logs begin at %s. --\n",
2142 format_timestamp_maybe_utc(start_buf
, sizeof(start_buf
), start
));
2144 printf("-- Logs begin at %s, end at %s. --\n",
2145 format_timestamp_maybe_utc(start_buf
, sizeof(start_buf
), start
),
2146 format_timestamp_maybe_utc(end_buf
, sizeof(end_buf
), end
));
2151 while (arg_lines
< 0 || n_shown
< arg_lines
|| (arg_follow
&& !first_line
)) {
2156 r
= sd_journal_next(j
);
2158 r
= sd_journal_previous(j
);
2160 log_error_errno(r
, "Failed to iterate through journal: %m");
2167 if (arg_until_set
&& !arg_reverse
) {
2170 r
= sd_journal_get_realtime_usec(j
, &usec
);
2172 log_error_errno(r
, "Failed to determine timestamp: %m");
2175 if (usec
> arg_until
)
2179 if (arg_since_set
&& arg_reverse
) {
2182 r
= sd_journal_get_realtime_usec(j
, &usec
);
2184 log_error_errno(r
, "Failed to determine timestamp: %m");
2187 if (usec
< arg_since
)
2191 if (!arg_merge
&& !arg_quiet
) {
2194 r
= sd_journal_get_monotonic_usec(j
, NULL
, &boot_id
);
2196 if (previous_boot_id_valid
&&
2197 !sd_id128_equal(boot_id
, previous_boot_id
))
2198 printf("%s-- Reboot --%s\n",
2199 ansi_highlight(), ansi_normal());
2201 previous_boot_id
= boot_id
;
2202 previous_boot_id_valid
= true;
2207 arg_all
* OUTPUT_SHOW_ALL
|
2208 arg_full
* OUTPUT_FULL_WIDTH
|
2209 on_tty() * OUTPUT_COLOR
|
2210 arg_catalog
* OUTPUT_CATALOG
|
2211 arg_utc
* OUTPUT_UTC
;
2213 r
= output_journal(stdout
, j
, arg_output
, 0, flags
, &ellipsized
);
2215 if (r
== -EADDRNOTAVAIL
)
2217 else if (r
< 0 || ferror(stdout
))
2224 if (arg_show_cursor
) {
2225 _cleanup_free_
char *cursor
= NULL
;
2227 r
= sd_journal_get_cursor(j
, &cursor
);
2228 if (r
< 0 && r
!= -EADDRNOTAVAIL
)
2229 log_error_errno(r
, "Failed to get cursor: %m");
2231 printf("-- cursor: %s\n", cursor
);
2237 r
= sd_journal_wait(j
, (uint64_t) -1);
2239 log_error_errno(r
, "Couldn't wait for journal event: %m");
2249 strv_free(arg_file
);
2251 strv_free(arg_syslog_identifier
);
2252 strv_free(arg_system_units
);
2253 strv_free(arg_user_units
);
2257 return r
< 0 ? EXIT_FAILURE
: EXIT_SUCCESS
;