]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
86aa7ba4 | 2 | |
86aa7ba4 | 3 | #include <errno.h> |
b6741478 | 4 | #include <fcntl.h> |
a8fbdf54 TA |
5 | #include <signal.h> |
6 | #include <stdint.h> | |
7 | #include <stdlib.h> | |
a8fbdf54 | 8 | #include <syslog.h> |
a8fbdf54 TA |
9 | #include <unistd.h> |
10 | ||
11 | #include "sd-id128.h" | |
12 | #include "sd-journal.h" | |
86aa7ba4 | 13 | |
b5efdb8a | 14 | #include "alloc-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
f97b34a6 | 16 | #include "format-util.h" |
d8e32c47 | 17 | #include "glyph-util.h" |
d99ae53a | 18 | #include "hashmap.h" |
07630cea | 19 | #include "hostname-util.h" |
b5ea030d | 20 | #include "id128-util.h" |
c004493c | 21 | #include "io-util.h" |
dfb33a97 | 22 | #include "journal-internal.h" |
1a8f0ce6 | 23 | #include "journal-util.h" |
8e044443 | 24 | #include "json.h" |
2108b567 | 25 | #include "locale-util.h" |
07630cea | 26 | #include "log.h" |
3ffd4af2 | 27 | #include "logs-show.h" |
a8fbdf54 TA |
28 | #include "macro.h" |
29 | #include "output-mode.h" | |
6bedfcbb | 30 | #include "parse-util.h" |
76f7cc3f | 31 | #include "pretty-print.h" |
a8fbdf54 | 32 | #include "sparse-endian.h" |
29a753df | 33 | #include "stdio-util.h" |
8b43440b | 34 | #include "string-table.h" |
07630cea | 35 | #include "string-util.h" |
cc25a67e | 36 | #include "strv.h" |
288a74cc | 37 | #include "terminal-util.h" |
a8fbdf54 | 38 | #include "time-util.h" |
07630cea | 39 | #include "utf8.h" |
2108b567 | 40 | #include "web-util.h" |
86aa7ba4 | 41 | |
07630cea | 42 | /* up to three lines (each up to 100 characters) or 300 characters, whichever is less */ |
a6f0104a ZJS |
43 | #define PRINT_LINE_THRESHOLD 3 |
44 | #define PRINT_CHAR_THRESHOLD 300 | |
45 | ||
8e044443 | 46 | #define JSON_THRESHOLD 4096U |
86aa7ba4 | 47 | |
d4205751 | 48 | static int print_catalog(FILE *f, sd_journal *j) { |
d4205751 | 49 | _cleanup_free_ char *t = NULL, *z = NULL; |
ee5c1311 | 50 | const char *newline, *prefix; |
2108b567 LP |
51 | int r; |
52 | ||
53 | assert(j); | |
d4205751 | 54 | |
d4205751 | 55 | r = sd_journal_get_catalog(j, &t); |
2108b567 LP |
56 | if (r == -ENOENT) |
57 | return 0; | |
d4205751 | 58 | if (r < 0) |
2108b567 | 59 | return log_error_errno(r, "Failed to find catalog entry: %m"); |
d4205751 | 60 | |
ee5c1311 LP |
61 | if (is_locale_utf8()) |
62 | prefix = strjoina(special_glyph(SPECIAL_GLYPH_LIGHT_SHADE), special_glyph(SPECIAL_GLYPH_LIGHT_SHADE)); | |
63 | else | |
64 | prefix = "--"; | |
65 | ||
cd7ae1b4 | 66 | newline = strjoina(ansi_normal(), "\n", ansi_grey(), prefix, ansi_normal(), " ", ansi_green()); |
ee5c1311 LP |
67 | |
68 | z = strreplace(strstrip(t), "\n", newline); | |
d4205751 LP |
69 | if (!z) |
70 | return log_oom(); | |
71 | ||
cd7ae1b4 | 72 | fprintf(f, "%s%s %s%s", ansi_grey(), prefix, ansi_normal(), ansi_green()); |
d4205751 | 73 | fputs(z, f); |
cd7ae1b4 | 74 | fprintf(f, "%s\n", ansi_normal()); |
d4205751 | 75 | |
2108b567 LP |
76 | return 1; |
77 | } | |
78 | ||
79 | static int url_from_catalog(sd_journal *j, char **ret) { | |
80 | _cleanup_free_ char *t = NULL, *url = NULL; | |
81 | const char *weblink; | |
82 | int r; | |
83 | ||
84 | assert(j); | |
85 | assert(ret); | |
86 | ||
87 | r = sd_journal_get_catalog(j, &t); | |
88 | if (r == -ENOENT) | |
89 | goto notfound; | |
90 | if (r < 0) | |
91 | return log_error_errno(r, "Failed to find catalog entry: %m"); | |
92 | ||
50ed5cbf LP |
93 | weblink = find_line_startswith(t, "Documentation:"); |
94 | if (!weblink) | |
95 | goto notfound; | |
2108b567 LP |
96 | |
97 | /* Skip whitespace to value */ | |
98 | weblink += strspn(weblink, " \t"); | |
99 | ||
100 | /* Cut out till next whitespace/newline */ | |
e8bec624 | 101 | url = strdupcspn(weblink, WHITESPACE); |
2108b567 LP |
102 | if (!url) |
103 | return log_oom(); | |
104 | ||
105 | if (!documentation_url_is_valid(url)) | |
106 | goto notfound; | |
107 | ||
108 | *ret = TAKE_PTR(url); | |
109 | return 1; | |
110 | ||
111 | notfound: | |
112 | *ret = NULL; | |
d4205751 LP |
113 | return 0; |
114 | } | |
115 | ||
b1cc0822 LP |
116 | static int parse_field( |
117 | const void *data, | |
118 | size_t length, | |
119 | const char *field, | |
120 | size_t field_len, | |
121 | char **target, | |
122 | size_t *target_len) { | |
123 | ||
ed054520 | 124 | size_t nl; |
d813d7a3 | 125 | char *buf; |
55d7bfc1 LP |
126 | |
127 | assert(data); | |
128 | assert(field); | |
129 | assert(target); | |
55d7bfc1 | 130 | |
ed054520 | 131 | if (length < field_len) |
55d7bfc1 LP |
132 | return 0; |
133 | ||
ed054520 | 134 | if (memcmp(data, field, field_len)) |
55d7bfc1 LP |
135 | return 0; |
136 | ||
ed054520 | 137 | nl = length - field_len; |
c165d97d | 138 | |
ed054520 | 139 | buf = newdup_suffix0(char, (const char*) data + field_len, nl); |
0d0f0c50 SL |
140 | if (!buf) |
141 | return log_oom(); | |
55d7bfc1 | 142 | |
b1cc0822 | 143 | free_and_replace(*target, buf); |
d813d7a3 | 144 | |
ed054520 VC |
145 | if (target_len) |
146 | *target_len = nl; | |
55d7bfc1 LP |
147 | |
148 | return 1; | |
149 | } | |
150 | ||
ed054520 VC |
151 | typedef struct ParseFieldVec { |
152 | const char *field; | |
153 | size_t field_len; | |
154 | char **target; | |
155 | size_t *target_len; | |
156 | } ParseFieldVec; | |
157 | ||
e3eec1fd LP |
158 | #define PARSE_FIELD_VEC_ENTRY(_field, _target, _target_len) { \ |
159 | .field = _field, \ | |
160 | .field_len = strlen(_field), \ | |
161 | .target = _target, \ | |
162 | .target_len = _target_len \ | |
163 | } | |
ed054520 | 164 | |
b1cc0822 LP |
165 | static int parse_fieldv( |
166 | const void *data, | |
167 | size_t length, | |
168 | const ParseFieldVec *fields, | |
169 | size_t n_fields) { | |
170 | ||
171 | int r; | |
ed054520 | 172 | |
b1cc0822 | 173 | for (size_t i = 0; i < n_fields; i++) { |
ed054520 | 174 | const ParseFieldVec *f = &fields[i]; |
ed054520 VC |
175 | |
176 | r = parse_field(data, length, f->field, f->field_len, f->target, f->target_len); | |
177 | if (r < 0) | |
178 | return r; | |
b1cc0822 | 179 | if (r > 0) |
ed054520 VC |
180 | break; |
181 | } | |
182 | ||
183 | return 0; | |
184 | } | |
185 | ||
2f063186 ZJS |
186 | static int field_set_test(const Set *fields, const char *name, size_t n) { |
187 | char *s; | |
cc25a67e LK |
188 | |
189 | if (!fields) | |
190 | return 1; | |
191 | ||
2f82562b | 192 | s = strndupa_safe(name, n); |
54ff74d2 | 193 | return set_contains(fields, s); |
cc25a67e LK |
194 | } |
195 | ||
08ace05b LP |
196 | static bool shall_print(const char *p, size_t l, OutputFlags flags) { |
197 | assert(p); | |
198 | ||
199 | if (flags & OUTPUT_SHOW_ALL) | |
55d7bfc1 LP |
200 | return true; |
201 | ||
a6f0104a | 202 | if (l >= PRINT_CHAR_THRESHOLD) |
55d7bfc1 LP |
203 | return false; |
204 | ||
31f7bf19 | 205 | if (!utf8_is_printable(p, l)) |
55d7bfc1 LP |
206 | return false; |
207 | ||
208 | return true; | |
209 | } | |
210 | ||
b4766d5f ZJS |
211 | static bool print_multiline( |
212 | FILE *f, | |
213 | unsigned prefix, | |
214 | unsigned n_columns, | |
215 | OutputFlags flags, | |
216 | int priority, | |
194da5ca | 217 | bool audit, |
b4766d5f ZJS |
218 | const char* message, |
219 | size_t message_len, | |
05c7d9bf | 220 | size_t highlight[2]) { |
b4766d5f ZJS |
221 | |
222 | const char *color_on = "", *color_off = "", *highlight_on = ""; | |
31f7bf19 | 223 | const char *pos, *end; |
94e0bd7d | 224 | bool ellipsized = false; |
a6f0104a | 225 | int line = 0; |
31f7bf19 | 226 | |
194da5ca | 227 | if (flags & OUTPUT_COLOR) { |
37b8d2f6 | 228 | get_log_colors(priority, &color_on, &color_off, &highlight_on); |
31f7bf19 | 229 | |
194da5ca ZJS |
230 | if (audit && strempty(color_on)) { |
231 | color_on = ANSI_BLUE; | |
232 | color_off = ANSI_NORMAL; | |
233 | } | |
234 | } | |
235 | ||
47d80904 UU |
236 | /* A special case: make sure that we print a newline when |
237 | the message is empty. */ | |
238 | if (message_len == 0) | |
239 | fputs("\n", f); | |
240 | ||
a6f0104a ZJS |
241 | for (pos = message; |
242 | pos < message + message_len; | |
243 | pos = end + 1, line++) { | |
a6f0104a | 244 | bool tail_line; |
f996072f | 245 | int len, indent = (line > 0) * prefix; |
31f7bf19 ZJS |
246 | for (end = pos; end < message + message_len && *end != '\n'; end++) |
247 | ; | |
248 | len = end - pos; | |
249 | assert(len >= 0); | |
250 | ||
2526d626 | 251 | /* We need to figure out when we are showing not-last line, *and* |
a6f0104a ZJS |
252 | * will skip subsequent lines. In that case, we will put the dots |
253 | * at the end of the line, instead of putting dots in the middle | |
254 | * or not at all. | |
255 | */ | |
256 | tail_line = | |
257 | line + 1 == PRINT_LINE_THRESHOLD || | |
2526d626 | 258 | end + 1 >= message + PRINT_CHAR_THRESHOLD; |
a6f0104a ZJS |
259 | |
260 | if (flags & (OUTPUT_FULL_WIDTH | OUTPUT_SHOW_ALL) || | |
261 | (prefix + len + 1 < n_columns && !tail_line)) { | |
b4766d5f ZJS |
262 | if (highlight && |
263 | (size_t) (pos - message) <= highlight[0] && | |
264 | highlight[0] < (size_t) len) { | |
265 | ||
266 | fprintf(f, "%*s%s%.*s", | |
f996072f | 267 | indent, "", |
b4766d5f ZJS |
268 | color_on, (int) highlight[0], pos); |
269 | fprintf(f, "%s%.*s", | |
270 | highlight_on, | |
271 | (int) (MIN((size_t) len, highlight[1]) - highlight[0]), | |
272 | pos + highlight[0]); | |
273 | if ((size_t) len > highlight[1]) | |
274 | fprintf(f, "%s%.*s", | |
275 | color_on, | |
276 | (int) (len - highlight[1]), | |
277 | pos + highlight[1]); | |
278 | fprintf(f, "%s\n", color_off); | |
279 | ||
280 | } else | |
281 | fprintf(f, "%*s%s%.*s%s\n", | |
f996072f | 282 | indent, "", |
b4766d5f | 283 | color_on, len, pos, color_off); |
a6f0104a ZJS |
284 | continue; |
285 | } | |
31f7bf19 | 286 | |
a6f0104a ZJS |
287 | /* Beyond this point, ellipsization will happen. */ |
288 | ellipsized = true; | |
31f7bf19 | 289 | |
a6f0104a ZJS |
290 | if (prefix < n_columns && n_columns - prefix >= 3) { |
291 | if (n_columns - prefix > (unsigned) len + 3) | |
292 | fprintf(f, "%*s%s%.*s...%s\n", | |
f996072f | 293 | indent, "", |
b4b02cbe | 294 | color_on, len, pos, color_off); |
a6f0104a | 295 | else { |
c2b2df60 | 296 | _cleanup_free_ char *e = NULL; |
a6f0104a ZJS |
297 | |
298 | e = ellipsize_mem(pos, len, n_columns - prefix, | |
299 | tail_line ? 100 : 90); | |
300 | if (!e) | |
301 | fprintf(f, "%*s%s%.*s%s\n", | |
f996072f | 302 | indent, "", |
a6f0104a ZJS |
303 | color_on, len, pos, color_off); |
304 | else | |
305 | fprintf(f, "%*s%s%s%s\n", | |
f996072f | 306 | indent, "", |
a6f0104a ZJS |
307 | color_on, e, color_off); |
308 | } | |
309 | } else | |
31f7bf19 ZJS |
310 | fputs("...\n", f); |
311 | ||
a6f0104a ZJS |
312 | if (tail_line) |
313 | break; | |
31f7bf19 | 314 | } |
94e0bd7d ZJS |
315 | |
316 | return ellipsized; | |
31f7bf19 ZJS |
317 | } |
318 | ||
893bcd3d | 319 | static int output_timestamp_monotonic( |
b1cc0822 LP |
320 | FILE *f, |
321 | OutputMode mode, | |
4e30b87d | 322 | const dual_timestamp *display_ts, |
893bcd3d | 323 | const sd_id128_t *boot_id, |
4e30b87d | 324 | const dual_timestamp *previous_display_ts, |
893bcd3d DB |
325 | const sd_id128_t *previous_boot_id) { |
326 | ||
327 | int written_chars = 0; | |
328 | ||
29a753df | 329 | assert(f); |
4e30b87d | 330 | assert(display_ts); |
893bcd3d | 331 | assert(boot_id); |
4e30b87d | 332 | assert(previous_display_ts); |
893bcd3d | 333 | assert(previous_boot_id); |
29a753df | 334 | |
4e30b87d | 335 | if (!VALID_MONOTONIC(display_ts->monotonic)) |
275e6be0 | 336 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No valid monotonic timestamp available"); |
29a753df | 337 | |
4e30b87d | 338 | written_chars += fprintf(f, "[%5"PRI_USEC".%06"PRI_USEC, display_ts->monotonic / USEC_PER_SEC, display_ts->monotonic % USEC_PER_SEC); |
893bcd3d DB |
339 | |
340 | if (mode == OUTPUT_SHORT_DELTA) { | |
341 | uint64_t delta; | |
342 | bool reliable_ts = true; | |
343 | ||
4e30b87d LP |
344 | if (VALID_MONOTONIC(previous_display_ts->monotonic) && sd_id128_equal(*boot_id, *previous_boot_id)) |
345 | delta = usec_sub_unsigned(display_ts->monotonic, previous_display_ts->monotonic); | |
346 | else if (VALID_REALTIME(display_ts->realtime) && VALID_REALTIME(previous_display_ts->realtime)) { | |
347 | delta = usec_sub_unsigned(display_ts->realtime, previous_display_ts->realtime); | |
893bcd3d DB |
348 | reliable_ts = false; |
349 | } else { | |
350 | written_chars += fprintf(f, "%16s", ""); | |
351 | goto finish; | |
352 | } | |
353 | ||
354 | written_chars += fprintf(f, " <%5"PRI_USEC".%06"PRI_USEC"%s>", delta / USEC_PER_SEC, delta % USEC_PER_SEC, reliable_ts ? " " : "*"); | |
355 | } | |
356 | ||
357 | finish: | |
358 | written_chars += fprintf(f, "%s", "]"); | |
893bcd3d | 359 | return written_chars; |
29a753df LP |
360 | } |
361 | ||
275e6be0 DB |
362 | static int output_timestamp_realtime( |
363 | FILE *f, | |
364 | sd_journal *j, | |
365 | OutputMode mode, | |
366 | OutputFlags flags, | |
6ed286d2 | 367 | usec_t usec) { |
275e6be0 | 368 | |
89eb3d7c | 369 | char buf[CONST_MAX(FORMAT_TIMESTAMP_MAX, 64U)]; |
29a753df LP |
370 | |
371 | assert(f); | |
372 | assert(j); | |
373 | ||
6ed286d2 YW |
374 | if (!VALID_REALTIME(usec)) |
375 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No valid realtime timestamp available."); | |
29a753df | 376 | |
49805b3d | 377 | if (IN_SET(mode, OUTPUT_SHORT_FULL, OUTPUT_WITH_UNIT)) { |
29a753df LP |
378 | const char *k; |
379 | ||
380 | if (flags & OUTPUT_UTC) | |
6ed286d2 | 381 | k = format_timestamp_style(buf, sizeof(buf), usec, TIMESTAMP_UTC); |
29a753df | 382 | else |
6ed286d2 | 383 | k = format_timestamp(buf, sizeof(buf), usec); |
baaa35ad ZJS |
384 | if (!k) |
385 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), | |
6ed286d2 | 386 | "Failed to format timestamp: %" PRIu64, usec); |
29a753df LP |
387 | |
388 | } else { | |
f9c443b7 YW |
389 | struct tm tm; |
390 | time_t t; | |
7e563bfc | 391 | |
6ed286d2 | 392 | t = (time_t) (usec / USEC_PER_SEC); |
29a753df LP |
393 | |
394 | switch (mode) { | |
395 | ||
396 | case OUTPUT_SHORT_UNIX: | |
6ed286d2 | 397 | xsprintf(buf, "%10"PRI_TIME".%06"PRIu64, t, usec % USEC_PER_SEC); |
29a753df LP |
398 | break; |
399 | ||
400 | case OUTPUT_SHORT_ISO: | |
0693e6b2 | 401 | case OUTPUT_SHORT_ISO_PRECISE: { |
402 | size_t tail = strftime(buf, sizeof(buf), "%Y-%m-%dT%H:%M:%S", | |
403 | localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)); | |
404 | if (tail == 0) | |
6ed286d2 | 405 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format ISO time."); |
7e563bfc | 406 | |
0693e6b2 | 407 | /* No usec in strftime, need to append */ |
408 | if (mode == OUTPUT_SHORT_ISO_PRECISE) { | |
6ed286d2 | 409 | assert_se(snprintf_ok(buf + tail, ELEMENTSOF(buf) - tail, ".%06"PRI_USEC, usec % USEC_PER_SEC)); |
0693e6b2 | 410 | tail += 7; |
411 | } | |
f9c443b7 | 412 | |
e2e01259 | 413 | int h = tm.tm_gmtoff / 60 / 60; |
414 | int m = labs((tm.tm_gmtoff / 60) % 60); | |
415 | snprintf(buf + tail, ELEMENTSOF(buf) - tail, "%+03d:%02d", h, m); | |
29a753df | 416 | break; |
f9c443b7 | 417 | } |
0693e6b2 | 418 | |
29a753df LP |
419 | case OUTPUT_SHORT: |
420 | case OUTPUT_SHORT_PRECISE: | |
421 | ||
f9c443b7 YW |
422 | if (strftime(buf, sizeof(buf), "%b %d %H:%M:%S", |
423 | localtime_or_gmtime_r(&t, &tm, flags & OUTPUT_UTC)) <= 0) | |
6ed286d2 | 424 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format syslog time."); |
29a753df LP |
425 | |
426 | if (mode == OUTPUT_SHORT_PRECISE) { | |
29a753df | 427 | assert(sizeof(buf) > strlen(buf)); |
6ed286d2 YW |
428 | if (!snprintf_ok(buf + strlen(buf), sizeof(buf) - strlen(buf), ".%06"PRIu64, usec % USEC_PER_SEC)) |
429 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to format precise time."); | |
29a753df LP |
430 | } |
431 | break; | |
432 | ||
433 | default: | |
04499a70 | 434 | assert_not_reached(); |
29a753df LP |
435 | } |
436 | } | |
437 | ||
438 | fputs(buf, f); | |
439 | return (int) strlen(buf); | |
440 | } | |
441 | ||
affde1d7 YW |
442 | static void parse_display_realtime( |
443 | sd_journal *j, | |
444 | const char *source_realtime, | |
445 | const char *source_monotonic, | |
446 | usec_t *ret) { | |
447 | ||
448 | usec_t t, s, u; | |
449 | ||
450 | assert(j); | |
451 | assert(ret); | |
452 | ||
453 | /* First, try _SOURCE_REALTIME_TIMESTAMP. */ | |
454 | if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t)) { | |
455 | *ret = t; | |
456 | return; | |
457 | } | |
458 | ||
459 | /* Read realtime timestamp in the entry header. */ | |
460 | if (sd_journal_get_realtime_usec(j, &t) < 0) { | |
461 | *ret = USEC_INFINITY; | |
462 | return; | |
463 | } | |
464 | ||
465 | /* If _SOURCE_MONOTONIC_TIMESTAMP is provided, adjust the header timestamp. */ | |
466 | if (source_monotonic && safe_atou64(source_monotonic, &s) >= 0 && VALID_MONOTONIC(s) && | |
467 | sd_journal_get_monotonic_usec(j, &u, &(sd_id128_t) {}) >= 0) { | |
468 | *ret = map_clock_usec_raw(t, u, s); | |
469 | return; | |
470 | } | |
471 | ||
472 | /* Otherwise, use the header timestamp as is. */ | |
473 | *ret = t; | |
474 | } | |
475 | ||
63d2c755 YW |
476 | static void parse_display_timestamp( |
477 | sd_journal *j, | |
affde1d7 YW |
478 | const char *source_realtime, |
479 | const char *source_monotonic, | |
63d2c755 YW |
480 | dual_timestamp *ret_display_ts, |
481 | sd_id128_t *ret_boot_id) { | |
482 | ||
affde1d7 YW |
483 | dual_timestamp header_ts = DUAL_TIMESTAMP_INFINITY, source_ts = DUAL_TIMESTAMP_INFINITY; |
484 | sd_id128_t boot_id = SD_ID128_NULL; | |
485 | usec_t t; | |
63d2c755 YW |
486 | |
487 | assert(j); | |
488 | assert(ret_display_ts); | |
489 | assert(ret_boot_id); | |
490 | ||
affde1d7 YW |
491 | if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t)) |
492 | source_ts.realtime = t; | |
493 | ||
494 | if (source_monotonic && safe_atou64(source_monotonic, &t) >= 0 && VALID_MONOTONIC(t)) | |
495 | source_ts.monotonic = t; | |
496 | ||
497 | (void) sd_journal_get_realtime_usec(j, &header_ts.realtime); | |
498 | (void) sd_journal_get_monotonic_usec(j, &header_ts.monotonic, &boot_id); | |
499 | ||
500 | /* Adjust timestamp if possible. */ | |
501 | if (header_ts.realtime != USEC_INFINITY && header_ts.monotonic != USEC_INFINITY) { | |
502 | if (source_ts.realtime == USEC_INFINITY && source_ts.monotonic != USEC_INFINITY) | |
503 | source_ts.realtime = map_clock_usec_raw(header_ts.realtime, header_ts.monotonic, source_ts.monotonic); | |
504 | else if (source_ts.realtime != USEC_INFINITY && source_ts.monotonic == USEC_INFINITY) | |
505 | source_ts.monotonic = map_clock_usec_raw(header_ts.monotonic, header_ts.realtime, source_ts.realtime); | |
506 | } | |
507 | ||
508 | ret_display_ts->realtime = source_ts.realtime != USEC_INFINITY ? source_ts.realtime : header_ts.realtime; | |
509 | ret_display_ts->monotonic = source_ts.monotonic != USEC_INFINITY ? source_ts.monotonic : header_ts.monotonic; | |
510 | *ret_boot_id = boot_id; | |
63d2c755 YW |
511 | } |
512 | ||
08ace05b LP |
513 | static int output_short( |
514 | FILE *f, | |
515 | sd_journal *j, | |
516 | OutputMode mode, | |
517 | unsigned n_columns, | |
cc25a67e | 518 | OutputFlags flags, |
2f063186 | 519 | const Set *output_fields, |
275e6be0 | 520 | const size_t highlight[2], |
affde1d7 YW |
521 | dual_timestamp *previous_display_ts, /* in and out, used only when mode is OUTPUT_SHORT_MONOTONIC, OUTPUT_SHORT_DELTA. */ |
522 | sd_id128_t *previous_boot_id) { /* in and out, used only when mode is OUTPUT_SHORT_MONOTONIC, OUTPUT_SHORT_DELTA. */ | |
08ace05b | 523 | |
86aa7ba4 | 524 | int r; |
86aa7ba4 | 525 | const void *data; |
2108b567 LP |
526 | size_t length, n = 0; |
527 | _cleanup_free_ char *hostname = NULL, *identifier = NULL, *comm = NULL, *pid = NULL, *fake_pid = NULL, | |
275e6be0 | 528 | *message = NULL, *priority = NULL, *transport = NULL, |
63d2c755 YW |
529 | *config_file = NULL, *unit = NULL, *user_unit = NULL, *documentation_url = NULL, |
530 | *realtime = NULL, *monotonic = NULL; | |
2108b567 | 531 | size_t hostname_len = 0, identifier_len = 0, comm_len = 0, pid_len = 0, fake_pid_len = 0, message_len = 0, |
275e6be0 | 532 | priority_len = 0, transport_len = 0, config_file_len = 0, |
2108b567 | 533 | unit_len = 0, user_unit_len = 0, documentation_url_len = 0; |
63d2c755 YW |
534 | dual_timestamp display_ts; |
535 | sd_id128_t boot_id; | |
49826187 | 536 | int p = LOG_INFO; |
194da5ca | 537 | bool ellipsized = false, audit; |
ed054520 | 538 | const ParseFieldVec fields[] = { |
63d2c755 YW |
539 | PARSE_FIELD_VEC_ENTRY("_PID=", &pid, &pid_len ), |
540 | PARSE_FIELD_VEC_ENTRY("_COMM=", &comm, &comm_len ), | |
541 | PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message, &message_len ), | |
542 | PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority, &priority_len ), | |
543 | PARSE_FIELD_VEC_ENTRY("_TRANSPORT=", &transport, &transport_len ), | |
544 | PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname, &hostname_len ), | |
545 | PARSE_FIELD_VEC_ENTRY("SYSLOG_PID=", &fake_pid, &fake_pid_len ), | |
546 | PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len ), | |
547 | PARSE_FIELD_VEC_ENTRY("CONFIG_FILE=", &config_file, &config_file_len ), | |
548 | PARSE_FIELD_VEC_ENTRY("_SYSTEMD_UNIT=", &unit, &unit_len ), | |
549 | PARSE_FIELD_VEC_ENTRY("_SYSTEMD_USER_UNIT=", &user_unit, &user_unit_len ), | |
550 | PARSE_FIELD_VEC_ENTRY("DOCUMENTATION=", &documentation_url, &documentation_url_len), | |
551 | PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, NULL ), | |
552 | PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, NULL ), | |
ed054520 | 553 | }; |
b4766d5f | 554 | size_t highlight_shifted[] = {highlight ? highlight[0] : 0, highlight ? highlight[1] : 0}; |
86aa7ba4 | 555 | |
08ace05b | 556 | assert(f); |
86aa7ba4 | 557 | assert(j); |
4e30b87d | 558 | assert(previous_display_ts); |
893bcd3d | 559 | assert(previous_boot_id); |
86aa7ba4 | 560 | |
b1cc0822 LP |
561 | /* Set the threshold to one bigger than the actual print threshold, so that if the line is actually |
562 | * longer than what we're willing to print, ellipsization will occur. This way we won't output a | |
563 | * misleading line without any indication of truncation. | |
a6f0104a | 564 | */ |
b1cc0822 | 565 | (void) sd_journal_set_data_threshold(j, flags & (OUTPUT_SHOW_ALL|OUTPUT_FULL_WIDTH) ? 0 : PRINT_CHAR_THRESHOLD + 1); |
93b73b06 | 566 | |
a72b6353 | 567 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
ed054520 | 568 | r = parse_fieldv(data, length, fields, ELEMENTSOF(fields)); |
55d7bfc1 | 569 | if (r < 0) |
08ace05b | 570 | return r; |
55d7bfc1 | 571 | } |
a3b076f6 | 572 | if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
d00f1d57 LP |
573 | log_debug_errno(r, "Skipping message we can't read: %m"); |
574 | return 0; | |
575 | } | |
a72b6353 | 576 | if (r < 0) |
b56d608e | 577 | return log_error_errno(r, "Failed to get journal fields: %m"); |
a72b6353 | 578 | |
07d21025 LP |
579 | if (!message) { |
580 | log_debug("Skipping message without MESSAGE= field."); | |
08ace05b | 581 | return 0; |
07d21025 | 582 | } |
55d7bfc1 | 583 | |
25aa35d4 SZ |
584 | if (identifier && set_contains(j->exclude_syslog_identifiers, identifier)) |
585 | return 0; | |
586 | ||
e8bc0ea2 | 587 | if (!(flags & OUTPUT_SHOW_ALL)) |
b4766d5f | 588 | strip_tab_ansi(&message, &message_len, highlight_shifted); |
e8bc0ea2 | 589 | |
61cecfa0 | 590 | if (flags & OUTPUT_TRUNCATE_NEWLINE) |
591 | truncate_nl_full(message, &message_len); | |
592 | ||
49826187 LP |
593 | if (priority_len == 1 && *priority >= '0' && *priority <= '7') |
594 | p = *priority - '0'; | |
595 | ||
194da5ca ZJS |
596 | audit = streq_ptr(transport, "audit"); |
597 | ||
affde1d7 YW |
598 | if (IN_SET(mode, OUTPUT_SHORT_MONOTONIC, OUTPUT_SHORT_DELTA)) { |
599 | parse_display_timestamp(j, realtime, monotonic, &display_ts, &boot_id); | |
63d2c755 | 600 | r = output_timestamp_monotonic(f, mode, &display_ts, &boot_id, previous_display_ts, previous_boot_id); |
affde1d7 YW |
601 | } else { |
602 | usec_t usec; | |
603 | parse_display_realtime(j, realtime, monotonic, &usec); | |
604 | r = output_timestamp_realtime(f, j, mode, flags, usec); | |
605 | } | |
29a753df LP |
606 | if (r < 0) |
607 | return r; | |
608 | n += r; | |
86aa7ba4 | 609 | |
29a753df | 610 | if (flags & OUTPUT_NO_HOSTNAME) { |
991e274b | 611 | /* Suppress display of the hostname if this is requested. */ |
12104159 | 612 | hostname = mfree(hostname); |
991e274b LP |
613 | hostname_len = 0; |
614 | } | |
615 | ||
08ace05b LP |
616 | if (hostname && shall_print(hostname, hostname_len, flags)) { |
617 | fprintf(f, " %.*s", (int) hostname_len, hostname); | |
55d7bfc1 LP |
618 | n += hostname_len + 1; |
619 | } | |
620 | ||
76f7cc3f ZJS |
621 | if (mode == OUTPUT_WITH_UNIT && ((unit && shall_print(unit, unit_len, flags)) || |
622 | (user_unit && shall_print(user_unit, user_unit_len, flags)))) { | |
49805b3d LB |
623 | if (unit) { |
624 | fprintf(f, " %.*s", (int) unit_len, unit); | |
625 | n += unit_len + 1; | |
626 | } | |
627 | if (user_unit) { | |
628 | if (unit) | |
629 | fprintf(f, "/%.*s", (int) user_unit_len, user_unit); | |
630 | else | |
631 | fprintf(f, " %.*s", (int) user_unit_len, user_unit); | |
632 | n += unit_len + 1; | |
633 | } | |
634 | } else if (identifier && shall_print(identifier, identifier_len, flags)) { | |
08ace05b | 635 | fprintf(f, " %.*s", (int) identifier_len, identifier); |
4cd9a9d9 | 636 | n += identifier_len + 1; |
08ace05b LP |
637 | } else if (comm && shall_print(comm, comm_len, flags)) { |
638 | fprintf(f, " %.*s", (int) comm_len, comm); | |
55d7bfc1 | 639 | n += comm_len + 1; |
b5936820 | 640 | } else |
1248e840 | 641 | fputs(" unknown", f); |
86aa7ba4 | 642 | |
08ace05b LP |
643 | if (pid && shall_print(pid, pid_len, flags)) { |
644 | fprintf(f, "[%.*s]", (int) pid_len, pid); | |
55d7bfc1 | 645 | n += pid_len + 2; |
08ace05b LP |
646 | } else if (fake_pid && shall_print(fake_pid, fake_pid_len, flags)) { |
647 | fprintf(f, "[%.*s]", (int) fake_pid_len, fake_pid); | |
6c1e6b98 | 648 | n += fake_pid_len + 2; |
86aa7ba4 LP |
649 | } |
650 | ||
2108b567 LP |
651 | fputs(": ", f); |
652 | ||
653 | if (urlify_enabled()) { | |
654 | _cleanup_free_ char *c = NULL; | |
655 | ||
656 | /* Insert a hyperlink to a documentation URL before the message. Note that we don't make the | |
657 | * whole message a hyperlink, since otherwise the whole screen might end up being just | |
658 | * hyperlinks. Moreover, we want to be able to highlight parts of the message (such as the | |
659 | * config file, see below) hence let's keep the documentation URL link separate. */ | |
660 | ||
661 | if (documentation_url && shall_print(documentation_url, documentation_url_len, flags)) { | |
662 | c = strndup(documentation_url, documentation_url_len); | |
663 | if (!c) | |
664 | return log_oom(); | |
665 | ||
666 | if (!documentation_url_is_valid(c)) /* Eat up invalid links */ | |
667 | c = mfree(c); | |
668 | } | |
669 | ||
670 | if (!c) | |
671 | (void) url_from_catalog(j, &c); /* Acquire from catalog if not embedded in log message itself */ | |
672 | ||
673 | if (c) { | |
674 | _cleanup_free_ char *urlified = NULL; | |
675 | ||
676 | if (terminal_urlify(c, special_glyph(SPECIAL_GLYPH_EXTERNAL_LINK), &urlified) >= 0) { | |
677 | fputs(urlified, f); | |
678 | fputc(' ', f); | |
679 | } | |
680 | } | |
681 | } | |
682 | ||
2b59bf51 ZJS |
683 | if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(message, message_len)) |
684 | fprintf(f, "[%s blob data]\n", FORMAT_BYTES(message_len)); | |
685 | else { | |
76f7cc3f ZJS |
686 | |
687 | /* URLify config_file string in message, if the message starts with it. | |
688 | * Skip URLification if the highlighted pattern overlaps. */ | |
689 | if (config_file && | |
690 | message_len >= config_file_len && | |
691 | memcmp(message, config_file, config_file_len) == 0 && | |
85fbebe6 | 692 | (message_len == config_file_len || IN_SET(message[config_file_len], ':', ' ')) && |
76f7cc3f ZJS |
693 | (!highlight || highlight_shifted[0] == 0 || highlight_shifted[0] > config_file_len)) { |
694 | ||
695 | _cleanup_free_ char *t = NULL, *urlified = NULL; | |
696 | ||
697 | t = strndup(config_file, config_file_len); | |
698 | if (t && terminal_urlify_path(t, NULL, &urlified) >= 0) { | |
85fbebe6 ZJS |
699 | size_t urlified_len = strlen(urlified); |
700 | size_t shift = urlified_len - config_file_len; | |
76f7cc3f ZJS |
701 | char *joined; |
702 | ||
85fbebe6 | 703 | joined = realloc(urlified, message_len + shift); |
76f7cc3f | 704 | if (joined) { |
85fbebe6 | 705 | memcpy(joined + urlified_len, message + config_file_len, message_len - config_file_len); |
76f7cc3f | 706 | free_and_replace(message, joined); |
85fbebe6 | 707 | TAKE_PTR(urlified); |
76f7cc3f ZJS |
708 | message_len += shift; |
709 | if (highlight) { | |
710 | highlight_shifted[0] += shift; | |
711 | highlight_shifted[1] += shift; | |
712 | } | |
713 | } | |
714 | } | |
715 | } | |
716 | ||
94e0bd7d | 717 | ellipsized |= |
194da5ca | 718 | print_multiline(f, n + 2, n_columns, flags, p, audit, |
b4766d5f ZJS |
719 | message, message_len, |
720 | highlight_shifted); | |
31f7bf19 | 721 | } |
55d7bfc1 | 722 | |
d4205751 | 723 | if (flags & OUTPUT_CATALOG) |
2108b567 | 724 | (void) print_catalog(f, j); |
d4205751 | 725 | |
affde1d7 YW |
726 | if (IN_SET(mode, OUTPUT_SHORT_MONOTONIC, OUTPUT_SHORT_DELTA)) { |
727 | *previous_display_ts = display_ts; | |
728 | *previous_boot_id = boot_id; | |
729 | } | |
63d2c755 | 730 | |
94e0bd7d | 731 | return ellipsized; |
86aa7ba4 LP |
732 | } |
733 | ||
affde1d7 | 734 | static int get_display_realtime(sd_journal *j, usec_t *ret) { |
63d2c755 YW |
735 | const void *data; |
736 | _cleanup_free_ char *realtime = NULL, *monotonic = NULL; | |
737 | size_t length; | |
738 | const ParseFieldVec message_fields[] = { | |
739 | PARSE_FIELD_VEC_ENTRY("_SOURCE_REALTIME_TIMESTAMP=", &realtime, NULL), | |
740 | PARSE_FIELD_VEC_ENTRY("_SOURCE_MONOTONIC_TIMESTAMP=", &monotonic, NULL), | |
741 | }; | |
742 | int r; | |
743 | ||
744 | assert(j); | |
affde1d7 | 745 | assert(ret); |
63d2c755 YW |
746 | |
747 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { | |
748 | r = parse_fieldv(data, length, message_fields, ELEMENTSOF(message_fields)); | |
749 | if (r < 0) | |
750 | return r; | |
751 | ||
752 | if (realtime && monotonic) | |
753 | break; | |
754 | } | |
755 | if (r < 0) | |
756 | return r; | |
757 | ||
affde1d7 | 758 | (void) parse_display_realtime(j, realtime, monotonic, ret); |
63d2c755 YW |
759 | |
760 | /* Restart all data before */ | |
761 | sd_journal_restart_data(j); | |
762 | sd_journal_restart_unique(j); | |
763 | sd_journal_restart_fields(j); | |
764 | ||
765 | return 0; | |
766 | } | |
767 | ||
08ace05b LP |
768 | static int output_verbose( |
769 | FILE *f, | |
770 | sd_journal *j, | |
771 | OutputMode mode, | |
772 | unsigned n_columns, | |
cc25a67e | 773 | OutputFlags flags, |
2f063186 | 774 | const Set *output_fields, |
275e6be0 | 775 | const size_t highlight[2], |
63d2c755 YW |
776 | dual_timestamp *previous_display_ts, /* unused */ |
777 | sd_id128_t *previous_boot_id) { /* unused */ | |
08ace05b | 778 | |
86aa7ba4 LP |
779 | const void *data; |
780 | size_t length; | |
7fd1b19b | 781 | _cleanup_free_ char *cursor = NULL; |
275e6be0 | 782 | char buf[FORMAT_TIMESTAMP_MAX + 7]; |
8924973a | 783 | const char *timestamp; |
affde1d7 | 784 | usec_t usec; |
86aa7ba4 LP |
785 | int r; |
786 | ||
08ace05b | 787 | assert(f); |
86aa7ba4 LP |
788 | assert(j); |
789 | ||
b1cc0822 | 790 | (void) sd_journal_set_data_threshold(j, 0); |
93b73b06 | 791 | |
affde1d7 | 792 | r = get_display_realtime(j, &usec); |
63d2c755 YW |
793 | if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
794 | log_debug_errno(r, "Skipping message we can't read: %m"); | |
795 | return 0; | |
796 | } | |
797 | if (r < 0) | |
798 | return log_error_errno(r, "Failed to get journal fields: %m"); | |
799 | ||
affde1d7 | 800 | if (!VALID_REALTIME(usec)) |
275e6be0 | 801 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "No valid realtime timestamp available"); |
86aa7ba4 LP |
802 | |
803 | r = sd_journal_get_cursor(j, &cursor); | |
f647962d MS |
804 | if (r < 0) |
805 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 806 | |
affde1d7 | 807 | timestamp = format_timestamp_style(buf, sizeof buf, usec, |
7b3eb5c9 | 808 | flags & OUTPUT_UTC ? TIMESTAMP_US_UTC : TIMESTAMP_US); |
8b701558 LP |
809 | fprintf(f, "%s%s%s %s[%s]%s\n", |
810 | timestamp && (flags & OUTPUT_COLOR) ? ANSI_UNDERLINE : "", | |
8924973a | 811 | timestamp ?: "(no timestamp)", |
8b701558 LP |
812 | timestamp && (flags & OUTPUT_COLOR) ? ANSI_NORMAL : "", |
813 | (flags & OUTPUT_COLOR) ? ANSI_GREY : "", | |
814 | cursor, | |
815 | (flags & OUTPUT_COLOR) ? ANSI_NORMAL : ""); | |
86aa7ba4 | 816 | |
a72b6353 | 817 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
79dc477f | 818 | _cleanup_free_ char *urlified = NULL; |
8b701558 LP |
819 | const char *on = "", *off = ""; |
820 | const char *c, *p = NULL; | |
821 | size_t fieldlen, valuelen; | |
7ac4fa7e | 822 | |
31f7bf19 | 823 | c = memchr(data, '=', length); |
baaa35ad | 824 | if (!c) |
805d67c5 YW |
825 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field."); |
826 | ||
31f7bf19 | 827 | fieldlen = c - (const char*) data; |
805d67c5 YW |
828 | if (!journal_field_valid(data, fieldlen, true)) |
829 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field."); | |
86aa7ba4 | 830 | |
cc25a67e LK |
831 | r = field_set_test(output_fields, data, fieldlen); |
832 | if (r < 0) | |
833 | return r; | |
79dc477f | 834 | if (r == 0) |
cc25a67e LK |
835 | continue; |
836 | ||
79dc477f | 837 | valuelen = length - 1 - fieldlen; |
8b701558 LP |
838 | p = c + 1; |
839 | ||
840 | if (flags & OUTPUT_COLOR) { | |
841 | if (startswith(data, "MESSAGE=")) { | |
842 | on = ANSI_HIGHLIGHT; | |
843 | off = ANSI_NORMAL; | |
844 | } else if (startswith(data, "CONFIG_FILE=")) { | |
845 | _cleanup_free_ char *u = NULL; | |
846 | ||
847 | u = memdup_suffix0(p, valuelen); | |
848 | if (!u) | |
849 | return log_oom(); | |
850 | ||
851 | if (terminal_urlify_path(u, NULL, &urlified) >= 0) { | |
852 | p = urlified; | |
853 | valuelen = strlen(urlified); | |
854 | } | |
79dc477f | 855 | |
8b701558 LP |
856 | } else if (startswith(data, "_")) { |
857 | /* Highlight trusted data as such */ | |
858 | on = ANSI_GREEN; | |
859 | off = ANSI_NORMAL; | |
79dc477f | 860 | } |
8b701558 | 861 | } |
7ac4fa7e | 862 | |
8980058a | 863 | if ((flags & OUTPUT_SHOW_ALL) || |
a6f0104a ZJS |
864 | (((length < PRINT_CHAR_THRESHOLD) || flags & OUTPUT_FULL_WIDTH) |
865 | && utf8_is_printable(data, length))) { | |
8b701558 | 866 | fprintf(f, " %s%.*s=", on, (int) fieldlen, (const char*)data); |
194da5ca | 867 | print_multiline(f, 4 + fieldlen + 1, 0, OUTPUT_FULL_WIDTH, 0, false, |
79dc477f | 868 | p, valuelen, |
194da5ca | 869 | NULL); |
7ac4fa7e | 870 | fputs(off, f); |
2b59bf51 | 871 | } else |
7ac4fa7e ZJS |
872 | fprintf(f, " %s%.*s=[%s blob data]%s\n", |
873 | on, | |
31f7bf19 ZJS |
874 | (int) (c - (const char*) data), |
875 | (const char*) data, | |
2b59bf51 | 876 | FORMAT_BYTES(length - (c - (const char *) data) - 1), |
7ac4fa7e | 877 | off); |
86aa7ba4 | 878 | } |
a72b6353 ZJS |
879 | if (r < 0) |
880 | return r; | |
881 | ||
d4205751 | 882 | if (flags & OUTPUT_CATALOG) |
2108b567 | 883 | (void) print_catalog(f, j); |
d4205751 | 884 | |
86aa7ba4 LP |
885 | return 0; |
886 | } | |
887 | ||
08ace05b LP |
888 | static int output_export( |
889 | FILE *f, | |
890 | sd_journal *j, | |
891 | OutputMode mode, | |
892 | unsigned n_columns, | |
cc25a67e | 893 | OutputFlags flags, |
2f063186 | 894 | const Set *output_fields, |
275e6be0 | 895 | const size_t highlight[2], |
63d2c755 YW |
896 | dual_timestamp *previous_display_ts, /* unused */ |
897 | sd_id128_t *previous_boot_id) { /* unused */ | |
08ace05b | 898 | |
2bc70e2e | 899 | sd_id128_t journal_boot_id, seqnum_id; |
7fd1b19b | 900 | _cleanup_free_ char *cursor = NULL; |
2bc70e2e | 901 | usec_t monotonic, realtime; |
86aa7ba4 | 902 | const void *data; |
2bc70e2e | 903 | uint64_t seqnum; |
86aa7ba4 | 904 | size_t length; |
85b55869 | 905 | int r; |
86aa7ba4 LP |
906 | |
907 | assert(j); | |
908 | ||
b1cc0822 | 909 | (void) sd_journal_set_data_threshold(j, 0); |
93b73b06 | 910 | |
86aa7ba4 | 911 | r = sd_journal_get_cursor(j, &cursor); |
f647962d MS |
912 | if (r < 0) |
913 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 914 | |
417cbcd6 | 915 | r = sd_journal_get_realtime_usec(j, &realtime); |
916 | if (r < 0) | |
917 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
918 | ||
919 | r = sd_journal_get_monotonic_usec(j, &monotonic, &journal_boot_id); | |
920 | if (r < 0) | |
921 | return log_error_errno(r, "Failed to get monotonic timestamp: %m"); | |
922 | ||
2bc70e2e LP |
923 | r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id); |
924 | if (r < 0) | |
925 | return log_error_errno(r, "Failed to get seqnum: %m"); | |
926 | ||
08ace05b LP |
927 | fprintf(f, |
928 | "__CURSOR=%s\n" | |
2bc70e2e LP |
929 | "__REALTIME_TIMESTAMP=" USEC_FMT "\n" |
930 | "__MONOTONIC_TIMESTAMP=" USEC_FMT "\n" | |
931 | "__SEQNUM=%" PRIu64 "\n" | |
932 | "__SEQNUM_ID=%s\n" | |
08ace05b LP |
933 | "_BOOT_ID=%s\n", |
934 | cursor, | |
417cbcd6 | 935 | realtime, |
936 | monotonic, | |
2bc70e2e LP |
937 | seqnum, |
938 | SD_ID128_TO_STRING(seqnum_id), | |
417cbcd6 | 939 | SD_ID128_TO_STRING(journal_boot_id)); |
86aa7ba4 | 940 | |
a72b6353 | 941 | JOURNAL_FOREACH_DATA_RETVAL(j, data, length, r) { |
805d67c5 | 942 | size_t fieldlen; |
cc25a67e | 943 | const char *c; |
86aa7ba4 | 944 | |
0ab896b3 ZJS |
945 | /* We already printed the boot id from the data in the header, hence let's suppress it here */ |
946 | if (memory_startswith(data, length, "_BOOT_ID=")) | |
112301ae LP |
947 | continue; |
948 | ||
cc25a67e | 949 | c = memchr(data, '=', length); |
baaa35ad | 950 | if (!c) |
805d67c5 | 951 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field."); |
cc25a67e | 952 | |
805d67c5 YW |
953 | fieldlen = c - (const char*) data; |
954 | if (!journal_field_valid(data, fieldlen, true)) | |
955 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field."); | |
956 | ||
957 | r = field_set_test(output_fields, data, fieldlen); | |
cc25a67e LK |
958 | if (r < 0) |
959 | return r; | |
960 | if (!r) | |
961 | continue; | |
962 | ||
0ade5ffe ZJS |
963 | if (utf8_is_printable_newline(data, length, false)) |
964 | fwrite(data, length, 1, f); | |
965 | else { | |
86aa7ba4 LP |
966 | uint64_t le64; |
967 | ||
805d67c5 | 968 | fwrite(data, fieldlen, 1, f); |
08ace05b | 969 | fputc('\n', f); |
805d67c5 | 970 | le64 = htole64(length - fieldlen - 1); |
08ace05b | 971 | fwrite(&le64, sizeof(le64), 1, f); |
805d67c5 | 972 | fwrite(c + 1, length - fieldlen - 1, 1, f); |
0ade5ffe | 973 | } |
86aa7ba4 | 974 | |
08ace05b | 975 | fputc('\n', f); |
86aa7ba4 | 976 | } |
a3b076f6 | 977 | if (IN_SET(r, -EADDRNOTAVAIL, -EBADMSG)) { |
4f5e1723 AW |
978 | log_debug_errno(r, "Skipping message we can't read: %m"); |
979 | return 0; | |
980 | } | |
86aa7ba4 | 981 | |
a72b6353 ZJS |
982 | if (r < 0) |
983 | return r; | |
984 | ||
08ace05b | 985 | fputc('\n', f); |
86aa7ba4 LP |
986 | |
987 | return 0; | |
988 | } | |
989 | ||
240a5fe8 | 990 | void json_escape( |
08ace05b LP |
991 | FILE *f, |
992 | const char* p, | |
993 | size_t l, | |
994 | OutputFlags flags) { | |
995 | ||
996 | assert(f); | |
997 | assert(p); | |
998 | ||
93b73b06 | 999 | if (!(flags & OUTPUT_SHOW_ALL) && l >= JSON_THRESHOLD) |
08ace05b LP |
1000 | fputs("null", f); |
1001 | ||
8980058a | 1002 | else if (!(flags & OUTPUT_SHOW_ALL) && !utf8_is_printable(p, l)) { |
86aa7ba4 LP |
1003 | bool not_first = false; |
1004 | ||
08ace05b | 1005 | fputs("[ ", f); |
86aa7ba4 LP |
1006 | |
1007 | while (l > 0) { | |
1008 | if (not_first) | |
08ace05b | 1009 | fprintf(f, ", %u", (uint8_t) *p); |
86aa7ba4 LP |
1010 | else { |
1011 | not_first = true; | |
08ace05b | 1012 | fprintf(f, "%u", (uint8_t) *p); |
86aa7ba4 LP |
1013 | } |
1014 | ||
1015 | p++; | |
1016 | l--; | |
1017 | } | |
1018 | ||
08ace05b | 1019 | fputs(" ]", f); |
86aa7ba4 | 1020 | } else { |
e768a4f0 | 1021 | fputc('"', f); |
86aa7ba4 LP |
1022 | |
1023 | while (l > 0) { | |
4c701096 | 1024 | if (IN_SET(*p, '"', '\\')) { |
08ace05b LP |
1025 | fputc('\\', f); |
1026 | fputc(*p, f); | |
31f7bf19 ZJS |
1027 | } else if (*p == '\n') |
1028 | fputs("\\n", f); | |
91a8a108 DM |
1029 | else if ((uint8_t) *p < ' ') |
1030 | fprintf(f, "\\u%04x", (uint8_t) *p); | |
08ace05b LP |
1031 | else |
1032 | fputc(*p, f); | |
86aa7ba4 LP |
1033 | |
1034 | p++; | |
1035 | l--; | |
1036 | } | |
1037 | ||
e768a4f0 | 1038 | fputc('"', f); |
86aa7ba4 LP |
1039 | } |
1040 | } | |
1041 | ||
a9e536a6 | 1042 | typedef struct JsonData { |
8e044443 | 1043 | JsonVariant* name; |
ddd6875d | 1044 | JsonVariant* values; |
a9e536a6 | 1045 | } JsonData; |
8e044443 | 1046 | |
ddd6875d | 1047 | static JsonData* json_data_free(JsonData *d) { |
ee9d31a6 DDM |
1048 | if (!d) |
1049 | return NULL; | |
1050 | ||
1051 | json_variant_unref(d->name); | |
ddd6875d | 1052 | json_variant_unref(d->values); |
ee9d31a6 DDM |
1053 | |
1054 | return mfree(d); | |
1055 | } | |
1056 | ||
ddd6875d YW |
1057 | DEFINE_TRIVIAL_CLEANUP_FUNC(JsonData*, json_data_free); |
1058 | ||
ee9d31a6 DDM |
1059 | DEFINE_HASH_OPS_WITH_VALUE_DESTRUCTOR(json_data_hash_ops_free, |
1060 | char, string_hash_func, string_compare_func, | |
a9e536a6 | 1061 | JsonData, json_data_free); |
ee9d31a6 | 1062 | |
8e044443 LP |
1063 | static int update_json_data( |
1064 | Hashmap *h, | |
1065 | OutputFlags flags, | |
1066 | const char *name, | |
1067 | const void *value, | |
1068 | size_t size) { | |
1069 | ||
1070 | _cleanup_(json_variant_unrefp) JsonVariant *v = NULL; | |
ddd6875d | 1071 | JsonData *d; |
8e044443 LP |
1072 | int r; |
1073 | ||
8cc3cdac LP |
1074 | assert(name); |
1075 | assert(value); | |
1076 | ||
1077 | if (size == SIZE_MAX) | |
1078 | size = strlen(value); | |
1079 | ||
8e044443 LP |
1080 | if (!(flags & OUTPUT_SHOW_ALL) && strlen(name) + 1 + size >= JSON_THRESHOLD) |
1081 | r = json_variant_new_null(&v); | |
1082 | else if (utf8_is_printable(value, size)) | |
1083 | r = json_variant_new_stringn(&v, value, size); | |
1084 | else | |
1085 | r = json_variant_new_array_bytes(&v, value, size); | |
1086 | if (r < 0) | |
1087 | return log_error_errno(r, "Failed to allocate JSON data: %m"); | |
1088 | ||
1089 | d = hashmap_get(h, name); | |
1090 | if (d) { | |
ddd6875d YW |
1091 | r = json_variant_append_array(&d->values, v); |
1092 | if (r < 0) | |
1093 | return log_error_errno(r, "Failed to append JSON value into array: %m"); | |
1094 | } else { | |
1095 | _cleanup_(json_data_freep) JsonData *e = NULL; | |
8e044443 | 1096 | |
ddd6875d YW |
1097 | e = new0(JsonData, 1); |
1098 | if (!e) | |
8e044443 LP |
1099 | return log_oom(); |
1100 | ||
ddd6875d | 1101 | r = json_variant_new_string(&e->name, name); |
8e044443 LP |
1102 | if (r < 0) |
1103 | return log_error_errno(r, "Failed to allocate JSON name variant: %m"); | |
1104 | ||
ddd6875d YW |
1105 | r = json_variant_append_array(&e->values, v); |
1106 | if (r < 0) | |
1107 | return log_error_errno(r, "Failed to create JSON value array: %m"); | |
8e044443 | 1108 | |
ddd6875d YW |
1109 | r = hashmap_put(h, json_variant_string(e->name), e); |
1110 | if (r < 0) | |
1111 | return log_error_errno(r, "Failed to insert JSON data into hashmap: %m"); | |
8e044443 | 1112 | |
ddd6875d | 1113 | TAKE_PTR(e); |
8e044443 LP |
1114 | } |
1115 | ||
8e044443 LP |
1116 | return 0; |
1117 | } | |
1118 | ||
1119 | static int update_json_data_split( | |
1120 | Hashmap *h, | |
1121 | OutputFlags flags, | |
2f063186 | 1122 | const Set *output_fields, |
8e044443 LP |
1123 | const void *data, |
1124 | size_t size) { | |
1125 | ||
805d67c5 | 1126 | size_t fieldlen; |
8e044443 LP |
1127 | const char *eq; |
1128 | char *name; | |
1129 | ||
1130 | assert(h); | |
1131 | assert(data || size == 0); | |
1132 | ||
1133 | if (memory_startswith(data, size, "_BOOT_ID=")) | |
1134 | return 0; | |
1135 | ||
1136 | eq = memchr(data, '=', MIN(size, JSON_THRESHOLD)); | |
1137 | if (!eq) | |
1138 | return 0; | |
1139 | ||
805d67c5 YW |
1140 | fieldlen = eq - (const char*) data; |
1141 | if (!journal_field_valid(data, fieldlen, true)) | |
1142 | return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid field."); | |
8e044443 | 1143 | |
2f82562b | 1144 | name = strndupa_safe(data, fieldlen); |
2f063186 | 1145 | if (output_fields && !set_contains(output_fields, name)) |
8e044443 LP |
1146 | return 0; |
1147 | ||
805d67c5 | 1148 | return update_json_data(h, flags, name, eq + 1, size - fieldlen - 1); |
8e044443 LP |
1149 | } |
1150 | ||
08ace05b LP |
1151 | static int output_json( |
1152 | FILE *f, | |
1153 | sd_journal *j, | |
1154 | OutputMode mode, | |
1155 | unsigned n_columns, | |
cc25a67e | 1156 | OutputFlags flags, |
2f063186 | 1157 | const Set *output_fields, |
275e6be0 | 1158 | const size_t highlight[2], |
63d2c755 YW |
1159 | dual_timestamp *previous_display_ts, /* unused */ |
1160 | sd_id128_t *previous_boot_id) { /* unused */ | |
08ace05b | 1161 | |
2bc70e2e | 1162 | char usecbuf[CONST_MAX(DECIMAL_STR_MAX(usec_t), DECIMAL_STR_MAX(uint64_t))]; |
8e044443 | 1163 | _cleanup_(json_variant_unrefp) JsonVariant *object = NULL; |
ee9d31a6 | 1164 | _cleanup_hashmap_free_ Hashmap *h = NULL; |
2bc70e2e | 1165 | sd_id128_t journal_boot_id, seqnum_id; |
7fd1b19b | 1166 | _cleanup_free_ char *cursor = NULL; |
2bc70e2e | 1167 | usec_t realtime, monotonic; |
8e044443 | 1168 | JsonVariant **array = NULL; |
a9e536a6 | 1169 | JsonData *d; |
2bc70e2e | 1170 | uint64_t seqnum; |
8e044443 | 1171 | size_t n = 0; |
8e044443 | 1172 | int r; |
86aa7ba4 LP |
1173 | |
1174 | assert(j); | |
1175 | ||
8e044443 | 1176 | (void) sd_journal_set_data_threshold(j, flags & OUTPUT_SHOW_ALL ? 0 : JSON_THRESHOLD); |
93b73b06 | 1177 | |
86aa7ba4 | 1178 | r = sd_journal_get_cursor(j, &cursor); |
f647962d MS |
1179 | if (r < 0) |
1180 | return log_error_errno(r, "Failed to get cursor: %m"); | |
86aa7ba4 | 1181 | |
417cbcd6 | 1182 | r = sd_journal_get_realtime_usec(j, &realtime); |
1183 | if (r < 0) | |
1184 | return log_error_errno(r, "Failed to get realtime timestamp: %m"); | |
1185 | ||
1186 | r = sd_journal_get_monotonic_usec(j, &monotonic, &journal_boot_id); | |
1187 | if (r < 0) | |
1188 | return log_error_errno(r, "Failed to get monotonic timestamp: %m"); | |
1189 | ||
2bc70e2e LP |
1190 | r = sd_journal_get_seqnum(j, &seqnum, &seqnum_id); |
1191 | if (r < 0) | |
1192 | return log_error_errno(r, "Failed to get seqnum: %m"); | |
1193 | ||
ee9d31a6 | 1194 | h = hashmap_new(&json_data_hash_ops_free); |
d99ae53a | 1195 | if (!h) |
b56d608e | 1196 | return log_oom(); |
d99ae53a | 1197 | |
8cc3cdac | 1198 | r = update_json_data(h, flags, "__CURSOR", cursor, SIZE_MAX); |
a72b6353 | 1199 | if (r < 0) |
ee9d31a6 | 1200 | return r; |
d99ae53a | 1201 | |
417cbcd6 | 1202 | xsprintf(usecbuf, USEC_FMT, realtime); |
8cc3cdac | 1203 | r = update_json_data(h, flags, "__REALTIME_TIMESTAMP", usecbuf, SIZE_MAX); |
8e044443 | 1204 | if (r < 0) |
ee9d31a6 | 1205 | return r; |
d99ae53a | 1206 | |
417cbcd6 | 1207 | xsprintf(usecbuf, USEC_FMT, monotonic); |
8cc3cdac | 1208 | r = update_json_data(h, flags, "__MONOTONIC_TIMESTAMP", usecbuf, SIZE_MAX); |
8e044443 | 1209 | if (r < 0) |
ee9d31a6 | 1210 | return r; |
d99ae53a | 1211 | |
8cc3cdac | 1212 | r = update_json_data(h, flags, "_BOOT_ID", SD_ID128_TO_STRING(journal_boot_id), SIZE_MAX); |
8e044443 | 1213 | if (r < 0) |
ee9d31a6 | 1214 | return r; |
d99ae53a | 1215 | |
2bc70e2e LP |
1216 | xsprintf(usecbuf, USEC_FMT, seqnum); |
1217 | r = update_json_data(h, flags, "__SEQNUM", usecbuf, SIZE_MAX); | |
1218 | if (r < 0) | |
ee9d31a6 | 1219 | return r; |
2bc70e2e LP |
1220 | |
1221 | r = update_json_data(h, flags, "__SEQNUM_ID", SD_ID128_TO_STRING(seqnum_id), SIZE_MAX); | |
1222 | if (r < 0) | |
ee9d31a6 | 1223 | return r; |
2bc70e2e | 1224 | |
8e044443 LP |
1225 | for (;;) { |
1226 | const void *data; | |
1227 | size_t size; | |
d99ae53a | 1228 | |
8e044443 | 1229 | r = sd_journal_enumerate_data(j, &data, &size); |
a3b076f6 | 1230 | if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
8e044443 | 1231 | log_debug_errno(r, "Skipping message we can't read: %m"); |
ee9d31a6 | 1232 | return 0; |
8e044443 | 1233 | } |
ee9d31a6 DDM |
1234 | if (r < 0) |
1235 | return log_error_errno(r, "Failed to read journal: %m"); | |
8e044443 LP |
1236 | if (r == 0) |
1237 | break; | |
d99ae53a | 1238 | |
8e044443 LP |
1239 | r = update_json_data_split(h, flags, output_fields, data, size); |
1240 | if (r < 0) | |
ee9d31a6 | 1241 | return r; |
8e044443 | 1242 | } |
d99ae53a | 1243 | |
8e044443 | 1244 | array = new(JsonVariant*, hashmap_size(h)*2); |
ee9d31a6 DDM |
1245 | if (!array) |
1246 | return log_oom(); | |
1247 | ||
1248 | CLEANUP_ARRAY(array, n, json_variant_unref_many); | |
d99ae53a | 1249 | |
90e74a66 | 1250 | HASHMAP_FOREACH(d, h) { |
ddd6875d | 1251 | assert(json_variant_elements(d->values) > 0); |
d99ae53a | 1252 | |
8e044443 | 1253 | array[n++] = json_variant_ref(d->name); |
d99ae53a | 1254 | |
ddd6875d YW |
1255 | if (json_variant_elements(d->values) == 1) |
1256 | array[n++] = json_variant_ref(json_variant_by_index(d->values, 0)); | |
1257 | else | |
1258 | array[n++] = json_variant_ref(d->values); | |
8e044443 | 1259 | } |
d99ae53a | 1260 | |
8e044443 | 1261 | r = json_variant_new_object(&object, array, n); |
ee9d31a6 DDM |
1262 | if (r < 0) |
1263 | return log_error_errno(r, "Failed to allocate JSON object: %m"); | |
d99ae53a | 1264 | |
ee9d31a6 DDM |
1265 | return json_variant_dump(object, |
1266 | output_mode_to_json_format_flags(mode) | | |
1267 | (FLAGS_SET(flags, OUTPUT_COLOR) ? JSON_FORMAT_COLOR : 0), | |
1268 | f, NULL); | |
86aa7ba4 LP |
1269 | } |
1270 | ||
4d5d1bba | 1271 | static int output_cat_field( |
08ace05b LP |
1272 | FILE *f, |
1273 | sd_journal *j, | |
cc25a67e | 1274 | OutputFlags flags, |
e3eec1fd | 1275 | int prio, |
4d5d1bba | 1276 | const char *field, |
05c7d9bf | 1277 | const size_t highlight[2]) { |
08ace05b | 1278 | |
e3eec1fd | 1279 | const char *color_on = "", *color_off = "", *highlight_on = ""; |
d3f2bdbf | 1280 | const void *data; |
4d5d1bba | 1281 | size_t l, fl; |
d3f2bdbf LP |
1282 | int r; |
1283 | ||
e3eec1fd LP |
1284 | if (FLAGS_SET(flags, OUTPUT_COLOR)) |
1285 | get_log_colors(prio, &color_on, &color_off, &highlight_on); | |
93b73b06 | 1286 | |
4d5d1bba | 1287 | r = sd_journal_get_data(j, field, &data, &l); |
a3b076f6 | 1288 | if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
4f5e1723 AW |
1289 | log_debug_errno(r, "Skipping message we can't read: %m"); |
1290 | return 0; | |
1291 | } | |
4d5d1bba LP |
1292 | if (r == -ENOENT) /* An entry without the requested field */ |
1293 | return 0; | |
1294 | if (r < 0) | |
8d3d7072 | 1295 | return log_error_errno(r, "Failed to get data: %m"); |
d3f2bdbf | 1296 | |
4d5d1bba LP |
1297 | fl = strlen(field); |
1298 | assert(l >= fl + 1); | |
1299 | assert(((char*) data)[fl] == '='); | |
1300 | ||
1301 | data = (const uint8_t*) data + fl + 1; | |
1302 | l -= fl + 1; | |
d3f2bdbf | 1303 | |
e3eec1fd LP |
1304 | if (FLAGS_SET(flags, OUTPUT_COLOR)) { |
1305 | if (highlight) { | |
1306 | assert(highlight[0] <= highlight[1]); | |
1307 | assert(highlight[1] <= l); | |
1308 | ||
1309 | fputs(color_on, f); | |
1310 | fwrite((const char*) data, 1, highlight[0], f); | |
1311 | fputs(highlight_on, f); | |
1312 | fwrite((const char*) data + highlight[0], 1, highlight[1] - highlight[0], f); | |
1313 | fputs(color_on, f); | |
1314 | fwrite((const char*) data + highlight[1], 1, l - highlight[1], f); | |
1315 | fputs(color_off, f); | |
1316 | } else { | |
1317 | fputs(color_on, f); | |
1318 | fwrite((const char*) data, 1, l, f); | |
1319 | fputs(color_off, f); | |
1320 | } | |
b4766d5f | 1321 | } else |
4d5d1bba LP |
1322 | fwrite((const char*) data, 1, l, f); |
1323 | ||
08ace05b | 1324 | fputc('\n', f); |
4d5d1bba LP |
1325 | return 0; |
1326 | } | |
1327 | ||
1328 | static int output_cat( | |
1329 | FILE *f, | |
1330 | sd_journal *j, | |
1331 | OutputMode mode, | |
1332 | unsigned n_columns, | |
1333 | OutputFlags flags, | |
2f063186 | 1334 | const Set *output_fields, |
275e6be0 | 1335 | const size_t highlight[2], |
63d2c755 YW |
1336 | dual_timestamp *previous_display_ts, /* unused */ |
1337 | sd_id128_t *previous_boot_id) { /* unused */ | |
4d5d1bba | 1338 | |
e3eec1fd | 1339 | int r, prio = LOG_INFO; |
4d5d1bba | 1340 | const char *field; |
4d5d1bba LP |
1341 | |
1342 | assert(j); | |
1343 | assert(f); | |
1344 | ||
1345 | (void) sd_journal_set_data_threshold(j, 0); | |
1346 | ||
e3eec1fd LP |
1347 | if (FLAGS_SET(flags, OUTPUT_COLOR)) { |
1348 | const void *data; | |
1349 | size_t l; | |
1350 | ||
1351 | /* Determine priority of this entry, so that we can color it nicely */ | |
1352 | ||
1353 | r = sd_journal_get_data(j, "PRIORITY", &data, &l); | |
a3b076f6 | 1354 | if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) { |
e3eec1fd LP |
1355 | log_debug_errno(r, "Skipping message we can't read: %m"); |
1356 | return 0; | |
1357 | } | |
1358 | if (r < 0) { | |
1359 | if (r != -ENOENT) | |
1360 | return log_error_errno(r, "Failed to get data: %m"); | |
1361 | ||
1362 | /* An entry without PRIORITY */ | |
1363 | } else if (l == 10 && memcmp(data, "PRIORITY=", 9) == 0) { | |
1364 | char c = ((char*) data)[9]; | |
1365 | ||
1366 | if (c >= '0' && c <= '7') | |
1367 | prio = c - '0'; | |
1368 | } | |
1369 | } | |
1370 | ||
4d5d1bba | 1371 | if (set_isempty(output_fields)) |
e3eec1fd | 1372 | return output_cat_field(f, j, flags, prio, "MESSAGE", highlight); |
4d5d1bba | 1373 | |
90e74a66 | 1374 | SET_FOREACH(field, output_fields) { |
e3eec1fd | 1375 | r = output_cat_field(f, j, flags, prio, field, streq(field, "MESSAGE") ? highlight : NULL); |
4d5d1bba LP |
1376 | if (r < 0) |
1377 | return r; | |
1378 | } | |
d3f2bdbf LP |
1379 | |
1380 | return 0; | |
1381 | } | |
1382 | ||
4e30b87d | 1383 | typedef int (*output_func_t)( |
08ace05b | 1384 | FILE *f, |
f2a3de01 | 1385 | sd_journal *j, |
08ace05b LP |
1386 | OutputMode mode, |
1387 | unsigned n_columns, | |
cc25a67e | 1388 | OutputFlags flags, |
2f063186 | 1389 | const Set *output_fields, |
275e6be0 | 1390 | const size_t highlight[2], |
63d2c755 YW |
1391 | dual_timestamp *previous_display_ts, |
1392 | sd_id128_t *previous_boot_id); | |
4e30b87d | 1393 | |
08ace05b | 1394 | |
4e30b87d | 1395 | static output_func_t output_funcs[_OUTPUT_MODE_MAX] = { |
71b7a6c4 ZJS |
1396 | [OUTPUT_SHORT] = output_short, |
1397 | [OUTPUT_SHORT_ISO] = output_short, | |
7e563bfc | 1398 | [OUTPUT_SHORT_ISO_PRECISE] = output_short, |
71b7a6c4 ZJS |
1399 | [OUTPUT_SHORT_PRECISE] = output_short, |
1400 | [OUTPUT_SHORT_MONOTONIC] = output_short, | |
893bcd3d | 1401 | [OUTPUT_SHORT_DELTA] = output_short, |
71b7a6c4 ZJS |
1402 | [OUTPUT_SHORT_UNIX] = output_short, |
1403 | [OUTPUT_SHORT_FULL] = output_short, | |
1404 | [OUTPUT_VERBOSE] = output_verbose, | |
1405 | [OUTPUT_EXPORT] = output_export, | |
1406 | [OUTPUT_JSON] = output_json, | |
1407 | [OUTPUT_JSON_PRETTY] = output_json, | |
1408 | [OUTPUT_JSON_SSE] = output_json, | |
1409 | [OUTPUT_JSON_SEQ] = output_json, | |
1410 | [OUTPUT_CAT] = output_cat, | |
1411 | [OUTPUT_WITH_UNIT] = output_short, | |
86aa7ba4 LP |
1412 | }; |
1413 | ||
9b972c9a | 1414 | int show_journal_entry( |
08ace05b LP |
1415 | FILE *f, |
1416 | sd_journal *j, | |
1417 | OutputMode mode, | |
1418 | unsigned n_columns, | |
94e0bd7d | 1419 | OutputFlags flags, |
c5da14cd | 1420 | Set *output_fields, |
05c7d9bf | 1421 | const size_t highlight[2], |
893bcd3d | 1422 | bool *ellipsized, |
4e30b87d | 1423 | dual_timestamp *previous_display_ts, |
893bcd3d | 1424 | sd_id128_t *previous_boot_id) { |
08ace05b | 1425 | |
be327321 ZJS |
1426 | int r; |
1427 | ||
df50185b | 1428 | assert(mode >= 0); |
86aa7ba4 | 1429 | assert(mode < _OUTPUT_MODE_MAX); |
4e30b87d | 1430 | assert(previous_display_ts); |
893bcd3d | 1431 | assert(previous_boot_id); |
86aa7ba4 | 1432 | |
34a35ece LP |
1433 | if (n_columns <= 0) |
1434 | n_columns = columns(); | |
1435 | ||
4e30b87d LP |
1436 | r = output_funcs[mode]( |
1437 | f, | |
1438 | j, | |
1439 | mode, | |
1440 | n_columns, | |
1441 | flags, | |
1442 | output_fields, | |
1443 | highlight, | |
4e30b87d LP |
1444 | previous_display_ts, |
1445 | previous_boot_id); | |
893bcd3d | 1446 | |
be327321 | 1447 | if (ellipsized && r > 0) |
94e0bd7d ZJS |
1448 | *ellipsized = true; |
1449 | ||
be327321 | 1450 | return r; |
86aa7ba4 LP |
1451 | } |
1452 | ||
ea6c2dd1 LP |
1453 | static int maybe_print_begin_newline(FILE *f, OutputFlags *flags) { |
1454 | assert(f); | |
1455 | assert(flags); | |
1456 | ||
1457 | if (!(*flags & OUTPUT_BEGIN_NEWLINE)) | |
1458 | return 0; | |
1459 | ||
1460 | /* Print a beginning new line if that's request, but only once | |
1461 | * on the first line we print. */ | |
1462 | ||
1463 | fputc('\n', f); | |
1464 | *flags &= ~OUTPUT_BEGIN_NEWLINE; | |
1465 | return 0; | |
1466 | } | |
1467 | ||
889e3960 ZJS |
1468 | int show_journal( |
1469 | FILE *f, | |
1470 | sd_journal *j, | |
1471 | OutputMode mode, | |
1472 | unsigned n_columns, | |
1473 | usec_t not_before, | |
1474 | unsigned how_many, | |
1475 | OutputFlags flags, | |
1476 | bool *ellipsized) { | |
86aa7ba4 | 1477 | |
86aa7ba4 | 1478 | int r; |
df50185b LP |
1479 | unsigned line = 0; |
1480 | bool need_seek = false; | |
085d7120 | 1481 | int warn_cutoff = flags & OUTPUT_WARN_CUTOFF; |
4e30b87d | 1482 | dual_timestamp previous_display_ts = DUAL_TIMESTAMP_NULL; |
893bcd3d | 1483 | sd_id128_t previous_boot_id = SD_ID128_NULL; |
86aa7ba4 | 1484 | |
1a6c43e9 | 1485 | assert(j); |
df50185b LP |
1486 | assert(mode >= 0); |
1487 | assert(mode < _OUTPUT_MODE_MAX); | |
1946b0bd | 1488 | |
f5fbe71d | 1489 | if (how_many == UINT_MAX) |
889e3960 ZJS |
1490 | need_seek = true; |
1491 | else { | |
1492 | /* Seek to end */ | |
1493 | r = sd_journal_seek_tail(j); | |
1494 | if (r < 0) | |
1495 | return log_error_errno(r, "Failed to seek to tail: %m"); | |
86aa7ba4 | 1496 | |
889e3960 ZJS |
1497 | r = sd_journal_previous_skip(j, how_many); |
1498 | if (r < 0) | |
1499 | return log_error_errno(r, "Failed to skip previous: %m"); | |
1500 | } | |
86aa7ba4 | 1501 | |
df50185b | 1502 | for (;;) { |
27f31daf | 1503 | usec_t usec; |
df50185b | 1504 | |
27f31daf HD |
1505 | if (need_seek) { |
1506 | r = sd_journal_next(j); | |
1507 | if (r < 0) | |
1508 | return log_error_errno(r, "Failed to iterate through journal: %m"); | |
1509 | } | |
df50185b | 1510 | |
27f31daf HD |
1511 | if (r == 0) |
1512 | break; | |
df50185b | 1513 | |
27f31daf | 1514 | need_seek = true; |
df50185b | 1515 | |
27f31daf HD |
1516 | if (not_before > 0) { |
1517 | r = sd_journal_get_monotonic_usec(j, &usec, NULL); | |
df50185b | 1518 | |
27f31daf HD |
1519 | /* -ESTALE is returned if the timestamp is not from this boot */ |
1520 | if (r == -ESTALE) | |
1521 | continue; | |
b1cc0822 | 1522 | if (r < 0) |
27f31daf | 1523 | return log_error_errno(r, "Failed to get journal time: %m"); |
df50185b | 1524 | |
27f31daf HD |
1525 | if (usec < not_before) |
1526 | continue; | |
df50185b LP |
1527 | } |
1528 | ||
27f31daf HD |
1529 | line++; |
1530 | maybe_print_begin_newline(f, &flags); | |
08984293 | 1531 | |
4e30b87d LP |
1532 | r = show_journal_entry( |
1533 | f, | |
1534 | j, | |
1535 | mode, | |
1536 | n_columns, | |
1537 | flags, | |
1538 | /* output_fields= */ NULL, | |
1539 | /* highlight= */ NULL, | |
1540 | ellipsized, | |
1541 | &previous_display_ts, | |
1542 | &previous_boot_id); | |
27f31daf HD |
1543 | if (r < 0) |
1544 | return r; | |
1545 | } | |
08984293 | 1546 | |
27f31daf HD |
1547 | if (warn_cutoff && line < how_many && not_before > 0) { |
1548 | sd_id128_t boot_id; | |
1549 | usec_t cutoff = 0; | |
08984293 | 1550 | |
27f31daf | 1551 | /* Check whether the cutoff line is too early */ |
1a8f0ce6 | 1552 | |
27f31daf HD |
1553 | r = sd_id128_get_boot(&boot_id); |
1554 | if (r < 0) | |
1555 | return log_error_errno(r, "Failed to get boot id: %m"); | |
1a8f0ce6 | 1556 | |
27f31daf HD |
1557 | r = sd_journal_get_cutoff_monotonic_usec(j, boot_id, &cutoff, NULL); |
1558 | if (r < 0) | |
1559 | return log_error_errno(r, "Failed to get journal cutoff time: %m"); | |
1a8f0ce6 | 1560 | |
27f31daf HD |
1561 | if (r > 0 && not_before < cutoff) { |
1562 | maybe_print_begin_newline(f, &flags); | |
08984293 | 1563 | |
27f31daf HD |
1564 | /* If we logged *something* and no permission error happened, than we can reliably |
1565 | * emit the warning about rotation. If we didn't log anything and access errors | |
1566 | * happened, emit hint about permissions. Otherwise, give a generic message, since we | |
1567 | * can't diagnose the issue. */ | |
08984293 | 1568 | |
27f31daf | 1569 | bool noaccess = journal_access_blocked(j); |
86aa7ba4 | 1570 | |
27f31daf | 1571 | if (line == 0 && noaccess) |
17e90001 | 1572 | fprintf(f, "Warning: some journal files were not opened due to insufficient permissions.\n"); |
27f31daf | 1573 | else if (!noaccess) |
71311efe | 1574 | fprintf(f, "Notice: journal has been rotated since unit was started, output may be incomplete.\n"); |
27f31daf HD |
1575 | else |
1576 | fprintf(f, "Warning: journal has been rotated since unit was started and some journal " | |
1577 | "files were not opened due to insufficient permissions, output may be incomplete.\n"); | |
1578 | } | |
df50185b | 1579 | |
27f31daf | 1580 | warn_cutoff = false; |
86aa7ba4 LP |
1581 | } |
1582 | ||
b56d608e | 1583 | return 0; |
1a6c43e9 MT |
1584 | } |
1585 | ||
886a64fe | 1586 | int add_matches_for_unit(sd_journal *j, const char *unit) { |
1a6c43e9 MT |
1587 | int r; |
1588 | ||
886a64fe | 1589 | assert(j); |
1a6c43e9 MT |
1590 | assert(unit); |
1591 | ||
bcd558f1 | 1592 | (void) ( |
886a64fe | 1593 | /* Look for messages from the service itself */ |
bcd558f1 | 1594 | (r = journal_add_match_pair(j, "_SYSTEMD_UNIT", unit)) || |
886a64fe ZJS |
1595 | |
1596 | /* Look for coredumps of the service */ | |
1597 | (r = sd_journal_add_disjunction(j)) || | |
e1771c8e LP |
1598 | (r = sd_journal_add_match(j, "MESSAGE_ID=fc2e22bc6ee647b6b90729ab34a250b1", SIZE_MAX)) || |
1599 | (r = sd_journal_add_match(j, "_UID=0", SIZE_MAX)) || | |
bcd558f1 | 1600 | (r = journal_add_match_pair(j, "COREDUMP_UNIT", unit)) || |
886a64fe ZJS |
1601 | |
1602 | /* Look for messages from PID 1 about this service */ | |
1603 | (r = sd_journal_add_disjunction(j)) || | |
e1771c8e | 1604 | (r = sd_journal_add_match(j, "_PID=1", SIZE_MAX)) || |
bcd558f1 | 1605 | (r = journal_add_match_pair(j, "UNIT", unit)) || |
2d0b2e87 ZJS |
1606 | |
1607 | /* Look for messages from authorized daemons about this service */ | |
1608 | (r = sd_journal_add_disjunction(j)) || | |
e1771c8e | 1609 | (r = sd_journal_add_match(j, "_UID=0", SIZE_MAX)) || |
bcd558f1 | 1610 | (r = journal_add_match_pair(j, "OBJECT_SYSTEMD_UNIT", unit)) |
886a64fe | 1611 | ); |
2d0b2e87 | 1612 | |
bcd558f1 | 1613 | if (r == 0 && endswith(unit, ".slice")) |
69ae3ee0 | 1614 | /* Show all messages belonging to a slice */ |
bcd558f1 | 1615 | (void) ( |
69ae3ee0 | 1616 | (r = sd_journal_add_disjunction(j)) || |
bcd558f1 YW |
1617 | (r = journal_add_match_pair(j, "_SYSTEMD_SLICE", unit)) |
1618 | ); | |
69ae3ee0 | 1619 | |
886a64fe ZJS |
1620 | return r; |
1621 | } | |
1a6c43e9 | 1622 | |
8ac0810f YW |
1623 | int add_matches_for_user_unit(sd_journal *j, const char *unit) { |
1624 | uid_t uid = getuid(); | |
886a64fe | 1625 | int r; |
1a6c43e9 | 1626 | |
886a64fe ZJS |
1627 | assert(j); |
1628 | assert(unit); | |
1a6c43e9 | 1629 | |
886a64fe ZJS |
1630 | (void) ( |
1631 | /* Look for messages from the user service itself */ | |
bcd558f1 YW |
1632 | (r = journal_add_match_pair(j, "_SYSTEMD_USER_UNIT", unit)) || |
1633 | (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || | |
886a64fe ZJS |
1634 | |
1635 | /* Look for messages from systemd about this service */ | |
1636 | (r = sd_journal_add_disjunction(j)) || | |
bcd558f1 YW |
1637 | (r = journal_add_match_pair(j, "USER_UNIT", unit)) || |
1638 | (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || | |
886a64fe ZJS |
1639 | |
1640 | /* Look for coredumps of the service */ | |
1641 | (r = sd_journal_add_disjunction(j)) || | |
bcd558f1 YW |
1642 | (r = journal_add_match_pair(j, "COREDUMP_USER_UNIT", unit)) || |
1643 | (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || | |
e1771c8e | 1644 | (r = sd_journal_add_match(j, "_UID=0", SIZE_MAX)) || |
2d0b2e87 ZJS |
1645 | |
1646 | /* Look for messages from authorized daemons about this service */ | |
1647 | (r = sd_journal_add_disjunction(j)) || | |
bcd558f1 YW |
1648 | (r = journal_add_match_pair(j, "OBJECT_SYSTEMD_USER_UNIT", unit)) || |
1649 | (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) || | |
e1771c8e | 1650 | (r = sd_journal_add_match(j, "_UID=0", SIZE_MAX)) |
886a64fe | 1651 | ); |
69ae3ee0 | 1652 | |
bcd558f1 | 1653 | if (r == 0 && endswith(unit, ".slice")) |
69ae3ee0 | 1654 | /* Show all messages belonging to a slice */ |
bcd558f1 | 1655 | (void) ( |
69ae3ee0 | 1656 | (r = sd_journal_add_disjunction(j)) || |
bcd558f1 YW |
1657 | (r = journal_add_match_pair(j, "_SYSTEMD_USER_SLICE", unit)) || |
1658 | (r = journal_add_matchf(j, "_UID="UID_FMT, uid)) | |
1659 | ); | |
69ae3ee0 | 1660 | |
1a6c43e9 MT |
1661 | return r; |
1662 | } | |
1663 | ||
c93d3c05 | 1664 | int add_match_boot_id(sd_journal *j, sd_id128_t id) { |
b6741478 LP |
1665 | int r; |
1666 | ||
c93d3c05 | 1667 | assert(j); |
b6741478 | 1668 | |
0aad30d4 YW |
1669 | if (sd_id128_is_null(id)) { |
1670 | r = sd_id128_get_boot(&id); | |
6c767d1e | 1671 | if (r < 0) |
0aad30d4 | 1672 | return log_error_errno(r, "Failed to get boot ID: %m"); |
b6741478 LP |
1673 | } |
1674 | ||
0aad30d4 | 1675 | r = journal_add_match_pair(j, "_BOOT_ID", SD_ID128_TO_STRING(id)); |
b6741478 | 1676 | if (r < 0) |
0aad30d4 | 1677 | return log_error_errno(r, "Failed to add match: %m"); |
b6741478 LP |
1678 | |
1679 | return 0; | |
1680 | } | |
1681 | ||
1682 | int add_match_this_boot(sd_journal *j, const char *machine) { | |
5ec76417 ZJS |
1683 | sd_id128_t boot_id; |
1684 | int r; | |
1685 | ||
1686 | assert(j); | |
1687 | ||
8e976dc9 YW |
1688 | r = id128_get_boot_for_machine(machine, &boot_id); |
1689 | if (r < 0) | |
1690 | return log_error_errno(r, "Failed to get boot ID%s%s: %m", | |
131701d1 | 1691 | isempty(machine) ? "" : " of container ", strempty(machine)); |
5ec76417 | 1692 | |
c93d3c05 | 1693 | r = add_match_boot_id(j, boot_id); |
f647962d | 1694 | if (r < 0) |
0aad30d4 | 1695 | return r; |
5ec76417 ZJS |
1696 | |
1697 | r = sd_journal_add_conjunction(j); | |
1698 | if (r < 0) | |
b56d608e | 1699 | return log_error_errno(r, "Failed to add conjunction: %m"); |
5ec76417 ZJS |
1700 | |
1701 | return 0; | |
1702 | } | |
1703 | ||
886a64fe | 1704 | int show_journal_by_unit( |
1a6c43e9 MT |
1705 | FILE *f, |
1706 | const char *unit, | |
d93dda3a | 1707 | const char *log_namespace, |
1a6c43e9 MT |
1708 | OutputMode mode, |
1709 | unsigned n_columns, | |
1710 | usec_t not_before, | |
1711 | unsigned how_many, | |
886a64fe | 1712 | OutputFlags flags, |
3c756001 LP |
1713 | int journal_open_flags, |
1714 | bool system_unit, | |
94e0bd7d | 1715 | bool *ellipsized) { |
1a6c43e9 | 1716 | |
4afd3348 | 1717 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
1a6c43e9 MT |
1718 | int r; |
1719 | ||
1720 | assert(mode >= 0); | |
1721 | assert(mode < _OUTPUT_MODE_MAX); | |
1722 | assert(unit); | |
1723 | ||
1a6c43e9 MT |
1724 | if (how_many <= 0) |
1725 | return 0; | |
1726 | ||
f7f062bf YW |
1727 | r = sd_journal_open_namespace(&j, log_namespace, |
1728 | journal_open_flags | | |
1729 | SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE | | |
1730 | SD_JOURNAL_ASSUME_IMMUTABLE); | |
f9045468 | 1731 | if (r < 0) |
b56d608e | 1732 | return log_error_errno(r, "Failed to open journal: %m"); |
f9045468 | 1733 | |
3c756001 | 1734 | if (system_unit) |
886a64fe ZJS |
1735 | r = add_matches_for_unit(j, unit); |
1736 | else | |
8ac0810f | 1737 | r = add_matches_for_user_unit(j, unit); |
1a6c43e9 | 1738 | if (r < 0) |
b56d608e | 1739 | return log_error_errno(r, "Failed to add unit matches: %m"); |
1a6c43e9 | 1740 | |
f8202704 VC |
1741 | r = sd_journal_add_conjunction(j); |
1742 | if (r < 0) | |
1743 | return log_error_errno(r, "Failed to add conjunction: %m"); | |
1744 | ||
1745 | r = add_match_this_boot(j, NULL); | |
1746 | if (r < 0) | |
1747 | return r; | |
1748 | ||
f1d34068 | 1749 | if (DEBUG_LOGGING) { |
c2b2df60 | 1750 | _cleanup_free_ char *filter = NULL; |
4ad16808 ZJS |
1751 | |
1752 | filter = journal_make_match_string(j); | |
b56d608e LP |
1753 | if (!filter) |
1754 | return log_oom(); | |
1755 | ||
4ad16808 ZJS |
1756 | log_debug("Journal filter: %s", filter); |
1757 | } | |
5ec76417 | 1758 | |
94e0bd7d | 1759 | return show_journal(f, j, mode, n_columns, not_before, how_many, flags, ellipsized); |
86aa7ba4 | 1760 | } |
8081939d YW |
1761 | |
1762 | static int discover_next_boot( | |
1763 | sd_journal *j, | |
1764 | sd_id128_t previous_boot_id, | |
1765 | bool advance_older, | |
1766 | BootId *ret) { | |
1767 | ||
185e2016 | 1768 | _cleanup_set_free_ Set *broken_ids = NULL; |
8081939d YW |
1769 | int r; |
1770 | ||
1771 | assert(j); | |
1772 | assert(ret); | |
1773 | ||
1774 | /* We expect the journal to be on the last position of a boot | |
1775 | * (in relation to the direction we are going), so that the next | |
1776 | * invocation of sd_journal_next/previous will be from a different | |
1777 | * boot. We then collect any information we desire and then jump | |
1778 | * to the last location of the new boot by using a _BOOT_ID match | |
1779 | * coming from the other journal direction. */ | |
1780 | ||
1781 | /* Make sure we aren't restricted by any _BOOT_ID matches, so that | |
1782 | * we can actually advance to a *different* boot. */ | |
1783 | sd_journal_flush_matches(j); | |
1784 | ||
185e2016 YW |
1785 | for (;;) { |
1786 | sd_id128_t *id_dup; | |
1787 | BootId boot; | |
1788 | ||
0141b214 | 1789 | r = sd_journal_step_one(j, !advance_older); |
8081939d YW |
1790 | if (r < 0) |
1791 | return r; | |
0141b214 | 1792 | if (r == 0) { |
87dfaba7 | 1793 | sd_journal_flush_matches(j); |
8081939d YW |
1794 | *ret = (BootId) {}; |
1795 | return 0; /* End of journal, yay. */ | |
1796 | } | |
1797 | ||
1798 | r = sd_journal_get_monotonic_usec(j, NULL, &boot.id); | |
1799 | if (r < 0) | |
1800 | return r; | |
1801 | ||
1802 | /* We iterate through this in a loop, until the boot ID differs from the previous one. Note that | |
1803 | * normally, this will only require a single iteration, as we moved to the last entry of the previous | |
1804 | * boot entry already. However, it might happen that the per-journal-field entry arrays are less | |
1805 | * complete than the main entry array, and hence might reference an entry that's not actually the last | |
1806 | * one of the boot ID as last one. Let's hence use the per-field array is initial seek position to | |
1807 | * speed things up, but let's not trust that it is complete, and hence, manually advance as | |
1808 | * necessary. */ | |
1809 | ||
185e2016 YW |
1810 | if (!sd_id128_is_null(previous_boot_id) && sd_id128_equal(boot.id, previous_boot_id)) |
1811 | continue; | |
8081939d | 1812 | |
185e2016 YW |
1813 | if (set_contains(broken_ids, &boot.id)) |
1814 | continue; | |
8081939d | 1815 | |
185e2016 YW |
1816 | /* Yay, we found a new boot ID from the entry object. Let's check there exist corresponding |
1817 | * entries matching with the _BOOT_ID= data. */ | |
8081939d | 1818 | |
185e2016 YW |
1819 | r = add_match_boot_id(j, boot.id); |
1820 | if (r < 0) | |
1821 | return r; | |
8081939d | 1822 | |
185e2016 YW |
1823 | /* First, seek to the first (or the last when we are going upwards) occurrence of this boot ID. |
1824 | * You may think this is redundant. Yes, that's redundant unless the journal is corrupted. | |
1825 | * But when the journal is corrupted, especially, badly 'truncated', then the below may fail. | |
1826 | * See https://github.com/systemd/systemd/pull/29334#issuecomment-1736567951. */ | |
1827 | if (advance_older) | |
1828 | r = sd_journal_seek_tail(j); | |
1829 | else | |
1830 | r = sd_journal_seek_head(j); | |
1831 | if (r < 0) | |
1832 | return r; | |
8081939d | 1833 | |
185e2016 YW |
1834 | r = sd_journal_step_one(j, 0); |
1835 | if (r < 0) | |
1836 | return r; | |
1837 | if (r == 0) { | |
1838 | log_debug("Whoopsie! We found a boot ID %s but can't read its first entry. " | |
1839 | "The journal seems to be corrupted. Ignoring the boot ID.", | |
1840 | SD_ID128_TO_STRING(boot.id)); | |
1841 | goto try_again; | |
1842 | } | |
8081939d | 1843 | |
afcd9c60 | 1844 | r = sd_journal_get_realtime_usec(j, advance_older ? &boot.last_usec : &boot.first_usec); |
185e2016 YW |
1845 | if (r < 0) |
1846 | return r; | |
1847 | ||
1848 | /* Next, seek to the last occurrence of this boot ID. */ | |
1849 | if (advance_older) | |
1850 | r = sd_journal_seek_head(j); | |
1851 | else | |
1852 | r = sd_journal_seek_tail(j); | |
1853 | if (r < 0) | |
1854 | return r; | |
1855 | ||
1856 | r = sd_journal_step_one(j, 0); | |
1857 | if (r < 0) | |
1858 | return r; | |
1859 | if (r == 0) { | |
1860 | log_debug("Whoopsie! We found a boot ID %s but can't read its last entry. " | |
1861 | "The journal seems to be corrupted. Ignoring the boot ID.", | |
1862 | SD_ID128_TO_STRING(boot.id)); | |
1863 | goto try_again; | |
1864 | } | |
1865 | ||
afcd9c60 | 1866 | r = sd_journal_get_realtime_usec(j, advance_older ? &boot.first_usec : &boot.last_usec); |
185e2016 YW |
1867 | if (r < 0) |
1868 | return r; | |
1869 | ||
1870 | sd_journal_flush_matches(j); | |
1871 | *ret = boot; | |
1872 | return 1; | |
1873 | ||
1874 | try_again: | |
1875 | /* Save the bad boot ID. */ | |
1876 | id_dup = newdup(sd_id128_t, &boot.id, 1); | |
1877 | if (!id_dup) | |
1878 | return -ENOMEM; | |
1879 | ||
1880 | r = set_ensure_consume(&broken_ids, &id128_hash_ops_free, id_dup); | |
1881 | if (r < 0) | |
1882 | return r; | |
1883 | ||
1884 | /* Move to the previous position again. */ | |
1885 | sd_journal_flush_matches(j); | |
1886 | ||
1887 | if (!sd_id128_is_null(previous_boot_id)) { | |
1888 | r = add_match_boot_id(j, previous_boot_id); | |
1889 | if (r < 0) | |
1890 | return r; | |
1891 | } | |
1892 | ||
1893 | if (advance_older) | |
1894 | r = sd_journal_seek_head(j); | |
1895 | else | |
1896 | r = sd_journal_seek_tail(j); | |
1897 | if (r < 0) | |
1898 | return r; | |
1899 | ||
1900 | r = sd_journal_step_one(j, 0); | |
1901 | if (r < 0) | |
1902 | return r; | |
1903 | if (r == 0) | |
1904 | return log_debug_errno(SYNTHETIC_ERRNO(ENODATA), | |
1905 | "Whoopsie! Cannot seek to the last entry of boot %s.", | |
1906 | SD_ID128_TO_STRING(previous_boot_id)); | |
1907 | ||
1908 | sd_journal_flush_matches(j); | |
1909 | } | |
8081939d YW |
1910 | } |
1911 | ||
a4675155 | 1912 | int journal_find_boot(sd_journal *j, sd_id128_t boot_id, int offset, sd_id128_t *ret) { |
8081939d | 1913 | bool advance_older; |
a4675155 | 1914 | int r, offset_start; |
8081939d YW |
1915 | |
1916 | assert(j); | |
1917 | assert(ret); | |
1918 | ||
1919 | /* Adjust for the asymmetry that offset 0 is the last (and current) boot, while 1 is considered the | |
1920 | * (chronological) first boot in the journal. */ | |
1921 | advance_older = offset <= 0; | |
1922 | ||
87dfaba7 YW |
1923 | sd_journal_flush_matches(j); |
1924 | ||
a4675155 YW |
1925 | if (!sd_id128_is_null(boot_id)) { |
1926 | r = add_match_boot_id(j, boot_id); | |
1927 | if (r < 0) | |
1928 | return r; | |
8081939d | 1929 | |
a4675155 YW |
1930 | if (advance_older) |
1931 | r = sd_journal_seek_head(j); /* seek to oldest */ | |
1932 | else | |
1933 | r = sd_journal_seek_tail(j); /* seek to newest */ | |
1934 | if (r < 0) | |
1935 | return r; | |
8081939d | 1936 | |
a4675155 YW |
1937 | r = sd_journal_step_one(j, advance_older); |
1938 | if (r < 0) | |
1939 | return r; | |
1940 | if (r == 0) { | |
1941 | sd_journal_flush_matches(j); | |
1942 | *ret = SD_ID128_NULL; | |
1943 | return false; | |
1944 | } | |
1945 | if (offset == 0) { | |
1946 | /* If boot ID is specified without an offset, then let's short cut the loop below. */ | |
1947 | sd_journal_flush_matches(j); | |
1948 | *ret = boot_id; | |
1949 | return true; | |
1950 | } | |
1951 | ||
1952 | offset_start = advance_older ? -1 : 1; | |
1953 | } else { | |
1954 | if (advance_older) | |
1955 | r = sd_journal_seek_tail(j); /* seek to newest */ | |
1956 | else | |
1957 | r = sd_journal_seek_head(j); /* seek to oldest */ | |
1958 | if (r < 0) | |
1959 | return r; | |
1960 | ||
1961 | offset_start = advance_older ? 0 : 1; | |
1962 | } | |
1963 | ||
1964 | /* At this point the cursor is positioned at the newest/oldest entry of the reference boot ID if | |
1965 | * specified, or whole journal otherwise. The next invocation of _previous()/_next() will hence | |
1966 | * position us at the newest/oldest entry we have. */ | |
1967 | ||
1968 | for (int off = offset_start; ; off += advance_older ? -1 : 1) { | |
8081939d YW |
1969 | BootId boot; |
1970 | ||
1971 | r = discover_next_boot(j, boot_id, advance_older, &boot); | |
1972 | if (r < 0) | |
1973 | return r; | |
1974 | if (r == 0) { | |
1975 | *ret = SD_ID128_NULL; | |
1976 | return false; | |
1977 | } | |
1978 | ||
1979 | boot_id = boot.id; | |
1980 | log_debug("Found boot ID %s by offset %i", SD_ID128_TO_STRING(boot_id), off); | |
1981 | ||
a4675155 YW |
1982 | if (off == offset) { |
1983 | *ret = boot_id; | |
1984 | return true; | |
1985 | } | |
8081939d | 1986 | } |
8081939d YW |
1987 | } |
1988 | ||
1989 | int journal_get_boots(sd_journal *j, BootId **ret_boots, size_t *ret_n_boots) { | |
1990 | _cleanup_free_ BootId *boots = NULL; | |
1991 | size_t n_boots = 0; | |
1992 | int r; | |
1993 | ||
1994 | assert(j); | |
1995 | assert(ret_boots); | |
1996 | assert(ret_n_boots); | |
1997 | ||
87dfaba7 YW |
1998 | sd_journal_flush_matches(j); |
1999 | ||
8081939d YW |
2000 | r = sd_journal_seek_head(j); /* seek to oldest */ |
2001 | if (r < 0) | |
2002 | return r; | |
2003 | ||
2004 | /* No sd_journal_next()/_previous() here. | |
2005 | * | |
2006 | * At this point the read pointer is positioned before the oldest entry in the whole journal. The | |
2007 | * next invocation of _next() will hence position us at the oldest entry we have. */ | |
2008 | ||
2009 | sd_id128_t previous_boot_id = SD_ID128_NULL; | |
2010 | for (;;) { | |
2011 | BootId boot; | |
2012 | ||
2013 | r = discover_next_boot(j, previous_boot_id, /* advance_older = */ false, &boot); | |
2014 | if (r < 0) | |
2015 | return r; | |
2016 | if (r == 0) | |
2017 | break; | |
2018 | ||
2019 | previous_boot_id = boot.id; | |
2020 | ||
2021 | FOREACH_ARRAY(i, boots, n_boots) | |
2022 | if (sd_id128_equal(i->id, boot.id)) | |
2023 | /* The boot id is already stored, something wrong with the journal files. | |
2024 | * Exiting as otherwise this problem would cause an infinite loop. */ | |
1e8c0c67 | 2025 | goto finish; |
8081939d YW |
2026 | |
2027 | if (!GREEDY_REALLOC(boots, n_boots + 1)) | |
2028 | return -ENOMEM; | |
2029 | ||
2030 | boots[n_boots++] = boot; | |
2031 | } | |
2032 | ||
1e8c0c67 | 2033 | finish: |
8081939d YW |
2034 | *ret_boots = TAKE_PTR(boots); |
2035 | *ret_n_boots = n_boots; | |
2036 | return n_boots > 0; | |
2037 | } |