]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/logs-show.c
Merge pull request #11042 from yuwata/tiny-coding-style-fixes
[thirdparty/systemd.git] / src / shared / logs-show.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <stdint.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <syslog.h>
11 #include <time.h>
12 #include <unistd.h>
13
14 #include "sd-id128.h"
15 #include "sd-journal.h"
16
17 #include "alloc-util.h"
18 #include "fd-util.h"
19 #include "format-util.h"
20 #include "hashmap.h"
21 #include "hostname-util.h"
22 #include "io-util.h"
23 #include "journal-internal.h"
24 #include "json.h"
25 #include "log.h"
26 #include "logs-show.h"
27 #include "macro.h"
28 #include "output-mode.h"
29 #include "parse-util.h"
30 #include "process-util.h"
31 #include "sparse-endian.h"
32 #include "stdio-util.h"
33 #include "string-table.h"
34 #include "string-util.h"
35 #include "strv.h"
36 #include "terminal-util.h"
37 #include "time-util.h"
38 #include "utf8.h"
39 #include "util.h"
40
41 /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */
42 #define PRINT_LINE_THRESHOLD 3
43 #define PRINT_CHAR_THRESHOLD 300
44
45 #define JSON_THRESHOLD 4096U
46
47 static int print_catalog(FILE *f, sd_journal *j) {
48 int r;
49 _cleanup_free_ char *t = NULL, *z = NULL;
50
51 r = sd_journal_get_catalog(j, &t);
52 if (r < 0)
53 return r;
54
55 z = strreplace(strstrip(t), "\n", "\n-- ");
56 if (!z)
57 return log_oom();
58
59 fputs("-- ", f);
60 fputs(z, f);
61 fputc('\n', f);
62
63 return 0;
64 }
65
66 static int parse_field(const void *data, size_t length, const char *field, size_t field_len, char **target, size_t *target_len) {
67 size_t nl;
68 char *buf;
69
70 assert(data);
71 assert(field);
72 assert(target);
73
74 if (length < field_len)
75 return 0;
76
77 if (memcmp(data, field, field_len))
78 return 0;
79
80 nl = length - field_len;
81
82 buf = newdup_suffix0(char, (const char*) data + field_len, nl);
83 if (!buf)
84 return log_oom();
85
86 free(*target);
87 *target = buf;
88
89 if (target_len)
90 *target_len = nl;
91
92 return 1;
93 }
94
95 typedef struct ParseFieldVec {
96 const char *field;
97 size_t field_len;
98 char **target;
99 size_t *target_len;
100 } ParseFieldVec;
101
102 #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) \
103 { .field = _field, .field_len = strlen(_field), .target = _target, .target_len = _target_len }
104
105 static int parse_fieldv(const void *data, size_t length, const ParseFieldVec *fields, unsigned n_fields) {
106 unsigned i;
107
108 for (i = 0; i < n_fields; i++) {
109 const ParseFieldVec *f = &fields[i];
110 int r;
111
112 r = parse_field(data, length, f->field, f->field_len, f->target, f->target_len);
113 if (r < 0)
114 return r;
115 else if (r > 0)
116 break;
117 }
118
119 return 0;
120 }
121
122 static int field_set_test(Set *fields, const char *name, size_t n) {
123 char *s = NULL;
124
125 if (!fields)
126 return 1;
127
128 s = strndupa(name, n);
129 if (!s)
130 return log_oom();
131
132 return set_get(fields, s) ? 1 : 0;
133 }
134
135 static bool shall_print(const char *p, size_t l, OutputFlags flags) {
136 assert(p);
137
138 if (flags & OUTPUT_SHOW_ALL)
139 return true;
140
141 if (l >= PRINT_CHAR_THRESHOLD)
142 return false;
143
144 if (!utf8_is_printable(p, l))
145 return false;
146
147 return true;
148 }
149
150 static bool print_multiline(
151 FILE *f,
152 unsigned prefix,
153 unsigned n_columns,
154 OutputFlags flags,
155 int priority,
156 const char* message,
157 size_t message_len,
158 size_t highlight[2]) {
159
160 const char *color_on = "", *color_off = "", *highlight_on = "";
161 const char *pos, *end;
162 bool ellipsized = false;
163 int line = 0;
164
165 if (flags & OUTPUT_COLOR) {
166 if (priority <= LOG_ERR) {
167 color_on = ANSI_HIGHLIGHT_RED;
168 color_off = ANSI_NORMAL;
169 highlight_on = ANSI_HIGHLIGHT;
170 } else if (priority <= LOG_NOTICE) {
171 color_on = ANSI_HIGHLIGHT;
172 color_off = ANSI_NORMAL;
173 highlight_on = ANSI_HIGHLIGHT_RED;
174 }
175 }
176
177 /* A special case: make sure that we print a newline when
178 the message is empty. */
179 if (message_len == 0)
180 fputs("\n", f);
181
182 for (pos = message;
183 pos < message + message_len;
184 pos = end + 1, line++) {
185 bool continuation = line > 0;
186 bool tail_line;
187 int len;
188 for (end = pos; end < message + message_len && *end != '\n'; end++)
189 ;
190 len = end - pos;
191 assert(len >= 0);
192
193 /* We need to figure out when we are showing not-last line, *and*
194 * will skip subsequent lines. In that case, we will put the dots
195 * at the end of the line, instead of putting dots in the middle
196 * or not at all.
197 */
198 tail_line =
199 line + 1 == PRINT_LINE_THRESHOLD ||
200 end + 1 >= message + PRINT_CHAR_THRESHOLD;
201
202 if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) ||
203 (prefix + len + 1 < n_columns && !tail_line)) {
204 if (highlight &&
205 (size_t) (pos - message) <= highlight[0] &&
206 highlight[0] < (size_t) len) {
207
208 fprintf(f, "%*s%s%.*s",
209 continuation * prefix, "",
210 color_on, (int) highlight[0], pos);
211 fprintf(f, "%s%.*s",
212 highlight_on,
213 (int) (MIN((size_t) len, highlight[1]) - highlight[0]),
214 pos + highlight[0]);
215 if ((size_t) len > highlight[1])
216 fprintf(f, "%s%.*s",
217 color_on,
218 (int) (len - highlight[1]),
219 pos + highlight[1]);
220 fprintf(f, "%s\n", color_off);
221
222 } else
223 fprintf(f, "%*s%s%.*s%s\n",
224 continuation * prefix, "",
225 color_on, len, pos, color_off);
226 continue;
227 }
228
229 /* Beyond this point, ellipsization will happen. */
230 ellipsized = true;
231
232 if (prefix < n_columns && n_columns - prefix >= 3) {
233 if (n_columns - prefix > (unsigned) len + 3)
234 fprintf(f, "%*s%s%.*s...%s\n",
235 continuation * prefix, "",
236 color_on, len, pos, color_off);
237 else {
238 _cleanup_free_ char *e;
239
240 e = ellipsize_mem(pos, len, n_columns - prefix,
241 tail_line ? 100 : 90);
242 if (!e)
243 fprintf(f, "%*s%s%.*s%s\n",
244 continuation * prefix, "",
245 color_on, len, pos, color_off);
246 else
247 fprintf(f, "%*s%s%s%s\n",
248 continuation * prefix, "",
249 color_on, e, color_off);
250 }
251 } else
252 fputs("...\n", f);
253
254 if (tail_line)
255 break;
256 }
257
258 return ellipsized;
259 }
260
261 static int output_timestamp_monotonic(FILE *f, sd_journal *j, const char *monotonic) {
262 sd_id128_t boot_id;
263 uint64_t t;
264 int r;
265
266 assert(f);
267 assert(j);
268
269 r = -ENXIO;
270 if (monotonic)
271 r = safe_atou64(monotonic, &t);
272 if (r < 0)
273 r = sd_journal_get_monotonic_usec(j, &t, &boot_id);
274 if (r < 0)
275 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
276
277 fprintf(f, "[%5"PRI_USEC".%06"PRI_USEC"]", t / USEC_PER_SEC, t % USEC_PER_SEC);
278 return 1 + 5 + 1 + 6 + 1;
279 }
280
281 static int output_timestamp_realtime(FILE *f, sd_journal *j, OutputMode mode, OutputFlags flags, const char *realtime) {
282 char buf[MAX(FORMAT_TIMESTAMP_MAX, 64)];
283 struct tm *(*gettime_r)(const time_t *, struct tm *);
284 struct tm tm;
285 uint64_t x;
286 time_t t;
287 int r;
288
289 assert(f);
290 assert(j);
291
292 if (realtime)
293 r = safe_atou64(realtime, &x);
294 if (!realtime || r < 0 || !VALID_REALTIME(x))
295 r = sd_journal_get_realtime_usec(j, &x);
296 if (r < 0)
297 return log_error_errno(r, "Failed to get realtime timestamp: %m");
298
299 if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) {
300 const char *k;
301
302 if (flags & OUTPUT_UTC)
303 k = format_timestamp_utc(buf, sizeof(buf), x);
304 else
305 k = format_timestamp(buf, sizeof(buf), x);
306 if (!k)
307 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
308 "Failed to format timestamp: %" PRIu64, x);
309
310 } else {
311 char usec[7];
312
313 gettime_r = (flags & OUTPUT_UTC) ? gmtime_r : localtime_r;
314 t = (time_t) (x / USEC_PER_SEC);
315
316 switch (mode) {
317
318 case OUTPUT_SHORT_UNIX:
319 xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, x % USEC_PER_SEC);
320 break;
321
322 case OUTPUT_SHORT_ISO:
323 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S%z", gettime_r(&t, &tm)) <= 0)
324 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
325 "Failed to format ISO time");
326 break;
327
328 case OUTPUT_SHORT_ISO_PRECISE:
329 /* No usec in strftime, so we leave space and copy over */
330 if (strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S.xxxxxx%z", gettime_r(&t, &tm)) <= 0)
331 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
332 "Failed to format ISO-precise time");
333 xsprintf(usec, "%06"PRI_USEC, x % USEC_PER_SEC);
334 memcpy(buf + 20, usec, 6);
335 break;
336
337 case OUTPUT_SHORT:
338 case OUTPUT_SHORT_PRECISE:
339
340 if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", gettime_r(&t, &tm)) <= 0)
341 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
342 "Failed to format syslog time");
343
344 if (mode == OUTPUT_SHORT_PRECISE) {
345 size_t k;
346
347 assert(sizeof(buf) > strlen(buf));
348 k = sizeof(buf) - strlen(buf);
349
350 r = snprintf(buf + strlen(buf), k, ".%06"PRIu64, x % USEC_PER_SEC);
351 if (r <= 0 || (size_t) r >= k) /* too long? */
352 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
353 "Failed to format precise time");
354 }
355 break;
356
357 default:
358 assert_not_reached("Unknown time format");
359 }
360 }
361
362 fputs(buf, f);
363 return (int) strlen(buf);
364 }
365
366 static int output_short(
367 FILE *f,
368 sd_journal *j,
369 OutputMode mode,
370 unsigned n_columns,
371 OutputFlags flags,
372 Set *output_fields,
373 const size_t highlight[2]) {
374
375 int r;
376 const void *data;
377 size_t length;
378 size_t n = 0;
379 _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;
380 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;
381 int p = LOG_INFO;
382 bool ellipsized = false;
383 const ParseFieldVec fields[] = {
384 PARSE_FIELD_VEC_ENTRY("_PID=", &pid, &pid_len),
385 PARSE_FIELD_VEC_ENTRY("_COMM=", &comm, &comm_len),
386 PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message, &message_len),
387 PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority, &priority_len),
388 PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname, &hostname_len),
389 PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid, &fake_pid_len),
390 PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len),
391 PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, &realtime_len),
392 PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, &monotonic_len),
393 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit, &unit_len),
394 PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit, &user_unit_len),
395 };
396 size_t highlight_shifted[] = {highlight ? highlight[0] : 0, highlight ? highlight[1] : 0};
397
398 assert(f);
399 assert(j);
400
401 /* Set the threshold to one bigger than the actual print
402 * threshold, so that if the line is actually longer than what
403 * we're willing to print, ellipsization will occur. This way
404 * we won't output a misleading line without any indication of
405 * truncation.
406 */
407 sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1);
408
409 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
410 r = parse_fieldv(data, length, fields, ELEMENTSOF(fields));
411 if (r < 0)
412 return r;
413 }
414 if (r == -EBADMSG) {
415 log_debug_errno(r, "Skipping message we can't read: %m");
416 return 0;
417 }
418 if (r < 0)
419 return log_error_errno(r, "Failed to get journal fields: %m");
420
421 if (!message) {
422 log_debug("Skipping message without MESSAGE= field.");
423 return 0;
424 }
425
426 if (!(flags & OUTPUT_SHOW_ALL))
427 strip_tab_ansi(&message, &message_len, highlight_shifted);
428
429 if (priority_len == 1 && *priority >= '0' && *priority <= '7')
430 p = *priority - '0';
431
432 if (mode == OUTPUT_SHORT_MONOTONIC)
433 r = output_timestamp_monotonic(f, j, monotonic);
434 else
435 r = output_timestamp_realtime(f, j, mode, flags, realtime);
436 if (r < 0)
437 return r;
438 n += r;
439
440 if (flags & OUTPUT_NO_HOSTNAME) {
441 /* Suppress display of the hostname if this is requested. */
442 hostname = mfree(hostname);
443 hostname_len = 0;
444 }
445
446 if (hostname && shall_print(hostname, hostname_len, flags)) {
447 fprintf(f, " %.*s", (int) hostname_len, hostname);
448 n += hostname_len + 1;
449 }
450
451 if (mode == OUTPUT_WITH_UNIT && ((unit && shall_print(unit, unit_len, flags)) || (user_unit && shall_print(user_unit, user_unit_len, flags)))) {
452 if (unit) {
453 fprintf(f, " %.*s", (int) unit_len, unit);
454 n += unit_len + 1;
455 }
456 if (user_unit) {
457 if (unit)
458 fprintf(f, "/%.*s", (int) user_unit_len, user_unit);
459 else
460 fprintf(f, " %.*s", (int) user_unit_len, user_unit);
461 n += unit_len + 1;
462 }
463 } else if (identifier && shall_print(identifier, identifier_len, flags)) {
464 fprintf(f, " %.*s", (int) identifier_len, identifier);
465 n += identifier_len + 1;
466 } else if (comm && shall_print(comm, comm_len, flags)) {
467 fprintf(f, " %.*s", (int) comm_len, comm);
468 n += comm_len + 1;
469 } else
470 fputs(" unknown", f);
471
472 if (pid && shall_print(pid, pid_len, flags)) {
473 fprintf(f, "[%.*s]", (int) pid_len, pid);
474 n += pid_len + 2;
475 } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) {
476 fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid);
477 n += fake_pid_len + 2;
478 }
479
480 if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) {
481 char bytes[FORMAT_BYTES_MAX];
482 fprintf(f, ": [%s blob data]\n", format_bytes(bytes, sizeof(bytes), message_len));
483 } else {
484 fputs(": ", f);
485 ellipsized |=
486 print_multiline(f, n + 2, n_columns, flags, p,
487 message, message_len,
488 highlight_shifted);
489 }
490
491 if (flags & OUTPUT_CATALOG)
492 print_catalog(f, j);
493
494 return ellipsized;
495 }
496
497 static int output_verbose(
498 FILE *f,
499 sd_journal *j,
500 OutputMode mode,
501 unsigned n_columns,
502 OutputFlags flags,
503 Set *output_fields,
504 const size_t highlight[2]) {
505
506 const void *data;
507 size_t length;
508 _cleanup_free_ char *cursor = NULL;
509 uint64_t realtime = 0;
510 char ts[FORMAT_TIMESTAMP_MAX + 7];
511 const char *timestamp;
512 int r;
513
514 assert(f);
515 assert(j);
516
517 sd_journal_set_data_threshold(j, 0);
518
519 r = sd_journal_get_data(j, "_SOURCE_REALTIME_TIMESTAMP", &data, &length);
520 if (r == -ENOENT)
521 log_debug("Source realtime timestamp not found");
522 else if (r < 0)
523 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get source realtime timestamp: %m");
524 else {
525 _cleanup_free_ char *value = NULL;
526
527 r = parse_field(data, length, "_SOURCE_REALTIME_TIMESTAMP=",
528 STRLEN("_SOURCE_REALTIME_TIMESTAMP="), &value,
529 NULL);
530 if (r < 0)
531 return r;
532 assert(r > 0);
533
534 r = safe_atou64(value, &realtime);
535 if (r < 0)
536 log_debug_errno(r, "Failed to parse realtime timestamp: %m");
537 }
538
539 if (r < 0) {
540 r = sd_journal_get_realtime_usec(j, &realtime);
541 if (r < 0)
542 return log_full_errno(r == -EADDRNOTAVAIL ? LOG_DEBUG : LOG_ERR, r, "Failed to get realtime timestamp: %m");
543 }
544
545 r = sd_journal_get_cursor(j, &cursor);
546 if (r < 0)
547 return log_error_errno(r, "Failed to get cursor: %m");
548
549 timestamp = flags & OUTPUT_UTC ? format_timestamp_us_utc(ts, sizeof ts, realtime)
550 : format_timestamp_us(ts, sizeof ts, realtime);
551 fprintf(f, "%s [%s]\n",
552 timestamp ?: "(no timestamp)",
553 cursor);
554
555 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
556 const char *c;
557 int fieldlen;
558 const char *on = "", *off = "";
559
560 c = memchr(data, '=', length);
561 if (!c)
562 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
563 "Invalid field.");
564 fieldlen = c - (const char*) data;
565
566 r = field_set_test(output_fields, data, fieldlen);
567 if (r < 0)
568 return r;
569 if (!r)
570 continue;
571
572 if (flags & OUTPUT_COLOR && startswith(data, "MESSAGE=")) {
573 on = ANSI_HIGHLIGHT;
574 off = ANSI_NORMAL;
575 }
576
577 if ((flags & OUTPUT_SHOW_ALL) ||
578 (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH)
579 && utf8_is_printable(data, length))) {
580 fprintf(f, " %s%.*s=", on, fieldlen, (const char*)data);
581 print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, c + 1, length - fieldlen - 1, NULL);
582 fputs(off, f);
583 } else {
584 char bytes[FORMAT_BYTES_MAX];
585
586 fprintf(f, " %s%.*s=[%s blob data]%s\n",
587 on,
588 (int) (c - (const char*) data),
589 (const char*) data,
590 format_bytes(bytes, sizeof(bytes), length - (c - (const char *) data) - 1),
591 off);
592 }
593 }
594
595 if (r < 0)
596 return r;
597
598 if (flags & OUTPUT_CATALOG)
599 print_catalog(f, j);
600
601 return 0;
602 }
603
604 static int output_export(
605 FILE *f,
606 sd_journal *j,
607 OutputMode mode,
608 unsigned n_columns,
609 OutputFlags flags,
610 Set *output_fields,
611 const size_t highlight[2]) {
612
613 sd_id128_t boot_id;
614 char sid[33];
615 int r;
616 usec_t realtime, monotonic;
617 _cleanup_free_ char *cursor = NULL;
618 const void *data;
619 size_t length;
620
621 assert(j);
622
623 sd_journal_set_data_threshold(j, 0);
624
625 r = sd_journal_get_realtime_usec(j, &realtime);
626 if (r < 0)
627 return log_error_errno(r, "Failed to get realtime timestamp: %m");
628
629 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
630 if (r < 0)
631 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
632
633 r = sd_journal_get_cursor(j, &cursor);
634 if (r < 0)
635 return log_error_errno(r, "Failed to get cursor: %m");
636
637 fprintf(f,
638 "__CURSOR=%s\n"
639 "__REALTIME_TIMESTAMP="USEC_FMT"\n"
640 "__MONOTONIC_TIMESTAMP="USEC_FMT"\n"
641 "_BOOT_ID=%s\n",
642 cursor,
643 realtime,
644 monotonic,
645 sd_id128_to_string(boot_id, sid));
646
647 JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) {
648 const char *c;
649
650 /* We already printed the boot id from the data in the header, hence let's suppress it here */
651 if (memory_startswith(data, length, "_BOOT_ID="))
652 continue;
653
654 c = memchr(data, '=', length);
655 if (!c)
656 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
657 "Invalid field.");
658
659 r = field_set_test(output_fields, data, c - (const char *) data);
660 if (r < 0)
661 return r;
662 if (!r)
663 continue;
664
665 if (utf8_is_printable_newline(data, length, false))
666 fwrite(data, length, 1, f);
667 else {
668 uint64_t le64;
669
670 fwrite(data, c - (const char*) data, 1, f);
671 fputc('\n', f);
672 le64 = htole64(length - (c - (const char*) data) - 1);
673 fwrite(&le64, sizeof(le64), 1, f);
674 fwrite(c + 1, length - (c - (const char*) data) - 1, 1, f);
675 }
676
677 fputc('\n', f);
678 }
679 if (r == -EBADMSG) {
680 log_debug_errno(r, "Skipping message we can't read: %m");
681 return 0;
682 }
683
684 if (r < 0)
685 return r;
686
687 fputc('\n', f);
688
689 return 0;
690 }
691
692 void json_escape(
693 FILE *f,
694 const char* p,
695 size_t l,
696 OutputFlags flags) {
697
698 assert(f);
699 assert(p);
700
701 if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD)
702 fputs("null", f);
703
704 else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) {
705 bool not_first = false;
706
707 fputs("[ ", f);
708
709 while (l > 0) {
710 if (not_first)
711 fprintf(f, ", %u", (uint8_t) *p);
712 else {
713 not_first = true;
714 fprintf(f, "%u", (uint8_t) *p);
715 }
716
717 p++;
718 l--;
719 }
720
721 fputs(" ]", f);
722 } else {
723 fputc('\"', f);
724
725 while (l > 0) {
726 if (IN_SET(*p, '"', '\\')) {
727 fputc('\\', f);
728 fputc(*p, f);
729 } else if (*p == '\n')
730 fputs("\\n", f);
731 else if ((uint8_t) *p < ' ')
732 fprintf(f, "\\u%04x", (uint8_t) *p);
733 else
734 fputc(*p, f);
735
736 p++;
737 l--;
738 }
739
740 fputc('\"', f);
741 }
742 }
743
744 struct json_data {
745 JsonVariant* name;
746 size_t n_values;
747 JsonVariant* values[];
748 };
749
750 static int update_json_data(
751 Hashmap *h,
752 OutputFlags flags,
753 const char *name,
754 const void *value,
755 size_t size) {
756
757 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
758 struct json_data *d;
759 int r;
760
761 if (!(flags & OUTPUT_SHOW_ALL) && strlen(name) + 1 + size >= JSON_THRESHOLD)
762 r = json_variant_new_null(&v);
763 else if (utf8_is_printable(value, size))
764 r = json_variant_new_stringn(&v, value, size);
765 else
766 r = json_variant_new_array_bytes(&v, value, size);
767 if (r < 0)
768 return log_error_errno(r, "Failed to allocate JSON data: %m");
769
770 d = hashmap_get(h, name);
771 if (d) {
772 struct json_data *w;
773
774 w = realloc(d, offsetof(struct json_data, values) + sizeof(JsonVariant*) * (d->n_values + 1));
775 if (!w)
776 return log_oom();
777
778 d = w;
779 assert_se(hashmap_update(h, json_variant_string(d->name), d) >= 0);
780 } else {
781 _cleanup_(json_variant_unrefp) JsonVariant *n = NULL;
782
783 r = json_variant_new_string(&n, name);
784 if (r < 0)
785 return log_error_errno(r, "Failed to allocate JSON name variant: %m");
786
787 d = malloc0(offsetof(struct json_data, values) + sizeof(JsonVariant*));
788 if (!d)
789 return log_oom();
790
791 r = hashmap_put(h, json_variant_string(n), d);
792 if (r < 0) {
793 free(d);
794 return log_error_errno(r, "Failed to insert JSON name into hashmap: %m");
795 }
796
797 d->name = TAKE_PTR(n);
798 }
799
800 d->values[d->n_values++] = TAKE_PTR(v);
801 return 0;
802 }
803
804 static int update_json_data_split(
805 Hashmap *h,
806 OutputFlags flags,
807 Set *output_fields,
808 const void *data,
809 size_t size) {
810
811 const char *eq;
812 char *name;
813
814 assert(h);
815 assert(data || size == 0);
816
817 if (memory_startswith(data, size, "_BOOT_ID="))
818 return 0;
819
820 eq = memchr(data, '=', MIN(size, JSON_THRESHOLD));
821 if (!eq)
822 return 0;
823
824 if (eq == data)
825 return 0;
826
827 name = strndupa(data, eq - (const char*) data);
828 if (output_fields && !set_get(output_fields, name))
829 return 0;
830
831 return update_json_data(h, flags, name, eq + 1, size - (eq - (const char*) data) - 1);
832 }
833
834 static int output_json(
835 FILE *f,
836 sd_journal *j,
837 OutputMode mode,
838 unsigned n_columns,
839 OutputFlags flags,
840 Set *output_fields,
841 const size_t highlight[2]) {
842
843 char sid[SD_ID128_STRING_MAX], usecbuf[DECIMAL_STR_MAX(usec_t)];
844 _cleanup_(json_variant_unrefp) JsonVariant *object = NULL;
845 _cleanup_free_ char *cursor = NULL;
846 uint64_t realtime, monotonic;
847 JsonVariant **array = NULL;
848 struct json_data *d;
849 sd_id128_t boot_id;
850 Hashmap *h = NULL;
851 size_t n = 0;
852 Iterator i;
853 int r;
854
855 assert(j);
856
857 (void) sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD);
858
859 r = sd_journal_get_realtime_usec(j, &realtime);
860 if (r < 0)
861 return log_error_errno(r, "Failed to get realtime timestamp: %m");
862
863 r = sd_journal_get_monotonic_usec(j, &monotonic, &boot_id);
864 if (r < 0)
865 return log_error_errno(r, "Failed to get monotonic timestamp: %m");
866
867 r = sd_journal_get_cursor(j, &cursor);
868 if (r < 0)
869 return log_error_errno(r, "Failed to get cursor: %m");
870
871 h = hashmap_new(&string_hash_ops);
872 if (!h)
873 return log_oom();
874
875 r = update_json_data(h, flags, "__CURSOR", cursor, strlen(cursor));
876 if (r < 0)
877 goto finish;
878
879 xsprintf(usecbuf, USEC_FMT, realtime);
880 r = update_json_data(h, flags, "__REALTIME_TIMESTAMP", usecbuf, strlen(usecbuf));
881 if (r < 0)
882 goto finish;
883
884 xsprintf(usecbuf, USEC_FMT, monotonic);
885 r = update_json_data(h, flags, "__MONOTONIC_TIMESTAMP", usecbuf, strlen(usecbuf));
886 if (r < 0)
887 goto finish;
888
889 sd_id128_to_string(boot_id, sid);
890 r = update_json_data(h, flags, "_BOOT_ID", sid, strlen(sid));
891 if (r < 0)
892 goto finish;
893
894 for (;;) {
895 const void *data;
896 size_t size;
897
898 r = sd_journal_enumerate_data(j, &data, &size);
899 if (r == -EBADMSG) {
900 log_debug_errno(r, "Skipping message we can't read: %m");
901 r = 0;
902 goto finish;
903 }
904 if (r < 0) {
905 log_error_errno(r, "Failed to read journal: %m");
906 goto finish;
907 }
908 if (r == 0)
909 break;
910
911 r = update_json_data_split(h, flags, output_fields, data, size);
912 if (r < 0)
913 goto finish;
914 }
915
916 array = new(JsonVariant*, hashmap_size(h)*2);
917 if (!array) {
918 r = log_oom();
919 goto finish;
920 }
921
922 HASHMAP_FOREACH(d, h, i) {
923 assert(d->n_values > 0);
924
925 array[n++] = json_variant_ref(d->name);
926
927 if (d->n_values == 1)
928 array[n++] = json_variant_ref(d->values[0]);
929 else {
930 _cleanup_(json_variant_unrefp) JsonVariant *q = NULL;
931
932 r = json_variant_new_array(&q, d->values, d->n_values);
933 if (r < 0) {
934 log_error_errno(r, "Failed to create JSON array: %m");
935 goto finish;
936 }
937
938 array[n++] = TAKE_PTR(q);
939 }
940 }
941
942 r = json_variant_new_object(&object, array, n);
943 if (r < 0) {
944 log_error_errno(r, "Failed to allocate JSON object: %m");
945 goto finish;
946 }
947
948 json_variant_dump(object,
949 output_mode_to_json_format_flags(mode) |
950 (FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0),
951 f, NULL);
952
953 r = 0;
954
955 finish:
956 while ((d = hashmap_steal_first(h))) {
957 size_t k;
958
959 json_variant_unref(d->name);
960 for (k = 0; k < d->n_values; k++)
961 json_variant_unref(d->values[k]);
962
963 free(d);
964 }
965
966 hashmap_free(h);
967
968 json_variant_unref_many(array, n);
969 free(array);
970
971 return r;
972 }
973
974 static int output_cat(
975 FILE *f,
976 sd_journal *j,
977 OutputMode mode,
978 unsigned n_columns,
979 OutputFlags flags,
980 Set *output_fields,
981 const size_t highlight[2]) {
982
983 const void *data;
984 size_t l;
985 int r;
986 const char *highlight_on = "", *highlight_off = "";
987
988 assert(j);
989 assert(f);
990
991 if (flags & OUTPUT_COLOR) {
992 highlight_on = ANSI_HIGHLIGHT_RED;
993 highlight_off = ANSI_NORMAL;
994 }
995
996 sd_journal_set_data_threshold(j, 0);
997
998 r = sd_journal_get_data(j, "MESSAGE", &data, &l);
999 if (r == -EBADMSG) {
1000 log_debug_errno(r, "Skipping message we can't read: %m");
1001 return 0;
1002 }
1003 if (r < 0) {
1004 /* An entry without MESSAGE=? */
1005 if (r == -ENOENT)
1006 return 0;
1007
1008 return log_error_errno(r, "Failed to get data: %m");
1009 }
1010
1011 assert(l >= 8);
1012
1013 if (highlight && (flags & OUTPUT_COLOR)) {
1014 assert(highlight[0] <= highlight[1]);
1015 assert(highlight[1] <= l - 8);
1016
1017 fwrite((const char*) data + 8, 1, highlight[0], f);
1018 fwrite(highlight_on, 1, strlen(highlight_on), f);
1019 fwrite((const char*) data + 8 + highlight[0], 1, highlight[1] - highlight[0], f);
1020 fwrite(highlight_off, 1, strlen(highlight_off), f);
1021 fwrite((const char*) data + 8 + highlight[1], 1, l - 8 - highlight[1], f);
1022 } else
1023 fwrite((const char*) data + 8, 1, l - 8, f);
1024 fputc('\n', f);
1025
1026 return 0;
1027 }
1028
1029 static int (*output_funcs[_OUTPUT_MODE_MAX])(
1030 FILE *f,
1031 sd_journal *j,
1032 OutputMode mode,
1033 unsigned n_columns,
1034 OutputFlags flags,
1035 Set *output_fields,
1036 const size_t highlight[2]) = {
1037
1038 [OUTPUT_SHORT] = output_short,
1039 [OUTPUT_SHORT_ISO] = output_short,
1040 [OUTPUT_SHORT_ISO_PRECISE] = output_short,
1041 [OUTPUT_SHORT_PRECISE] = output_short,
1042 [OUTPUT_SHORT_MONOTONIC] = output_short,
1043 [OUTPUT_SHORT_UNIX] = output_short,
1044 [OUTPUT_SHORT_FULL] = output_short,
1045 [OUTPUT_VERBOSE] = output_verbose,
1046 [OUTPUT_EXPORT] = output_export,
1047 [OUTPUT_JSON] = output_json,
1048 [OUTPUT_JSON_PRETTY] = output_json,
1049 [OUTPUT_JSON_SSE] = output_json,
1050 [OUTPUT_JSON_SEQ] = output_json,
1051 [OUTPUT_CAT] = output_cat,
1052 [OUTPUT_WITH_UNIT] = output_short,
1053 };
1054
1055 int show_journal_entry(
1056 FILE *f,
1057 sd_journal *j,
1058 OutputMode mode,
1059 unsigned n_columns,
1060 OutputFlags flags,
1061 char **output_fields,
1062 const size_t highlight[2],
1063 bool *ellipsized) {
1064
1065 int ret;
1066 _cleanup_set_free_free_ Set *fields = NULL;
1067 assert(mode >= 0);
1068 assert(mode < _OUTPUT_MODE_MAX);
1069
1070 if (n_columns <= 0)
1071 n_columns = columns();
1072
1073 if (output_fields) {
1074 fields = set_new(&string_hash_ops);
1075 if (!fields)
1076 return log_oom();
1077
1078 ret = set_put_strdupv(fields, output_fields);
1079 if (ret < 0)
1080 return ret;
1081 }
1082
1083 ret = output_funcs[mode](f, j, mode, n_columns, flags, fields, highlight);
1084
1085 if (ellipsized && ret > 0)
1086 *ellipsized = true;
1087
1088 return ret;
1089 }
1090
1091 static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) {
1092 assert(f);
1093 assert(flags);
1094
1095 if (!(*flags & OUTPUT_BEGIN_NEWLINE))
1096 return 0;
1097
1098 /* Print a beginning new line if that's request, but only once
1099 * on the first line we print. */
1100
1101 fputc('\n', f);
1102 *flags &= ~OUTPUT_BEGIN_NEWLINE;
1103 return 0;
1104 }
1105
1106 int show_journal(
1107 FILE *f,
1108 sd_journal *j,
1109 OutputMode mode,
1110 unsigned n_columns,
1111 usec_t not_before,
1112 unsigned how_many,
1113 OutputFlags flags,
1114 bool *ellipsized) {
1115
1116 int r;
1117 unsigned line = 0;
1118 bool need_seek = false;
1119 int warn_cutoff = flags & OUTPUT_WARN_CUTOFF;
1120
1121 assert(j);
1122 assert(mode >= 0);
1123 assert(mode < _OUTPUT_MODE_MAX);
1124
1125 if (how_many == (unsigned) -1)
1126 need_seek = true;
1127 else {
1128 /* Seek to end */
1129 r = sd_journal_seek_tail(j);
1130 if (r < 0)
1131 return log_error_errno(r, "Failed to seek to tail: %m");
1132
1133 r = sd_journal_previous_skip(j, how_many);
1134 if (r < 0)
1135 return log_error_errno(r, "Failed to skip previous: %m");
1136 }
1137
1138 for (;;) {
1139 for (;;) {
1140 usec_t usec;
1141
1142 if (need_seek) {
1143 r = sd_journal_next(j);
1144 if (r < 0)
1145 return log_error_errno(r, "Failed to iterate through journal: %m");
1146 }
1147
1148 if (r == 0)
1149 break;
1150
1151 need_seek = true;
1152
1153 if (not_before > 0) {
1154 r = sd_journal_get_monotonic_usec(j, &usec, NULL);
1155
1156 /* -ESTALE is returned if the
1157 timestamp is not from this boot */
1158 if (r == -ESTALE)
1159 continue;
1160 else if (r < 0)
1161 return log_error_errno(r, "Failed to get journal time: %m");
1162
1163 if (usec < not_before)
1164 continue;
1165 }
1166
1167 line++;
1168 maybe_print_begin_newline(f, &flags);
1169
1170 r = show_journal_entry(f, j, mode, n_columns, flags, NULL, NULL, ellipsized);
1171 if (r < 0)
1172 return r;
1173 }
1174
1175 if (warn_cutoff && line < how_many && not_before > 0) {
1176 sd_id128_t boot_id;
1177 usec_t cutoff = 0;
1178
1179 /* Check whether the cutoff line is too early */
1180
1181 r = sd_id128_get_boot(&boot_id);
1182 if (r < 0)
1183 return log_error_errno(r, "Failed to get boot id: %m");
1184
1185 r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL);
1186 if (r < 0)
1187 return log_error_errno(r, "Failed to get journal cutoff time: %m");
1188
1189 if (r > 0 && not_before < cutoff) {
1190 maybe_print_begin_newline(f, &flags);
1191 fprintf(f, "Warning: Journal has been rotated since unit was started. Log output is incomplete or unavailable.\n");
1192 }
1193
1194 warn_cutoff = false;
1195 }
1196
1197 if (!(flags & OUTPUT_FOLLOW))
1198 break;
1199
1200 r = sd_journal_wait(j, USEC_INFINITY);
1201 if (r < 0)
1202 return log_error_errno(r, "Failed to wait for journal: %m");
1203
1204 }
1205
1206 return 0;
1207 }
1208
1209 int add_matches_for_unit(sd_journal *j, const char *unit) {
1210 const char *m1, *m2, *m3, *m4;
1211 int r;
1212
1213 assert(j);
1214 assert(unit);
1215
1216 m1 = strjoina("_SYSTEMD_UNIT=", unit);
1217 m2 = strjoina("COREDUMP_UNIT=", unit);
1218 m3 = strjoina("UNIT=", unit);
1219 m4 = strjoina("OBJECT_SYSTEMD_UNIT=", unit);
1220
1221 (void)(
1222 /* Look for messages from the service itself */
1223 (r = sd_journal_add_match(j, m1, 0)) ||
1224
1225 /* Look for coredumps of the service */
1226 (r = sd_journal_add_disjunction(j)) ||
1227 (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", 0)) ||
1228 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1229 (r = sd_journal_add_match(j, m2, 0)) ||
1230
1231 /* Look for messages from PID 1 about this service */
1232 (r = sd_journal_add_disjunction(j)) ||
1233 (r = sd_journal_add_match(j, "_PID=1", 0)) ||
1234 (r = sd_journal_add_match(j, m3, 0)) ||
1235
1236 /* Look for messages from authorized daemons about this service */
1237 (r = sd_journal_add_disjunction(j)) ||
1238 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1239 (r = sd_journal_add_match(j, m4, 0))
1240 );
1241
1242 if (r == 0 && endswith(unit, ".slice")) {
1243 const char *m5;
1244
1245 m5 = strjoina("_SYSTEMD_SLICE=", unit);
1246
1247 /* Show all messages belonging to a slice */
1248 (void)(
1249 (r = sd_journal_add_disjunction(j)) ||
1250 (r = sd_journal_add_match(j, m5, 0))
1251 );
1252 }
1253
1254 return r;
1255 }
1256
1257 int add_matches_for_user_unit(sd_journal *j, const char *unit, uid_t uid) {
1258 int r;
1259 char *m1, *m2, *m3, *m4;
1260 char muid[sizeof("_UID=") + DECIMAL_STR_MAX(uid_t)];
1261
1262 assert(j);
1263 assert(unit);
1264
1265 m1 = strjoina("_SYSTEMD_USER_UNIT=", unit);
1266 m2 = strjoina("USER_UNIT=", unit);
1267 m3 = strjoina("COREDUMP_USER_UNIT=", unit);
1268 m4 = strjoina("OBJECT_SYSTEMD_USER_UNIT=", unit);
1269 sprintf(muid, "_UID="UID_FMT, uid);
1270
1271 (void) (
1272 /* Look for messages from the user service itself */
1273 (r = sd_journal_add_match(j, m1, 0)) ||
1274 (r = sd_journal_add_match(j, muid, 0)) ||
1275
1276 /* Look for messages from systemd about this service */
1277 (r = sd_journal_add_disjunction(j)) ||
1278 (r = sd_journal_add_match(j, m2, 0)) ||
1279 (r = sd_journal_add_match(j, muid, 0)) ||
1280
1281 /* Look for coredumps of the service */
1282 (r = sd_journal_add_disjunction(j)) ||
1283 (r = sd_journal_add_match(j, m3, 0)) ||
1284 (r = sd_journal_add_match(j, muid, 0)) ||
1285 (r = sd_journal_add_match(j, "_UID=0", 0)) ||
1286
1287 /* Look for messages from authorized daemons about this service */
1288 (r = sd_journal_add_disjunction(j)) ||
1289 (r = sd_journal_add_match(j, m4, 0)) ||
1290 (r = sd_journal_add_match(j, muid, 0)) ||
1291 (r = sd_journal_add_match(j, "_UID=0", 0))
1292 );
1293
1294 if (r == 0 && endswith(unit, ".slice")) {
1295 const char *m5;
1296
1297 m5 = strjoina("_SYSTEMD_SLICE=", unit);
1298
1299 /* Show all messages belonging to a slice */
1300 (void)(
1301 (r = sd_journal_add_disjunction(j)) ||
1302 (r = sd_journal_add_match(j, m5, 0)) ||
1303 (r = sd_journal_add_match(j, muid, 0))
1304 );
1305 }
1306
1307 return r;
1308 }
1309
1310 static int get_boot_id_for_machine(const char *machine, sd_id128_t *boot_id) {
1311 _cleanup_close_pair_ int pair[2] = { -1, -1 };
1312 _cleanup_close_ int pidnsfd = -1, mntnsfd = -1, rootfd = -1;
1313 pid_t pid, child;
1314 char buf[37];
1315 ssize_t k;
1316 int r;
1317
1318 assert(machine);
1319 assert(boot_id);
1320
1321 if (!machine_name_is_valid(machine))
1322 return -EINVAL;
1323
1324 r = container_get_leader(machine, &pid);
1325 if (r < 0)
1326 return r;
1327
1328 r = namespace_open(pid, &pidnsfd, &mntnsfd, NULL, NULL, &rootfd);
1329 if (r < 0)
1330 return r;
1331
1332 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, pair) < 0)
1333 return -errno;
1334
1335 r = namespace_fork("(sd-bootidns)", "(sd-bootid)", NULL, 0, FORK_RESET_SIGNALS|FORK_DEATHSIG,
1336 pidnsfd, mntnsfd, -1, -1, rootfd, &child);
1337 if (r < 0)
1338 return r;
1339 if (r == 0) {
1340 int fd;
1341
1342 pair[0] = safe_close(pair[0]);
1343
1344 fd = open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_CLOEXEC|O_NOCTTY);
1345 if (fd < 0)
1346 _exit(EXIT_FAILURE);
1347
1348 r = loop_read_exact(fd, buf, 36, false);
1349 safe_close(fd);
1350 if (r < 0)
1351 _exit(EXIT_FAILURE);
1352
1353 k = send(pair[1], buf, 36, MSG_NOSIGNAL);
1354 if (k != 36)
1355 _exit(EXIT_FAILURE);
1356
1357 _exit(EXIT_SUCCESS);
1358 }
1359
1360 pair[1] = safe_close(pair[1]);
1361
1362 r = wait_for_terminate_and_check("(sd-bootidns)", child, 0);
1363 if (r < 0)
1364 return r;
1365 if (r != EXIT_SUCCESS)
1366 return -EIO;
1367
1368 k = recv(pair[0], buf, 36, 0);
1369 if (k != 36)
1370 return -EIO;
1371
1372 buf[36] = 0;
1373 r = sd_id128_from_string(buf, boot_id);
1374 if (r < 0)
1375 return r;
1376
1377 return 0;
1378 }
1379
1380 int add_match_this_boot(sd_journal *j, const char *machine) {
1381 char match[9+32+1] = "_BOOT_ID=";
1382 sd_id128_t boot_id;
1383 int r;
1384
1385 assert(j);
1386
1387 if (machine) {
1388 r = get_boot_id_for_machine(machine, &boot_id);
1389 if (r < 0)
1390 return log_error_errno(r, "Failed to get boot id of container %s: %m", machine);
1391 } else {
1392 r = sd_id128_get_boot(&boot_id);
1393 if (r < 0)
1394 return log_error_errno(r, "Failed to get boot id: %m");
1395 }
1396
1397 sd_id128_to_string(boot_id, match + 9);
1398 r = sd_journal_add_match(j, match, strlen(match));
1399 if (r < 0)
1400 return log_error_errno(r, "Failed to add match: %m");
1401
1402 r = sd_journal_add_conjunction(j);
1403 if (r < 0)
1404 return log_error_errno(r, "Failed to add conjunction: %m");
1405
1406 return 0;
1407 }
1408
1409 int show_journal_by_unit(
1410 FILE *f,
1411 const char *unit,
1412 OutputMode mode,
1413 unsigned n_columns,
1414 usec_t not_before,
1415 unsigned how_many,
1416 uid_t uid,
1417 OutputFlags flags,
1418 int journal_open_flags,
1419 bool system_unit,
1420 bool *ellipsized) {
1421
1422 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
1423 int r;
1424
1425 assert(mode >= 0);
1426 assert(mode < _OUTPUT_MODE_MAX);
1427 assert(unit);
1428
1429 if (how_many <= 0)
1430 return 0;
1431
1432 r = sd_journal_open(&j, journal_open_flags);
1433 if (r < 0)
1434 return log_error_errno(r, "Failed to open journal: %m");
1435
1436 r = add_match_this_boot(j, NULL);
1437 if (r < 0)
1438 return r;
1439
1440 if (system_unit)
1441 r = add_matches_for_unit(j, unit);
1442 else
1443 r = add_matches_for_user_unit(j, unit, uid);
1444 if (r < 0)
1445 return log_error_errno(r, "Failed to add unit matches: %m");
1446
1447 if (DEBUG_LOGGING) {
1448 _cleanup_free_ char *filter;
1449
1450 filter = journal_make_match_string(j);
1451 if (!filter)
1452 return log_oom();
1453
1454 log_debug("Journal filter: %s", filter);
1455 }
1456
1457 return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized);
1458 }