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