HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/libsystemd/sd-journal/sd-journal.c
Git blob: 3208ce9733f473c236071d28f6f34639076b6b14
Lines 1–1000 of 3,837
/* SPDX-License-Identifier: LGPL-2.1-or-later */
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <sys/vfs.h>
#include <unistd.h>
#include "sd-journal.h"
#include "alloc-util.h"
#include "catalog.h"
#include "dirent-util.h"
#include "env-file.h"
#include "escape.h"
#include "extract-word.h"
#include "fd-util.h"
#include "fileio.h"
#include "format-util.h"
#include "hashmap.h"
#include "hostname-util.h"
#include "id128-util.h"
#include "inotify-util.h"
#include "io-util.h"
#include "journal-def.h"
#include "journal-file.h"
#include "journal-internal.h"
#include "list.h"
#include "log.h"
#include "lookup3.h"
#include "nulstr-util.h"
#include "origin-id.h"
#include "path-util.h"
#include "prioq.h"
#include "replace-var.h"
#include "set.h"
#include "sort-util.h"
#include "stat-util.h"
#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "syslog-util.h"
#include "time-util.h"
#include "uid-classification.h"
#define JOURNAL_FILES_RECHECK_USEC (2 * USEC_PER_SEC)
/* The maximum size of variable values we'll expand in catalog entries. We bind this to PATH_MAX for now, as
* we want to be able to show all officially valid paths at least */
#define REPLACE_VAR_MAX PATH_MAX
#define DEFAULT_DATA_THRESHOLD (64*1024)
DEFINE_PRIVATE_ORIGIN_ID_HELPERS(sd_journal, journal);
static void remove_file_real(sd_journal *j, JournalFile *f);
static int journal_file_read_tail_timestamp(sd_journal *j, JournalFile *f);
static void journal_file_unlink_newest_by_boot_id(sd_journal *j, JournalFile *f);
static int journal_put_error(sd_journal *j, int r, const char *path) {
_cleanup_free_ char *copy = NULL;
/* Memorize an error we encountered, and store which
* file/directory it was generated from. Note that we store
* only *one* path per error code, as the error code is the
* key into the hashmap, and the path is the value. This means
* we keep track only of all error kinds, but not of all error
* locations. This has the benefit that the hashmap cannot
* grow beyond bounds.
*
* We return an error here only if we didn't manage to
* memorize the real error. */
if (r >= 0)
return r;
if (path) {
copy = strdup(path);
if (!copy)
return -ENOMEM;
}
r = hashmap_ensure_put(&j->errors, &trivial_hash_ops_value_free, INT_TO_PTR(r), copy);
if (r == -EEXIST)
return 0;
if (r < 0)
return r;
TAKE_PTR(copy);
return 0;
}
static void detach_location(sd_journal *j) {
JournalFile *f;
assert(j);
j->current_file = NULL;
j->current_field = 0;
ORDERED_HASHMAP_FOREACH(f, j->files)
journal_file_reset_location(f);
}
static void init_location(Location *l, LocationType type, JournalFile *f, Object *o) {
assert(l);
assert(IN_SET(type, LOCATION_DISCRETE, LOCATION_SEEK));
assert(f);
*l = (Location) {
.type = type,
.seqnum = le64toh(o->entry.seqnum),
.seqnum_id = f->header->seqnum_id,
.realtime = le64toh(o->entry.realtime),
.monotonic = le64toh(o->entry.monotonic),
.boot_id = o->entry.boot_id,
.xor_hash = le64toh(o->entry.xor_hash),
.seqnum_set = true,
.realtime_set = true,
.monotonic_set = true,
.xor_hash_set = true,
};
}
static void set_location(sd_journal *j, JournalFile *f, Object *o) {
assert(j);
assert(f);
assert(o);
init_location(&j->current_location, LOCATION_DISCRETE, f, o);
j->current_file = f;
j->current_field = 0;
/* Let f know its candidate entry was picked. */
assert(f->location_type == LOCATION_SEEK);
f->location_type = LOCATION_DISCRETE;
}
static int match_is_valid(const void *data, size_t size) {
const char *b = ASSERT_PTR(data);
if (size < 2)
return false;
if (((char*) data)[0] == '_' && ((char*) data)[1] == '_')
return false;
for (const char *p = b; p < b + size; p++) {
if (*p == '=')
return p > b;
if (*p == '_')
continue;
if (*p >= 'A' && *p <= 'Z')
continue;
if (ascii_isdigit(*p))
continue;
return false;
}
return false;
}
static bool same_field(const void *_a, size_t s, const void *_b, size_t t) {
const uint8_t *a = _a, *b = _b;
for (size_t j = 0; j < s && j < t; j++) {
if (a[j] != b[j])
return false;
if (a[j] == '=')
return true;
}
assert_not_reached();
}
static Match *match_new(Match *p, MatchType t) {
Match *m;
m = new(Match, 1);
if (!m)
return NULL;
*m = (Match) {
.type = t,
.parent = p,
};
if (p)
LIST_PREPEND(matches, p->matches, m);
return m;
}
static Match *match_free(Match *m) {
assert(m);
while (m->matches)
match_free(m->matches);
if (m->parent)
LIST_REMOVE(matches, m->parent->matches, m);
free(m->data);
return mfree(m);
}
static Match *match_free_if_empty(Match *m) {
if (!m || m->matches)
return m;
return match_free(m);
}
_public_ int sd_journal_add_match(sd_journal *j, const void *data, size_t size) {
Match *add_here = NULL, *m = NULL;
uint64_t hash;
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
assert_return(data, -EINVAL);
/* If the size is unspecified, assume it's a string. Note: 0 is the public value we document for
* this, for historical reasons. Internally, we pretty widely started using SIZE_MAX for this in
* similar cases however, hence accept that too. And internally we actually prefer it, to make things
* less surprising. */
if (IN_SET(size, 0, SIZE_MAX))
size = strlen(data);
if (!match_is_valid(data, size))
return -EINVAL;
/* level 0: AND term
* level 1: OR terms
* level 2: AND terms
* level 3: OR terms
* level 4: concrete matches */
if (!j->level0) {
j->level0 = match_new(NULL, MATCH_AND_TERM);
if (!j->level0)
return -ENOMEM;
}
if (!j->level1) {
j->level1 = match_new(j->level0, MATCH_OR_TERM);
if (!j->level1)
return -ENOMEM;
}
if (!j->level2) {
j->level2 = match_new(j->level1, MATCH_AND_TERM);
if (!j->level2)
return -ENOMEM;
}
assert(j->level0->type == MATCH_AND_TERM);
assert(j->level1->type == MATCH_OR_TERM);
assert(j->level2->type == MATCH_AND_TERM);
/* Old-style Jenkins (unkeyed) hashing only here. We do not cover new-style siphash (keyed) hashing
* here, since it's different for each file, and thus can't be pre-calculated in the Match object. */
hash = jenkins_hash64(data, size);
LIST_FOREACH(matches, l3, j->level2->matches) {
assert(l3->type == MATCH_OR_TERM);
LIST_FOREACH(matches, l4, l3->matches) {
assert(l4->type == MATCH_DISCRETE);
/* Exactly the same match already? Then ignore
* this addition */
if (l4->hash == hash &&
l4->size == size &&
memcmp(l4->data, data, size) == 0)
return 0;
/* Same field? Then let's add this to this OR term */
if (same_field(data, size, l4->data, l4->size)) {
add_here = l3;
break;
}
}
if (add_here)
break;
}
if (!add_here) {
add_here = match_new(j->level2, MATCH_OR_TERM);
if (!add_here)
goto fail;
}
m = match_new(add_here, MATCH_DISCRETE);
if (!m)
goto fail;
m->hash = hash;
m->size = size;
m->data = memdup(data, size);
if (!m->data)
goto fail;
detach_location(j);
return 0;
fail:
match_free(m);
match_free_if_empty(add_here);
j->level2 = match_free_if_empty(j->level2);
j->level1 = match_free_if_empty(j->level1);
j->level0 = match_free_if_empty(j->level0);
return -ENOMEM;
}
int journal_add_match_pair(sd_journal *j, const char *field, const char *value) {
_cleanup_free_ char *s = NULL;
assert(j);
assert(field);
assert(value);
s = strjoin(field, "=", value);
if (!s)
return -ENOMEM;
return sd_journal_add_match(j, s, SIZE_MAX);
}
int journal_add_matchf(sd_journal *j, const char *format, ...) {
_cleanup_free_ char *s = NULL;
va_list ap;
int r;
assert(j);
assert(format);
va_start(ap, format);
r = vasprintf(&s, format, ap);
va_end(ap);
if (r < 0)
return -ENOMEM;
return sd_journal_add_match(j, s, SIZE_MAX);
}
_public_ int sd_journal_add_conjunction(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
if (!j->level0)
return 0;
if (!j->level1)
return 0;
if (!j->level1->matches)
return 0;
j->level1 = NULL;
j->level2 = NULL;
return 0;
}
_public_ int sd_journal_add_disjunction(sd_journal *j) {
assert_return(j, -EINVAL);
assert_return(!journal_origin_changed(j), -ECHILD);
if (!j->level0)
return 0;
if (!j->level1)
return 0;
if (!j->level2)
return 0;
if (!j->level2->matches)
return 0;
j->level2 = NULL;
return 0;
}
static char *match_make_string(Match *m) {
_cleanup_free_ char *p = NULL;
bool enclose = false;
if (!m)
return strdup("none");
if (m->type == MATCH_DISCRETE)
return cescape_length(m->data, m->size);
LIST_FOREACH(matches, i, m->matches) {
_cleanup_free_ char *t = NULL;
t = match_make_string(i);
if (!t)
return NULL;
if (p) {
if (!strextend(&p, m->type == MATCH_OR_TERM ? " OR " : " AND ", t))
return NULL;
enclose = true;
} else
p = TAKE_PTR(t);
}
if (enclose)
return strjoin("(", p, ")");
return TAKE_PTR(p);
}
char* journal_make_match_string(sd_journal *j) {
assert(j);
return match_make_string(j->level0);
}
_public_ void sd_journal_flush_matches(sd_journal *j) {
if (!j || journal_origin_changed(j))
return;
if (j->level0)
match_free(j->level0);
j->level0 = j->level1 = j->level2 = NULL;
detach_location(j);
}
static int newest_by_boot_id_compare(const NewestByBootId *a, const NewestByBootId *b) {
return id128_compare_func(&a->boot_id, &b->boot_id);
}
static void journal_file_unlink_newest_by_boot_id(sd_journal *j, JournalFile *f) {
NewestByBootId *found;
assert(j);
assert(f);
if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) /* not linked currently, hence this is a NOP */
return;
found = typesafe_bsearch(&(NewestByBootId) { .boot_id = f->newest_boot_id },
j->newest_by_boot_id, j->n_newest_by_boot_id, newest_by_boot_id_compare);
assert(found);
assert_se(prioq_remove(found->prioq, f, &f->newest_boot_id_prioq_idx) > 0);
f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
/* The prioq may be empty, but that should not cause any issue. Let's keep it. */
}
static void journal_clear_newest_by_boot_id(sd_journal *j) {
FOREACH_ARRAY(i, j->newest_by_boot_id, j->n_newest_by_boot_id) {
JournalFile *f;
while ((f = prioq_peek(i->prioq)))
journal_file_unlink_newest_by_boot_id(j, f);
prioq_free(i->prioq);
}
j->newest_by_boot_id = mfree(j->newest_by_boot_id);
j->n_newest_by_boot_id = 0;
}
static int journal_file_newest_monotonic_compare(const void *a, const void *b) {
const JournalFile *x = a, *y = b;
return -CMP(x->newest_monotonic_usec, y->newest_monotonic_usec); /* Invert order, we want newest first! */
}
static int journal_file_reshuffle_newest_by_boot_id(sd_journal *j, JournalFile *f) {
NewestByBootId *found;
int r;
assert(j);
assert(f);
found = typesafe_bsearch(&(NewestByBootId) { .boot_id = f->newest_boot_id },
j->newest_by_boot_id, j->n_newest_by_boot_id, newest_by_boot_id_compare);
if (found) {
/* There's already a priority queue for this boot ID */
if (f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL) {
r = prioq_put(found->prioq, f, &f->newest_boot_id_prioq_idx); /* Insert if we aren't in there yet */
if (r < 0)
return r;
} else
prioq_reshuffle(found->prioq, f, &f->newest_boot_id_prioq_idx); /* Reshuffle otherwise */
} else {
_cleanup_(prioq_freep) Prioq *q = NULL;
/* No priority queue yet, then allocate one */
assert(f->newest_boot_id_prioq_idx == PRIOQ_IDX_NULL); /* we can't be a member either */
q = prioq_new(journal_file_newest_monotonic_compare);
if (!q)
return -ENOMEM;
r = prioq_put(q, f, &f->newest_boot_id_prioq_idx);
if (r < 0)
return r;
if (!GREEDY_REALLOC(j->newest_by_boot_id, j->n_newest_by_boot_id + 1)) {
f->newest_boot_id_prioq_idx = PRIOQ_IDX_NULL;
return -ENOMEM;
}
j->newest_by_boot_id[j->n_newest_by_boot_id++] = (NewestByBootId) {
.boot_id = f->newest_boot_id,
.prioq = TAKE_PTR(q),
};
typesafe_qsort(j->newest_by_boot_id, j->n_newest_by_boot_id, newest_by_boot_id_compare);
}
return 0;
}
static int journal_file_find_newest_for_boot_id(
sd_journal *j,
sd_id128_t id,
JournalFile **ret) {
JournalFile *prev = NULL;
int r;
assert(j);
assert(ret);
/* Before we use it, let's refresh the timestamp from the header, and reshuffle our prioq
* accordingly. We do this only a bunch of times, to not be caught in some update loop. */
for (unsigned n_tries = 0;; n_tries++) {
NewestByBootId *found;
JournalFile *f;
found = typesafe_bsearch(&(NewestByBootId) { .boot_id = id },
j->newest_by_boot_id, j->n_newest_by_boot_id, newest_by_boot_id_compare);
f = found ? prioq_peek(found->prioq) : NULL;
if (!f)
return log_debug_errno(SYNTHETIC_ERRNO(ENODATA),
"Requested delta for boot ID %s, but we have no information about that boot ID.", SD_ID128_TO_STRING(id));
if (f == prev || n_tries >= 5) {
/* This was already the best answer in the previous run, or we tried too often, use it */
*ret = f;
return 0;
}
prev = f;
/* Let's read the journal file's current timestamp once, before we return it, maybe it has changed. */
r = journal_file_read_tail_timestamp(j, f);
if (r < 0)
return log_debug_errno(r, "Failed to read tail timestamp while trying to find newest journal file for boot ID %s.", SD_ID128_TO_STRING(id));
if (r == 0) {
/* No new entry found. */
*ret = f;
return 0;
}
/* Refreshing the timestamp we read might have reshuffled the prioq, hence let's check the
* prioq again and only use the information once we reached an equilibrium or hit a limit */
}
}
static int compare_boot_ids(sd_journal *j, sd_id128_t a, sd_id128_t b) {
JournalFile *x, *y;
assert(j);
/* Try to find the newest open journal file for the two boot ids */
if (journal_file_find_newest_for_boot_id(j, a, &x) < 0 ||
journal_file_find_newest_for_boot_id(j, b, &y) < 0)
return 0;
/* Only compare the boot id timestamps if they originate from the same machine. If they are from
* different machines, then we timestamps of the boot ids might be as off as the timestamps on the
* entries and hence not useful for comparing. */
if (!sd_id128_equal(x->newest_machine_id, y->newest_machine_id))
return 0;
return CMP(x->newest_realtime_usec, y->newest_realtime_usec);
}
static int compare_with_location(
sd_journal *j,
const JournalFile *f,
const Location *l,
const JournalFile *current_file) {
int r;
assert(j);
assert(f);
assert(l);
assert(f->location_type == LOCATION_SEEK);
assert(IN_SET(l->type, LOCATION_DISCRETE, LOCATION_SEEK));
if (l->monotonic_set &&
sd_id128_equal(f->current_boot_id, l->boot_id) &&
l->realtime_set &&
f->current_realtime == l->realtime &&
l->xor_hash_set &&
f->current_xor_hash == l->xor_hash &&
l->seqnum_set &&
sd_id128_equal(f->header->seqnum_id, l->seqnum_id) &&
f->current_seqnum == l->seqnum &&
f != current_file)
return 0;
if (l->seqnum_set &&
sd_id128_equal(f->header->seqnum_id, l->seqnum_id)) {
r = CMP(f->current_seqnum, l->seqnum);
if (r != 0)
return r;
}
if (l->monotonic_set) {
/* If both arguments have the same boot ID, then we can compare the monotonic timestamps. If
* they are distinct, then we might able to lookup the timestamps of those boot IDs (if they
* are from the same machine) and order by that. */
if (sd_id128_equal(f->current_boot_id, l->boot_id))
r = CMP(f->current_monotonic, l->monotonic);
else
r = compare_boot_ids(j, f->current_boot_id, l->boot_id);
if (r != 0)
return r;
}
if (l->realtime_set) {
r = CMP(f->current_realtime, l->realtime);
if (r != 0)
return r;
}
if (l->xor_hash_set) {
r = CMP(f->current_xor_hash, l->xor_hash);
if (r != 0)
return r;
}
return 0;
}
static int next_for_match(
sd_journal *j,
Match *m,
JournalFile *f,
uint64_t after_offset,
direction_t direction,
Object **ret,
uint64_t *ret_offset) {
int r;
uint64_t np = 0;
assert(j);
assert(m);
assert(f);
if (m->type == MATCH_DISCRETE) {
Object *d;
uint64_t hash;
/* If the keyed hash logic is used, we need to calculate the hash fresh per file. Otherwise
* we can use what we pre-calculated. */
if (JOURNAL_HEADER_KEYED_HASH(f->header))
hash = journal_file_hash_data(f, m->data, m->size);
else
hash = m->hash;
r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, NULL);
if (r <= 0)
return r;
return journal_file_move_to_entry_by_offset_for_data(f, d, after_offset, direction, ret, ret_offset);
} else if (m->type == MATCH_OR_TERM) {
/* Find the earliest match beyond after_offset */
LIST_FOREACH(matches, i, m->matches) {
uint64_t cp;
r = next_for_match(j, i, f, after_offset, direction, NULL, &cp);
if (r < 0)
return r;
else if (r > 0) {
if (np == 0 || (direction == DIRECTION_DOWN ? cp < np : cp > np))
np = cp;
}
}
if (np == 0)
return 0;
} else if (m->type == MATCH_AND_TERM) {
Match *last_moved;
/* Always jump to the next matching entry and repeat
* this until we find an offset that matches for all
* matches. */
if (!m->matches)
return 0;
r = next_for_match(j, m->matches, f, after_offset, direction, NULL, &np);
if (r <= 0)
return r;
assert(direction == DIRECTION_DOWN ? np >= after_offset : np <= after_offset);
last_moved = m->matches;
LIST_LOOP_BUT_ONE(matches, i, m->matches, last_moved) {
uint64_t cp;
r = next_for_match(j, i, f, np, direction, NULL, &cp);
if (r <= 0)
return r;
assert(direction == DIRECTION_DOWN ? cp >= np : cp <= np);
if (direction == DIRECTION_DOWN ? cp > np : cp < np) {
np = cp;
last_moved = i;
}
}
}
assert(np > 0);
if (ret) {
r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret);
if (r < 0)
return r;
}
if (ret_offset)
*ret_offset = np;
return 1;
}
static int move_by_boot_for_data(
sd_journal *j,
JournalFile *f,
direction_t direction,
sd_id128_t boot_id,
uint64_t data_offset,
Object **ret,
uint64_t *ret_offset) {
int r;
assert(j);
assert(f);
assert(IN_SET(direction, DIRECTION_DOWN, DIRECTION_UP));
for (;;) {
/* First, move to the last (or first when DIRECTION_UP) entry for the boot. */
uint64_t p = 0;
r = journal_file_move_to_entry_by_monotonic(f, boot_id,
direction == DIRECTION_DOWN ? USEC_INFINITY : 0,
direction == DIRECTION_DOWN ? DIRECTION_UP : DIRECTION_DOWN,
NULL, &p);
if (r <= 0)
return r;
/* Then, move to the first entry of the next boot (or the last entry of the previous boot with DIRECTION_UP). */
Object *entry;
r = journal_file_next_entry(f, p, direction, &entry, NULL);
if (r <= 0) /* r == 0 means that no next (or previous) boot found. That is, we are at HEAD or TAIL now. */
return r;
assert(entry->object.type == OBJECT_ENTRY);
boot_id = entry->entry.boot_id;
/* Note, this object cannot be reused, as journal_file_move_to_entry_by_monotonic() may invalidate the object. */
Object *data;
r = journal_file_move_to_object(f, OBJECT_DATA, data_offset, &data);
if (r < 0)
return r;
/* Then, move to the matching entry. */
r = journal_file_move_to_entry_by_monotonic_for_data(f, data, boot_id,
direction == DIRECTION_DOWN ? 0 : USEC_INFINITY, direction,
ret, ret_offset);
if (r != 0) /* Here r == 0 is OK, as that means the boot contains no entry matching with the data. */
return r;
}
}
static int find_location_for_match(
sd_journal *j,
Match *m,
JournalFile *f,
direction_t direction,
Object **ret,
uint64_t *ret_offset) {
int r;
assert(j);
assert(m);
assert(f);
if (m->type == MATCH_DISCRETE) {
Object *d;
uint64_t dp, hash;
if (JOURNAL_HEADER_KEYED_HASH(f->header))
hash = journal_file_hash_data(f, m->data, m->size);
else
hash = m->hash;
r = journal_file_find_data_object_with_hash(f, m->data, m->size, hash, &d, &dp);
if (r <= 0)
return r;
if (j->current_location.type == LOCATION_HEAD)
return direction == DIRECTION_DOWN ? journal_file_move_to_entry_for_data(f, d, DIRECTION_DOWN, ret, ret_offset) : 0;
if (j->current_location.type == LOCATION_TAIL)
return direction == DIRECTION_UP ? journal_file_move_to_entry_for_data(f, d, DIRECTION_UP, ret, ret_offset) : 0;
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
return journal_file_move_to_entry_by_seqnum_for_data(f, d, j->current_location.seqnum, direction, ret, ret_offset);
if (j->current_location.monotonic_set) {
r = journal_file_move_to_entry_by_monotonic_for_data(f, d, j->current_location.boot_id, j->current_location.monotonic, direction, ret, ret_offset);
if (r != 0)
return r;
/* The data object might have been invalidated. */
r = journal_file_move_to_object(f, OBJECT_DATA, dp, &d);
if (r < 0)
return r;
/* If not found, fall back to realtime if set, or go to the first entry of the next boot
* (or the last entry of the previous boot when DIRECTION_UP). */
}
if (j->current_location.realtime_set)
return journal_file_move_to_entry_by_realtime_for_data(f, d, j->current_location.realtime, direction, ret, ret_offset);
if (j->current_location.monotonic_set)
return move_by_boot_for_data(j, f, direction, j->current_location.boot_id, dp, ret, ret_offset);
return journal_file_move_to_entry_for_data(f, d, direction, ret, ret_offset);
} else if (m->type == MATCH_OR_TERM) {
uint64_t np = 0;
/* Find the earliest match */
LIST_FOREACH(matches, i, m->matches) {
uint64_t cp;
r = find_location_for_match(j, i, f, direction, NULL, &cp);
if (r < 0)
return r;
else if (r > 0) {
if (np == 0 || (direction == DIRECTION_DOWN ? np > cp : np < cp))
np = cp;
}
}
if (np == 0)
return 0;
if (ret) {
r = journal_file_move_to_object(f, OBJECT_ENTRY, np, ret);
if (r < 0)
return r;
}
if (ret_offset)
*ret_offset = np;
return 1;
} else {
uint64_t np = 0;
assert(m->type == MATCH_AND_TERM);
/* First jump to the last match, and then find the
* next one where all matches match */
if (!m->matches)
return 0;
LIST_FOREACH(matches, i, m->matches) {
uint64_t cp;
r = find_location_for_match(j, i, f, direction, NULL, &cp);
if (r <= 0)
return r;
if (np == 0 || (direction == DIRECTION_DOWN ? cp > np : cp < np))
np = cp;
}
return next_for_match(j, m, f, np, direction, ret, ret_offset);
}
}
static int find_location_with_matches(
sd_journal *j,
JournalFile *f,
direction_t direction,
Object **ret,
uint64_t *ret_offset) {
int r;
assert(j);
assert(f);
if (j->level0)
return find_location_for_match(j, j->level0, f, direction, ret, ret_offset);
/* No matches is simple */
if (j->current_location.type == LOCATION_HEAD)
return direction == DIRECTION_DOWN ? journal_file_next_entry(f, 0, DIRECTION_DOWN, ret, ret_offset) : 0;
if (j->current_location.type == LOCATION_TAIL)
return direction == DIRECTION_UP ? journal_file_next_entry(f, 0, DIRECTION_UP, ret, ret_offset) : 0;
if (j->current_location.seqnum_set && sd_id128_equal(j->current_location.seqnum_id, f->header->seqnum_id))
return journal_file_move_to_entry_by_seqnum(f, j->current_location.seqnum, direction, ret, ret_offset);
if (j->current_location.monotonic_set) {
r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id, j->current_location.monotonic, direction, ret, ret_offset);
if (r != 0)
return r;
/* If not found, fall back to realtime if set, or go to the first entry of the next boot
* (or the last entry of the previous boot when DIRECTION_UP). */
}
if (j->current_location.realtime_set)
return journal_file_move_to_entry_by_realtime(f, j->current_location.realtime, direction, ret, ret_offset);
if (j->current_location.monotonic_set) {
uint64_t p = 0;
/* If not found in the above, first move to the last (or first when DIRECTION_UP) entry for the boot. */
r = journal_file_move_to_entry_by_monotonic(f, j->current_location.boot_id,
direction == DIRECTION_DOWN ? USEC_INFINITY : 0,
direction == DIRECTION_DOWN ? DIRECTION_UP : DIRECTION_DOWN,
NULL, &p);
if (r <= 0)
return r;
/* Then, move to the next or previous boot. */
return journal_file_next_entry(f, p, direction, ret, ret_offset);
}
return journal_file_next_entry(f, 0, direction, ret, ret_offset);
}
static int next_with_matches(
sd_journal *j,
JournalFile *f,
direction_t direction,
Object **ret,
uint64_t *ret_offset) {
assert(j);
assert(f);
/* No matches is easy. We simple advance the file
* pointer by one. */
if (!j->level0)
return journal_file_next_entry(f, f->current_offset, direction, ret, ret_offset);
/* If we have a match then we look for the next matching entry
* with an offset at least one step larger */
return next_for_match(j, j->level0, f,
direction == DIRECTION_DOWN ? f->current_offset + 1
: f->current_offset - 1,
direction, ret, ret_offset);
}
static int next_beyond_location(sd_journal *j, JournalFile *f, direction_t direction) {
Object *c;