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