test-journal-interleaving.c revision 8580d1f73db36e9383e674e388b4fb55828c0c66
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/***
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering This file is part of systemd.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Marius Vollmer
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
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
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
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***/
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <unistd.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include <fcntl.h>
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "sd-journal.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "journal-file.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "journal-vacuum.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "util.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "log.h"
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#include "rm-rf.h"
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering/* This program tests skipping around in a multi-file journal.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic bool arg_keep = false;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
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 text, file, line, func, strerror(eno));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering abort();
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering#define assert_ret(expr) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering do { \
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering int _r_ = (expr); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (_unlikely_(_r_ < 0)) \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering } while (false)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic JournalFile *test_open(const char *name) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering JournalFile *f;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return f;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_close(JournalFile *f) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering journal_file_close (f);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void append_number(JournalFile *f, int n, uint64_t *seqnum) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char *p;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering dual_timestamp ts;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering static dual_timestamp previous_ts = {};
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering struct iovec iovec[1];
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering dual_timestamp_get(&ts);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (ts.monotonic <= previous_ts.monotonic)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering ts.monotonic = previous_ts.monotonic + 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (ts.realtime <= previous_ts.realtime)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering ts.realtime = previous_ts.realtime + 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering previous_ts = ts;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering iovec[0].iov_base = p;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering iovec[0].iov_len = strlen(p);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering free(p);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_number (sd_journal *j, int n) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering const void *d;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering _cleanup_free_ char *k;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering size_t l;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int x;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(k = strndup(d, l));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("%s\n", k);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(safe_atoi(k + 7, &x) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(n == x);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_numbers_down (sd_journal *j, int count) {
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack int i;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack for (i = 1; i <= count; i++) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack test_check_number(j, i);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert_ret(r = sd_journal_next(j));
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack if (i == count)
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert_se(r == 0);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack else
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert_se(r == 1);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack }
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_check_numbers_up (sd_journal *j, int count) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering for (int i = count; i >= 1; i--) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_check_number(j, i);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(r = sd_journal_previous(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (i == 1)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(r == 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(r == 1);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void setup_sequential(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering JournalFile *one, *two;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering one = test_open("one.journal");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering two = test_open("two.journal");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 1, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 2, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 3, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 4, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(one);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(two);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void setup_interleaved(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering JournalFile *one, *two;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering one = test_open("one.journal");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering two = test_open("two.journal");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 1, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 2, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 3, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 4, NULL);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(one);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(two);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_skip(void (*setup)(void)) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char t[] = "/tmp/journal-skip-XXXXXX";
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_journal *j;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering int r;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(mkdtemp(t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(chdir(t) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering setup();
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to head, iterate down.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_seek_head(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_next(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_check_numbers_down(j, 4);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_journal_close(j);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to tail, iterate up.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_seek_tail(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_previous(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_check_numbers_up(j, 4);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_journal_close(j);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering /* Seek to tail, skip to head, iterate down.
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack */
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_seek_tail(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(r = sd_journal_previous_skip(j, 4));
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering assert_se(r == 4);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack test_check_numbers_down(j, 4);
3f9da416457c4265b8f1179516a32ad1a987ff7dLennart Poettering sd_journal_close(j);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* Seek to head, skip to tail, iterate up.
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(sd_journal_seek_head(j));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_ret(r = sd_journal_next_skip(j, 4));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(r == 4);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_check_numbers_up(j, 4);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_journal_close(j);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_info("Done...");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (arg_keep)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_info("Not removing %s", t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
16ac401407959cbc62312e61c2dd76dbc3a0793bLennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering puts("------------------------------------------------------------");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
03e334a1c7dc8c20c38902aa039440763acc9b17Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringstatic void test_sequence_numbers(void) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering char t[] = "/tmp/journal-seq-XXXXXX";
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering JournalFile *one, *two;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering uint64_t seqnum = 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering sd_id128_t seqnum_id;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(mkdtemp(t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(chdir(t) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
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
151b9b9662a90455262ce575a8a8ae74bf4ff336Lennart Poettering append_number(one, 1, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 1);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 2, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 2);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
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
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
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
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
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 3, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 3);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 4, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 4);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(two);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 5, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 5);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(one, 6, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack assert_se(seqnum == 6);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(one);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* restart server */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering seqnum = 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR, 0,
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering true, false, NULL, NULL, NULL, &two) == 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering append_number(two, 7, &seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(seqnum == 5);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* So..., here we have the same seqnum in two files with the
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering * same seqnum_id. */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_close(two);
5892a914d173e4b968d2a14fbf717373dee3999aDaniel Mack
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_info("Done...");
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (arg_keep)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_info("Not removing %s", t);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering else {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering }
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poetteringint main(int argc, char *argv[]) {
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering log_set_max_level(LOG_DEBUG);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering /* journal_file_open requires a valid machine id */
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering if (access("/etc/machine-id", F_OK) != 0)
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return EXIT_TEST_SKIP;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering arg_keep = argc > 1;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_skip(setup_sequential);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_skip(setup_interleaved);
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering test_sequence_numbers();
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering return 0;
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering}
e821075a23fdfa3ca7738fc30bb2d4c430fe10c0Lennart Poettering