test-journal-interleaving.c revision 8580d1f73db36e9383e674e388b4fb55828c0c66
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Marius Vollmer
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering (at your option) any later version.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering systemd is distributed in the hope that it will be useful, but
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Lesser General Public License for more details.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/* This program tests skipping around in a multi-file journal.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic bool arg_keep = false;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringnoreturn static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_internal(LOG_CRIT, 0, file, line, func,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering "'%s' failed at %s:%u (%s): %s.",
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } while (false)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic JournalFile *test_open(const char *name) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void append_number(JournalFile *f, int n, uint64_t *seqnum) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (ts.monotonic <= previous_ts.monotonic)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering ts.monotonic = previous_ts.monotonic + 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_number (sd_journal *j, int n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering const void *d;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_numbers_down (sd_journal *j, int count) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_numbers_up (sd_journal *j, int count) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void setup_sequential(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void setup_interleaved(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_skip(void (*setup)(void)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to head, iterate down.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to tail, iterate up.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering /* Seek to tail, skip to head, iterate down.
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(r = sd_journal_previous_skip(j, 4));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to head, skip to tail, iterate up.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(r = sd_journal_next_skip(j, 4));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering puts("------------------------------------------------------------");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_sequence_numbers(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0644,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering true, false, NULL, NULL, NULL, &one) == 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(one->header->state == STATE_ONLINE);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering true, false, NULL, NULL, one, &two) == 0);
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering assert_se(two->header->state == STATE_ONLINE);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* restart server */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR, 0,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering true, false, NULL, NULL, NULL, &two) == 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* So..., here we have the same seqnum in two files with the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * same seqnum_id. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* journal_file_open requires a valid machine id */