1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
14 #include <sys/socket.h>
20 #include "sd-journal.h"
22 #include "alloc-util.h"
24 #include "format-util.h"
26 #include "hostname-util.h"
28 #include "journal-internal.h"
30 #include "logs-show.h"
32 #include "output-mode.h"
33 #include "parse-util.h"
34 #include "process-util.h"
35 #include "sparse-endian.h"
36 #include "stdio-util.h"
37 #include "string-table.h"
38 #include "string-util.h"
40 #include "terminal-util.h"
41 #include "time-util.h"
45 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
46 #define PRINT_LINE_THRESHOLD 3
47 #define PRINT_CHAR_THRESHOLD 300
49 #define JSON_THRESHOLD 4096
51 static int print_catalog(FILE *f
, sd_journal
*j
) {
53 _cleanup_free_
char *t
= NULL
, *z
= NULL
;
56 r
= sd_journal_get_catalog(j
, &t
);
60 z
= strreplace(strstrip(t
), "\n", "\n-- ");
71 static int parse_field(const void *data
, size_t length
, const char *field
, size_t field_len
, char **target
, size_t *target_len
) {
79 if (length
< field_len
)
82 if (memcmp(data
, field
, field_len
))
85 nl
= length
- field_len
;
88 buf
= newdup_suffix0(char, (const char*) data
+ field_len
, nl
);
101 typedef struct ParseFieldVec
{
108 #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) \
109 { .field = _field, .field_len = strlen(_field), .target = _target, .target_len = _target_len }
111 static int parse_fieldv(const void *data
, size_t length
, const ParseFieldVec
*fields
, unsigned n_fields
) {
114 for (i
= 0; i
< n_fields
; i
++) {
115 const ParseFieldVec
*f
= &fields
[i
];
118 r
= parse_field(data
, length
, f
->field
, f
->field_len
, f
->target
, f
->target_len
);
128 static int field_set_test(Set
*fields
, const char *name
, size_t n
) {
134 s
= strndupa(name
, n
);
138 return set_get(fields
, s
) ? 1 : 0;
141 static bool shall_print(const char *p
, size_t l
, OutputFlags flags
) {
144 if (flags
& OUTPUT_SHOW_ALL
)
147 if (l
>= PRINT_CHAR_THRESHOLD
)
150 if (!utf8_is_printable(p
, l
))
156 static bool print_multiline(
164 size_t highlight
[2]) {
166 const char *color_on
= "", *color_off
= "", *highlight_on
= "";
167 const char *pos
, *end
;
168 bool ellipsized
= false;
171 if (flags
& OUTPUT_COLOR
) {
172 if (priority
<= LOG_ERR
) {
173 color_on
= ANSI_HIGHLIGHT_RED
;
174 color_off
= ANSI_NORMAL
;
175 highlight_on
= ANSI_HIGHLIGHT
;
176 } else if (priority
<= LOG_NOTICE
) {
177 color_on
= ANSI_HIGHLIGHT
;
178 color_off
= ANSI_NORMAL
;
179 highlight_on
= ANSI_HIGHLIGHT_RED
;
183 /* A special case: make sure that we print a newline when
184 the message is empty. */
185 if (message_len
== 0)
189 pos
< message
+ message_len
;
190 pos
= end
+ 1, line
++) {
191 bool continuation
= line
> 0;
194 for (end
= pos
; end
< message
+ message_len
&& *end
!= '\n'; end
++)
199 /* We need to figure out when we are showing not-last line, *and*
200 * will skip subsequent lines. In that case, we will put the dots
201 * at the end of the line, instead of putting dots in the middle
205 line
+ 1 == PRINT_LINE_THRESHOLD
||
206 end
+ 1 >= message
+ PRINT_CHAR_THRESHOLD
;
208 if (flags
& (OUTPUT_FULL_WIDTH
| OUTPUT_SHOW_ALL
) ||
209 (prefix
+ len
+ 1 < n_columns
&& !tail_line
)) {
211 (size_t) (pos
- message
) <= highlight
[0] &&
212 highlight
[0] < (size_t) len
) {
214 fprintf(f
, "%*s%s%.*s",
215 continuation
* prefix
, "",
216 color_on
, (int) highlight
[0], pos
);
219 (int) (MIN((size_t) len
, highlight
[1]) - highlight
[0]),
221 if ((size_t) len
> highlight
[1])
224 (int) (len
- highlight
[1]),
226 fprintf(f
, "%s\n", color_off
);
229 fprintf(f
, "%*s%s%.*s%s\n",
230 continuation
* prefix
, "",
231 color_on
, len
, pos
, color_off
);
235 /* Beyond this point, ellipsization will happen. */
238 if (prefix
< n_columns
&& n_columns
- prefix
>= 3) {
239 if (n_columns
- prefix
> (unsigned) len
+ 3)
240 fprintf(f
, "%*s%s%.*s...%s\n",
241 continuation
* prefix
, "",
242 color_on
, len
, pos
, color_off
);
244 _cleanup_free_
char *e
;
246 e
= ellipsize_mem(pos
, len
, n_columns
- prefix
,
247 tail_line
? 100 : 90);
249 fprintf(f
, "%*s%s%.*s%s\n",
250 continuation
* prefix
, "",
251 color_on
, len
, pos
, color_off
);
253 fprintf(f
, "%*s%s%s%s\n",
254 continuation
* prefix
, "",
255 color_on
, e
, color_off
);
267 static int output_timestamp_monotonic(FILE *f
, sd_journal
*j
, const char *monotonic
) {
277 r
= safe_atou64(monotonic
, &t
);
279 r
= sd_journal_get_monotonic_usec(j
, &t
, &boot_id
);
281 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
283 fprintf(f
, "[%5"PRI_USEC
".%06"PRI_USEC
"]", t
/ USEC_PER_SEC
, t
% USEC_PER_SEC
);
284 return 1 + 5 + 1 + 6 + 1;
287 static int output_timestamp_realtime(FILE *f
, sd_journal
*j
, OutputMode mode
, OutputFlags flags
, const char *realtime
) {
288 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, 64)];
289 struct tm
*(*gettime_r
)(const time_t *, struct tm
*);
300 r
= safe_atou64(realtime
, &x
);
302 r
= sd_journal_get_realtime_usec(j
, &x
);
304 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
306 if (x
> USEC_TIMESTAMP_FORMATTABLE_MAX
) {
307 log_error("Timestamp cannot be printed");
311 if (mode
== OUTPUT_SHORT_FULL
) {
314 if (flags
& OUTPUT_UTC
)
315 k
= format_timestamp_utc(buf
, sizeof(buf
), x
);
317 k
= format_timestamp(buf
, sizeof(buf
), x
);
319 log_error("Failed to format timestamp.");
326 gettime_r
= (flags
& OUTPUT_UTC
) ? gmtime_r
: localtime_r
;
327 t
= (time_t) (x
/ USEC_PER_SEC
);
331 case OUTPUT_SHORT_UNIX
:
332 xsprintf(buf
, "%10"PRI_TIME
".%06"PRIu64
, t
, x
% USEC_PER_SEC
);
335 case OUTPUT_SHORT_ISO
:
336 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t
, &tm
)) <= 0) {
337 log_error("Failed to format ISO time");
342 case OUTPUT_SHORT_ISO_PRECISE
:
343 /* No usec in strftime, so we leave space and copy over */
344 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t
, &tm
)) <= 0) {
345 log_error("Failed to format ISO-precise time");
348 xsprintf(usec
, "%06"PRI_USEC
, x
% USEC_PER_SEC
);
349 memcpy(buf
+ 20, usec
, 6);
353 case OUTPUT_SHORT_PRECISE
:
355 if (strftime(buf
, sizeof(buf
), "%b %d %H:%M:%S", gettime_r(&t
, &tm
)) <= 0) {
356 log_error("Failed to format syslog time");
360 if (mode
== OUTPUT_SHORT_PRECISE
) {
363 assert(sizeof(buf
) > strlen(buf
));
364 k
= sizeof(buf
) - strlen(buf
);
366 r
= snprintf(buf
+ strlen(buf
), k
, ".%06"PRIu64
, x
% USEC_PER_SEC
);
367 if (r
<= 0 || (size_t) r
>= k
) { /* too long? */
368 log_error("Failed to format precise time");
375 assert_not_reached("Unknown time format");
380 return (int) strlen(buf
);
383 static int output_short(
390 size_t highlight
[2]) {
396 _cleanup_free_
char *hostname
= NULL
, *identifier
= NULL
, *comm
= NULL
, *pid
= NULL
, *fake_pid
= NULL
, *message
= NULL
, *realtime
= NULL
, *monotonic
= NULL
, *priority
= NULL
;
397 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;
399 bool ellipsized
= false;
400 const ParseFieldVec fields
[] = {
401 PARSE_FIELD_VEC_ENTRY("_PID=", &pid
, &pid_len
),
402 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm
, &comm_len
),
403 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message
, &message_len
),
404 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority
, &priority_len
),
405 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname
, &hostname_len
),
406 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid
, &fake_pid_len
),
407 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier
, &identifier_len
),
408 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime
, &realtime_len
),
409 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic
, &monotonic_len
),
411 size_t highlight_shifted
[] = {highlight
? highlight
[0] : 0, highlight
? highlight
[1] : 0};
416 /* Set the threshold to one bigger than the actual print
417 * threshold, so that if the line is actually longer than what
418 * we're willing to print, ellipsization will occur. This way
419 * we won't output a misleading line without any indication of
422 sd_journal_set_data_threshold(j
, flags
& (OUTPUT_SHOW_ALL
|OUTPUT_FULL_WIDTH
) ? 0 : PRINT_CHAR_THRESHOLD
+ 1);
424 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
426 r
= parse_fieldv(data
, length
, fields
, ELEMENTSOF(fields
));
431 log_debug_errno(r
, "Skipping message we can't read: %m");
435 return log_error_errno(r
, "Failed to get journal fields: %m");
438 log_debug("Skipping message without MESSAGE= field.");
442 if (!(flags
& OUTPUT_SHOW_ALL
))
443 strip_tab_ansi(&message
, &message_len
, highlight_shifted
);
445 if (priority_len
== 1 && *priority
>= '0' && *priority
<= '7')
448 if (mode
== OUTPUT_SHORT_MONOTONIC
)
449 r
= output_timestamp_monotonic(f
, j
, monotonic
);
451 r
= output_timestamp_realtime(f
, j
, mode
, flags
, realtime
);
456 if (flags
& OUTPUT_NO_HOSTNAME
) {
457 /* Suppress display of the hostname if this is requested. */
458 hostname
= mfree(hostname
);
462 if (hostname
&& shall_print(hostname
, hostname_len
, flags
)) {
463 fprintf(f
, " %.*s", (int) hostname_len
, hostname
);
464 n
+= hostname_len
+ 1;
467 if (identifier
&& shall_print(identifier
, identifier_len
, flags
)) {
468 fprintf(f
, " %.*s", (int) identifier_len
, identifier
);
469 n
+= identifier_len
+ 1;
470 } else if (comm
&& shall_print(comm
, comm_len
, flags
)) {
471 fprintf(f
, " %.*s", (int) comm_len
, comm
);
474 fputs(" unknown", f
);
476 if (pid
&& shall_print(pid
, pid_len
, flags
)) {
477 fprintf(f
, "[%.*s]", (int) pid_len
, pid
);
479 } else if (fake_pid
&& shall_print(fake_pid
, fake_pid_len
, flags
)) {
480 fprintf(f
, "[%.*s]", (int) fake_pid_len
, fake_pid
);
481 n
+= fake_pid_len
+ 2;
484 if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(message
, message_len
)) {
485 char bytes
[FORMAT_BYTES_MAX
];
486 fprintf(f
, ": [%s blob data]\n", format_bytes(bytes
, sizeof(bytes
), message_len
));
490 print_multiline(f
, n
+ 2, n_columns
, flags
, p
,
491 message
, message_len
,
495 if (flags
& OUTPUT_CATALOG
)
501 static int output_verbose(
508 size_t highlight
[2]) {
512 _cleanup_free_
char *cursor
= NULL
;
513 uint64_t realtime
= 0;
514 char ts
[FORMAT_TIMESTAMP_MAX
+ 7];
515 const char *timestamp
;
521 sd_journal_set_data_threshold(j
, 0);
523 r
= sd_journal_get_data(j
, "_SOURCE_REALTIME_TIMESTAMP", &data
, &length
);
525 log_debug("Source realtime timestamp not found");
527 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get source realtime timestamp: %m");
529 _cleanup_free_
char *value
= NULL
;
531 r
= parse_field(data
, length
, "_SOURCE_REALTIME_TIMESTAMP=",
532 STRLEN("_SOURCE_REALTIME_TIMESTAMP="), &value
,
538 r
= safe_atou64(value
, &realtime
);
540 log_debug_errno(r
, "Failed to parse realtime timestamp: %m");
544 r
= sd_journal_get_realtime_usec(j
, &realtime
);
546 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get realtime timestamp: %m");
549 r
= sd_journal_get_cursor(j
, &cursor
);
551 return log_error_errno(r
, "Failed to get cursor: %m");
553 timestamp
= flags
& OUTPUT_UTC
? format_timestamp_us_utc(ts
, sizeof ts
, realtime
)
554 : format_timestamp_us(ts
, sizeof ts
, realtime
);
555 fprintf(f
, "%s [%s]\n",
556 timestamp
?: "(no timestamp)",
559 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
562 const char *on
= "", *off
= "";
564 c
= memchr(data
, '=', length
);
566 log_error("Invalid field.");
569 fieldlen
= c
- (const char*) data
;
571 r
= field_set_test(output_fields
, data
, fieldlen
);
577 if (flags
& OUTPUT_COLOR
&& startswith(data
, "MESSAGE=")) {
582 if ((flags
& OUTPUT_SHOW_ALL
) ||
583 (((length
< PRINT_CHAR_THRESHOLD
) || flags
& OUTPUT_FULL_WIDTH
)
584 && utf8_is_printable(data
, length
))) {
585 fprintf(f
, " %s%.*s=", on
, fieldlen
, (const char*)data
);
586 print_multiline(f
, 4 + fieldlen
+ 1, 0, OUTPUT_FULL_WIDTH
, 0, c
+ 1, length
- fieldlen
- 1, NULL
);
589 char bytes
[FORMAT_BYTES_MAX
];
591 fprintf(f
, " %s%.*s=[%s blob data]%s\n",
593 (int) (c
- (const char*) data
),
595 format_bytes(bytes
, sizeof(bytes
), length
- (c
- (const char *) data
) - 1),
603 if (flags
& OUTPUT_CATALOG
)
609 static int output_export(
616 size_t highlight
[2]) {
621 usec_t realtime
, monotonic
;
622 _cleanup_free_
char *cursor
= NULL
;
628 sd_journal_set_data_threshold(j
, 0);
630 r
= sd_journal_get_realtime_usec(j
, &realtime
);
632 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
634 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
636 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
638 r
= sd_journal_get_cursor(j
, &cursor
);
640 return log_error_errno(r
, "Failed to get cursor: %m");
644 "__REALTIME_TIMESTAMP="USEC_FMT
"\n"
645 "__MONOTONIC_TIMESTAMP="USEC_FMT
"\n"
650 sd_id128_to_string(boot_id
, sid
));
652 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
655 /* We already printed the boot id, from the data in
656 * the header, hence let's suppress it here */
658 startswith(data
, "_BOOT_ID="))
661 c
= memchr(data
, '=', length
);
663 log_error("Invalid field.");
667 r
= field_set_test(output_fields
, data
, c
- (const char *) data
);
673 if (utf8_is_printable_newline(data
, length
, false))
674 fwrite(data
, length
, 1, f
);
678 fwrite(data
, c
- (const char*) data
, 1, f
);
680 le64
= htole64(length
- (c
- (const char*) data
) - 1);
681 fwrite(&le64
, sizeof(le64
), 1, f
);
682 fwrite(c
+ 1, length
- (c
- (const char*) data
) - 1, 1, f
);
688 log_debug_errno(r
, "Skipping message we can't read: %m");
709 if (!(flags
& OUTPUT_SHOW_ALL
) && l
>= JSON_THRESHOLD
)
712 else if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(p
, l
)) {
713 bool not_first
= false;
719 fprintf(f
, ", %u", (uint8_t) *p
);
722 fprintf(f
, "%u", (uint8_t) *p
);
734 if (IN_SET(*p
, '"', '\\')) {
737 } else if (*p
== '\n')
739 else if ((uint8_t) *p
< ' ')
740 fprintf(f
, "\\u%04x", (uint8_t) *p
);
752 static int output_json(
759 size_t highlight
[2]) {
761 uint64_t realtime
, monotonic
;
762 _cleanup_free_
char *cursor
= NULL
;
769 bool done
, separator
;
773 sd_journal_set_data_threshold(j
, flags
& OUTPUT_SHOW_ALL
? 0 : JSON_THRESHOLD
);
775 r
= sd_journal_get_realtime_usec(j
, &realtime
);
777 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
779 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
781 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
783 r
= sd_journal_get_cursor(j
, &cursor
);
785 return log_error_errno(r
, "Failed to get cursor: %m");
787 if (mode
== OUTPUT_JSON_PRETTY
)
790 "\t\"__CURSOR\" : \"%s\",\n"
791 "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\",\n"
792 "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\",\n"
793 "\t\"_BOOT_ID\" : \"%s\"",
797 sd_id128_to_string(boot_id
, sid
));
799 if (mode
== OUTPUT_JSON_SSE
)
803 "{ \"__CURSOR\" : \"%s\", "
804 "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\", "
805 "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\", "
806 "\"_BOOT_ID\" : \"%s\"",
810 sd_id128_to_string(boot_id
, sid
));
813 h
= hashmap_new(&string_hash_ops
);
817 /* First round, iterate through the entry and count how often each field appears */
818 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
824 memcmp(data
, "_BOOT_ID=", 9) == 0)
827 eq
= memchr(data
, '=', length
);
831 n
= strndup(data
, eq
- (const char*) data
);
837 u
= PTR_TO_UINT(hashmap_get(h
, n
));
839 r
= hashmap_put(h
, n
, UINT_TO_PTR(1));
846 r
= hashmap_update(h
, n
, UINT_TO_PTR(u
+ 1));
856 log_debug_errno(r
, "Skipping message we can't read: %m");
867 SD_JOURNAL_FOREACH_DATA(j
, data
, length
) {
873 /* We already printed the boot id, from the data in
874 * the header, hence let's suppress it here */
876 memcmp(data
, "_BOOT_ID=", 9) == 0)
879 eq
= memchr(data
, '=', length
);
883 m
= eq
- (const char*) data
;
885 n
= strndup(data
, m
);
891 if (output_fields
&& !set_get(output_fields
, n
)) {
897 if (mode
== OUTPUT_JSON_PRETTY
)
903 u
= PTR_TO_UINT(hashmap_get2(h
, n
, (void**) &kk
));
905 /* We already printed this, let's jump to the next */
911 /* Field only appears once, output it directly */
913 json_escape(f
, data
, m
, flags
);
916 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
918 hashmap_remove(h
, n
);
927 /* Field appears multiple times, output it as array */
928 json_escape(f
, data
, m
, flags
);
930 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
932 /* Iterate through the end of the list */
934 while (sd_journal_enumerate_data(j
, &data
, &length
) > 0) {
938 if (memcmp(data
, n
, m
) != 0)
941 if (((const char*) data
)[m
] != '=')
945 json_escape(f
, (const char*) data
+ m
+ 1, length
- m
- 1, flags
);
950 hashmap_remove(h
, n
);
954 /* Iterate data fields form the beginning */
964 if (mode
== OUTPUT_JSON_PRETTY
)
966 else if (mode
== OUTPUT_JSON_SSE
)
974 while ((k
= hashmap_steal_first_key(h
)))
982 static int output_cat(
989 size_t highlight
[2]) {
994 const char *highlight_on
= "", *highlight_off
= "";
999 if (flags
& OUTPUT_COLOR
) {
1000 highlight_on
= ANSI_HIGHLIGHT_RED
;
1001 highlight_off
= ANSI_NORMAL
;
1004 sd_journal_set_data_threshold(j
, 0);
1006 r
= sd_journal_get_data(j
, "MESSAGE", &data
, &l
);
1007 if (r
== -EBADMSG
) {
1008 log_debug_errno(r
, "Skipping message we can't read: %m");
1012 /* An entry without MESSAGE=? */
1016 return log_error_errno(r
, "Failed to get data: %m");
1021 if (highlight
&& (flags
& OUTPUT_COLOR
)) {
1022 assert(highlight
[0] <= highlight
[1]);
1023 assert(highlight
[1] <= l
- 8);
1025 fwrite((const char*) data
+ 8, 1, highlight
[0], f
);
1026 fwrite(highlight_on
, 1, strlen(highlight_on
), f
);
1027 fwrite((const char*) data
+ 8 + highlight
[0], 1, highlight
[1] - highlight
[0], f
);
1028 fwrite(highlight_off
, 1, strlen(highlight_off
), f
);
1029 fwrite((const char*) data
+ 8 + highlight
[1], 1, l
- 8 - highlight
[1], f
);
1031 fwrite((const char*) data
+ 8, 1, l
- 8, f
);
1037 static int (*output_funcs
[_OUTPUT_MODE_MAX
])(
1044 size_t highlight
[2]) = {
1046 [OUTPUT_SHORT
] = output_short
,
1047 [OUTPUT_SHORT_ISO
] = output_short
,
1048 [OUTPUT_SHORT_ISO_PRECISE
] = output_short
,
1049 [OUTPUT_SHORT_PRECISE
] = output_short
,
1050 [OUTPUT_SHORT_MONOTONIC
] = output_short
,
1051 [OUTPUT_SHORT_UNIX
] = output_short
,
1052 [OUTPUT_SHORT_FULL
] = output_short
,
1053 [OUTPUT_VERBOSE
] = output_verbose
,
1054 [OUTPUT_EXPORT
] = output_export
,
1055 [OUTPUT_JSON
] = output_json
,
1056 [OUTPUT_JSON_PRETTY
] = output_json
,
1057 [OUTPUT_JSON_SSE
] = output_json
,
1058 [OUTPUT_CAT
] = output_cat
1067 char **output_fields
,
1068 size_t highlight
[2],
1072 _cleanup_set_free_free_ Set
*fields
= NULL
;
1074 assert(mode
< _OUTPUT_MODE_MAX
);
1077 n_columns
= columns();
1079 if (output_fields
) {
1080 fields
= set_new(&string_hash_ops
);
1084 ret
= set_put_strdupv(fields
, output_fields
);
1089 ret
= output_funcs
[mode
](f
, j
, mode
, n_columns
, flags
, fields
, highlight
);
1091 if (ellipsized
&& ret
> 0)
1097 static int maybe_print_begin_newline(FILE *f
, OutputFlags
*flags
) {
1101 if (!(*flags
& OUTPUT_BEGIN_NEWLINE
))
1104 /* Print a beginning new line if that's request, but only once
1105 * on the first line we print. */
1108 *flags
&= ~OUTPUT_BEGIN_NEWLINE
;
1112 static int show_journal(FILE *f
,
1123 bool need_seek
= false;
1124 int warn_cutoff
= flags
& OUTPUT_WARN_CUTOFF
;
1128 assert(mode
< _OUTPUT_MODE_MAX
);
1131 r
= sd_journal_seek_tail(j
);
1133 return log_error_errno(r
, "Failed to seek to tail: %m");
1135 r
= sd_journal_previous_skip(j
, how_many
);
1137 return log_error_errno(r
, "Failed to skip previous: %m");
1144 r
= sd_journal_next(j
);
1146 return log_error_errno(r
, "Failed to iterate through journal: %m");
1154 if (not_before
> 0) {
1155 r
= sd_journal_get_monotonic_usec(j
, &usec
, NULL
);
1157 /* -ESTALE is returned if the
1158 timestamp is not from this boot */
1162 return log_error_errno(r
, "Failed to get journal time: %m");
1164 if (usec
< not_before
)
1169 maybe_print_begin_newline(f
, &flags
);
1171 r
= output_journal(f
, j
, mode
, n_columns
, flags
, NULL
, NULL
, ellipsized
);
1176 if (warn_cutoff
&& line
< how_many
&& not_before
> 0) {
1180 /* Check whether the cutoff line is too early */
1182 r
= sd_id128_get_boot(&boot_id
);
1184 return log_error_errno(r
, "Failed to get boot id: %m");
1186 r
= sd_journal_get_cutoff_monotonic_usec(j
, boot_id
, &cutoff
, NULL
);
1188 return log_error_errno(r
, "Failed to get journal cutoff time: %m");
1190 if (r
> 0 && not_before
< cutoff
) {
1191 maybe_print_begin_newline(f
, &flags
);
1192 fprintf(f
, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1195 warn_cutoff
= false;
1198 if (!(flags
& OUTPUT_FOLLOW
))
1201 r
= sd_journal_wait(j
, USEC_INFINITY
);
1203 return log_error_errno(r
, "Failed to wait for journal: %m");
1210 int add_matches_for_unit(sd_journal
*j
, const char *unit
) {
1211 const char *m1
, *m2
, *m3
, *m4
;
1217 m1
= strjoina("_SYSTEMD_UNIT=", unit
);
1218 m2
= strjoina("COREDUMP_UNIT=", unit
);
1219 m3
= strjoina("UNIT=", unit
);
1220 m4
= strjoina("OBJECT_SYSTEMD_UNIT=", unit
);
1223 /* Look for messages from the service itself */
1224 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1226 /* Look for coredumps of the service */
1227 (r
= sd_journal_add_disjunction(j
)) ||
1228 (r
= sd_journal_add_match(j
, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1229 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1230 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1232 /* Look for messages from PID 1 about this service */
1233 (r
= sd_journal_add_disjunction(j
)) ||
1234 (r
= sd_journal_add_match(j
, "_PID=1", 0)) ||
1235 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1237 /* Look for messages from authorized daemons about this service */
1238 (r
= sd_journal_add_disjunction(j
)) ||
1239 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1240 (r
= sd_journal_add_match(j
, m4
, 0))
1243 if (r
== 0 && endswith(unit
, ".slice")) {
1246 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1248 /* Show all messages belonging to a slice */
1250 (r
= sd_journal_add_disjunction(j
)) ||
1251 (r
= sd_journal_add_match(j
, m5
, 0))
1258 int add_matches_for_user_unit(sd_journal
*j
, const char *unit
, uid_t uid
) {
1260 char *m1
, *m2
, *m3
, *m4
;
1261 char muid
[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t
)];
1266 m1
= strjoina("_SYSTEMD_USER_UNIT=", unit
);
1267 m2
= strjoina("USER_UNIT=", unit
);
1268 m3
= strjoina("COREDUMP_USER_UNIT=", unit
);
1269 m4
= strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit
);
1270 sprintf(muid
, "_UID="UID_FMT
, uid
);
1273 /* Look for messages from the user service itself */
1274 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1275 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1277 /* Look for messages from systemd about this service */
1278 (r
= sd_journal_add_disjunction(j
)) ||
1279 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1280 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1282 /* Look for coredumps of the service */
1283 (r
= sd_journal_add_disjunction(j
)) ||
1284 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1285 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1286 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1288 /* Look for messages from authorized daemons about this service */
1289 (r
= sd_journal_add_disjunction(j
)) ||
1290 (r
= sd_journal_add_match(j
, m4
, 0)) ||
1291 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1292 (r
= sd_journal_add_match(j
, "_UID=0", 0))
1295 if (r
== 0 && endswith(unit
, ".slice")) {
1298 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1300 /* Show all messages belonging to a slice */
1302 (r
= sd_journal_add_disjunction(j
)) ||
1303 (r
= sd_journal_add_match(j
, m5
, 0)) ||
1304 (r
= sd_journal_add_match(j
, muid
, 0))
1311 static int get_boot_id_for_machine(const char *machine
, sd_id128_t
*boot_id
) {
1312 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1313 _cleanup_close_
int pidnsfd
= -1, mntnsfd
= -1, rootfd
= -1;
1322 if (!machine_name_is_valid(machine
))
1325 r
= container_get_leader(machine
, &pid
);
1329 r
= namespace_open(pid
, &pidnsfd
, &mntnsfd
, NULL
, NULL
, &rootfd
);
1333 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1336 r
= safe_fork("(sd-bootid)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
1342 pair
[0] = safe_close(pair
[0]);
1344 r
= namespace_enter(pidnsfd
, mntnsfd
, -1, -1, rootfd
);
1346 _exit(EXIT_FAILURE
);
1348 fd
= open("/proc/sys/kernel/random/boot_id", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1350 _exit(EXIT_FAILURE
);
1352 r
= loop_read_exact(fd
, buf
, 36, false);
1355 _exit(EXIT_FAILURE
);
1357 k
= send(pair
[1], buf
, 36, MSG_NOSIGNAL
);
1359 _exit(EXIT_FAILURE
);
1361 _exit(EXIT_SUCCESS
);
1364 pair
[1] = safe_close(pair
[1]);
1366 r
= wait_for_terminate_and_check("(sd-bootid)", child
, 0);
1369 if (r
!= EXIT_SUCCESS
)
1372 k
= recv(pair
[0], buf
, 36, 0);
1377 r
= sd_id128_from_string(buf
, boot_id
);
1384 int add_match_this_boot(sd_journal
*j
, const char *machine
) {
1385 char match
[9+32+1] = "_BOOT_ID=";
1392 r
= get_boot_id_for_machine(machine
, &boot_id
);
1394 return log_error_errno(r
, "Failed to get boot id of container %s: %m", machine
);
1396 r
= sd_id128_get_boot(&boot_id
);
1398 return log_error_errno(r
, "Failed to get boot id: %m");
1401 sd_id128_to_string(boot_id
, match
+ 9);
1402 r
= sd_journal_add_match(j
, match
, strlen(match
));
1404 return log_error_errno(r
, "Failed to add match: %m");
1406 r
= sd_journal_add_conjunction(j
);
1408 return log_error_errno(r
, "Failed to add conjunction: %m");
1413 int show_journal_by_unit(
1422 int journal_open_flags
,
1426 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1430 assert(mode
< _OUTPUT_MODE_MAX
);
1436 r
= sd_journal_open(&j
, journal_open_flags
);
1438 return log_error_errno(r
, "Failed to open journal: %m");
1440 r
= add_match_this_boot(j
, NULL
);
1445 r
= add_matches_for_unit(j
, unit
);
1447 r
= add_matches_for_user_unit(j
, unit
, uid
);
1449 return log_error_errno(r
, "Failed to add unit matches: %m");
1451 if (DEBUG_LOGGING
) {
1452 _cleanup_free_
char *filter
;
1454 filter
= journal_make_match_string(j
);
1458 log_debug("Journal filter: %s", filter
);
1461 return show_journal(f
, j
, mode
, n_columns
, not_before
, how_many
, flags
, ellipsized
);