1 /* SPDX-License-Identifier: LGPL-2.1+ */
9 #include <sys/socket.h>
15 #include "sd-journal.h"
17 #include "alloc-util.h"
19 #include "format-util.h"
21 #include "hostname-util.h"
23 #include "journal-internal.h"
25 #include "logs-show.h"
27 #include "output-mode.h"
28 #include "parse-util.h"
29 #include "process-util.h"
30 #include "sparse-endian.h"
31 #include "stdio-util.h"
32 #include "string-table.h"
33 #include "string-util.h"
35 #include "terminal-util.h"
36 #include "time-util.h"
40 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
41 #define PRINT_LINE_THRESHOLD 3
42 #define PRINT_CHAR_THRESHOLD 300
44 #define JSON_THRESHOLD 4096
46 static int print_catalog(FILE *f
, sd_journal
*j
) {
48 _cleanup_free_
char *t
= NULL
, *z
= NULL
;
50 r
= sd_journal_get_catalog(j
, &t
);
54 z
= strreplace(strstrip(t
), "\n", "\n-- ");
65 static int parse_field(const void *data
, size_t length
, const char *field
, size_t field_len
, char **target
, size_t *target_len
) {
73 if (length
< field_len
)
76 if (memcmp(data
, field
, field_len
))
79 nl
= length
- field_len
;
81 buf
= newdup_suffix0(char, (const char*) data
+ field_len
, nl
);
94 typedef struct ParseFieldVec
{
101 #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) \
102 { .field = _field, .field_len = strlen(_field), .target = _target, .target_len = _target_len }
104 static int parse_fieldv(const void *data
, size_t length
, const ParseFieldVec
*fields
, unsigned n_fields
) {
107 for (i
= 0; i
< n_fields
; i
++) {
108 const ParseFieldVec
*f
= &fields
[i
];
111 r
= parse_field(data
, length
, f
->field
, f
->field_len
, f
->target
, f
->target_len
);
121 static int field_set_test(Set
*fields
, const char *name
, size_t n
) {
127 s
= strndupa(name
, n
);
131 return set_get(fields
, s
) ? 1 : 0;
134 static bool shall_print(const char *p
, size_t l
, OutputFlags flags
) {
137 if (flags
& OUTPUT_SHOW_ALL
)
140 if (l
>= PRINT_CHAR_THRESHOLD
)
143 if (!utf8_is_printable(p
, l
))
149 static bool print_multiline(
157 size_t highlight
[2]) {
159 const char *color_on
= "", *color_off
= "", *highlight_on
= "";
160 const char *pos
, *end
;
161 bool ellipsized
= false;
164 if (flags
& OUTPUT_COLOR
) {
165 if (priority
<= LOG_ERR
) {
166 color_on
= ANSI_HIGHLIGHT_RED
;
167 color_off
= ANSI_NORMAL
;
168 highlight_on
= ANSI_HIGHLIGHT
;
169 } else if (priority
<= LOG_NOTICE
) {
170 color_on
= ANSI_HIGHLIGHT
;
171 color_off
= ANSI_NORMAL
;
172 highlight_on
= ANSI_HIGHLIGHT_RED
;
176 /* A special case: make sure that we print a newline when
177 the message is empty. */
178 if (message_len
== 0)
182 pos
< message
+ message_len
;
183 pos
= end
+ 1, line
++) {
184 bool continuation
= line
> 0;
187 for (end
= pos
; end
< message
+ message_len
&& *end
!= '\n'; end
++)
192 /* We need to figure out when we are showing not-last line, *and*
193 * will skip subsequent lines. In that case, we will put the dots
194 * at the end of the line, instead of putting dots in the middle
198 line
+ 1 == PRINT_LINE_THRESHOLD
||
199 end
+ 1 >= message
+ PRINT_CHAR_THRESHOLD
;
201 if (flags
& (OUTPUT_FULL_WIDTH
| OUTPUT_SHOW_ALL
) ||
202 (prefix
+ len
+ 1 < n_columns
&& !tail_line
)) {
204 (size_t) (pos
- message
) <= highlight
[0] &&
205 highlight
[0] < (size_t) len
) {
207 fprintf(f
, "%*s%s%.*s",
208 continuation
* prefix
, "",
209 color_on
, (int) highlight
[0], pos
);
212 (int) (MIN((size_t) len
, highlight
[1]) - highlight
[0]),
214 if ((size_t) len
> highlight
[1])
217 (int) (len
- highlight
[1]),
219 fprintf(f
, "%s\n", color_off
);
222 fprintf(f
, "%*s%s%.*s%s\n",
223 continuation
* prefix
, "",
224 color_on
, len
, pos
, color_off
);
228 /* Beyond this point, ellipsization will happen. */
231 if (prefix
< n_columns
&& n_columns
- prefix
>= 3) {
232 if (n_columns
- prefix
> (unsigned) len
+ 3)
233 fprintf(f
, "%*s%s%.*s...%s\n",
234 continuation
* prefix
, "",
235 color_on
, len
, pos
, color_off
);
237 _cleanup_free_
char *e
;
239 e
= ellipsize_mem(pos
, len
, n_columns
- prefix
,
240 tail_line
? 100 : 90);
242 fprintf(f
, "%*s%s%.*s%s\n",
243 continuation
* prefix
, "",
244 color_on
, len
, pos
, color_off
);
246 fprintf(f
, "%*s%s%s%s\n",
247 continuation
* prefix
, "",
248 color_on
, e
, color_off
);
260 static int output_timestamp_monotonic(FILE *f
, sd_journal
*j
, const char *monotonic
) {
270 r
= safe_atou64(monotonic
, &t
);
272 r
= sd_journal_get_monotonic_usec(j
, &t
, &boot_id
);
274 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
276 fprintf(f
, "[%5"PRI_USEC
".%06"PRI_USEC
"]", t
/ USEC_PER_SEC
, t
% USEC_PER_SEC
);
277 return 1 + 5 + 1 + 6 + 1;
280 static int output_timestamp_realtime(FILE *f
, sd_journal
*j
, OutputMode mode
, OutputFlags flags
, const char *realtime
) {
281 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, 64)];
282 struct tm
*(*gettime_r
)(const time_t *, struct tm
*);
292 r
= safe_atou64(realtime
, &x
);
293 if (!realtime
|| r
< 0 || !VALID_REALTIME(x
))
294 r
= sd_journal_get_realtime_usec(j
, &x
);
296 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
298 if (IN_SET(mode
, OUTPUT_SHORT_FULL
, OUTPUT_WITH_UNIT
)) {
301 if (flags
& OUTPUT_UTC
)
302 k
= format_timestamp_utc(buf
, sizeof(buf
), x
);
304 k
= format_timestamp(buf
, sizeof(buf
), x
);
306 log_error("Failed to format timestamp: %"PRIu64
, x
);
313 gettime_r
= (flags
& OUTPUT_UTC
) ? gmtime_r
: localtime_r
;
314 t
= (time_t) (x
/ USEC_PER_SEC
);
318 case OUTPUT_SHORT_UNIX
:
319 xsprintf(buf
, "%10"PRI_TIME
".%06"PRIu64
, t
, x
% USEC_PER_SEC
);
322 case OUTPUT_SHORT_ISO
:
323 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t
, &tm
)) <= 0) {
324 log_error("Failed to format ISO time");
329 case OUTPUT_SHORT_ISO_PRECISE
:
330 /* No usec in strftime, so we leave space and copy over */
331 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t
, &tm
)) <= 0) {
332 log_error("Failed to format ISO-precise time");
335 xsprintf(usec
, "%06"PRI_USEC
, x
% USEC_PER_SEC
);
336 memcpy(buf
+ 20, usec
, 6);
340 case OUTPUT_SHORT_PRECISE
:
342 if (strftime(buf
, sizeof(buf
), "%b %d %H:%M:%S", gettime_r(&t
, &tm
)) <= 0) {
343 log_error("Failed to format syslog time");
347 if (mode
== OUTPUT_SHORT_PRECISE
) {
350 assert(sizeof(buf
) > strlen(buf
));
351 k
= sizeof(buf
) - strlen(buf
);
353 r
= snprintf(buf
+ strlen(buf
), k
, ".%06"PRIu64
, x
% USEC_PER_SEC
);
354 if (r
<= 0 || (size_t) r
>= k
) { /* too long? */
355 log_error("Failed to format precise time");
362 assert_not_reached("Unknown time format");
367 return (int) strlen(buf
);
370 static int output_short(
377 size_t highlight
[2]) {
383 _cleanup_free_
char *hostname
= NULL
, *identifier
= NULL
, *comm
= NULL
, *pid
= NULL
, *fake_pid
= NULL
, *message
= NULL
, *realtime
= NULL
, *monotonic
= NULL
, *priority
= NULL
, *unit
= NULL
, *user_unit
= NULL
;
384 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, unit_len
= 0, user_unit_len
= 0;
386 bool ellipsized
= false;
387 const ParseFieldVec fields
[] = {
388 PARSE_FIELD_VEC_ENTRY("_PID=", &pid
, &pid_len
),
389 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm
, &comm_len
),
390 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message
, &message_len
),
391 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority
, &priority_len
),
392 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname
, &hostname_len
),
393 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid
, &fake_pid_len
),
394 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier
, &identifier_len
),
395 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime
, &realtime_len
),
396 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic
, &monotonic_len
),
397 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit
, &unit_len
),
398 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit
, &user_unit_len
),
400 size_t highlight_shifted
[] = {highlight
? highlight
[0] : 0, highlight
? highlight
[1] : 0};
405 /* Set the threshold to one bigger than the actual print
406 * threshold, so that if the line is actually longer than what
407 * we're willing to print, ellipsization will occur. This way
408 * we won't output a misleading line without any indication of
411 sd_journal_set_data_threshold(j
, flags
& (OUTPUT_SHOW_ALL
|OUTPUT_FULL_WIDTH
) ? 0 : PRINT_CHAR_THRESHOLD
+ 1);
413 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
414 r
= parse_fieldv(data
, length
, fields
, ELEMENTSOF(fields
));
419 log_debug_errno(r
, "Skipping message we can't read: %m");
423 return log_error_errno(r
, "Failed to get journal fields: %m");
426 log_debug("Skipping message without MESSAGE= field.");
430 if (!(flags
& OUTPUT_SHOW_ALL
))
431 strip_tab_ansi(&message
, &message_len
, highlight_shifted
);
433 if (priority_len
== 1 && *priority
>= '0' && *priority
<= '7')
436 if (mode
== OUTPUT_SHORT_MONOTONIC
)
437 r
= output_timestamp_monotonic(f
, j
, monotonic
);
439 r
= output_timestamp_realtime(f
, j
, mode
, flags
, realtime
);
444 if (flags
& OUTPUT_NO_HOSTNAME
) {
445 /* Suppress display of the hostname if this is requested. */
446 hostname
= mfree(hostname
);
450 if (hostname
&& shall_print(hostname
, hostname_len
, flags
)) {
451 fprintf(f
, " %.*s", (int) hostname_len
, hostname
);
452 n
+= hostname_len
+ 1;
455 if (mode
== OUTPUT_WITH_UNIT
&& ((unit
&& shall_print(unit
, unit_len
, flags
)) || (user_unit
&& shall_print(user_unit
, user_unit_len
, flags
)))) {
457 fprintf(f
, " %.*s", (int) unit_len
, unit
);
462 fprintf(f
, "/%.*s", (int) user_unit_len
, user_unit
);
464 fprintf(f
, " %.*s", (int) user_unit_len
, user_unit
);
467 } else 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 the header, hence let's suppress it here */
656 if (memory_startswith(data
, length
, "_BOOT_ID="))
659 c
= memchr(data
, '=', length
);
661 log_error("Invalid field.");
665 r
= field_set_test(output_fields
, data
, c
- (const char *) data
);
671 if (utf8_is_printable_newline(data
, length
, false))
672 fwrite(data
, length
, 1, f
);
676 fwrite(data
, c
- (const char*) data
, 1, f
);
678 le64
= htole64(length
- (c
- (const char*) data
) - 1);
679 fwrite(&le64
, sizeof(le64
), 1, f
);
680 fwrite(c
+ 1, length
- (c
- (const char*) data
) - 1, 1, f
);
686 log_debug_errno(r
, "Skipping message we can't read: %m");
707 if (!(flags
& OUTPUT_SHOW_ALL
) && l
>= JSON_THRESHOLD
)
710 else if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(p
, l
)) {
711 bool not_first
= false;
717 fprintf(f
, ", %u", (uint8_t) *p
);
720 fprintf(f
, "%u", (uint8_t) *p
);
732 if (IN_SET(*p
, '"', '\\')) {
735 } else if (*p
== '\n')
737 else if ((uint8_t) *p
< ' ')
738 fprintf(f
, "\\u%04x", (uint8_t) *p
);
750 static int output_json(
757 size_t highlight
[2]) {
759 uint64_t realtime
, monotonic
;
760 _cleanup_free_
char *cursor
= NULL
;
767 bool done
, separator
;
771 sd_journal_set_data_threshold(j
, flags
& OUTPUT_SHOW_ALL
? 0 : JSON_THRESHOLD
);
773 r
= sd_journal_get_realtime_usec(j
, &realtime
);
775 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
777 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
779 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
781 r
= sd_journal_get_cursor(j
, &cursor
);
783 return log_error_errno(r
, "Failed to get cursor: %m");
785 if (mode
== OUTPUT_JSON_PRETTY
)
788 "\t\"__CURSOR\" : \"%s\",\n"
789 "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\",\n"
790 "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\",\n"
791 "\t\"_BOOT_ID\" : \"%s\"",
795 sd_id128_to_string(boot_id
, sid
));
797 if (mode
== OUTPUT_JSON_SSE
)
801 "{ \"__CURSOR\" : \"%s\", "
802 "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\", "
803 "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\", "
804 "\"_BOOT_ID\" : \"%s\"",
808 sd_id128_to_string(boot_id
, sid
));
811 h
= hashmap_new(&string_hash_ops
);
815 /* First round, iterate through the entry and count how often each field appears */
816 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
821 if (memory_startswith(data
, length
, "_BOOT_ID="))
824 eq
= memchr(data
, '=', length
);
828 n
= memdup_suffix0(data
, eq
- (const char*) data
);
834 u
= PTR_TO_UINT(hashmap_get(h
, n
));
836 r
= hashmap_put(h
, n
, UINT_TO_PTR(1));
843 r
= hashmap_update(h
, n
, UINT_TO_PTR(u
+ 1));
852 log_debug_errno(r
, "Skipping message we can't read: %m");
862 SD_JOURNAL_FOREACH_DATA(j
, data
, length
) {
865 _cleanup_free_
char *n
= NULL
;
869 /* We already printed the boot id from the data in
870 * the header, hence let's suppress it here */
871 if (memory_startswith(data
, length
, "_BOOT_ID="))
874 eq
= memchr(data
, '=', length
);
878 m
= eq
- (const char*) data
;
879 n
= memdup_suffix0(data
, m
);
885 if (output_fields
&& !set_get(output_fields
, n
))
889 fputs(mode
== OUTPUT_JSON_PRETTY
? ",\n\t" : ", ", f
);
891 u
= PTR_TO_UINT(hashmap_get2(h
, n
, (void**) &kk
));
893 /* We already printed this, let's jump to the next */
897 /* Field only appears once, output it directly */
899 json_escape(f
, data
, m
, flags
);
902 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
904 hashmap_remove(h
, n
);
910 /* Field appears multiple times, output it as array */
911 json_escape(f
, data
, m
, flags
);
913 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
915 /* Iterate through the end of the list */
917 while (sd_journal_enumerate_data(j
, &data
, &length
) > 0) {
921 if (memcmp(data
, n
, m
) != 0)
924 if (((const char*) data
)[m
] != '=')
928 json_escape(f
, (const char*) data
+ m
+ 1, length
- m
- 1, flags
);
933 hashmap_remove(h
, n
);
936 /* Iterate data fields form the beginning */
946 if (mode
== OUTPUT_JSON_PRETTY
)
948 else if (mode
== OUTPUT_JSON_SSE
)
956 while ((k
= hashmap_steal_first_key(h
)))
964 static int output_cat(
971 size_t highlight
[2]) {
976 const char *highlight_on
= "", *highlight_off
= "";
981 if (flags
& OUTPUT_COLOR
) {
982 highlight_on
= ANSI_HIGHLIGHT_RED
;
983 highlight_off
= ANSI_NORMAL
;
986 sd_journal_set_data_threshold(j
, 0);
988 r
= sd_journal_get_data(j
, "MESSAGE", &data
, &l
);
990 log_debug_errno(r
, "Skipping message we can't read: %m");
994 /* An entry without MESSAGE=? */
998 return log_error_errno(r
, "Failed to get data: %m");
1003 if (highlight
&& (flags
& OUTPUT_COLOR
)) {
1004 assert(highlight
[0] <= highlight
[1]);
1005 assert(highlight
[1] <= l
- 8);
1007 fwrite((const char*) data
+ 8, 1, highlight
[0], f
);
1008 fwrite(highlight_on
, 1, strlen(highlight_on
), f
);
1009 fwrite((const char*) data
+ 8 + highlight
[0], 1, highlight
[1] - highlight
[0], f
);
1010 fwrite(highlight_off
, 1, strlen(highlight_off
), f
);
1011 fwrite((const char*) data
+ 8 + highlight
[1], 1, l
- 8 - highlight
[1], f
);
1013 fwrite((const char*) data
+ 8, 1, l
- 8, f
);
1019 static int (*output_funcs
[_OUTPUT_MODE_MAX
])(
1026 size_t highlight
[2]) = {
1028 [OUTPUT_SHORT
] = output_short
,
1029 [OUTPUT_SHORT_ISO
] = output_short
,
1030 [OUTPUT_SHORT_ISO_PRECISE
] = output_short
,
1031 [OUTPUT_SHORT_PRECISE
] = output_short
,
1032 [OUTPUT_SHORT_MONOTONIC
] = output_short
,
1033 [OUTPUT_SHORT_UNIX
] = output_short
,
1034 [OUTPUT_SHORT_FULL
] = output_short
,
1035 [OUTPUT_VERBOSE
] = output_verbose
,
1036 [OUTPUT_EXPORT
] = output_export
,
1037 [OUTPUT_JSON
] = output_json
,
1038 [OUTPUT_JSON_PRETTY
] = output_json
,
1039 [OUTPUT_JSON_SSE
] = output_json
,
1040 [OUTPUT_CAT
] = output_cat
,
1041 [OUTPUT_WITH_UNIT
] = output_short
,
1044 int show_journal_entry(
1050 char **output_fields
,
1051 size_t highlight
[2],
1055 _cleanup_set_free_free_ Set
*fields
= NULL
;
1057 assert(mode
< _OUTPUT_MODE_MAX
);
1060 n_columns
= columns();
1062 if (output_fields
) {
1063 fields
= set_new(&string_hash_ops
);
1067 ret
= set_put_strdupv(fields
, output_fields
);
1072 ret
= output_funcs
[mode
](f
, j
, mode
, n_columns
, flags
, fields
, highlight
);
1074 if (ellipsized
&& ret
> 0)
1080 static int maybe_print_begin_newline(FILE *f
, OutputFlags
*flags
) {
1084 if (!(*flags
& OUTPUT_BEGIN_NEWLINE
))
1087 /* Print a beginning new line if that's request, but only once
1088 * on the first line we print. */
1091 *flags
&= ~OUTPUT_BEGIN_NEWLINE
;
1107 bool need_seek
= false;
1108 int warn_cutoff
= flags
& OUTPUT_WARN_CUTOFF
;
1112 assert(mode
< _OUTPUT_MODE_MAX
);
1114 if (how_many
== (unsigned) -1)
1118 r
= sd_journal_seek_tail(j
);
1120 return log_error_errno(r
, "Failed to seek to tail: %m");
1122 r
= sd_journal_previous_skip(j
, how_many
);
1124 return log_error_errno(r
, "Failed to skip previous: %m");
1132 r
= sd_journal_next(j
);
1134 return log_error_errno(r
, "Failed to iterate through journal: %m");
1142 if (not_before
> 0) {
1143 r
= sd_journal_get_monotonic_usec(j
, &usec
, NULL
);
1145 /* -ESTALE is returned if the
1146 timestamp is not from this boot */
1150 return log_error_errno(r
, "Failed to get journal time: %m");
1152 if (usec
< not_before
)
1157 maybe_print_begin_newline(f
, &flags
);
1159 r
= show_journal_entry(f
, j
, mode
, n_columns
, flags
, NULL
, NULL
, ellipsized
);
1164 if (warn_cutoff
&& line
< how_many
&& not_before
> 0) {
1168 /* Check whether the cutoff line is too early */
1170 r
= sd_id128_get_boot(&boot_id
);
1172 return log_error_errno(r
, "Failed to get boot id: %m");
1174 r
= sd_journal_get_cutoff_monotonic_usec(j
, boot_id
, &cutoff
, NULL
);
1176 return log_error_errno(r
, "Failed to get journal cutoff time: %m");
1178 if (r
> 0 && not_before
< cutoff
) {
1179 maybe_print_begin_newline(f
, &flags
);
1180 fprintf(f
, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1183 warn_cutoff
= false;
1186 if (!(flags
& OUTPUT_FOLLOW
))
1189 r
= sd_journal_wait(j
, USEC_INFINITY
);
1191 return log_error_errno(r
, "Failed to wait for journal: %m");
1198 int add_matches_for_unit(sd_journal
*j
, const char *unit
) {
1199 const char *m1
, *m2
, *m3
, *m4
;
1205 m1
= strjoina("_SYSTEMD_UNIT=", unit
);
1206 m2
= strjoina("COREDUMP_UNIT=", unit
);
1207 m3
= strjoina("UNIT=", unit
);
1208 m4
= strjoina("OBJECT_SYSTEMD_UNIT=", unit
);
1211 /* Look for messages from the service itself */
1212 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1214 /* Look for coredumps of the service */
1215 (r
= sd_journal_add_disjunction(j
)) ||
1216 (r
= sd_journal_add_match(j
, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1217 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1218 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1220 /* Look for messages from PID 1 about this service */
1221 (r
= sd_journal_add_disjunction(j
)) ||
1222 (r
= sd_journal_add_match(j
, "_PID=1", 0)) ||
1223 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1225 /* Look for messages from authorized daemons about this service */
1226 (r
= sd_journal_add_disjunction(j
)) ||
1227 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1228 (r
= sd_journal_add_match(j
, m4
, 0))
1231 if (r
== 0 && endswith(unit
, ".slice")) {
1234 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1236 /* Show all messages belonging to a slice */
1238 (r
= sd_journal_add_disjunction(j
)) ||
1239 (r
= sd_journal_add_match(j
, m5
, 0))
1246 int add_matches_for_user_unit(sd_journal
*j
, const char *unit
, uid_t uid
) {
1248 char *m1
, *m2
, *m3
, *m4
;
1249 char muid
[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t
)];
1254 m1
= strjoina("_SYSTEMD_USER_UNIT=", unit
);
1255 m2
= strjoina("USER_UNIT=", unit
);
1256 m3
= strjoina("COREDUMP_USER_UNIT=", unit
);
1257 m4
= strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit
);
1258 sprintf(muid
, "_UID="UID_FMT
, uid
);
1261 /* Look for messages from the user service itself */
1262 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1263 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1265 /* Look for messages from systemd about this service */
1266 (r
= sd_journal_add_disjunction(j
)) ||
1267 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1268 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1270 /* Look for coredumps of the service */
1271 (r
= sd_journal_add_disjunction(j
)) ||
1272 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1273 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1274 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1276 /* Look for messages from authorized daemons about this service */
1277 (r
= sd_journal_add_disjunction(j
)) ||
1278 (r
= sd_journal_add_match(j
, m4
, 0)) ||
1279 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1280 (r
= sd_journal_add_match(j
, "_UID=0", 0))
1283 if (r
== 0 && endswith(unit
, ".slice")) {
1286 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1288 /* Show all messages belonging to a slice */
1290 (r
= sd_journal_add_disjunction(j
)) ||
1291 (r
= sd_journal_add_match(j
, m5
, 0)) ||
1292 (r
= sd_journal_add_match(j
, muid
, 0))
1299 static int get_boot_id_for_machine(const char *machine
, sd_id128_t
*boot_id
) {
1300 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1301 _cleanup_close_
int pidnsfd
= -1, mntnsfd
= -1, rootfd
= -1;
1310 if (!machine_name_is_valid(machine
))
1313 r
= container_get_leader(machine
, &pid
);
1317 r
= namespace_open(pid
, &pidnsfd
, &mntnsfd
, NULL
, NULL
, &rootfd
);
1321 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1324 r
= safe_fork("(sd-bootid)", FORK_RESET_SIGNALS
|FORK_DEATHSIG
, &child
);
1330 pair
[0] = safe_close(pair
[0]);
1332 r
= namespace_enter(pidnsfd
, mntnsfd
, -1, -1, rootfd
);
1334 _exit(EXIT_FAILURE
);
1336 fd
= open("/proc/sys/kernel/random/boot_id", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1338 _exit(EXIT_FAILURE
);
1340 r
= loop_read_exact(fd
, buf
, 36, false);
1343 _exit(EXIT_FAILURE
);
1345 k
= send(pair
[1], buf
, 36, MSG_NOSIGNAL
);
1347 _exit(EXIT_FAILURE
);
1349 _exit(EXIT_SUCCESS
);
1352 pair
[1] = safe_close(pair
[1]);
1354 r
= wait_for_terminate_and_check("(sd-bootid)", child
, 0);
1357 if (r
!= EXIT_SUCCESS
)
1360 k
= recv(pair
[0], buf
, 36, 0);
1365 r
= sd_id128_from_string(buf
, boot_id
);
1372 int add_match_this_boot(sd_journal
*j
, const char *machine
) {
1373 char match
[9+32+1] = "_BOOT_ID=";
1380 r
= get_boot_id_for_machine(machine
, &boot_id
);
1382 return log_error_errno(r
, "Failed to get boot id of container %s: %m", machine
);
1384 r
= sd_id128_get_boot(&boot_id
);
1386 return log_error_errno(r
, "Failed to get boot id: %m");
1389 sd_id128_to_string(boot_id
, match
+ 9);
1390 r
= sd_journal_add_match(j
, match
, strlen(match
));
1392 return log_error_errno(r
, "Failed to add match: %m");
1394 r
= sd_journal_add_conjunction(j
);
1396 return log_error_errno(r
, "Failed to add conjunction: %m");
1401 int show_journal_by_unit(
1410 int journal_open_flags
,
1414 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1418 assert(mode
< _OUTPUT_MODE_MAX
);
1424 r
= sd_journal_open(&j
, journal_open_flags
);
1426 return log_error_errno(r
, "Failed to open journal: %m");
1428 r
= add_match_this_boot(j
, NULL
);
1433 r
= add_matches_for_unit(j
, unit
);
1435 r
= add_matches_for_user_unit(j
, unit
, uid
);
1437 return log_error_errno(r
, "Failed to add unit matches: %m");
1439 if (DEBUG_LOGGING
) {
1440 _cleanup_free_
char *filter
;
1442 filter
= journal_make_match_string(j
);
1446 log_debug("Journal filter: %s", filter
);
1449 return show_journal(f
, j
, mode
, n_columns
, not_before
, how_many
, flags
, ellipsized
);