7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer This file is part of systemd.
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer Copyright 2013 Marius Vollmer
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek Copyright 2013 Zbigniew Jędrzejewski-Szmek
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 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 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/* This program tests skipping around in a multi-file journal.
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.",
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer } while (false)
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic JournalFile *test_open(const char *name) {
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek assert_ret(journal_file_open(name, O_RDWR|O_CREAT, 0644, true, false, NULL, NULL, NULL, &f));
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic void append_number(JournalFile *f, int n, uint64_t *seqnum) {
44cf96e33e83bc9fadad552bda3962e5305e5c2bLennart Poettering if (ts.monotonic <= previous_ts.monotonic)
44cf96e33e83bc9fadad552bda3962e5305e5c2bLennart Poettering ts.monotonic = previous_ts.monotonic + 1;
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek assert_ret(journal_file_append_entry(f, &ts, iovec, 1, seqnum, NULL, NULL));
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic void test_check_number (sd_journal *j, int n) {
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer const void *d;
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic void test_check_numbers_down (sd_journal *j, int count) {
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic void test_check_numbers_up (sd_journal *j, int count) {
510b857f7d1e7e8d38912890536342dd5dd647ddLennart Poetteringstatic void test_skip(void (*setup)(void)) {
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer /* Seek to head, iterate down.
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(sd_journal_open_directory(&j, t, 0));
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer /* Seek to tail, iterate up.
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(sd_journal_open_directory(&j, t, 0));
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer /* Seek to tail, skip to head, iterate down.
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(sd_journal_open_directory(&j, t, 0));
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(r = sd_journal_previous_skip(j, 4));
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer /* Seek to head, skip to tail, iterate up.
7a050b54b7c78717d5efb2e380623ccad2a70148Marius Vollmer assert_ret(sd_journal_open_directory(&j, t, 0));
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek puts("------------------------------------------------------------");
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmekstatic void test_sequence_numbers(void) {
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek char t[] = "/tmp/journal-seq-XXXXXX";
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 printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek printf("seqnum=%"PRIu64"\n", seqnum);
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 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
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);
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 printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek /* restart server */
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);
0c0cdb06c139b52ff103287f6909b3daa5b2dc54Ronny Chevalier assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek printf("seqnum=%"PRIu64"\n", seqnum);
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek /* So..., here we have the same seqnum in two files with the
5cb24cd32bce87cc618b857c059f1187e03d2b24Zbigniew Jędrzejewski-Szmek * same seqnum_id. */
8580d1f73db36e9383e674e388b4fb55828c0c66Lennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
c6878637502b1717a110a9a7e8bba32a8583fcdfLennart Poettering assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
143bfdaf0b890fa7acadf02d1eafacaef1b696bdHolger Hans Peter Freyther /* journal_file_open requires a valid machine id */