test-journal-interleaving.c revision 44cf96e33e83bc9fadad552bda3962e5305e5c2b
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Marius Vollmer
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 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 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/>.
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering/* This program tests skipping around in a multi-file journal.
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic bool arg_keep = false;
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 log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering } while (false)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic JournalFile *test_open(const char *name) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void append_number(JournalFile *f, int n, uint64_t *seqnum) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (ts.monotonic <= previous_ts.monotonic)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering ts.monotonic = previous_ts.monotonic + 1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void test_check_number (sd_journal *j, int n) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering const void *d;
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void test_check_numbers_down (sd_journal *j, int count) {
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_check_numbers_up (sd_journal *j, int count) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringstatic void setup_sequential(void) {
ad867662936a4c7ab2c7116d804c272338801231Lennart Poetteringstatic void setup_interleaved(void) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic void test_skip(void (*setup)(void)) {
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering /* Seek to head, iterate down.
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering /* Seek to tail, iterate up.
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
309e9d86f0e7f9c5f0a2a09227bdfdb3174d4436Lennart Poettering /* Seek to tail, skip to head, iterate down.
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(r = sd_journal_previous_skip(j, 4));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* Seek to head, skip to tail, iterate up.
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(sd_journal_open_directory(&j, t, 0));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering assert_ret(r = sd_journal_next_skip(j, 4));
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering journal_directory_vacuum(".", 3000000, 0, NULL, true);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering puts("------------------------------------------------------------");
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringstatic void test_sequence_numbers(void) {
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 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 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0644,
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering true, false, NULL, NULL, one, &two) == 0);
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));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* restart server */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering assert_se(journal_file_open("two.journal", O_RDWR, 0,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering true, false, NULL, NULL, NULL, &two) == 0);
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* So..., here we have the same seqnum in two files with the
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering * same seqnum_id. */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering journal_directory_vacuum(".", 3000000, 0, NULL, true);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering /* journal_file_open requires a valid machine id */