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