]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/journal/test-journal-interleaving.c
test-journal: move tests to /var/tmp/ and set FS_NOCOW_FL
[thirdparty/systemd.git] / src / journal / test-journal-interleaving.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
7a050b54 2
7a050b54 3#include <fcntl.h>
cf0fbc49 4#include <unistd.h>
7a050b54 5
c6878637 6#include "sd-journal.h"
6bedfcbb 7
b5efdb8a 8#include "alloc-util.h"
949082ac 9#include "chattr-util.h"
5cfa2c3d 10#include "io-util.h"
7a050b54 11#include "journal-file.h"
5cb24cd3 12#include "journal-vacuum.h"
7a050b54 13#include "log.h"
6bedfcbb 14#include "parse-util.h"
c6878637 15#include "rm-rf.h"
317bb217 16#include "tests.h"
5cfa2c3d 17#include "util.h"
7a050b54 18
317bb217 19/* This program tests skipping around in a multi-file journal. */
7a050b54 20
5cb24cd3
ZJS
21static bool arg_keep = false;
22
848e863a 23_noreturn_ static void log_assert_errno(const char *text, int error, const char *file, int line, const char *func) {
9eec7d12
ZJS
24 log_internal(LOG_CRIT, error, file, line, func,
25 "'%s' failed at %s:%u (%s): %m", text, file, line, func);
7a050b54
MV
26 abort();
27}
28
29#define assert_ret(expr) \
30 do { \
31 int _r_ = (expr); \
32 if (_unlikely_(_r_ < 0)) \
33 log_assert_errno(#expr, -_r_, __FILE__, __LINE__, __PRETTY_FUNCTION__); \
34 } while (false)
35
510b857f 36static JournalFile *test_open(const char *name) {
7a050b54 37 JournalFile *f;
57850536 38 assert_ret(journal_file_open(-1, name, O_RDWR|O_CREAT, 0644, true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &f));
7a050b54
MV
39 return f;
40}
41
510b857f 42static void test_close(JournalFile *f) {
69a3a6fd 43 (void) journal_file_close (f);
7a050b54
MV
44}
45
510b857f 46static void append_number(JournalFile *f, int n, uint64_t *seqnum) {
7a050b54
MV
47 char *p;
48 dual_timestamp ts;
44cf96e3 49 static dual_timestamp previous_ts = {};
7a050b54
MV
50 struct iovec iovec[1];
51
52 dual_timestamp_get(&ts);
53
44cf96e3
LP
54 if (ts.monotonic <= previous_ts.monotonic)
55 ts.monotonic = previous_ts.monotonic + 1;
56
57 if (ts.realtime <= previous_ts.realtime)
58 ts.realtime = previous_ts.realtime + 1;
59
60 previous_ts = ts;
61
7a050b54 62 assert_se(asprintf(&p, "NUMBER=%d", n) >= 0);
5cfa2c3d 63 iovec[0] = IOVEC_MAKE_STRING(p);
d180c349 64 assert_ret(journal_file_append_entry(f, &ts, NULL, iovec, 1, seqnum, NULL, NULL));
510b857f 65 free(p);
7a050b54
MV
66}
67
510b857f 68static void test_check_number (sd_journal *j, int n) {
7a050b54 69 const void *d;
510b857f 70 _cleanup_free_ char *k;
7a050b54
MV
71 size_t l;
72 int x;
73
74 assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l));
75 assert_se(k = strndup(d, l));
76 printf("%s\n", k);
77
78 assert_se(safe_atoi(k + 7, &x) >= 0);
79 assert_se(n == x);
80}
81
510b857f
LP
82static void test_check_numbers_down (sd_journal *j, int count) {
83 int i;
84
85 for (i = 1; i <= count; i++) {
7a050b54
MV
86 int r;
87 test_check_number(j, i);
88 assert_ret(r = sd_journal_next(j));
89 if (i == count)
90 assert_se(r == 0);
91 else
92 assert_se(r == 1);
93 }
94
95}
96
510b857f 97static void test_check_numbers_up (sd_journal *j, int count) {
7a050b54
MV
98 for (int i = count; i >= 1; i--) {
99 int r;
100 test_check_number(j, i);
101 assert_ret(r = sd_journal_previous(j));
102 if (i == 1)
103 assert_se(r == 0);
104 else
105 assert_se(r == 1);
106 }
107
108}
109
110static void setup_sequential(void) {
111 JournalFile *one, *two;
112 one = test_open("one.journal");
113 two = test_open("two.journal");
5cb24cd3
ZJS
114 append_number(one, 1, NULL);
115 append_number(one, 2, NULL);
116 append_number(two, 3, NULL);
117 append_number(two, 4, NULL);
7a050b54
MV
118 test_close(one);
119 test_close(two);
120}
121
122static void setup_interleaved(void) {
123 JournalFile *one, *two;
124 one = test_open("one.journal");
125 two = test_open("two.journal");
5cb24cd3
ZJS
126 append_number(one, 1, NULL);
127 append_number(two, 2, NULL);
128 append_number(one, 3, NULL);
129 append_number(two, 4, NULL);
7a050b54
MV
130 test_close(one);
131 test_close(two);
132}
133
949082ac
LP
134static void mkdtemp_chdir_chattr(char *path) {
135 assert_se(mkdtemp(path));
136 assert_se(chdir(path) >= 0);
137
138 /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our
139 * directory during the test run */
140 (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL, NULL);
141}
142
510b857f 143static void test_skip(void (*setup)(void)) {
949082ac 144 char t[] = "/var/tmp/journal-skip-XXXXXX";
7a050b54
MV
145 sd_journal *j;
146 int r;
147
949082ac 148 mkdtemp_chdir_chattr(t);
7a050b54
MV
149
150 setup();
151
152 /* Seek to head, iterate down.
153 */
154 assert_ret(sd_journal_open_directory(&j, t, 0));
155 assert_ret(sd_journal_seek_head(j));
156 assert_ret(sd_journal_next(j));
157 test_check_numbers_down(j, 4);
158 sd_journal_close(j);
159
160 /* Seek to tail, iterate up.
161 */
162 assert_ret(sd_journal_open_directory(&j, t, 0));
163 assert_ret(sd_journal_seek_tail(j));
164 assert_ret(sd_journal_previous(j));
165 test_check_numbers_up(j, 4);
166 sd_journal_close(j);
167
168 /* Seek to tail, skip to head, iterate down.
169 */
170 assert_ret(sd_journal_open_directory(&j, t, 0));
171 assert_ret(sd_journal_seek_tail(j));
172 assert_ret(r = sd_journal_previous_skip(j, 4));
173 assert_se(r == 4);
174 test_check_numbers_down(j, 4);
175 sd_journal_close(j);
176
177 /* Seek to head, skip to tail, iterate up.
178 */
179 assert_ret(sd_journal_open_directory(&j, t, 0));
180 assert_ret(sd_journal_seek_head(j));
181 assert_ret(r = sd_journal_next_skip(j, 4));
182 assert_se(r == 4);
183 test_check_numbers_up(j, 4);
184 sd_journal_close(j);
185
5cb24cd3
ZJS
186 log_info("Done...");
187
188 if (arg_keep)
189 log_info("Not removing %s", t);
190 else {
8580d1f7 191 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 192
c6878637 193 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3
ZJS
194 }
195
196 puts("------------------------------------------------------------");
197}
198
199static void test_sequence_numbers(void) {
200
949082ac 201 char t[] = "/var/tmp/journal-seq-XXXXXX";
5cb24cd3
ZJS
202 JournalFile *one, *two;
203 uint64_t seqnum = 0;
204 sd_id128_t seqnum_id;
205
949082ac 206 mkdtemp_chdir_chattr(t);
5cb24cd3 207
5d1ce257 208 assert_se(journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, 0644,
57850536 209 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &one) == 0);
5cb24cd3
ZJS
210
211 append_number(one, 1, &seqnum);
212 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 213 assert_se(seqnum == 1);
5cb24cd3
ZJS
214 append_number(one, 2, &seqnum);
215 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 216 assert_se(seqnum == 2);
5cb24cd3 217
0c0cdb06
RC
218 assert_se(one->header->state == STATE_ONLINE);
219 assert_se(!sd_id128_equal(one->header->file_id, one->header->machine_id));
220 assert_se(!sd_id128_equal(one->header->file_id, one->header->boot_id));
221 assert_se(sd_id128_equal(one->header->file_id, one->header->seqnum_id));
5cb24cd3
ZJS
222
223 memcpy(&seqnum_id, &one->header->seqnum_id, sizeof(sd_id128_t));
224
5d1ce257 225 assert_se(journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, 0644,
57850536 226 true, (uint64_t) -1, false, NULL, NULL, NULL, one, &two) == 0);
5cb24cd3 227
0c0cdb06
RC
228 assert_se(two->header->state == STATE_ONLINE);
229 assert_se(!sd_id128_equal(two->header->file_id, one->header->file_id));
230 assert_se(sd_id128_equal(one->header->machine_id, one->header->machine_id));
231 assert_se(sd_id128_equal(one->header->boot_id, one->header->boot_id));
232 assert_se(sd_id128_equal(one->header->seqnum_id, one->header->seqnum_id));
5cb24cd3
ZJS
233
234 append_number(two, 3, &seqnum);
235 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 236 assert_se(seqnum == 3);
5cb24cd3
ZJS
237 append_number(two, 4, &seqnum);
238 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 239 assert_se(seqnum == 4);
5cb24cd3
ZJS
240
241 test_close(two);
242
243 append_number(one, 5, &seqnum);
244 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 245 assert_se(seqnum == 5);
5cb24cd3
ZJS
246
247 append_number(one, 6, &seqnum);
248 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 249 assert_se(seqnum == 6);
5cb24cd3
ZJS
250
251 test_close(one);
252
253 /* restart server */
254 seqnum = 0;
255
5d1ce257 256 assert_se(journal_file_open(-1, "two.journal", O_RDWR, 0,
57850536 257 true, (uint64_t) -1, false, NULL, NULL, NULL, NULL, &two) == 0);
5cb24cd3 258
0c0cdb06 259 assert_se(sd_id128_equal(two->header->seqnum_id, seqnum_id));
5cb24cd3
ZJS
260
261 append_number(two, 7, &seqnum);
262 printf("seqnum=%"PRIu64"\n", seqnum);
0c0cdb06 263 assert_se(seqnum == 5);
5cb24cd3
ZJS
264
265 /* So..., here we have the same seqnum in two files with the
266 * same seqnum_id. */
267
268 test_close(two);
269
270 log_info("Done...");
271
272 if (arg_keep)
273 log_info("Not removing %s", t);
274 else {
8580d1f7 275 journal_directory_vacuum(".", 3000000, 0, 0, NULL, true);
5cb24cd3 276
c6878637 277 assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0);
5cb24cd3 278 }
7a050b54
MV
279}
280
281int main(int argc, char *argv[]) {
6d7c4033 282 test_setup_logging(LOG_DEBUG);
5cb24cd3 283
143bfdaf 284 /* journal_file_open requires a valid machine id */
317bb217
ZJS
285 if (access("/etc/machine-id", F_OK) != 0)
286 return log_tests_skipped("/etc/machine-id not found");
143bfdaf 287
5cb24cd3
ZJS
288 arg_keep = argc > 1;
289
7a050b54
MV
290 test_skip(setup_sequential);
291 test_skip(setup_interleaved);
292
5cb24cd3
ZJS
293 test_sequence_numbers();
294
7a050b54
MV
295 return 0;
296}