test-journal-interleaving.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/***
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering This file is part of systemd.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Marius Vollmer
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is free software; you can redistribute it and/or modify it
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering under the terms of the GNU Lesser General Public License as published by
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering (at your option) any later version.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering systemd is distributed in the hope that it will be useful, but
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering Lesser General Public License for more details.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering You should have received a copy of the GNU Lesser General Public License
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering***/
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <unistd.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering#include <fcntl.h>
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "sd-journal.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "alloc-util.h"
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering#include "journal-file.h"
c34255bdb217c2a1d3ac6348252437ab8be9ca46Lennart Poettering#include "journal-vacuum.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "log.h"
24882e06c135584f16f31ba8a00fecde8b7f6fadLennart Poettering#include "parse-util.h"
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering#include "rm-rf.h"
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering#include "util.h"
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering/* This program tests skipping around in a multi-file journal.
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic bool arg_keep = false;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringnoreturn static void log_assert_errno(const char *text, int eno, const char *file, int line, const char *func) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_internal(LOG_CRIT, 0, file, line, func,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering "'%s' failed at %s:%u (%s): %s.",
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt text, file, line, func, strerror(eno));
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt abort();
d5099efc47d4e6ac60816b5381a5f607ab03f06eMichal Schmidt}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering#define assert_ret(expr) \
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering do { \
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering int _r_ = (expr); \
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (_unlikely_(_r_ < 0)) \
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
afc6adb5ec7e73bc13156c43f52fb015cd80cc68Lennart Poettering } while (false)
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic JournalFile *test_open(const char *name) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering JournalFile *f;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return f;
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering}
cde93897cdefdd7c7f66c400a61e42ceee5f6a46Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_close(JournalFile *f) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering journal_file_close (f);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void append_number(JournalFile *f, int n, uint64_t *seqnum) {
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering char *p;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dual_timestamp ts;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering static dual_timestamp previous_ts = {};
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering struct iovec iovec[1];
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering dual_timestamp_get(&ts);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (ts.monotonic <= previous_ts.monotonic)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering ts.monotonic = previous_ts.monotonic + 1;
d3e84ddb885e9d5f0ae9930eb905910e3a81f157Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (ts.realtime <= previous_ts.realtime)
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering ts.realtime = previous_ts.realtime + 1;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering previous_ts = ts;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering iovec[0].iov_base = p;
1ddb263d21099ae42195c2bc382bdf72a7f24f82Lennart Poettering iovec[0].iov_len = strlen(p);
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
d04c1fb8e215600b4950c6778c6c16ddafc14024Lennart Poettering free(p);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_check_number (sd_journal *j, int n) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering const void *d;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering _cleanup_free_ char *k;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering size_t l;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int x;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(k = strndup(d, l));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("%s\n", k);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(safe_atoi(k + 7, &x) >= 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(n == x);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void test_check_numbers_down (sd_journal *j, int count) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int i;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
56f64d95763a799ba4475daf44d8e9f72a1bd474Michal Schmidt for (i = 1; i <= count; i++) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_check_number(j, i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_ret(r = sd_journal_next(j));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering if (i == count)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poetteringstatic void test_check_numbers_up (sd_journal *j, int count) {
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering for (int i = count; i >= 1; i--) {
b87633c4b20e3221748d6c98336cf6c85123cd66Lennart Poettering int r;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_check_number(j, i);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_ret(r = sd_journal_previous(j));
da927ba997d68401563b927f92e6e40e021a8e5cMichal Schmidt if (i == 1)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering else
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(r == 1);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void setup_sequential(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering JournalFile *one, *two;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering one = test_open("one.journal");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering two = test_open("two.journal");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(one, 1, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(one, 2, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(two, 3, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(two, 4, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_close(one);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_close(two);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poetteringstatic void setup_interleaved(void) {
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering JournalFile *one, *two;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering one = test_open("one.journal");
76b543756ef69ce69784d571aefe8de65eaeb331Lennart Poettering two = test_open("two.journal");
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt append_number(one, 1, NULL);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt append_number(two, 2, NULL);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(one, 3, NULL);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering append_number(two, 4, NULL);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt test_close(one);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt test_close(two);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidtstatic void test_skip(void (*setup)(void)) {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt char t[] = "/tmp/journal-skip-XXXXXX";
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sd_journal *j;
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering int r;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(mkdtemp(t));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(chdir(t) >= 0);
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering setup();
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering /* Seek to head, iterate down.
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering */
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering assert_ret(sd_journal_seek_head(j));
ebeccf9eecf5939a2ef772c3160e89efcad96194Lennart Poettering assert_ret(sd_journal_next(j));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_check_numbers_down(j, 4);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering sd_journal_close(j);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* Seek to tail, iterate up.
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering */
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(sd_journal_seek_tail(j));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(sd_journal_previous(j));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_check_numbers_up(j, 4);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt sd_journal_close(j);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* Seek to tail, skip to head, iterate down.
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering */
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(sd_journal_seek_tail(j));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(r = sd_journal_previous_skip(j, 4));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(r == 4);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_check_numbers_down(j, 4);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sd_journal_close(j);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering /* Seek to head, skip to tail, iterate up.
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt */
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_ret(sd_journal_open_directory(&j, t, 0));
943aca8efb39453e3994ccdd1e08534b788c5aeeLennart Poettering assert_ret(sd_journal_seek_head(j));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_ret(r = sd_journal_next_skip(j, 4));
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering assert_se(r == 4);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering test_check_numbers_up(j, 4);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering sd_journal_close(j);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering log_info("Done...");
11b90e69e5620c2483b019340eff121d504db115Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering if (arg_keep)
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering log_info("Not removing %s", t);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt else {
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
19befb2d5fc087f96e40ddc432b2cc9385666209Lennart Poettering }
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering puts("------------------------------------------------------------");
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering}
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poetteringstatic void test_sequence_numbers(void) {
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering char t[] = "/tmp/journal-seq-XXXXXX";
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt JournalFile *one, *two;
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt uint64_t seqnum = 0;
6797c324a653f119a3d7133122648aaa4878ddd6Lennart Poettering sd_id128_t seqnum_id;
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(mkdtemp(t));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(chdir(t) >= 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0644,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering true, false, NULL, NULL, NULL, &one) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering append_number(one, 1, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(seqnum == 1);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering append_number(one, 2, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 2);
5bb658a1784a0fd4f0f32adb4b1fb636ff503f7dKay Sievers
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(one->header->state == STATE_ONLINE);
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt
f647962d64e844689f3e2acfce6102fc47e76df2Michal Schmidt memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering true, false, NULL, NULL, one, &two) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(two->header->state == STATE_ONLINE);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
71fda00f320379f5cbee8e118848de98caaa229dLennart Poettering append_number(two, 3, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 3);
a658cafa98ab55ea948c29bc87eb3945d515fb41Lennart Poettering append_number(two, 4, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 4);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_close(two);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(one, 5, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 5);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering append_number(one, 6, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 6);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_close(one);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* restart server */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering seqnum = 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR, 0,
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering true, false, NULL, NULL, NULL, &two) == 0);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering append_number(two, 7, &seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering printf("seqnum=%"PRIu64"\n", seqnum);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering assert_se(seqnum == 5);
c335068380fe8c9d843cdb2cf8a00f822cfabed3Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering /* So..., here we have the same seqnum in two files with the
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering * same seqnum_id. */
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_close(two);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering log_info("Done...");
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering if (arg_keep)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering log_info("Not removing %s", t);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering else {
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering }
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poetteringint main(int argc, char *argv[]) {
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering log_set_max_level(LOG_DEBUG);
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering /* journal_file_open requires a valid machine id */
d9e34bfda3d34dcde00a876cb052e7de0655e1cbLennart Poettering if (access("/etc/machine-id", F_OK) != 0)
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return EXIT_TEST_SKIP;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering arg_keep = argc > 1;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_skip(setup_sequential);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_skip(setup_interleaved);
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering test_sequence_numbers();
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering return 0;
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering}
1ee306e1248866617c96ed9f4263f375588ad838Lennart Poettering