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"
51 #include "terminal-util.h"
52 #include "time-util.h"
56 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
57 #define PRINT_LINE_THRESHOLD 3
58 #define PRINT_CHAR_THRESHOLD 300
60 #define JSON_THRESHOLD 4096
62 static int print_catalog(FILE *f
, sd_journal
*j
) {
64 _cleanup_free_
char *t
= NULL
, *z
= NULL
;
67 r
= sd_journal_get_catalog(j
, &t
);
71 z
= strreplace(strstrip(t
), "\n", "\n-- ");
82 static int parse_field(const void *data
, size_t length
, const char *field
, char **target
, size_t *target_size
) {
94 if (memcmp(data
, field
, fl
))
98 buf
= new(char, nl
+1);
102 memcpy(buf
, (const char*) data
+ fl
, nl
);
114 static bool shall_print(const char *p
, size_t l
, OutputFlags flags
) {
117 if (flags
& OUTPUT_SHOW_ALL
)
120 if (l
>= PRINT_CHAR_THRESHOLD
)
123 if (!utf8_is_printable(p
, l
))
129 static bool print_multiline(FILE *f
, unsigned prefix
, unsigned n_columns
, OutputFlags flags
, int priority
, const char* message
, size_t message_len
) {
130 const char *color_on
= "", *color_off
= "";
131 const char *pos
, *end
;
132 bool ellipsized
= false;
135 if (flags
& OUTPUT_COLOR
) {
136 if (priority
<= LOG_ERR
) {
137 color_on
= ANSI_HIGHLIGHT_RED
;
138 color_off
= ANSI_NORMAL
;
139 } else if (priority
<= LOG_NOTICE
) {
140 color_on
= ANSI_HIGHLIGHT
;
141 color_off
= ANSI_NORMAL
;
145 /* A special case: make sure that we print a newline when
146 the message is empty. */
147 if (message_len
== 0)
151 pos
< message
+ message_len
;
152 pos
= end
+ 1, line
++) {
153 bool continuation
= line
> 0;
156 for (end
= pos
; end
< message
+ message_len
&& *end
!= '\n'; end
++)
161 /* We need to figure out when we are showing not-last line, *and*
162 * will skip subsequent lines. In that case, we will put the dots
163 * at the end of the line, instead of putting dots in the middle
167 line
+ 1 == PRINT_LINE_THRESHOLD
||
168 end
+ 1 >= message
+ PRINT_CHAR_THRESHOLD
;
170 if (flags
& (OUTPUT_FULL_WIDTH
| OUTPUT_SHOW_ALL
) ||
171 (prefix
+ len
+ 1 < n_columns
&& !tail_line
)) {
172 fprintf(f
, "%*s%s%.*s%s\n",
173 continuation
* prefix
, "",
174 color_on
, len
, pos
, color_off
);
178 /* Beyond this point, ellipsization will happen. */
181 if (prefix
< n_columns
&& n_columns
- prefix
>= 3) {
182 if (n_columns
- prefix
> (unsigned) len
+ 3)
183 fprintf(f
, "%*s%s%.*s...%s\n",
184 continuation
* prefix
, "",
185 color_on
, len
, pos
, color_off
);
187 _cleanup_free_
char *e
;
189 e
= ellipsize_mem(pos
, len
, n_columns
- prefix
,
190 tail_line
? 100 : 90);
192 fprintf(f
, "%*s%s%.*s%s\n",
193 continuation
* prefix
, "",
194 color_on
, len
, pos
, color_off
);
196 fprintf(f
, "%*s%s%s%s\n",
197 continuation
* prefix
, "",
198 color_on
, e
, color_off
);
210 static int output_timestamp_monotonic(FILE *f
, sd_journal
*j
, const char *monotonic
) {
220 r
= safe_atou64(monotonic
, &t
);
222 r
= sd_journal_get_monotonic_usec(j
, &t
, &boot_id
);
224 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
226 fprintf(f
, "[%5"PRI_USEC
".%06"PRI_USEC
"]", t
/ USEC_PER_SEC
, t
% USEC_PER_SEC
);
227 return 1 + 5 + 1 + 6 + 1;
230 static int output_timestamp_realtime(FILE *f
, sd_journal
*j
, OutputMode mode
, OutputFlags flags
, const char *realtime
) {
231 char buf
[MAX(FORMAT_TIMESTAMP_MAX
, 64)];
232 struct tm
*(*gettime_r
)(const time_t *, struct tm
*);
243 r
= safe_atou64(realtime
, &x
);
245 r
= sd_journal_get_realtime_usec(j
, &x
);
247 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
249 if (x
> USEC_TIMESTAMP_FORMATTABLE_MAX
) {
250 log_error("Timestamp cannot be printed");
254 if (mode
== OUTPUT_SHORT_FULL
) {
257 if (flags
& OUTPUT_UTC
)
258 k
= format_timestamp_utc(buf
, sizeof(buf
), x
);
260 k
= format_timestamp(buf
, sizeof(buf
), x
);
262 log_error("Failed to format timestamp.");
269 gettime_r
= (flags
& OUTPUT_UTC
) ? gmtime_r
: localtime_r
;
270 t
= (time_t) (x
/ USEC_PER_SEC
);
274 case OUTPUT_SHORT_UNIX
:
275 xsprintf(buf
, "%10"PRI_TIME
".%06"PRIu64
, t
, x
% USEC_PER_SEC
);
278 case OUTPUT_SHORT_ISO
:
279 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t
, &tm
)) <= 0) {
280 log_error("Failed to format ISO time");
285 case OUTPUT_SHORT_ISO_PRECISE
:
286 /* No usec in strftime, so we leave space and copy over */
287 if (strftime(buf
, sizeof(buf
), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t
, &tm
)) <= 0) {
288 log_error("Failed to format ISO-precise time");
291 xsprintf(usec
, "%06"PRI_USEC
, x
% USEC_PER_SEC
);
292 memcpy(buf
+ 20, usec
, 6);
296 case OUTPUT_SHORT_PRECISE
:
298 if (strftime(buf
, sizeof(buf
), "%b %d %H:%M:%S", gettime_r(&t
, &tm
)) <= 0) {
299 log_error("Failed to format syslog time");
303 if (mode
== OUTPUT_SHORT_PRECISE
) {
306 assert(sizeof(buf
) > strlen(buf
));
307 k
= sizeof(buf
) - strlen(buf
);
309 r
= snprintf(buf
+ strlen(buf
), k
, ".%06"PRIu64
, x
% USEC_PER_SEC
);
310 if (r
<= 0 || (size_t) r
>= k
) { /* too long? */
311 log_error("Failed to format precise time");
318 assert_not_reached("Unknown time format");
323 return (int) strlen(buf
);
326 static int output_short(
337 _cleanup_free_
char *hostname
= NULL
, *identifier
= NULL
, *comm
= NULL
, *pid
= NULL
, *fake_pid
= NULL
, *message
= NULL
, *realtime
= NULL
, *monotonic
= NULL
, *priority
= NULL
;
338 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;
340 bool ellipsized
= false;
345 /* Set the threshold to one bigger than the actual print
346 * threshold, so that if the line is actually longer than what
347 * we're willing to print, ellipsization will occur. This way
348 * we won't output a misleading line without any indication of
351 sd_journal_set_data_threshold(j
, flags
& (OUTPUT_SHOW_ALL
|OUTPUT_FULL_WIDTH
) ? 0 : PRINT_CHAR_THRESHOLD
+ 1);
353 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
355 r
= parse_field(data
, length
, "PRIORITY=", &priority
, &priority_len
);
361 r
= parse_field(data
, length
, "_HOSTNAME=", &hostname
, &hostname_len
);
367 r
= parse_field(data
, length
, "SYSLOG_IDENTIFIER=", &identifier
, &identifier_len
);
373 r
= parse_field(data
, length
, "_COMM=", &comm
, &comm_len
);
379 r
= parse_field(data
, length
, "_PID=", &pid
, &pid_len
);
385 r
= parse_field(data
, length
, "SYSLOG_PID=", &fake_pid
, &fake_pid_len
);
391 r
= parse_field(data
, length
, "_SOURCE_REALTIME_TIMESTAMP=", &realtime
, &realtime_len
);
397 r
= parse_field(data
, length
, "_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic
, &monotonic_len
);
403 r
= parse_field(data
, length
, "MESSAGE=", &message
, &message_len
);
408 log_debug_errno(r
, "Skipping message we can't read: %m");
412 return log_error_errno(r
, "Failed to get journal fields: %m");
415 log_debug("Skipping message without MESSAGE= field.");
419 if (!(flags
& OUTPUT_SHOW_ALL
))
420 strip_tab_ansi(&message
, &message_len
);
422 if (priority_len
== 1 && *priority
>= '0' && *priority
<= '7')
425 if (mode
== OUTPUT_SHORT_MONOTONIC
)
426 r
= output_timestamp_monotonic(f
, j
, monotonic
);
428 r
= output_timestamp_realtime(f
, j
, mode
, flags
, realtime
);
433 if (flags
& OUTPUT_NO_HOSTNAME
) {
434 /* Suppress display of the hostname if this is requested. */
435 hostname
= mfree(hostname
);
439 if (hostname
&& shall_print(hostname
, hostname_len
, flags
)) {
440 fprintf(f
, " %.*s", (int) hostname_len
, hostname
);
441 n
+= hostname_len
+ 1;
444 if (identifier
&& shall_print(identifier
, identifier_len
, flags
)) {
445 fprintf(f
, " %.*s", (int) identifier_len
, identifier
);
446 n
+= identifier_len
+ 1;
447 } else if (comm
&& shall_print(comm
, comm_len
, flags
)) {
448 fprintf(f
, " %.*s", (int) comm_len
, comm
);
451 fputs(" unknown", f
);
453 if (pid
&& shall_print(pid
, pid_len
, flags
)) {
454 fprintf(f
, "[%.*s]", (int) pid_len
, pid
);
456 } else if (fake_pid
&& shall_print(fake_pid
, fake_pid_len
, flags
)) {
457 fprintf(f
, "[%.*s]", (int) fake_pid_len
, fake_pid
);
458 n
+= fake_pid_len
+ 2;
461 if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(message
, message_len
)) {
462 char bytes
[FORMAT_BYTES_MAX
];
463 fprintf(f
, ": [%s blob data]\n", format_bytes(bytes
, sizeof(bytes
), message_len
));
467 print_multiline(f
, n
+ 2, n_columns
, flags
, p
, message
, message_len
);
470 if (flags
& OUTPUT_CATALOG
)
476 static int output_verbose(
485 _cleanup_free_
char *cursor
= NULL
;
486 uint64_t realtime
= 0;
487 char ts
[FORMAT_TIMESTAMP_MAX
+ 7];
488 const char *timestamp
;
494 sd_journal_set_data_threshold(j
, 0);
496 r
= sd_journal_get_data(j
, "_SOURCE_REALTIME_TIMESTAMP", &data
, &length
);
498 log_debug("Source realtime timestamp not found");
500 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get source realtime timestamp: %m");
502 _cleanup_free_
char *value
= NULL
;
504 r
= parse_field(data
, length
, "_SOURCE_REALTIME_TIMESTAMP=", &value
, NULL
);
509 r
= safe_atou64(value
, &realtime
);
511 log_debug_errno(r
, "Failed to parse realtime timestamp: %m");
515 r
= sd_journal_get_realtime_usec(j
, &realtime
);
517 return log_full_errno(r
== -EADDRNOTAVAIL
? LOG_DEBUG
: LOG_ERR
, r
, "Failed to get realtime timestamp: %m");
520 r
= sd_journal_get_cursor(j
, &cursor
);
522 return log_error_errno(r
, "Failed to get cursor: %m");
524 timestamp
= flags
& OUTPUT_UTC
? format_timestamp_us_utc(ts
, sizeof ts
, realtime
)
525 : format_timestamp_us(ts
, sizeof ts
, realtime
);
526 fprintf(f
, "%s [%s]\n",
527 timestamp
?: "(no timestamp)",
530 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
533 const char *on
= "", *off
= "";
535 c
= memchr(data
, '=', length
);
537 log_error("Invalid field.");
540 fieldlen
= c
- (const char*) data
;
542 if (flags
& OUTPUT_COLOR
&& startswith(data
, "MESSAGE=")) {
547 if ((flags
& OUTPUT_SHOW_ALL
) ||
548 (((length
< PRINT_CHAR_THRESHOLD
) || flags
& OUTPUT_FULL_WIDTH
)
549 && utf8_is_printable(data
, length
))) {
550 fprintf(f
, " %s%.*s=", on
, fieldlen
, (const char*)data
);
551 print_multiline(f
, 4 + fieldlen
+ 1, 0, OUTPUT_FULL_WIDTH
, 0, c
+ 1, length
- fieldlen
- 1);
554 char bytes
[FORMAT_BYTES_MAX
];
556 fprintf(f
, " %s%.*s=[%s blob data]%s\n",
558 (int) (c
- (const char*) data
),
560 format_bytes(bytes
, sizeof(bytes
), length
- (c
- (const char *) data
) - 1),
568 if (flags
& OUTPUT_CATALOG
)
574 static int output_export(
584 usec_t realtime
, monotonic
;
585 _cleanup_free_
char *cursor
= NULL
;
591 sd_journal_set_data_threshold(j
, 0);
593 r
= sd_journal_get_realtime_usec(j
, &realtime
);
595 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
597 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
599 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
601 r
= sd_journal_get_cursor(j
, &cursor
);
603 return log_error_errno(r
, "Failed to get cursor: %m");
607 "__REALTIME_TIMESTAMP="USEC_FMT
"\n"
608 "__MONOTONIC_TIMESTAMP="USEC_FMT
"\n"
613 sd_id128_to_string(boot_id
, sid
));
615 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
617 /* We already printed the boot id, from the data in
618 * the header, hence let's suppress it here */
620 startswith(data
, "_BOOT_ID="))
623 if (utf8_is_printable_newline(data
, length
, false))
624 fwrite(data
, length
, 1, f
);
629 c
= memchr(data
, '=', length
);
631 log_error("Invalid field.");
635 fwrite(data
, c
- (const char*) data
, 1, f
);
637 le64
= htole64(length
- (c
- (const char*) data
) - 1);
638 fwrite(&le64
, sizeof(le64
), 1, f
);
639 fwrite(c
+ 1, length
- (c
- (const char*) data
) - 1, 1, f
);
662 if (!(flags
& OUTPUT_SHOW_ALL
) && l
>= JSON_THRESHOLD
)
665 else if (!(flags
& OUTPUT_SHOW_ALL
) && !utf8_is_printable(p
, l
)) {
666 bool not_first
= false;
672 fprintf(f
, ", %u", (uint8_t) *p
);
675 fprintf(f
, "%u", (uint8_t) *p
);
687 if (*p
== '"' || *p
== '\\') {
690 } else if (*p
== '\n')
692 else if ((uint8_t) *p
< ' ')
693 fprintf(f
, "\\u%04x", (uint8_t) *p
);
705 static int output_json(
712 uint64_t realtime
, monotonic
;
713 _cleanup_free_
char *cursor
= NULL
;
720 bool done
, separator
;
724 sd_journal_set_data_threshold(j
, flags
& OUTPUT_SHOW_ALL
? 0 : JSON_THRESHOLD
);
726 r
= sd_journal_get_realtime_usec(j
, &realtime
);
728 return log_error_errno(r
, "Failed to get realtime timestamp: %m");
730 r
= sd_journal_get_monotonic_usec(j
, &monotonic
, &boot_id
);
732 return log_error_errno(r
, "Failed to get monotonic timestamp: %m");
734 r
= sd_journal_get_cursor(j
, &cursor
);
736 return log_error_errno(r
, "Failed to get cursor: %m");
738 if (mode
== OUTPUT_JSON_PRETTY
)
741 "\t\"__CURSOR\" : \"%s\",\n"
742 "\t\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\",\n"
743 "\t\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\",\n"
744 "\t\"_BOOT_ID\" : \"%s\"",
748 sd_id128_to_string(boot_id
, sid
));
750 if (mode
== OUTPUT_JSON_SSE
)
754 "{ \"__CURSOR\" : \"%s\", "
755 "\"__REALTIME_TIMESTAMP\" : \""USEC_FMT
"\", "
756 "\"__MONOTONIC_TIMESTAMP\" : \""USEC_FMT
"\", "
757 "\"_BOOT_ID\" : \"%s\"",
761 sd_id128_to_string(boot_id
, sid
));
764 h
= hashmap_new(&string_hash_ops
);
768 /* First round, iterate through the entry and count how often each field appears */
769 JOURNAL_FOREACH_DATA_RETVAL(j
, data
, length
, r
) {
775 memcmp(data
, "_BOOT_ID=", 9) == 0)
778 eq
= memchr(data
, '=', length
);
782 n
= strndup(data
, eq
- (const char*) data
);
788 u
= PTR_TO_UINT(hashmap_get(h
, n
));
790 r
= hashmap_put(h
, n
, UINT_TO_PTR(1));
797 r
= hashmap_update(h
, n
, UINT_TO_PTR(u
+ 1));
813 SD_JOURNAL_FOREACH_DATA(j
, data
, length
) {
819 /* We already printed the boot id, from the data in
820 * the header, hence let's suppress it here */
822 memcmp(data
, "_BOOT_ID=", 9) == 0)
825 eq
= memchr(data
, '=', length
);
830 if (mode
== OUTPUT_JSON_PRETTY
)
836 m
= eq
- (const char*) data
;
838 n
= strndup(data
, m
);
844 u
= PTR_TO_UINT(hashmap_get2(h
, n
, (void**) &kk
));
846 /* We already printed this, let's jump to the next */
852 /* Field only appears once, output it directly */
854 json_escape(f
, data
, m
, flags
);
857 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
859 hashmap_remove(h
, n
);
868 /* Field appears multiple times, output it as array */
869 json_escape(f
, data
, m
, flags
);
871 json_escape(f
, eq
+ 1, length
- m
- 1, flags
);
873 /* Iterate through the end of the list */
875 while (sd_journal_enumerate_data(j
, &data
, &length
) > 0) {
879 if (memcmp(data
, n
, m
) != 0)
882 if (((const char*) data
)[m
] != '=')
886 json_escape(f
, (const char*) data
+ m
+ 1, length
- m
- 1, flags
);
891 hashmap_remove(h
, n
);
895 /* Iterate data fields form the beginning */
905 if (mode
== OUTPUT_JSON_PRETTY
)
907 else if (mode
== OUTPUT_JSON_SSE
)
915 while ((k
= hashmap_steal_first_key(h
)))
923 static int output_cat(
937 sd_journal_set_data_threshold(j
, 0);
939 r
= sd_journal_get_data(j
, "MESSAGE", &data
, &l
);
941 /* An entry without MESSAGE=? */
945 return log_error_errno(r
, "Failed to get data: %m");
950 fwrite((const char*) data
+ 8, 1, l
- 8, f
);
956 static int (*output_funcs
[_OUTPUT_MODE_MAX
])(
961 OutputFlags flags
) = {
963 [OUTPUT_SHORT
] = output_short
,
964 [OUTPUT_SHORT_ISO
] = output_short
,
965 [OUTPUT_SHORT_ISO_PRECISE
] = output_short
,
966 [OUTPUT_SHORT_PRECISE
] = output_short
,
967 [OUTPUT_SHORT_MONOTONIC
] = output_short
,
968 [OUTPUT_SHORT_UNIX
] = output_short
,
969 [OUTPUT_SHORT_FULL
] = output_short
,
970 [OUTPUT_VERBOSE
] = output_verbose
,
971 [OUTPUT_EXPORT
] = output_export
,
972 [OUTPUT_JSON
] = output_json
,
973 [OUTPUT_JSON_PRETTY
] = output_json
,
974 [OUTPUT_JSON_SSE
] = output_json
,
975 [OUTPUT_CAT
] = output_cat
988 assert(mode
< _OUTPUT_MODE_MAX
);
991 n_columns
= columns();
993 ret
= output_funcs
[mode
](f
, j
, mode
, n_columns
, flags
);
996 if (ellipsized
&& ret
> 0)
1002 static int maybe_print_begin_newline(FILE *f
, OutputFlags
*flags
) {
1006 if (!(*flags
& OUTPUT_BEGIN_NEWLINE
))
1009 /* Print a beginning new line if that's request, but only once
1010 * on the first line we print. */
1013 *flags
&= ~OUTPUT_BEGIN_NEWLINE
;
1017 static int show_journal(FILE *f
,
1028 bool need_seek
= false;
1029 int warn_cutoff
= flags
& OUTPUT_WARN_CUTOFF
;
1033 assert(mode
< _OUTPUT_MODE_MAX
);
1036 r
= sd_journal_seek_tail(j
);
1038 return log_error_errno(r
, "Failed to seek to tail: %m");
1040 r
= sd_journal_previous_skip(j
, how_many
);
1042 return log_error_errno(r
, "Failed to skip previous: %m");
1049 r
= sd_journal_next(j
);
1051 return log_error_errno(r
, "Failed to iterate through journal: %m");
1059 if (not_before
> 0) {
1060 r
= sd_journal_get_monotonic_usec(j
, &usec
, NULL
);
1062 /* -ESTALE is returned if the
1063 timestamp is not from this boot */
1067 return log_error_errno(r
, "Failed to get journal time: %m");
1069 if (usec
< not_before
)
1074 maybe_print_begin_newline(f
, &flags
);
1076 r
= output_journal(f
, j
, mode
, n_columns
, flags
, ellipsized
);
1081 if (warn_cutoff
&& line
< how_many
&& not_before
> 0) {
1085 /* Check whether the cutoff line is too early */
1087 r
= sd_id128_get_boot(&boot_id
);
1089 return log_error_errno(r
, "Failed to get boot id: %m");
1091 r
= sd_journal_get_cutoff_monotonic_usec(j
, boot_id
, &cutoff
, NULL
);
1093 return log_error_errno(r
, "Failed to get journal cutoff time: %m");
1095 if (r
> 0 && not_before
< cutoff
) {
1096 maybe_print_begin_newline(f
, &flags
);
1097 fprintf(f
, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1100 warn_cutoff
= false;
1103 if (!(flags
& OUTPUT_FOLLOW
))
1106 r
= sd_journal_wait(j
, USEC_INFINITY
);
1108 return log_error_errno(r
, "Failed to wait for journal: %m");
1115 int add_matches_for_unit(sd_journal
*j
, const char *unit
) {
1116 const char *m1
, *m2
, *m3
, *m4
;
1122 m1
= strjoina("_SYSTEMD_UNIT=", unit
);
1123 m2
= strjoina("COREDUMP_UNIT=", unit
);
1124 m3
= strjoina("UNIT=", unit
);
1125 m4
= strjoina("OBJECT_SYSTEMD_UNIT=", unit
);
1128 /* Look for messages from the service itself */
1129 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1131 /* Look for coredumps of the service */
1132 (r
= sd_journal_add_disjunction(j
)) ||
1133 (r
= sd_journal_add_match(j
, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1134 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1135 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1137 /* Look for messages from PID 1 about this service */
1138 (r
= sd_journal_add_disjunction(j
)) ||
1139 (r
= sd_journal_add_match(j
, "_PID=1", 0)) ||
1140 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1142 /* Look for messages from authorized daemons about this service */
1143 (r
= sd_journal_add_disjunction(j
)) ||
1144 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1145 (r
= sd_journal_add_match(j
, m4
, 0))
1148 if (r
== 0 && endswith(unit
, ".slice")) {
1151 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1153 /* Show all messages belonging to a slice */
1155 (r
= sd_journal_add_disjunction(j
)) ||
1156 (r
= sd_journal_add_match(j
, m5
, 0))
1163 int add_matches_for_user_unit(sd_journal
*j
, const char *unit
, uid_t uid
) {
1165 char *m1
, *m2
, *m3
, *m4
;
1166 char muid
[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t
)];
1171 m1
= strjoina("_SYSTEMD_USER_UNIT=", unit
);
1172 m2
= strjoina("USER_UNIT=", unit
);
1173 m3
= strjoina("COREDUMP_USER_UNIT=", unit
);
1174 m4
= strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit
);
1175 sprintf(muid
, "_UID="UID_FMT
, uid
);
1178 /* Look for messages from the user service itself */
1179 (r
= sd_journal_add_match(j
, m1
, 0)) ||
1180 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1182 /* Look for messages from systemd about this service */
1183 (r
= sd_journal_add_disjunction(j
)) ||
1184 (r
= sd_journal_add_match(j
, m2
, 0)) ||
1185 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1187 /* Look for coredumps of the service */
1188 (r
= sd_journal_add_disjunction(j
)) ||
1189 (r
= sd_journal_add_match(j
, m3
, 0)) ||
1190 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1191 (r
= sd_journal_add_match(j
, "_UID=0", 0)) ||
1193 /* Look for messages from authorized daemons about this service */
1194 (r
= sd_journal_add_disjunction(j
)) ||
1195 (r
= sd_journal_add_match(j
, m4
, 0)) ||
1196 (r
= sd_journal_add_match(j
, muid
, 0)) ||
1197 (r
= sd_journal_add_match(j
, "_UID=0", 0))
1200 if (r
== 0 && endswith(unit
, ".slice")) {
1203 m5
= strjoina("_SYSTEMD_SLICE=", unit
);
1205 /* Show all messages belonging to a slice */
1207 (r
= sd_journal_add_disjunction(j
)) ||
1208 (r
= sd_journal_add_match(j
, m5
, 0)) ||
1209 (r
= sd_journal_add_match(j
, muid
, 0))
1216 static int get_boot_id_for_machine(const char *machine
, sd_id128_t
*boot_id
) {
1217 _cleanup_close_pair_
int pair
[2] = { -1, -1 };
1218 _cleanup_close_
int pidnsfd
= -1, mntnsfd
= -1, rootfd
= -1;
1228 if (!machine_name_is_valid(machine
))
1231 r
= container_get_leader(machine
, &pid
);
1235 r
= namespace_open(pid
, &pidnsfd
, &mntnsfd
, NULL
, NULL
, &rootfd
);
1239 if (socketpair(AF_UNIX
, SOCK_DGRAM
, 0, pair
) < 0)
1249 pair
[0] = safe_close(pair
[0]);
1251 r
= namespace_enter(pidnsfd
, mntnsfd
, -1, -1, rootfd
);
1253 _exit(EXIT_FAILURE
);
1255 fd
= open("/proc/sys/kernel/random/boot_id", O_RDONLY
|O_CLOEXEC
|O_NOCTTY
);
1257 _exit(EXIT_FAILURE
);
1259 r
= loop_read_exact(fd
, buf
, 36, false);
1262 _exit(EXIT_FAILURE
);
1264 k
= send(pair
[1], buf
, 36, MSG_NOSIGNAL
);
1266 _exit(EXIT_FAILURE
);
1268 _exit(EXIT_SUCCESS
);
1271 pair
[1] = safe_close(pair
[1]);
1273 r
= wait_for_terminate(child
, &si
);
1274 if (r
< 0 || si
.si_code
!= CLD_EXITED
|| si
.si_status
!= EXIT_SUCCESS
)
1275 return r
< 0 ? r
: -EIO
;
1277 k
= recv(pair
[0], buf
, 36, 0);
1282 r
= sd_id128_from_string(buf
, boot_id
);
1289 int add_match_this_boot(sd_journal
*j
, const char *machine
) {
1290 char match
[9+32+1] = "_BOOT_ID=";
1297 r
= get_boot_id_for_machine(machine
, &boot_id
);
1299 return log_error_errno(r
, "Failed to get boot id of container %s: %m", machine
);
1301 r
= sd_id128_get_boot(&boot_id
);
1303 return log_error_errno(r
, "Failed to get boot id: %m");
1306 sd_id128_to_string(boot_id
, match
+ 9);
1307 r
= sd_journal_add_match(j
, match
, strlen(match
));
1309 return log_error_errno(r
, "Failed to add match: %m");
1311 r
= sd_journal_add_conjunction(j
);
1313 return log_error_errno(r
, "Failed to add conjunction: %m");
1318 int show_journal_by_unit(
1327 int journal_open_flags
,
1331 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
1335 assert(mode
< _OUTPUT_MODE_MAX
);
1341 r
= sd_journal_open(&j
, journal_open_flags
);
1343 return log_error_errno(r
, "Failed to open journal: %m");
1345 r
= add_match_this_boot(j
, NULL
);
1350 r
= add_matches_for_unit(j
, unit
);
1352 r
= add_matches_for_user_unit(j
, unit
, uid
);
1354 return log_error_errno(r
, "Failed to add unit matches: %m");
1356 if (_unlikely_(log_get_max_level() >= LOG_DEBUG
)) {
1357 _cleanup_free_
char *filter
;
1359 filter
= journal_make_match_string(j
);
1363 log_debug("Journal filter: %s", filter
);
1366 return show_journal(f
, j
, mode
, n_columns
, not_before
, how_many
, flags
, ellipsized
);