HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/shared/logs-show.c
Git blob: 513d76558e31319a9b649382f7cde8d9726f9564
Lines 2001–2347 of 2,347
if (r < 0)
return r;
r = sd_journal_add_conjunction(j);
if (r < 0)
return r;
}
if (!sd_id128_is_null(id))
return add_matches_for_invocation_id(j, id);
if (type == LOG_SYSTEM_UNIT_INVOCATION_ID)
return add_matches_for_unit_full(j, /* flags= */ 0, unit);
if (type == LOG_USER_UNIT_INVOCATION_ID)
return add_matches_for_user_unit_full(j, /* flags= */ 0, UID_INVALID, unit);
return -EINVAL;
}
int discover_next_id(
sd_journal *j,
LogIdType type,
sd_id128_t boot_id, /* optional, used when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID */
const char *unit, /* mandatory when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID */
sd_id128_t previous_id,
bool advance_older,
LogId *ret) {
_cleanup_set_free_ Set *broken_ids = NULL;
int r;
assert(j);
assert(type >= 0 && type < _LOG_ID_TYPE_MAX);
assert(type == LOG_BOOT_ID || unit);
assert(ret);
/* We expect the journal to be on the last position of a boot
* (in relation to the direction we are going), so that the next
* invocation of sd_journal_next/previous will be from a different
* boot. We then collect any information we desire and then jump
* to the last location of the new boot by using a _BOOT_ID match
* coming from the other journal direction. */
/* Make sure we aren't restricted by any _BOOT_ID matches, so that
* we can actually advance to a *different* boot. */
r = set_matches_for_discover_id(j, type, boot_id, unit, SD_ID128_NULL);
if (r < 0)
return r;
for (;;) {
sd_id128_t *id_dup;
LogId id;
r = sd_journal_step_one(j, !advance_older);
if (r < 0)
return r;
if (r == 0) {
sd_journal_flush_matches(j);
*ret = (LogId) {};
return 0; /* End of journal, yay. */
}
if (type == LOG_BOOT_ID)
r = sd_journal_get_monotonic_usec(j, NULL, &id.id);
else
r = journal_get_invocation_id(j, &id.id);
if (r < 0)
return r;
if (sd_id128_is_null(id.id))
continue;
/* We iterate through this in a loop, until the boot or invocation ID differs from the
* previous one. Note that normally, this will only require a single iteration, as we moved
* to the last entry of the previous boot or invocation entry already. However, it might
* happen that the per-journal-field entry arrays are less complete than the main entry
* array, and hence might reference an entry that's not actually the last one of the boot or
* invocation ID as last one. Let's hence use the per-field array is initial seek position to
* speed things up, but let's not trust that it is complete, and hence, manually advance as
* necessary. */
if (!sd_id128_is_null(previous_id) && sd_id128_equal(id.id, previous_id))
continue;
if (set_contains(broken_ids, &id.id))
continue;
/* Yay, we found a new boot or invocation ID from the entry object. Let's check there exist
* corresponding entries matching with the _BOOT_ID=, INVOCATION_ID= or friends data. */
r = set_matches_for_discover_id(j, type, boot_id, unit, id.id);
if (r < 0)
return r;
/* First, seek to the first (or the last when we are going upwards) occurrence of this boot
* or invocation ID. You may think this is redundant. Yes, that's redundant unless the
* journal is corrupted. But when the journal is corrupted, especially, badly 'truncated',
* then the below may fail.
* See https://github.com/systemd/systemd/pull/29334#issuecomment-1736567951. */
if (advance_older)
r = sd_journal_seek_tail(j);
else
r = sd_journal_seek_head(j);
if (r < 0)
return r;
r = sd_journal_step_one(j, 0);
if (r < 0)
return r;
if (r == 0) {
log_debug("Whoopsie! We found a %s %s but can't read its first entry. "
"The journal seems to be corrupted. Ignoring the %s.",
log_id_type_to_string(type),
SD_ID128_TO_STRING(id.id),
log_id_type_to_string(type));
goto try_again;
}
r = sd_journal_get_realtime_usec(j, advance_older ? &id.last_usec : &id.first_usec);
if (r < 0)
return r;
/* Next, seek to the last occurrence of this boot or invocation ID. */
if (advance_older)
r = sd_journal_seek_head(j);
else
r = sd_journal_seek_tail(j);
if (r < 0)
return r;
r = sd_journal_step_one(j, 0);
if (r < 0)
return r;
if (r == 0) {
log_debug("Whoopsie! We found a %s %s but can't read its last entry. "
"The journal seems to be corrupted. Ignoring the %s.",
log_id_type_to_string(type),
SD_ID128_TO_STRING(id.id),
log_id_type_to_string(type));
goto try_again;
}
r = sd_journal_get_realtime_usec(j, advance_older ? &id.first_usec : &id.last_usec);
if (r < 0)
return r;
sd_journal_flush_matches(j);
*ret = id;
return 1;
try_again:
/* Save the bad boot or invocation ID. */
id_dup = newdup(sd_id128_t, &id.id, 1);
if (!id_dup)
return -ENOMEM;
r = set_ensure_consume(&broken_ids, &id128_hash_ops_free, id_dup);
if (r < 0)
return r;
/* Move to the previous position again. */
r = set_matches_for_discover_id(j, type, boot_id, unit, previous_id);
if (r < 0)
return r;
if (advance_older)
r = sd_journal_seek_head(j);
else
r = sd_journal_seek_tail(j);
if (r < 0)
return r;
r = sd_journal_step_one(j, 0);
if (r < 0)
return r;
if (r == 0)
return log_debug_errno(SYNTHETIC_ERRNO(ENODATA),
"Whoopsie! Cannot seek to the last entry of %s %s.",
log_id_type_to_string(type),
SD_ID128_TO_STRING(previous_id));
sd_journal_flush_matches(j);
}
}
int journal_find_log_id(
sd_journal *j,
LogIdType type,
sd_id128_t boot_id, /* optional, used when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID */
const char *unit, /* mandatory when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID
* unless an invocation ID is explicitly specified without an offset. */
sd_id128_t previous_id,
int offset,
sd_id128_t *ret) {
bool advance_older;
int r, offset_start;
assert(j);
assert(type >= 0 && type < _LOG_ID_TYPE_MAX);
assert(type == LOG_BOOT_ID || (!sd_id128_is_null(previous_id) && offset == 0) || unit);
assert(ret);
/* Adjust for the asymmetry that offset 0 is the last (and current) boot or invocation, while 1 is
* considered the (chronological) first boot or invocation in the journal. */
advance_older = offset <= 0;
r = set_matches_for_discover_id(j, type, boot_id, unit, previous_id);
if (r < 0)
return r;
if (!sd_id128_is_null(previous_id)) {
if (advance_older)
r = sd_journal_seek_head(j); /* seek to oldest */
else
r = sd_journal_seek_tail(j); /* seek to newest */
if (r < 0)
return r;
r = sd_journal_step_one(j, advance_older);
if (r < 0)
return r;
if (r == 0) {
sd_journal_flush_matches(j);
*ret = SD_ID128_NULL;
return false;
}
if (offset == 0) {
/* If a non-null ID is specified without an offset, then let's short cut the loop below. */
sd_journal_flush_matches(j);
*ret = previous_id;
return true;
}
offset_start = advance_older ? -1 : 1;
} else {
if (advance_older)
r = sd_journal_seek_tail(j); /* seek to newest */
else
r = sd_journal_seek_head(j); /* seek to oldest */
if (r < 0)
return r;
offset_start = advance_older ? 0 : 1;
}
/* At this point the cursor is positioned at the newest/oldest entry of the reference boot or
* invocation ID if specified, or whole journal otherwise. The next invocation of _previous()/_next()
* will hence position us at the newest/oldest entry we have. */
for (int off = offset_start; ; off += advance_older ? -1 : 1) {
LogId id;
r = discover_next_id(j, type, boot_id, unit, previous_id, advance_older, &id);
if (r < 0)
return r;
if (r == 0) {
*ret = SD_ID128_NULL;
return false;
}
previous_id = id.id;
log_debug("Found %s %s by offset %i.",
log_id_type_to_string(type), SD_ID128_TO_STRING(previous_id), off);
if (off == offset) {
*ret = previous_id;
return true;
}
}
}
int journal_get_log_ids(
sd_journal *j,
LogIdType type,
sd_id128_t boot_id, /* optional, used when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID */
const char *unit, /* mandatory when type == JOURNAL_{SYSTEM,USER}_UNIT_INVOCATION_ID */
bool advance_older,
size_t max_ids,
LogId **ret_ids,
size_t *ret_n_ids) {
_cleanup_free_ LogId *ids = NULL;
size_t n_ids = 0;
int r;
assert(j);
assert(type >= 0 && type < _LOG_ID_TYPE_MAX);
assert(type == LOG_BOOT_ID || unit);
assert(ret_ids);
assert(ret_n_ids);
sd_journal_flush_matches(j);
if (advance_older)
r = sd_journal_seek_tail(j); /* seek to newest */
else
r = sd_journal_seek_head(j); /* seek to oldest */
if (r < 0)
return r;
/* No sd_journal_next()/_previous() here.
*
* At this point the read pointer is positioned before the oldest entry in the whole journal. The
* next invocation of _next() will hence position us at the oldest entry we have. */
sd_id128_t previous_id = SD_ID128_NULL;
for (;;) {
LogId id;
if (n_ids >= max_ids)
break;
r = discover_next_id(j, type, boot_id, unit, previous_id, advance_older, &id);
if (r < 0)
return r;
if (r == 0)
break;
previous_id = id.id;
FOREACH_ARRAY(i, ids, n_ids)
if (sd_id128_equal(i->id, id.id))
/* The boot or invocation ID is already stored, something wrong with the
* journal files. Exiting as otherwise this problem would cause an infinite
* loop. */
goto finish;
if (!GREEDY_REALLOC_APPEND(ids, n_ids, &id, 1))
return -ENOMEM;
}
finish:
*ret_ids = TAKE_PTR(ids);
*ret_n_ids = n_ids;
return n_ids > 0;
}
void journal_browse_prepare(void) {
/* Increase max number of open files if we can, we might needs this when browsing journal files,
* which might be split up into many files. */
(void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE);
sigbus_install();
}