HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/libsystemd/sd-journal/test-journal-interleaving.c

77,979 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 90fbecac1a7569ff7ec7a5ca5a77ad92caded86c

Lines 1–1000 of 1,968

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
 
#if HAVE_VALGRIND_VALGRIND_H
#  include <valgrind/valgrind.h>
#endif
 
#include "sd-id128.h"
#include "sd-journal.h"
 
#include "alloc-util.h"
#include "argv-util.h"
#include "chattr-util.h"
#include "fd-util.h"
#include "fileio.h"
#include "hashmap.h"
#include "iovec-util.h"
#include "journal-file-util.h"
#include "journal-internal.h"
#include "journal-vacuum.h"
#include "log.h"
#include "logs-show.h"
#include "lookup3.h"
#include "parse-util.h"
#include "path-util.h"
#include "process-util.h"
#include "random-util.h"
#include "rm-rf.h"
#include "sigbus.h"
#include "string-util.h"
#include "strv.h"
#include "tests.h"
#include "time-util.h"
#include "tmpfile-util.h"
 
/* This program tests skipping around in a multi-file journal. */
 
static bool arg_keep = false;
static dual_timestamp previous_ts = {};
 
static JournalFile* test_open_internal(const char *name, JournalFileFlags flags) {
        _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
        JournalFile *f;
 
        ASSERT_NOT_NULL((m = mmap_cache_new()));
        ASSERT_OK(journal_file_open(-EBADF, name, O_RDWR|O_CREAT, flags, 0644, UINT64_MAX, NULL, m, NULL, &f));
        return f;
}
 
static JournalFile* test_open(const char *name) {
        return test_open_internal(name, JOURNAL_COMPRESS);
}
 
static JournalFile* test_open_strict(const char *name) {
        return test_open_internal(name, JOURNAL_COMPRESS | JOURNAL_STRICT_ORDER);
}
 
static char* test_done(char *t) {
        if (!t)
                return NULL;
 
        log_info("Done...");
 
        if (arg_keep)
                log_info("Not removing %s", t);
        else {
                journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
 
                ASSERT_OK(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL));
        }
 
        log_info("------------------------------------------------------------");
        return mfree(t);
}
 
DEFINE_TRIVIAL_CLEANUP_FUNC(char*, test_done);
 
static void append_number(JournalFile *f, unsigned n, const sd_id128_t *boot_id, uint64_t *seqnum, uint64_t *ret_offset) {
        _cleanup_free_ char *p = NULL, *q = NULL, *s = NULL;
        dual_timestamp ts;
        struct iovec iovec[3];
        size_t n_iov = 0;
 
        dual_timestamp_now(&ts);
 
        if (ts.monotonic <= previous_ts.monotonic)
                ts.monotonic = previous_ts.monotonic + 1;
 
        if (ts.realtime <= previous_ts.realtime)
                ts.realtime = previous_ts.realtime + 1;
 
        previous_ts = ts;
 
        ASSERT_OK(asprintf(&p, "NUMBER=%u", n));
        iovec[n_iov++] = IOVEC_MAKE_STRING(p);
 
        ASSERT_NOT_NULL((s = strjoin("LESS_THAN_FIVE=", yes_no(n < 5))));
        iovec[n_iov++] = IOVEC_MAKE_STRING(s);
 
        if (boot_id) {
                ASSERT_NOT_NULL((q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))));
                iovec[n_iov++] = IOVEC_MAKE_STRING(q);
        }
 
        ASSERT_OK(journal_file_append_entry(f, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, ret_offset));
}
 
static void append_unreferenced_data(JournalFile *f, const sd_id128_t *boot_id) {
        _cleanup_free_ char *q = NULL;
        dual_timestamp ts;
        struct iovec iovec;
 
        assert(boot_id);
 
        ts.monotonic = usec_sub_unsigned(previous_ts.monotonic, 10);
        ts.realtime = usec_sub_unsigned(previous_ts.realtime, 10);
 
        ASSERT_NOT_NULL((q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))));
        iovec = IOVEC_MAKE_STRING(q);
 
        ASSERT_ERROR(journal_file_append_entry(f, &ts, boot_id, &iovec, 1, NULL, NULL, NULL, NULL), EREMCHG);
}
 
static void test_check_number(sd_journal *j, unsigned expected) {
        sd_id128_t boot_id;
        const void *d;
        size_t l;
 
        ASSERT_OK(sd_journal_get_monotonic_usec(j, NULL, &boot_id));
        ASSERT_OK(sd_journal_get_data(j, "NUMBER", &d, &l));
 
        _cleanup_free_ char *k = NULL;
        ASSERT_NOT_NULL((k = strndup(d, l)));
        printf("%s %s (expected=%u)\n", SD_ID128_TO_STRING(boot_id), k, expected);
 
        unsigned x;
        ASSERT_OK(safe_atou(k + STRLEN("NUMBER="), &x));
        ASSERT_EQ(x, expected);
}
 
static void test_check_numbers_down(sd_journal *j, unsigned count) {
        for (unsigned i = 1; i <= count; i++) {
                test_check_number(j, i);
                if (i == count)
                        ASSERT_OK_ZERO(sd_journal_next(j));
                else
                        ASSERT_OK_POSITIVE(sd_journal_next(j));
        }
}
 
static void test_check_numbers_up(sd_journal *j, unsigned count) {
        for (unsigned i = count; i >= 1; i--) {
                test_check_number(j, i);
                if (i == 1)
                        ASSERT_OK_ZERO(sd_journal_previous(j));
                else
                        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        }
}
 
static void setup_sequential(void) {
        _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
        sd_id128_t id;
 
        f1 = test_open("one.journal");
        f2 = test_open("two.journal");
        f3 = test_open("three.journal");
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_number(f1, 1, &id, NULL, NULL);
        append_number(f1, 2, &id, NULL, NULL);
        append_number(f1, 3, &id, NULL, NULL);
        append_number(f2, 4, &id, NULL, NULL);
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_number(f2, 5, &id, NULL, NULL);
        append_number(f2, 6, &id, NULL, NULL);
        append_number(f3, 7, &id, NULL, NULL);
        append_number(f3, 8, &id, NULL, NULL);
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_number(f3, 9, &id, NULL, NULL);
}
 
static void setup_interleaved(void) {
        _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
        sd_id128_t id;
 
        f1 = test_open("one.journal");
        f2 = test_open("two.journal");
        f3 = test_open("three.journal");
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_number(f1, 1, &id, NULL, NULL);
        append_number(f2, 2, &id, NULL, NULL);
        append_number(f3, 3, &id, NULL, NULL);
        append_number(f1, 4, &id, NULL, NULL);
        append_number(f2, 5, &id, NULL, NULL);
        append_number(f3, 6, &id, NULL, NULL);
        append_number(f1, 7, &id, NULL, NULL);
        append_number(f2, 8, &id, NULL, NULL);
        append_number(f3, 9, &id, NULL, NULL);
}
 
static void setup_unreferenced_data(void) {
        _cleanup_(journal_file_offline_closep) JournalFile *f1 = NULL, *f2 = NULL, *f3 = NULL;
        sd_id128_t id;
 
        /* For issue #29275. */
 
        f1 = test_open_strict("one.journal");
        f2 = test_open_strict("two.journal");
        f3 = test_open_strict("three.journal");
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_number(f1, 1, &id, NULL, NULL);
        append_number(f1, 2, &id, NULL, NULL);
        append_number(f1, 3, &id, NULL, NULL);
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_unreferenced_data(f1, &id);
        append_number(f2, 4, &id, NULL, NULL);
        append_number(f2, 5, &id, NULL, NULL);
        append_number(f2, 6, &id, NULL, NULL);
        ASSERT_OK(sd_id128_randomize(&id));
        log_info("boot_id: %s", SD_ID128_TO_STRING(id));
        append_unreferenced_data(f2, &id);
        append_number(f3, 7, &id, NULL, NULL);
        append_number(f3, 8, &id, NULL, NULL);
        append_number(f3, 9, &id, NULL, NULL);
}
 
static void mkdtemp_chdir_chattr(const char *template, char **ret) {
        _cleanup_(rm_rf_physical_and_freep) char *path = NULL;
 
        assert(ret);
 
        ASSERT_OK(mkdtemp_malloc(template, &path));
        ASSERT_OK_ERRNO(chdir(path));
 
        /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our
         * directory during the test run */
        (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL);
 
        *ret = TAKE_PTR(path);
}
 
static void test_cursor(sd_journal *j) {
        _cleanup_strv_free_ char **cursors = NULL;
        int r;
 
        ASSERT_OK(sd_journal_seek_head(j));
 
        for (;;) {
                ASSERT_OK(r = sd_journal_next(j));
                if (r == 0)
                        break;
 
                _cleanup_free_ char *cursor = NULL;
                ASSERT_OK(sd_journal_get_cursor(j, &cursor));
                ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
                ASSERT_OK(strv_consume(&cursors, TAKE_PTR(cursor)));
        }
 
        STRV_FOREACH(c, cursors) {
                ASSERT_OK(sd_journal_seek_cursor(j, *c));
                ASSERT_OK(sd_journal_next(j));
                ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
        }
 
        ASSERT_OK(sd_journal_seek_head(j));
        STRV_FOREACH(c, cursors) {
                ASSERT_OK(sd_journal_next(j));
                ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
        }
 
        STRV_FOREACH(c, cursors) {
                ASSERT_OK(sd_journal_seek_cursor(j, *c));
                ASSERT_OK(sd_journal_previous(j));
                ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
        }
 
        ASSERT_OK(sd_journal_seek_tail(j));
        STRV_FOREACH_BACKWARDS(c, cursors) {
                ASSERT_OK(sd_journal_previous(j));
                ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, *c));
        }
}
 
static void test_skip_one(void (*setup)(void)) {
        _cleanup_(test_donep) char *t = NULL;
        sd_journal *j;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-skip-XXXXXX", &t);
 
        setup();
 
        /* Seek to head, iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to head, iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to head twice, iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to head, move to previous, then iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to head, walk several steps, then iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        ASSERT_OK_POSITIVE(sd_journal_next(j));       /* pointing to the first entry */
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        ASSERT_OK_ZERO(sd_journal_previous(j));       /* no-op */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail, iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));   /* pointing to the last entry */
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail twice, iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));   /* pointing to the last entry */
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));   /* pointing to the last entry */
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail, move to next, then iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        ASSERT_OK_POSITIVE(sd_journal_previous(j));   /* pointing to the last entry */
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail, walk several steps, then iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        ASSERT_OK_POSITIVE(sd_journal_previous(j));   /* pointing to the last entry. */
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        ASSERT_OK_ZERO(sd_journal_next(j));           /* no-op */
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail, skip to head, iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_EQ(sd_journal_previous_skip(j, 9), 9); /* pointing to the first entry. */
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to tail, skip to head in a more complex way, then iterate down. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_tail(j));
        ASSERT_OK_ZERO(sd_journal_next(j));
        ASSERT_EQ(sd_journal_previous_skip(j, 4), 4);
        ASSERT_EQ(sd_journal_previous_skip(j, 5), 5);
        ASSERT_OK_ZERO(sd_journal_previous(j));
        ASSERT_OK_ZERO(sd_journal_previous_skip(j, 5));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_previous_skip(j, 5));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_EQ(sd_journal_previous_skip(j, 5), 3);
        test_check_numbers_down(j, 9);
        sd_journal_close(j);
 
        /* Seek to head, skip to tail, iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_EQ(sd_journal_next_skip(j, 9), 9);
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* Seek to head, skip to tail in a more complex way, then iterate up. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_ZERO(sd_journal_previous(j));
        ASSERT_EQ(sd_journal_next_skip(j, 4), 4);
        ASSERT_EQ(sd_journal_next_skip(j, 5), 5);
        ASSERT_OK_ZERO(sd_journal_next(j));
        ASSERT_OK_ZERO(sd_journal_next_skip(j, 5));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_OK_POSITIVE(sd_journal_next_skip(j, 5));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        ASSERT_EQ(sd_journal_next_skip(j, 5), 3);
        test_check_numbers_up(j, 9);
        sd_journal_close(j);
 
        /* For issue #31516. */
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        test_cursor(j);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
        test_cursor(j);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=no", SIZE_MAX));
        test_cursor(j);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=hoge", SIZE_MAX));
        test_cursor(j);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=3", SIZE_MAX));
        test_cursor(j);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=3", SIZE_MAX));
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=4", SIZE_MAX));
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=5", SIZE_MAX));
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=6", SIZE_MAX));
        test_cursor(j);
        sd_journal_close(j);
}
 
TEST(skip) {
        test_skip_one(setup_sequential);
        test_skip_one(setup_interleaved);
}
 
/* Read the first entry so that two.journal becomes the next candidate with LOCATION_SEEK, then simulate
 * vacuuming removing it while it is still open and mapped. */
static void test_remove_unlinked_selected_file_one(bool truncate, bool refresh_stat) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
        _cleanup_close_ int fd = -EBADF;
        uint8_t type = OBJECT_UNUSED;
        JournalFile *f;
        struct stat st;
 
        ASSERT_TRUE(!refresh_stat || truncate);
 
        mkdtemp_chdir_chattr("/var/tmp/journal-unlinked-XXXXXX", &t);
        setup_interleaved();
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 1);
 
        f = ASSERT_NOT_NULL(ordered_hashmap_get(j->files, strjoina(t, "/two.journal")));
        ASSERT_EQ((int) f->location_type, LOCATION_SEEK);
 
        if (truncate && !refresh_stat)
                sigbus_install();
 
        /* Keep the file open and mapped while the name goes away, like vacuuming does. */
        fd = ASSERT_OK_ERRNO(open("two.journal", O_WRONLY|O_CLOEXEC));
        ASSERT_OK_ERRNO(unlink("two.journal"));
 
        if (truncate) {
                /* The lookup only stats the file when the selected entry falls outside the cached size, and
                 * journal_file_fstat() then reports the unlinked file as -EIDRM. Truncating to the selected
                 * offset and refreshing the stat takes that path. Truncating the whole file without
                 * refreshing leaves the cached size stale, so touching the mapping raises SIGBUS and the
                 * lookup fails with -EIO. */
                ASSERT_OK_ERRNO(ftruncate(fd, refresh_stat ? f->current_offset : 0));
                if (refresh_stat)
                        ASSERT_ERROR(journal_file_fstat(f), EIDRM);
                else {
                        ASSERT_EQ(READ_NOW(f->header->state), STATE_OFFLINE);
                        ASSERT_TRUE(mmap_cache_fd_got_sigbus(f->cache_fd));
                }
        } else {
                /* Emulate a deallocated range, which reads back as zeroes, by zeroing the selected entry's
                 * object type. */
                ASSERT_OK_EQ_ERRNO(pwrite(fd, &type, sizeof(type), f->current_offset), (ssize_t) sizeof(type));
                ASSERT_OK_ERRNO(fsync(fd));
        }
 
        ASSERT_OK_ERRNO(fstat(f->fd, &st));
        ASSERT_EQ(st.st_nlink, 0U);
        if (refresh_stat)
                ASSERT_EQ((uint64_t) st.st_size, f->current_offset);
        else if (truncate)
                ASSERT_EQ(st.st_size, 0);
 
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        if (truncate && !refresh_stat)
                sigbus_reset();
        test_check_number(j, 3);
        ASSERT_NULL(ordered_hashmap_get(j->files, strjoina(t, "/two.journal")));
}
 
TEST(remove_unlinked_selected_file) {
        test_remove_unlinked_selected_file_one(/* truncate= */ false, /* refresh_stat= */ false);
}
 
TEST(remove_truncated_unlinked_selected_file) {
#if HAS_FEATURE_ADDRESS_SANITIZER
        return (void) log_tests_skipped("SIGBUS recovery cannot be tested under AddressSanitizer");
#endif
#if HAVE_VALGRIND_VALGRIND_H
        if (RUNNING_ON_VALGRIND)
                return (void) log_tests_skipped("SIGBUS recovery cannot be tested under Valgrind");
#endif
 
        test_remove_unlinked_selected_file_one(/* truncate= */ true, /* refresh_stat= */ false);
}
 
TEST(remove_truncated_unlinked_selected_file_after_fstat) {
        test_remove_unlinked_selected_file_one(/* truncate= */ true, /* refresh_stat= */ true);
}
 
/* Corruption in a file that is still linked must stay visible to the caller. Whether libsystemd should
 * skip such a file, or just the corrupt entry, is a separate question; this only pins today's behaviour. */
TEST(keep_linked_selected_file_error) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
        _cleanup_close_ int fd = -EBADF;
        JournalFile *f;
        uint8_t type = OBJECT_UNUSED;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-linked-XXXXXX", &t);
        setup_interleaved();
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 1);
 
        f = ASSERT_NOT_NULL(ordered_hashmap_get(j->files, strjoina(t, "/two.journal")));
        ASSERT_EQ((int) f->location_type, LOCATION_SEEK);
 
        fd = ASSERT_OK_ERRNO(open("two.journal", O_WRONLY|O_CLOEXEC));
        ASSERT_OK_EQ_ERRNO(pwrite(fd, &type, sizeof(type), f->current_offset), (ssize_t) sizeof(type));
        ASSERT_OK_ERRNO(fsync(fd));
 
        ASSERT_ERROR(sd_journal_next(j), EBADMSG);
        ASSERT_NOT_NULL(ordered_hashmap_get(j->files, strjoina(t, "/two.journal")));
}
 
static void assert_no_current_entry(sd_journal *j) {
        _cleanup_free_ char *cursor = NULL;
        sd_id128_t id;
        const void *data;
        uint64_t value;
        size_t size;
 
        ASSERT_ERROR(sd_journal_get_data(j, "NUMBER", &data, &size), EADDRNOTAVAIL);
        ASSERT_ERROR(sd_journal_get_cursor(j, &cursor), EADDRNOTAVAIL);
        ASSERT_ERROR(sd_journal_get_realtime_usec(j, &value), EADDRNOTAVAIL);
        ASSERT_ERROR(sd_journal_get_monotonic_usec(j, &value, &id), EADDRNOTAVAIL);
        ASSERT_ERROR(sd_journal_get_seqnum(j, &value, &id), EADDRNOTAVAIL);
}
 
static unsigned count_journal_entries(sd_journal *j) {
        unsigned n = 0;
        int r;
 
        ASSERT_OK(sd_journal_seek_head(j));
        for (;;) {
                ASSERT_OK(r = sd_journal_next(j));
                if (r == 0)
                        return n;
 
                n++;
        }
}
 
TEST(reader_position_invalidation) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-position-XXXXXX", &t);
        setup_sequential();
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        assert_no_current_entry(j);
 
        ASSERT_OK(sd_journal_seek_head(j));
        assert_no_current_entry(j);
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 1);
        ASSERT_OK_ZERO(sd_journal_previous(j));
        test_check_number(j, 1);
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 2);
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        test_check_number(j, 1);
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 2);
 
        ASSERT_OK(sd_journal_seek_tail(j));
        assert_no_current_entry(j);
        ASSERT_OK_POSITIVE(sd_journal_previous(j));
        test_check_number(j, 9);
        ASSERT_OK_ZERO(sd_journal_next(j));
        test_check_number(j, 9);
 
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 1);
 
        ASSERT_OK(sd_journal_add_conjunction(j));
        test_check_number(j, 1);
        ASSERT_OK(sd_journal_add_disjunction(j));
        test_check_number(j, 1);
 
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=2", SIZE_MAX));
        assert_no_current_entry(j);
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 2);
 
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=2", SIZE_MAX));
        test_check_number(j, 2);
        ASSERT_OK(sd_journal_add_conjunction(j));
        test_check_number(j, 2);
        ASSERT_OK(sd_journal_add_disjunction(j));
        test_check_number(j, 2);
 
        ASSERT_OK(sd_journal_add_match(j, "LESS_THAN_FIVE=yes", SIZE_MAX));
        assert_no_current_entry(j);
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 2);
 
        sd_journal_flush_matches(j);
        assert_no_current_entry(j);
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 1);
}
 
TEST(cursor_and_data_lifetime) {
        static const uint8_t binary[] = { 'B', 'I', 'N', 'A', 'R', 'Y', '=', 'a', 0, 'b' };
        static const char small[] = "SMALL=x";
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(journal_file_offline_closep) JournalFile *f = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
        _cleanup_free_ char *cursor = NULL, *expected = NULL, *with_unknown = NULL;
        struct iovec iovec[] = {
                IOVEC_MAKE_STRING(small),
                IOVEC_MAKE((void*) binary, sizeof(binary)),
        };
        uint64_t seqnum = 0, value, expected_xor;
        sd_id128_t seqnum_id, boot_id, id;
        dual_timestamp ts;
        const void *data;
        size_t size;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-cursor-XXXXXX", &t);
        ASSERT_OK(sd_id128_randomize(&seqnum_id));
        ASSERT_OK(sd_id128_randomize(&boot_id));
        ASSERT_NOT_NULL(dual_timestamp_now(&ts));
 
        f = test_open_internal("test.journal", 0);
        ASSERT_OK(journal_file_append_entry(
                        f,
                        &ts,
                        &boot_id,
                        iovec,
                        ELEMENTSOF(iovec),
                        &seqnum,
                        &seqnum_id,
                        NULL,
                        NULL));
        ASSERT_EQ(seqnum, 1U);
        f = journal_file_offline_close(f);
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
 
        expected_xor = jenkins_hash64(small, strlen(small)) ^ jenkins_hash64(binary, sizeof(binary));
        ASSERT_OK(asprintf(
                        &expected,
                        "s=%s;i=1;b=%s;m=%" PRIx64 ";t=%" PRIx64 ";x=%" PRIx64,
                        SD_ID128_TO_STRING(seqnum_id),
                        SD_ID128_TO_STRING(boot_id),
                        ts.monotonic,
                        ts.realtime,
                        expected_xor));
        ASSERT_OK(sd_journal_get_cursor(j, &cursor));
        ASSERT_STREQ(cursor, expected);
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
        ASSERT_OK_ZERO(sd_journal_test_cursor(j, "i=0"));
        ASSERT_ERROR(sd_journal_test_cursor(j, "broken"), EINVAL);
 
        ASSERT_NOT_NULL(with_unknown = strjoin(cursor, ";q=ignored"));
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, with_unknown));
        ASSERT_ERROR(sd_journal_test_cursor(j, "provider=ignored"), EINVAL);
        ASSERT_OK(sd_journal_seek_cursor(j, with_unknown));
        assert_no_current_entry(j);
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
 
        ASSERT_ERROR(sd_journal_seek_cursor(j, "provider=ignored"), EINVAL);
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
        ASSERT_ERROR(sd_journal_seek_cursor(j, "i=not-a-number"), EINVAL);
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_seek_cursor(j, ""), EINVAL));
 
        ASSERT_OK(sd_journal_set_data_threshold(j, 4));
        ASSERT_OK(sd_journal_get_data_threshold(j, &size));
        ASSERT_EQ(size, 4U);
        ASSERT_OK(sd_journal_get_data(j, "BINARY", &data, &size));
        ASSERT_EQ(size, sizeof(binary));
        ASSERT_EQ(memcmp(data, binary, size), 0);
 
        ASSERT_OK(sd_journal_get_realtime_usec(j, &value));
        ASSERT_EQ(value, ts.realtime);
        ASSERT_OK(sd_journal_get_monotonic_usec(j, &value, &id));
        ASSERT_EQ(value, ts.monotonic);
        ASSERT_EQ_ID128(id, boot_id);
        ASSERT_OK_POSITIVE(sd_journal_test_cursor(j, cursor));
        ASSERT_EQ(memcmp(data, binary, size), 0);
        ASSERT_ERROR(sd_journal_get_data(j, "MISSING", &data, &size), ENOENT);
 
        ASSERT_OK(sd_journal_set_data_threshold(j, 0));
        sd_journal_restart_data(j);
        ASSERT_OK_POSITIVE(sd_journal_enumerate_data(j, &data, &size));
        ASSERT_EQ(size, strlen(small));
        ASSERT_EQ(memcmp(data, small, size), 0);
        ASSERT_OK(sd_journal_get_seqnum(j, &value, &id));
        ASSERT_EQ(value, seqnum);
        ASSERT_EQ_ID128(id, seqnum_id);
        ASSERT_EQ(memcmp(data, small, size), 0);
        ASSERT_OK_POSITIVE(sd_journal_enumerate_data(j, &data, &size));
        ASSERT_EQ(size, sizeof(binary));
        ASSERT_EQ(memcmp(data, binary, size), 0);
        ASSERT_OK_ZERO(sd_journal_enumerate_data(j, &data, &size));
 
        ASSERT_OK(sd_journal_query_unique(j, "BINARY"));
        ASSERT_OK_POSITIVE(sd_journal_enumerate_unique(j, &data, &size));
        ASSERT_EQ(size, sizeof(binary));
        ASSERT_EQ(memcmp(data, binary, size), 0);
        ASSERT_OK(sd_journal_get_realtime_usec(j, &value));
        ASSERT_EQ(memcmp(data, binary, size), 0);
        ASSERT_OK_ZERO(sd_journal_enumerate_unique(j, &data, &size));
        sd_journal_restart_unique(j);
        ASSERT_OK_POSITIVE(sd_journal_enumerate_unique(j, &data, &size));
        ASSERT_EQ(size, sizeof(binary));
        ASSERT_EQ(memcmp(data, binary, size), 0);
 
        unsigned n_fields = 0;
        sd_journal_restart_fields(j);
        for (;;) {
                const char *field;
                int r;
 
                ASSERT_OK(r = sd_journal_enumerate_fields(j, &field));
                if (r == 0)
                        break;
 
                ASSERT_OK(sd_journal_get_realtime_usec(j, &value));
                ASSERT_TRUE(STR_IN_SET(field, "BINARY", "SMALL"));
                n_fields++;
        }
        ASSERT_EQ(n_fields, 2U);
}
 
TEST(open_and_follow_edge_cases) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_free_ char *bad = NULL, *moved = NULL, *one = NULL, *renamed = NULL, *two = NULL;
        _cleanup_(journal_file_offline_closep) JournalFile *writer = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
        _cleanup_close_ int directory_fd = -EBADF, failure_fd = -EBADF;
        int owned_directory_fd, r;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-open-XXXXXX", &t);
        setup_sequential();
 
        ASSERT_NOT_NULL(bad = path_join(t, "bad.journal"));
        ASSERT_NOT_NULL(moved = path_join(t, "moved.journal"));
        ASSERT_NOT_NULL(one = path_join(t, "one.journal"));
        ASSERT_NOT_NULL(renamed = strjoin(t, ".renamed"));
        ASSERT_NOT_NULL(two = path_join(t, "two.journal"));
        ASSERT_OK(write_string_file(bad, "not a journal", WRITE_STRING_FILE_CREATE));
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_EQ(count_journal_entries(j), 9U);
        sd_journal_close(j);
        j = NULL;
 
        const char *bad_paths[] = { bad, NULL };
        ASSERT_FAIL(sd_journal_open_files(&j, bad_paths, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_NULL(j);
 
        const char *paths[] = { one, two, NULL };
        ASSERT_OK(sd_journal_open_files(&j, paths, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_EQ(count_journal_entries(j), 6U);
        sd_journal_close(j);
        j = NULL;
 
        ASSERT_OK(sd_journal_open_directory(&j, t, 0));
        ASSERT_OK_POSITIVE(sd_journal_get_fd(j));
        ASSERT_EQ(sd_journal_get_events(j), POLLIN);
        uint64_t timeout;
        ASSERT_OK_ZERO(sd_journal_get_timeout(j, &timeout));
        ASSERT_EQ(timeout, UINT64_MAX);
        ASSERT_OK_POSITIVE(sd_journal_reliable_fd(j));
        ASSERT_OK_ZERO(sd_journal_process(j));
 
        writer = test_open("one.journal");
        append_number(writer, 10, NULL, NULL, NULL);
        ASSERT_OK(r = sd_journal_wait(j, 5 * USEC_PER_SEC));
        ASSERT_TRUE(IN_SET(r, SD_JOURNAL_APPEND, SD_JOURNAL_INVALIDATE));
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=10", SIZE_MAX));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 10);
        writer = journal_file_offline_close(writer);
        sd_journal_close(j);
        j = NULL;
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_get_fd(j), EUNATCH));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_get_events(j), EUNATCH));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_get_timeout(j, &timeout), EUNATCH));
        ASSERT_RETURN_EXPECTED(ASSERT_ERROR(sd_journal_wait(j, 0), EUNATCH));
        ASSERT_OK_POSITIVE(sd_journal_reliable_fd(j));
 
        r = pidref_safe_fork("(journal-fork-test)", FORK_WAIT|FORK_LOG, NULL);
        if (r == 0) {
                ASSERT_RETURN_EXPECTED_SE(sd_journal_next(j) == -ECHILD);
                ASSERT_RETURN_EXPECTED_SE(sd_journal_get_fd(j) == -ECHILD);
                sd_journal_close(j);
                _exit(EXIT_SUCCESS);
        }
        ASSERT_OK(r);
        sd_journal_close(j);
        j = NULL;
 
        directory_fd = open(t, O_RDONLY|O_DIRECTORY|O_CLOEXEC);
        ASSERT_OK_ERRNO(directory_fd);
        owned_directory_fd = directory_fd;
        ASSERT_OK(sd_journal_open_directory_fd(
                        &j,
                        TAKE_FD(directory_fd),
                        SD_JOURNAL_TAKE_DIRECTORY_FD|SD_JOURNAL_ASSUME_IMMUTABLE));
 
        ASSERT_OK_ERRNO(rename(t, renamed));
        ASSERT_OK_ERRNO(mkdir(t, 0755));
        ASSERT_EQ(count_journal_entries(j), 10U);
        sd_journal_close(j);
        j = NULL;
        ASSERT_ERROR_ERRNO(fcntl(owned_directory_fd, F_GETFD), EBADF);
        ASSERT_OK_ERRNO(rmdir(t));
        ASSERT_OK_ERRNO(rename(renamed, t));
 
        failure_fd = open(one, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
        ASSERT_OK_ERRNO(failure_fd);
        int failure_fds[] = { failure_fd, -EBADF };
        ASSERT_ERROR(sd_journal_open_files_fd(
                             &j,
                             failure_fds,
                             ELEMENTSOF(failure_fds),
                             SD_JOURNAL_ASSUME_IMMUTABLE), EBADF);
        ASSERT_NULL(j);
        ASSERT_OK_ERRNO(fcntl(failure_fd, F_GETFD));
 
        int fds[] = {
                open(one, O_RDONLY|O_CLOEXEC|O_NONBLOCK),
                open(two, O_RDONLY|O_CLOEXEC|O_NONBLOCK),
        };
        ASSERT_OK_ERRNO(fds[0]);
        ASSERT_OK_ERRNO(fds[1]);
        ASSERT_OK_ERRNO(rename(one, moved));
        ASSERT_OK(sd_journal_open_files_fd(&j, fds, ELEMENTSOF(fds), SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK_ERRNO(unlink(moved));
        ASSERT_OK_ERRNO(unlink(two));
        ASSERT_EQ(count_journal_entries(j), 7U);
        sd_journal_flush_matches(j);
        ASSERT_OK(sd_journal_add_match(j, "NUMBER=10", SIZE_MAX));
        ASSERT_OK(sd_journal_seek_head(j));
        ASSERT_OK_POSITIVE(sd_journal_next(j));
        test_check_number(j, 10);
}
 
static void test_boot_id_one(void (*setup)(void), size_t n_ids_expected) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(sd_journal_closep) sd_journal *j = NULL;
        _cleanup_free_ LogId *ids = NULL;
        size_t n_ids;
 
        mkdtemp_chdir_chattr("/var/tmp/journal-boot-id-XXXXXX", &t);
 
        setup();
 
        ASSERT_OK(sd_journal_open_directory(&j, t, SD_JOURNAL_ASSUME_IMMUTABLE));
        ASSERT_OK(journal_get_boots(
                                j,
                                /* advance_older= */ false, /* max_ids= */ SIZE_MAX,
                                &ids, &n_ids));
        ASSERT_NOT_NULL(ids);
        ASSERT_EQ(n_ids, n_ids_expected);
 
        for (size_t i = 0; i < n_ids; i++) {
                sd_id128_t id;
 
                /* positive offset */
                ASSERT_OK_POSITIVE(journal_find_boot(j, SD_ID128_NULL, (int) (i + 1), &id));
                ASSERT_EQ_ID128(id, ids[i].id);
 
                /* negative offset */
                ASSERT_OK_POSITIVE(journal_find_boot(j, SD_ID128_NULL, (int) (i + 1) - (int) n_ids, &id));
                ASSERT_EQ_ID128(id, ids[i].id);
 
                for (size_t k = 0; k < n_ids; k++) {
                        int offset = (int) k - (int) i;
 
                        /* relative offset */
                        ASSERT_OK_POSITIVE(journal_find_boot(j, ids[i].id, offset, &id));
                        ASSERT_EQ_ID128(id, ids[k].id);
                }
        }
 
        for (size_t i = 0; i <= n_ids_expected + 1; i++) {
                _cleanup_free_ LogId *ids_limited = NULL;
                size_t n_ids_limited;
 
                ASSERT_OK(journal_get_boots(
                                        j,
                                        /* advance_older= */ false, /* max_ids= */ i,
                                        &ids_limited, &n_ids_limited));
                ASSERT_TRUE(ids_limited || i == 0);
                ASSERT_EQ(n_ids_limited, MIN(i, n_ids_expected));
                ASSERT_EQ(memcmp_safe(ids, ids_limited, n_ids_limited * sizeof(LogId)), 0);
        }
 
        for (size_t i = 0; i <= n_ids_expected + 1; i++) {
                _cleanup_free_ LogId *ids_limited = NULL;
                size_t n_ids_limited;
 
                ASSERT_OK(journal_get_boots(
                                        j,
                                        /* advance_older= */ true, /* max_ids= */ i,
                                        &ids_limited, &n_ids_limited));
                ASSERT_TRUE(ids_limited || i == 0);
                ASSERT_EQ(n_ids_limited, MIN(i, n_ids_expected));
                for (size_t k = 0; k < n_ids_limited; k++)
                        ASSERT_EQ(memcmp(&ids[n_ids - k - 1], &ids_limited[k], sizeof(LogId)), 0);
        }
}
 
TEST(boot_id) {
        test_boot_id_one(setup_sequential, 3);
        test_boot_id_one(setup_unreferenced_data, 3);
}
 
static void test_sequence_numbers_one(void) {
        _cleanup_(test_donep) char *t = NULL;
        _cleanup_(journal_file_offline_closep) JournalFile *one = NULL, *two = NULL;
        _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
        uint64_t seqnum = 0;
        sd_id128_t seqnum_id;
 
        ASSERT_NOT_NULL((m = mmap_cache_new()));
 
        mkdtemp_chdir_chattr("/var/tmp/journal-seq-XXXXXX", &t);
 
        ASSERT_OK(journal_file_open(-EBADF, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644,