test-journal.c revision 6eb7a9a0010d035e5bdbbf70227088ce02b2120e
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/***
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2011 Lennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering***/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <fcntl.h>
a9cdc94f7ff40f22a3cf9472f612a80730a1b010Dave Reisner#include <unistd.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include <systemd/sd-journal.h>
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "log.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "journal-file.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "journal-authenticate.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#include "journal-vacuum.h"
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic bool arg_keep = false;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic void test_non_empty(void) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers dual_timestamp ts;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers JournalFile *f;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering struct iovec iovec;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering static const char test[] = "TEST1=1", test2[] = "TEST2=2";
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Object *o;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering uint64_t p;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering char t[] = "/tmp/journal-XXXXXX";
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_set_max_level(LOG_DEBUG);
0affed79d2e30013f07cb94e6f07e3fcb81c02faLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(mkdtemp(t));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(chdir(t) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, true, true, NULL, NULL, NULL, &f) == 0);
7085053a437456ab87d726f3697002dd811fdf7aDaniel Wallace
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering dual_timestamp_get(&ts);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering iovec.iov_base = (void*) test;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering iovec.iov_len = strlen(test);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering iovec.iov_base = (void*) test2;
1b12a7b5896f94bdf33b3a6661ebabd761ea6adcHarald Hoyer iovec.iov_len = strlen(test2);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering iovec.iov_base = (void*) test;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering iovec.iov_len = strlen(test);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_append_entry(f, &ts, &iovec, 1, NULL, NULL, NULL) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#ifdef HAVE_GCRYPT
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_append_tag(f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#endif
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_dump(f);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry(f, o, p, DIRECTION_DOWN, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 2);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry(f, o, p, DIRECTION_DOWN, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 3);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry(f, o, p, DIRECTION_DOWN, &o, &p) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry(f, NULL, 0, DIRECTION_DOWN, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_skip_entry(f, o, p, 2, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 3);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_skip_entry(f, o, p, -2, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_skip_entry(f, o, p, -2, &o, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_find_data_object(f, test, strlen(test), NULL, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 3);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_find_data_object(f, test2, strlen(test2), NULL, &p) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_UP, &o, NULL) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 2);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(journal_file_next_entry_for_data(f, NULL, 0, p, DIRECTION_DOWN, &o, NULL) == 1);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert(le64toh(o->entry.seqnum) == 2);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
b92bea5d2a9481de69bb627a7b442a9f58fca43dZbigniew Jędrzejewski-Szmek assert(journal_file_find_data_object(f, "quux", 4, NULL, &p) == 0);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(journal_file_move_to_entry_by_seqnum(f, 1, DIRECTION_DOWN, &o, NULL) == 1);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(le64toh(o->entry.seqnum) == 1);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(journal_file_move_to_entry_by_seqnum(f, 3, DIRECTION_DOWN, &o, NULL) == 1);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(le64toh(o->entry.seqnum) == 3);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(journal_file_move_to_entry_by_seqnum(f, 2, DIRECTION_DOWN, &o, NULL) == 1);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(le64toh(o->entry.seqnum) == 2);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers assert(journal_file_move_to_entry_by_seqnum(f, 10, DIRECTION_DOWN, &o, NULL) == 0);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_rotate(&f, true, true);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers journal_file_rotate(&f, true, true);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers journal_file_close(f);
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers log_info("Done...");
ffc06c3513d9a0693c7f810d03b20705127ba55aKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers if (arg_keep)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_info("Not removing %s", t);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering puts("------------------------------------------------------------");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sieversstatic void test_empty(void) {
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers JournalFile *f1, *f2, *f3, *f4;
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers char t[] = "/tmp/journal-XXXXXX";
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering log_set_max_level(LOG_DEBUG);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(mkdtemp(t));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(chdir(t) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_open("test.journal", O_RDWR|O_CREAT, 0666, false, false, NULL, NULL, NULL, &f1) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_open("test-compress.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, NULL, &f2) == 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(journal_file_open("test-seal.journal", O_RDWR|O_CREAT, 0666, false, true, NULL, NULL, NULL, &f3) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers assert_se(journal_file_open("test-seal-compress.journal", O_RDWR|O_CREAT, 0666, true, true, NULL, NULL, NULL, &f4) == 0);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_print_header(f1);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers puts("");
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers journal_file_print_header(f2);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers puts("");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_print_header(f3);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers puts("");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_file_print_header(f4);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers puts("");
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_info("Done...");
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
e1636421f46db6d06fbd028ef20a3113fa3e11f8Lennart Poettering if (arg_keep)
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers log_info("Not removing %s", t);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering else {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering journal_directory_vacuum(".", 3000000, 0, 0, NULL);
4d7859d173282e16bb75254c2b4ec14a915ef30bKay Sievers
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering }
17d33cecaa762f7e43200307328af5e9135e2091Giovanni Campagna}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint main(int argc, char *argv[]) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering arg_keep = argc > 1;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_non_empty();
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering test_empty();
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return 0;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering}
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering