2 This file is part of systemd.
4 Copyright 2012 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <sys/socket.h>
32 #include "sd-journal.h"
34 #include "alloc-util.h"
36 #include "format-util.h"
38 #include "hostname-util.h"
40 #include "journal-internal.h"
42 #include "logs-show.h"
44 #include "output-mode.h"
45 #include "parse-util.h"
46 #include "process-util.h"
47 #include "sparse-endian.h"
48 #include "stdio-util.h"
49 #include "string-table.h"
50 #include "string-util.h"
52 #include "terminal-util.h"
53 #include "time-util.h"
57 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
58 #define PRINT_LINE_THRESHOLD 3
59 #define PRINT_CHAR_THRESHOLD 300
61 #define JSON_THRESHOLD 4096
63 static int print_catalog(FILE *f
, sd_journal
*j
) {
65 _cleanup_free_
char *t
= NULL
, *z
= NULL
;
68 r
= sd_journal_get_catalog(j
, &t
);
72 z
= strreplace(strstrip(t
), "\n", "\n-- ");
83 static int parse_field(const void *data
, size_t length
, const char *field
, size_t field_len
, char **target
, size_t *target_len
) {
91 if (length
< field_len
)
94 if (memcmp(data
, field
, field_len
))
97 nl
= length
- field_len
;
100 buf
= newdup_suffix0(char, (const char*) data
+ field_len
, nl
);
113 typedef struct ParseFieldVec
{
120 #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) \
121 { .field = _field, .field_len = strlen(_field), .target = _target, .target_len = _target_len }
123 static int parse_fieldv(const void *data
, size_t length
, const ParseFieldVec
*fields
, unsigned n_fields
) {
126 for (i
= 0; i
< n_fields
; i
++) {
127 const ParseFieldVec
*f
= &fields
[i
];
130 r
= parse_field(data
, length
, f
->field
, f
->field_len
, f
->target
, f
->target_len
);
140 static int field_set_test(Set
*fields
, const char *name
, size_t n
) {
146 s
= strndupa(name
, n
);
150 return set_get(fields
, s
) ? 1 : 0;
153 static bool shall_print(const char *p
, size_t l
, OutputFlags flags
) {
156 if (flags
& OUTPUT_SHOW_ALL
)
159 if (l
>= PRINT_CHAR_THRESHOLD
)
162 if (!utf8_is_printable(p
, l
))
168 static bool print_multiline(FILE *f
, unsigned prefix
, unsigned n_columns
, OutputFlags flags
, int priority
, const char* message
, size_t message_len
) {
169 const char *color_on
= "", *color_off
= "";
170 const char *pos
, *end
;
171 bool ellipsized
= false;
174 if (flags
& OUTPUT_COLOR
) {
175 if (priority
<= LOG_ERR
) {
176 color_on
= ANSI_HIGHLIGHT_RED
;
177 color_off
= ANSI_NORMAL
;
178 } else if (priority
<= LOG_NOTICE
) {
179 color_on
= ANSI_HIGHLIGHT
;
180 color_off
= ANSI_NORMAL
;
184 /* A special case: make sure that we print a newline when
185 the message is empty. */
186 if (message_len
== 0)
190 pos
< message
+ message_len
;
191 pos
= end
+ 1, line
++) {
192 bool continuation
= line
> 0;
195 for (end
= pos
; end
< message
+ message_len
&& *end
!= '\n'; end
++)
200 /* We need to figure out when we are showing not-last line, *and*
201 * will skip subsequent lines. In that case, we will put the dots
202 * at the end of the line, instead of putting dots in the middle
206 line
+ 1 == PRINT_LINE_THRESHOLD
||
207 end
+ 1 >= message
+ PRINT_CHAR_THRESHOLD
;
209 if (flags
& (OUTPUT_FULL_WIDTH
| OUTPUT_SHOW_ALL
) ||
210 (prefix
+ len
+ 1 < n_columns
&& !tail_line
)) {
211 fprintf(f
, "%*s%s%.*s%s\n",
212 continuation
* prefix
, "",
213 color_on
, len
, pos
, color_off
);
217 /* Beyond this point, ellipsization will happen. */
220 if (prefix
< n_columns
&& n_columns
- prefix
>= 3) {
221 if (n_columns
- prefix
> (unsigned) len
+ 3)
222 fprintf(f
, "%*s%s%.*s...%s\n",
223 continuation
* prefix
, "",
224 color_on
, len
, pos
, color_off
);
226 _cleanup_free_
char *e
;
228 e
= ellipsize_mem(pos
, len
, n_columns
- prefix
,
229 tail_line
? 100 : 90);
231 fprintf(f
, "%*s%s%.*s%s\n",
232 continuation
* prefix
, "",
233 color_on
, len
, pos
, color_off
);
235 fprintf(f
, "%*s%s%s%s\n",
236 continuation
* prefix
, "",
237 color_on
, e
, color_off
);
249 static int output_timestamp_monotonic(FILE *f
, sd_journal
*j
, const char *monotonic
) {
259 r
= safe_atou64(monotonic
, &t
);
261 r
= sd_journal_get_monotonic_usec(j
, &t
, &boot_id
);
263 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
265 fprintf(f
, "[%5"PRI_USEC
".%06"PRI_USEC
"]", t
/ USEC_PER_SEC
, t
% USEC_PER_SEC
);
266 return 1 + 5 + 1 + 6 + 1;
269 static int output_timestamp_realtime(FILE *f
, sd_journal
*j
, OutputMode mode
, OutputFlags flags
, const char *realtime
) {
270 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, 64)];
271 struct tm
*(*gettime_r
)(const time_t *, struct tm
*);
282 r
= safe_atou64(realtime
, &x
);
284 r
= sd_journal_get_realtime_usec(j
, &x
);
286 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
288 if (x
> USEC_TIMESTAMP_FORMATTABLE_MAX
) {
289 log_error("Timestamp cannot be printed");
293 if (mode
== OUTPUT_SHORT_FULL
) {
296 if (flags
& OUTPUT_UTC
)
297 k
= format_timestamp_utc(buf
, sizeof(buf
), x
);
299 k
= format_timestamp(buf
, sizeof(buf
), x
);
301 log_error("Failed to format timestamp.");
308 gettime_r
= (flags
& OUTPUT_UTC
) ? gmtime_r
: localtime_r
;
309 t
= (time_t) (x
/ USEC_PER_SEC
);
313 case OUTPUT_SHORT_UNIX
:
314 xsprintf(buf
, "%10"PRI_TIME
".%06"PRIu64
, t
, x
% USEC_PER_SEC
);
317 case OUTPUT_SHORT_ISO
:
318 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t
, &tm
)) <= 0) {
319 log_error("Failed to format ISO time");
324 case OUTPUT_SHORT_ISO_PRECISE
:
325 /* No usec in strftime, so we leave space and copy over */
326 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t
, &tm
)) <= 0) {
327 log_error("Failed to format ISO-precise time");
330 xsprintf(usec
, "%06"PRI_USEC
, x
% USEC_PER_SEC
);
331 memcpy(buf
+ 20, usec
, 6);
335 case OUTPUT_SHORT_PRECISE
:
337 if (strftime(buf
, sizeof(buf
), "%b %d %H:%M:%S", gettime_r(&t
, &tm
)) <= 0) {
338 log_error("Failed to format syslog time");
342 if (mode
== OUTPUT_SHORT_PRECISE
) {
345 assert(sizeof(buf
) > strlen(buf
));
346 k
= sizeof(buf
) - strlen(buf
);
348 r
= snprintf(buf
+ strlen(buf
), k
, ".%06"PRIu64
, x
% USEC_PER_SEC
);
349 if (r
<= 0 || (size_t) r
>= k
) { /* too long? */
350 log_error("Failed to format precise time");
357 assert_not_reached("Unknown time format");
362 return (int) strlen(buf
);
365 static int output_short(
371 Set
*output_fields
) {
377 _cleanup_free_
char *hostname
= NULL
, *identifier
= NULL
, *comm
= NULL
, *pid
= NULL
, *fake_pid
= NULL
, *message
= NULL
, *realtime
= NULL
, *monotonic
= NULL
, *priority
= NULL
;
378 size_t hostname_len
= 0, identifier_len
= 0, comm_len
= 0, pid_len
= 0, fake_pid_len
= 0, message_len
= 0, realtime_len
= 0, monotonic_len
= 0, priority_len
= 0;
380 bool ellipsized
= false;
381 const ParseFieldVec fields
[] = {
382 PARSE_FIELD_VEC_ENTRY("_PID=", &pid
, &pid_len
),
383 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm
, &comm_len
),
384 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message
, &message_len
),
385 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority
, &priority_len
),
386 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname
, &hostname_len
),
387 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid
, &fake_pid_len
),
388 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier
, &identifier_len
),
389 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime
, &realtime_len
),
390 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic
, &monotonic_len
),
396 /* Set the threshold to one bigger than the actual print
397 * threshold, so that if the line is actually longer than what
398 * we're willing to print, ellipsization will occur. This way
399 * we won't output a misleading line without any indication of
402 sd_journal_set_data_threshold(j
, flags
& (OUTPUT_SHOW_ALL
|OUTPUT_FULL_WIDTH
) ? 0 : PRINT_CHAR_THRESHOLD
+ 1);
404 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
406 r
= parse_fieldv(data
, length
, fields
, ELEMENTSOF(fields
));
411 log_debug_errno(r
, "Skipping message we can't read: %m");
415 return log_error_errno(r
, "Failed to get journal fields: %m");
418 log_debug("Skipping message without MESSAGE= field.");
422 if (!(flags
& OUTPUT_SHOW_ALL
))
423 strip_tab_ansi(&message
, &message_len
);
425 if (priority_len
== 1 && *priority
>= '0' && *priority
<= '7')
428 if (mode
== OUTPUT_SHORT_MONOTONIC
)
429 r
= output_timestamp_monotonic(f
, j
, monotonic
);
431 r
= output_timestamp_realtime(f
, j
, mode
, flags
, realtime
);
436 if (flags
& OUTPUT_NO_HOSTNAME
) {
437 /* Suppress display of the hostname if this is requested. */
438 hostname
= mfree(hostname
);
442 if (hostname
&& shall_print(hostname
, hostname_len
, flags
)) {
443 fprintf(f
, " %.*s", (int) hostname_len
, hostname
);
444 n
+= hostname_len
+ 1;
447 if (identifier
&& shall_print(identifier
, identifier_len
, flags
)) {
448 fprintf(f
, " %.*s", (int) identifier_len
, identifier
);
449 n
+= identifier_len
+ 1;
450 } else if (comm
&& shall_print(comm
, comm_len
, flags
)) {
451 fprintf(f
, " %.*s", (int) comm_len
, comm
);
454 fputs(" unknown", f
);
456 if (pid
&& shall_print(pid
, pid_len
, flags
)) {
457 fprintf(f
, "[%.*s]", (int) pid_len
, pid
);
459 } else if (fake_pid
&& shall_print(fake_pid
, fake_pid_len
, flags
)) {
460 fprintf(f
, "[%.*s]", (int) fake_pid_len
, fake_pid
);
461 n
+= fake_pid_len
+ 2;
464 if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(message
, message_len
)) {
465 char bytes
[FORMAT_BYTES_MAX
];
466 fprintf(f
, ": [%s blob data]\n", format_bytes(bytes
, sizeof(bytes
), message_len
));
470 print_multiline(f
, n
+ 2, n_columns
, flags
, p
, message
, message_len
);
473 if (flags
& OUTPUT_CATALOG
)
479 static int output_verbose(
485 Set
*output_fields
) {
489 _cleanup_free_
char *cursor
= NULL
;
490 uint64_t realtime
= 0;
491 char ts
[FORMAT_TIMESTAMP_MAX
+ 7];
492 const char *timestamp
;
498 sd_journal_set_data_threshold(j
, 0);
500 r
= sd_journal_get_data(j
, "_SOURCE_REALTIME_TIMESTAMP", &data
, &length
);
502 log_debug("Source realtime timestamp not found");
504 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get source realtime timestamp: %m");
506 _cleanup_free_
char *value
= NULL
;
508 r
= parse_field(data
, length
, "_SOURCE_REALTIME_TIMESTAMP=", strlen("_SOURCE_REALTIME_TIMESTAMP="), &value
, NULL
);
513 r
= safe_atou64(value
, &realtime
);
515 log_debug_errno(r
, "Failed to parse realtime timestamp: %m");
519 r
= sd_journal_get_realtime_usec(j
, &realtime
);
521 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get realtime timestamp: %m");
524 r
= sd_journal_get_cursor(j
, &cursor
);
526 return log_error_errno(r
, "Failed to get cursor: %m");
528 timestamp
= flags
& OUTPUT_UTC
? format_timestamp_us_utc(ts
, sizeof ts
, realtime
)
529 : format_timestamp_us(ts
, sizeof ts
, realtime
);
530 fprintf(f
, "%s [%s]\n",
531 timestamp
?: "(no timestamp)",
534 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
537 const char *on
= "", *off
= "";
539 c
= memchr(data
, '=', length
);
541 log_error("Invalid field.");
544 fieldlen
= c
- (const char*) data
;
546 r
= field_set_test(output_fields
, data
, fieldlen
);
552 if (flags
& OUTPUT_COLOR
&& startswith(data
, "MESSAGE=")) {
557 if ((flags
& OUTPUT_SHOW_ALL
) ||
558 (((length
< PRINT_CHAR_THRESHOLD
) || flags
& OUTPUT_FULL_WIDTH
)
559 && utf8_is_printable(data
, length
))) {
560 fprintf(f
, " %s%.*s=", on
, fieldlen
, (const char*)data
);
561 print_multiline(f
, 4 + fieldlen
+ 1, 0, OUTPUT_FULL_WIDTH
, 0, c
+ 1, length
- fieldlen
- 1);
564 char bytes
[FORMAT_BYTES_MAX
];
566 fprintf(f
, " %s%.*s=[%s blob data]%s\n",
568 (int) (c
- (const char*) data
),
570 format_bytes(bytes
, sizeof(bytes
), length
- (c
- (const char *) data
) - 1),
578 if (flags
& OUTPUT_CATALOG
)
584 static int output_export(
590 Set
*output_fields
) {
595 usec_t realtime
, monotonic
;
596 _cleanup_free_
char *cursor
= NULL
;
602 sd_journal_set_data_threshold(j
, 0);
604 r
= sd_journal_get_realtime_usec(j
, &realtime
);
606 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
608 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
610 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
612 r
= sd_journal_get_cursor(j
, &cursor
);
614 return log_error_errno(r
, "Failed to get cursor: %m");
618 "__REALTIME_TIMESTAMP="USEC_FMT
"\n"
619 "__MONOTONIC_TIMESTAMP="USEC_FMT
"\n"
624 sd_id128_to_string(boot_id
, sid
));
626 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
629 /* We already printed the boot id, from the data in
630 * the header, hence let's suppress it here */
632 startswith(data
, "_BOOT_ID="))
635 c
= memchr(data
, '=', length
);
637 log_error("Invalid field.");
641 r
= field_set_test(output_fields
, data
, c
- (const char *) data
);
647 if (utf8_is_printable_newline(data
, length
, false))
648 fwrite(data
, length
, 1, f
);
652 fwrite(data
, c
- (const char*) data
, 1, f
);
654 le64
= htole64(length
- (c
- (const char*) data
) - 1);
655 fwrite(&le64
, sizeof(le64
), 1, f
);
656 fwrite(c
+ 1, length
- (c
- (const char*) data
) - 1, 1, f
);
679 if (!(flags
& OUTPUT_SHOW_ALL
) && l
>= JSON_THRESHOLD
)
682 else if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(p
, l
)) {
683 bool not_first
= false;
689 fprintf(f
, ", %u", (uint8_t) *p
);
692 fprintf(f
, "%u", (uint8_t) *p
);
704 if (IN_SET(*p
, '"', '\\')) {
707 } else if (*p
== '\n')
709 else if ((uint8_t) *p
< ' ')
710 fprintf(f
, "\\u%04x", (uint8_t) *p
);
722 static int output_json(
728 Set
*output_fields
) {
730 uint64_t realtime
, monotonic
;
731 _cleanup_free_
char *cursor
= NULL
;
738 bool done
, separator
;
742 sd_journal_set_data_threshold(j
, flags
& OUTPUT_SHOW_ALL
? 0 : JSON_THRESHOLD
);
744 r
= sd_journal_get_realtime_usec(j
, &realtime
);
746 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
748 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
750 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
752 r
= sd_journal_get_cursor(j
, &cursor
);
754 return log_error_errno(r
, "Failed to get cursor: %m");
756 if (mode
== OUTPUT_JSON_PRETTY
)
759 "\t\"__CURSOR\" : \"%s\",\n"
760 "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\",\n"
761 "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\",\n"
762 "\t\"_BOOT_ID\" : \"%s\"",
766 sd_id128_to_string(boot_id
, sid
));
768 if (mode
== OUTPUT_JSON_SSE
)
772 "{ \"__CURSOR\" : \"%s\", "
773 "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\", "
774 "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\", "
775 "\"_BOOT_ID\" : \"%s\"",
779 sd_id128_to_string(boot_id
, sid
));
782 h
= hashmap_new(&string_hash_ops
);
786 /* First round, iterate through the entry and count how often each field appears */
787 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
793 memcmp(data
, "_BOOT_ID=", 9) == 0)
796 eq
= memchr(data
, '=', length
);
800 n
= strndup(data
, eq
- (const char*) data
);
806 u
= PTR_TO_UINT(hashmap_get(h
, n
));
808 r
= hashmap_put(h
, n
, UINT_TO_PTR(1));
815 r
= hashmap_update(h
, n
, UINT_TO_PTR(u
+ 1));
831 SD_JOURNAL_FOREACH_DATA(j
, data
, length
) {
837 /* We already printed the boot id, from the data in
838 * the header, hence let's suppress it here */
840 memcmp(data
, "_BOOT_ID=", 9) == 0)
843 eq
= memchr(data
, '=', length
);
847 m
= eq
- (const char*) data
;
849 n
= strndup(data
, m
);
855 if (output_fields
&& !set_get(output_fields
, n
)) {
861 if (mode
== OUTPUT_JSON_PRETTY
)
867 u
= PTR_TO_UINT(hashmap_get2(h
, n
, (void**) &kk
));
869 /* We already printed this, let's jump to the next */
875 /* Field only appears once, output it directly */
877 json_escape(f
, data
, m
, flags
);
880 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
882 hashmap_remove(h
, n
);
891 /* Field appears multiple times, output it as array */
892 json_escape(f
, data
, m
, flags
);
894 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
896 /* Iterate through the end of the list */
898 while (sd_journal_enumerate_data(j
, &data
, &length
) > 0) {
902 if (memcmp(data
, n
, m
) != 0)
905 if (((const char*) data
)[m
] != '=')
909 json_escape(f
, (const char*) data
+ m
+ 1, length
- m
- 1, flags
);
914 hashmap_remove(h
, n
);
918 /* Iterate data fields form the beginning */
928 if (mode
== OUTPUT_JSON_PRETTY
)
930 else if (mode
== OUTPUT_JSON_SSE
)
938 while ((k
= hashmap_steal_first_key(h
)))
946 static int output_cat(
952 Set
*output_fields
) {
961 sd_journal_set_data_threshold(j
, 0);
963 r
= sd_journal_get_data(j
, "MESSAGE", &data
, &l
);
965 /* An entry without MESSAGE=? */
969 return log_error_errno(r
, "Failed to get data: %m");
974 fwrite((const char*) data
+ 8, 1, l
- 8, f
);
980 static int (*output_funcs
[_OUTPUT_MODE_MAX
])(
986 Set
*output_fields
) = {
988 [OUTPUT_SHORT
] = output_short
,
989 [OUTPUT_SHORT_ISO
] = output_short
,
990 [OUTPUT_SHORT_ISO_PRECISE
] = output_short
,
991 [OUTPUT_SHORT_PRECISE
] = output_short
,
992 [OUTPUT_SHORT_MONOTONIC
] = output_short
,
993 [OUTPUT_SHORT_UNIX
] = output_short
,
994 [OUTPUT_SHORT_FULL
] = output_short
,
995 [OUTPUT_VERBOSE
] = output_verbose
,
996 [OUTPUT_EXPORT
] = output_export
,
997 [OUTPUT_JSON
] = output_json
,
998 [OUTPUT_JSON_PRETTY
] = output_json
,
999 [OUTPUT_JSON_SSE
] = output_json
,
1000 [OUTPUT_CAT
] = output_cat
1009 char **output_fields
,
1013 _cleanup_set_free_free_ Set
*fields
= NULL
;
1015 assert(mode
< _OUTPUT_MODE_MAX
);
1018 n_columns
= columns();
1020 if (output_fields
) {
1021 fields
= set_new(&string_hash_ops
);
1025 ret
= set_put_strdupv(fields
, output_fields
);
1030 ret
= output_funcs
[mode
](f
, j
, mode
, n_columns
, flags
, fields
);
1032 if (ellipsized
&& ret
> 0)
1038 static int maybe_print_begin_newline(FILE *f
, OutputFlags
*flags
) {
1042 if (!(*flags
& OUTPUT_BEGIN_NEWLINE
))
1045 /* Print a beginning new line if that's request, but only once
1046 * on the first line we print. */
1049 *flags
&= ~OUTPUT_BEGIN_NEWLINE
;
1053 static int show_journal(FILE *f
,
1064 bool need_seek
= false;
1065 int warn_cutoff
= flags
& OUTPUT_WARN_CUTOFF
;
1069 assert(mode
< _OUTPUT_MODE_MAX
);
1072 r
= sd_journal_seek_tail(j
);
1074 return log_error_errno(r
, "Failed to seek to tail: %m");
1076 r
= sd_journal_previous_skip(j
, how_many
);
1078 return log_error_errno(r
, "Failed to skip previous: %m");
1085 r
= sd_journal_next(j
);
1087 return log_error_errno(r
, "Failed to iterate through journal: %m");
1095 if (not_before
> 0) {
1096 r
= sd_journal_get_monotonic_usec(j
, &usec
, NULL
);
1098 /* -ESTALE is returned if the
1099 timestamp is not from this boot */
1103 return log_error_errno(r
, "Failed to get journal time: %m");
1105 if (usec
< not_before
)
1110 maybe_print_begin_newline(f
, &flags
);
1112 r
= output_journal(f
, j
, mode
, n_columns
, flags
, NULL
, ellipsized
);
1117 if (warn_cutoff
&& line
< how_many
&& not_before
> 0) {
1121 /* Check whether the cutoff line is too early */
1123 r
= sd_id128_get_boot(&boot_id
);
1125 return log_error_errno(r
, "Failed to get boot id: %m");
1127 r
= sd_journal_get_cutoff_monotonic_usec(j
, boot_id
, &cutoff
, NULL
);
1129 return log_error_errno(r
, "Failed to get journal cutoff time: %m");
1131 if (r
> 0 && not_before
< cutoff
) {
1132 maybe_print_begin_newline(f
, &flags
);
1133 fprintf(f
, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1136 warn_cutoff
= false;
1139 if (!(flags
& OUTPUT_FOLLOW
))
1142 r
= sd_journal_wait(j
, USEC_INFINITY
);
1144 return log_error_errno(r
, "Failed to wait for journal: %m");
1151 int add_matches_for_unit(sd_journal
*j
, const char *unit
) {
1152 const char *m1
, *m2
, *m3
, *m4
;
1158 m1
= strjoina("_SYSTEMD_UNIT=", unit
);
1159 m2
= strjoina("COREDUMP_UNIT=", unit
);
1160 m3
= strjoina("UNIT=", unit
);
1161 m4
= strjoina("OBJECT_SYSTEMD_UNIT=", unit
);
1164 /* Look for messages from the service itself */
1165 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1167 /* Look for coredumps of the service */
1168 (r
= sd_journal_add_disjunction(j
)) ||
1169 (r
= sd_journal_add_match(j
, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1170 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1171 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1173 /* Look for messages from PID 1 about this service */
1174 (r
= sd_journal_add_disjunction(j
)) ||
1175 (r
= sd_journal_add_match(j
, "_PID=1", 0)) ||
1176 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1178 /* Look for messages from authorized daemons about this service */
1179 (r
= sd_journal_add_disjunction(j
)) ||
1180 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1181 (r
= sd_journal_add_match(j
, m4
, 0))
1184 if (r
== 0 && endswith(unit
, ".slice")) {
1187 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1189 /* Show all messages belonging to a slice */
1191 (r
= sd_journal_add_disjunction(j
)) ||
1192 (r
= sd_journal_add_match(j
, m5
, 0))
1199 int add_matches_for_user_unit(sd_journal
*j
, const char *unit
, uid_t uid
) {
1201 char *m1
, *m2
, *m3
, *m4
;
1202 char muid
[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t
)];
1207 m1
= strjoina("_SYSTEMD_USER_UNIT=", unit
);
1208 m2
= strjoina("USER_UNIT=", unit
);
1209 m3
= strjoina("COREDUMP_USER_UNIT=", unit
);
1210 m4
= strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit
);
1211 sprintf(muid
, "_UID="UID_FMT
, uid
);
1214 /* Look for messages from the user service itself */
1215 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1216 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1218 /* Look for messages from systemd about this service */
1219 (r
= sd_journal_add_disjunction(j
)) ||
1220 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1221 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1223 /* Look for coredumps of the service */
1224 (r
= sd_journal_add_disjunction(j
)) ||
1225 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1226 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1227 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1229 /* Look for messages from authorized daemons about this service */
1230 (r
= sd_journal_add_disjunction(j
)) ||
1231 (r
= sd_journal_add_match(j
, m4
, 0)) ||
1232 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1233 (r
= sd_journal_add_match(j
, "_UID=0", 0))
1236 if (r
== 0 && endswith(unit
, ".slice")) {
1239 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1241 /* Show all messages belonging to a slice */
1243 (r
= sd_journal_add_disjunction(j
)) ||
1244 (r
= sd_journal_add_match(j
, m5
, 0)) ||
1245 (r
= sd_journal_add_match(j
, muid
, 0))
1252 static int get_boot_id_for_machine(const char *machine
, sd_id128_t
*boot_id
) {
1253 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1254 _cleanup_close_
int pidnsfd
= -1, mntnsfd
= -1, rootfd
= -1;
1264 if (!machine_name_is_valid(machine
))
1267 r
= container_get_leader(machine
, &pid
);
1271 r
= namespace_open(pid
, &pidnsfd
, &mntnsfd
, NULL
, NULL
, &rootfd
);
1275 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1285 pair
[0] = safe_close(pair
[0]);
1287 r
= namespace_enter(pidnsfd
, mntnsfd
, -1, -1, rootfd
);
1289 _exit(EXIT_FAILURE
);
1291 fd
= open("/proc/sys/kernel/random/boot_id", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1293 _exit(EXIT_FAILURE
);
1295 r
= loop_read_exact(fd
, buf
, 36, false);
1298 _exit(EXIT_FAILURE
);
1300 k
= send(pair
[1], buf
, 36, MSG_NOSIGNAL
);
1302 _exit(EXIT_FAILURE
);
1304 _exit(EXIT_SUCCESS
);
1307 pair
[1] = safe_close(pair
[1]);
1309 r
= wait_for_terminate(child
, &si
);
1310 if (r
< 0 || si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
1311 return r
< 0 ? r
: -EIO
;
1313 k
= recv(pair
[0], buf
, 36, 0);
1318 r
= sd_id128_from_string(buf
, boot_id
);
1325 int add_match_this_boot(sd_journal
*j
, const char *machine
) {
1326 char match
[9+32+1] = "_BOOT_ID=";
1333 r
= get_boot_id_for_machine(machine
, &boot_id
);
1335 return log_error_errno(r
, "Failed to get boot id of container %s: %m", machine
);
1337 r
= sd_id128_get_boot(&boot_id
);
1339 return log_error_errno(r
, "Failed to get boot id: %m");
1342 sd_id128_to_string(boot_id
, match
+ 9);
1343 r
= sd_journal_add_match(j
, match
, strlen(match
));
1345 return log_error_errno(r
, "Failed to add match: %m");
1347 r
= sd_journal_add_conjunction(j
);
1349 return log_error_errno(r
, "Failed to add conjunction: %m");
1354 int show_journal_by_unit(
1363 int journal_open_flags
,
1367 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1371 assert(mode
< _OUTPUT_MODE_MAX
);
1377 r
= sd_journal_open(&j
, journal_open_flags
);
1379 return log_error_errno(r
, "Failed to open journal: %m");
1381 r
= add_match_this_boot(j
, NULL
);
1386 r
= add_matches_for_unit(j
, unit
);
1388 r
= add_matches_for_user_unit(j
, unit
, uid
);
1390 return log_error_errno(r
, "Failed to add unit matches: %m");
1392 if (_unlikely_(log_get_max_level() >= LOG_DEBUG
)) {
1393 _cleanup_free_
char *filter
;
1395 filter
= journal_make_match_string(j
);
1399 log_debug("Journal filter: %s", filter
);
1402 return show_journal(f
, j
, mode
, n_columns
, not_before
, how_many
, flags
, ellipsized
);