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