]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
7a050b54 | 2 | |
7a050b54 | 3 | #include <fcntl.h> |
cf0fbc49 | 4 | #include <unistd.h> |
7a050b54 | 5 | |
3a9ca230 | 6 | #include "sd-id128.h" |
c6878637 | 7 | #include "sd-journal.h" |
6bedfcbb | 8 | |
b5efdb8a | 9 | #include "alloc-util.h" |
949082ac | 10 | #include "chattr-util.h" |
5cfa2c3d | 11 | #include "io-util.h" |
5cb24cd3 | 12 | #include "journal-vacuum.h" |
7a050b54 | 13 | #include "log.h" |
7c1784db | 14 | #include "logs-show.h" |
d02af6f3 | 15 | #include "managed-journal-file.h" |
6bedfcbb | 16 | #include "parse-util.h" |
c6878637 | 17 | #include "rm-rf.h" |
317bb217 | 18 | #include "tests.h" |
7a050b54 | 19 | |
317bb217 | 20 | /* This program tests skipping around in a multi-file journal. */ |
7a050b54 | 21 | |
5cb24cd3 ZJS |
22 | static bool arg_keep = false; |
23 | ||
c0f86d66 | 24 | _noreturn_ static void log_assert_errno(const char *text, int error, const char *file, unsigned line, const char *func) { |
9eec7d12 ZJS |
25 | log_internal(LOG_CRIT, error, file, line, func, |
26 | "'%s' failed at %s:%u (%s): %m", text, file, line, func); | |
7a050b54 MV |
27 | abort(); |
28 | } | |
29 | ||
30 | #define assert_ret(expr) \ | |
31 | do { \ | |
32 | int _r_ = (expr); \ | |
33 | if (_unlikely_(_r_ < 0)) \ | |
5a9b9157 | 34 | log_assert_errno(#expr, -_r_, PROJECT_FILE, __LINE__, __func__); \ |
7a050b54 MV |
35 | } while (false) |
36 | ||
d02af6f3 | 37 | static ManagedJournalFile *test_open(const char *name) { |
74fb5be6 | 38 | _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; |
d02af6f3 | 39 | ManagedJournalFile *f; |
74fb5be6 VC |
40 | |
41 | m = mmap_cache_new(); | |
42 | assert_se(m != NULL); | |
43 | ||
49615dbd | 44 | assert_ret(managed_journal_file_open(-1, name, O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, UINT64_MAX, NULL, m, NULL, NULL, &f)); |
7a050b54 MV |
45 | return f; |
46 | } | |
47 | ||
d02af6f3 DDM |
48 | static void test_close(ManagedJournalFile *f) { |
49 | (void) managed_journal_file_close(f); | |
7a050b54 MV |
50 | } |
51 | ||
7c1784db YW |
52 | static void append_number(ManagedJournalFile *f, int n, const sd_id128_t *boot_id, uint64_t *seqnum) { |
53 | _cleanup_free_ char *p = NULL, *q = NULL; | |
7a050b54 | 54 | dual_timestamp ts; |
44cf96e3 | 55 | static dual_timestamp previous_ts = {}; |
7c1784db YW |
56 | struct iovec iovec[2]; |
57 | size_t n_iov = 0; | |
7a050b54 MV |
58 | |
59 | dual_timestamp_get(&ts); | |
60 | ||
44cf96e3 LP |
61 | if (ts.monotonic <= previous_ts.monotonic) |
62 | ts.monotonic = previous_ts.monotonic + 1; | |
63 | ||
64 | if (ts.realtime <= previous_ts.realtime) | |
65 | ts.realtime = previous_ts.realtime + 1; | |
66 | ||
67 | previous_ts = ts; | |
68 | ||
7a050b54 | 69 | assert_se(asprintf(&p, "NUMBER=%d", n) >= 0); |
7c1784db YW |
70 | iovec[n_iov++] = IOVEC_MAKE_STRING(p); |
71 | ||
72 | if (boot_id) { | |
73 | assert_se(q = strjoin("_BOOT_ID=", SD_ID128_TO_STRING(*boot_id))); | |
74 | iovec[n_iov++] = IOVEC_MAKE_STRING(q); | |
75 | } | |
76 | ||
77 | assert_ret(journal_file_append_entry(f->file, &ts, boot_id, iovec, n_iov, seqnum, NULL, NULL, NULL)); | |
7a050b54 MV |
78 | } |
79 | ||
68da8adf | 80 | static void test_check_number(sd_journal *j, int n) { |
7c1784db | 81 | sd_id128_t boot_id; |
7a050b54 | 82 | const void *d; |
3d41b6b8 | 83 | _cleanup_free_ char *k = NULL; |
7a050b54 MV |
84 | size_t l; |
85 | int x; | |
86 | ||
7c1784db | 87 | assert_se(sd_journal_get_monotonic_usec(j, NULL, &boot_id) >= 0); |
7a050b54 MV |
88 | assert_ret(sd_journal_get_data(j, "NUMBER", &d, &l)); |
89 | assert_se(k = strndup(d, l)); | |
7c1784db | 90 | printf("%s %s (expected=%i)\n", SD_ID128_TO_STRING(boot_id), k, n); |
7a050b54 | 91 | |
43e460a5 | 92 | assert_se(safe_atoi(k + STRLEN("NUMBER="), &x) >= 0); |
7a050b54 MV |
93 | assert_se(n == x); |
94 | } | |
95 | ||
68da8adf | 96 | static void test_check_numbers_down(sd_journal *j, int count) { |
510b857f LP |
97 | int i; |
98 | ||
99 | for (i = 1; i <= count; i++) { | |
7a050b54 MV |
100 | int r; |
101 | test_check_number(j, i); | |
102 | assert_ret(r = sd_journal_next(j)); | |
103 | if (i == count) | |
104 | assert_se(r == 0); | |
105 | else | |
106 | assert_se(r == 1); | |
107 | } | |
108 | ||
109 | } | |
110 | ||
68da8adf | 111 | static void test_check_numbers_up(sd_journal *j, int count) { |
7a050b54 MV |
112 | for (int i = count; i >= 1; i--) { |
113 | int r; | |
114 | test_check_number(j, i); | |
115 | assert_ret(r = sd_journal_previous(j)); | |
116 | if (i == 1) | |
117 | assert_se(r == 0); | |
118 | else | |
119 | assert_se(r == 1); | |
120 | } | |
121 | ||
122 | } | |
123 | ||
124 | static void setup_sequential(void) { | |
43e460a5 | 125 | ManagedJournalFile *f1, *f2, *f3; |
7c1784db | 126 | sd_id128_t id; |
43e460a5 YW |
127 | |
128 | f1 = test_open("one.journal"); | |
129 | f2 = test_open("two.journal"); | |
130 | f3 = test_open("three.journal"); | |
7c1784db YW |
131 | assert_se(sd_id128_randomize(&id) >= 0); |
132 | log_info("boot_id: %s", SD_ID128_TO_STRING(id)); | |
133 | append_number(f1, 1, &id, NULL); | |
134 | append_number(f1, 2, &id, NULL); | |
135 | append_number(f1, 3, &id, NULL); | |
136 | append_number(f2, 4, &id, NULL); | |
137 | assert_se(sd_id128_randomize(&id) >= 0); | |
138 | log_info("boot_id: %s", SD_ID128_TO_STRING(id)); | |
139 | append_number(f2, 5, &id, NULL); | |
140 | append_number(f2, 6, &id, NULL); | |
141 | append_number(f3, 7, &id, NULL); | |
142 | append_number(f3, 8, &id, NULL); | |
143 | assert_se(sd_id128_randomize(&id) >= 0); | |
144 | log_info("boot_id: %s", SD_ID128_TO_STRING(id)); | |
145 | append_number(f3, 9, &id, NULL); | |
43e460a5 YW |
146 | test_close(f1); |
147 | test_close(f2); | |
148 | test_close(f3); | |
7a050b54 MV |
149 | } |
150 | ||
151 | static void setup_interleaved(void) { | |
43e460a5 | 152 | ManagedJournalFile *f1, *f2, *f3; |
7c1784db | 153 | sd_id128_t id; |
43e460a5 YW |
154 | |
155 | f1 = test_open("one.journal"); | |
156 | f2 = test_open("two.journal"); | |
157 | f3 = test_open("three.journal"); | |
7c1784db YW |
158 | assert_se(sd_id128_randomize(&id) >= 0); |
159 | log_info("boot_id: %s", SD_ID128_TO_STRING(id)); | |
160 | append_number(f1, 1, &id, NULL); | |
161 | append_number(f2, 2, &id, NULL); | |
162 | append_number(f3, 3, &id, NULL); | |
163 | append_number(f1, 4, &id, NULL); | |
164 | append_number(f2, 5, &id, NULL); | |
165 | append_number(f3, 6, &id, NULL); | |
166 | append_number(f1, 7, &id, NULL); | |
167 | append_number(f2, 8, &id, NULL); | |
168 | append_number(f3, 9, &id, NULL); | |
43e460a5 YW |
169 | test_close(f1); |
170 | test_close(f2); | |
171 | test_close(f3); | |
7a050b54 MV |
172 | } |
173 | ||
949082ac LP |
174 | static void mkdtemp_chdir_chattr(char *path) { |
175 | assert_se(mkdtemp(path)); | |
176 | assert_se(chdir(path) >= 0); | |
177 | ||
178 | /* Speed up things a bit on btrfs, ensuring that CoW is turned off for all files created in our | |
179 | * directory during the test run */ | |
180 | (void) chattr_path(path, FS_NOCOW_FL, FS_NOCOW_FL, NULL); | |
181 | } | |
182 | ||
68da8adf | 183 | static void test_skip_one(void (*setup)(void)) { |
949082ac | 184 | char t[] = "/var/tmp/journal-skip-XXXXXX"; |
7a050b54 MV |
185 | sd_journal *j; |
186 | int r; | |
187 | ||
949082ac | 188 | mkdtemp_chdir_chattr(t); |
7a050b54 MV |
189 | |
190 | setup(); | |
191 | ||
43e460a5 | 192 | /* Seek to head, iterate down. */ |
7a050b54 MV |
193 | assert_ret(sd_journal_open_directory(&j, t, 0)); |
194 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 | 195 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ |
43e460a5 | 196 | test_check_numbers_down(j, 9); |
7a050b54 MV |
197 | sd_journal_close(j); |
198 | ||
43e460a5 YW |
199 | /* Seek to head, iterate down. */ |
200 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
201 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 YW |
202 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ |
203 | assert_se(sd_journal_previous(j) == 0); /* no-op */ | |
43e460a5 YW |
204 | test_check_numbers_down(j, 9); |
205 | sd_journal_close(j); | |
206 | ||
45689fd2 YW |
207 | /* Seek to head twice, iterate down. */ |
208 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
209 | assert_ret(sd_journal_seek_head(j)); | |
210 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ | |
211 | assert_ret(sd_journal_seek_head(j)); | |
212 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ | |
213 | test_check_numbers_down(j, 9); | |
214 | sd_journal_close(j); | |
215 | ||
43e460a5 YW |
216 | /* Seek to head, move to previous, then iterate down. */ |
217 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
218 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 YW |
219 | assert_se(sd_journal_previous(j) == 0); /* no-op */ |
220 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ | |
43e460a5 YW |
221 | test_check_numbers_down(j, 9); |
222 | sd_journal_close(j); | |
223 | ||
224 | /* Seek to head, walk several steps, then iterate down. */ | |
225 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
226 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 YW |
227 | assert_se(sd_journal_previous(j) == 0); /* no-op */ |
228 | assert_se(sd_journal_previous(j) == 0); /* no-op */ | |
229 | assert_se(sd_journal_previous(j) == 0); /* no-op */ | |
230 | assert_se(sd_journal_next(j) == 1); /* pointing to the first entry */ | |
231 | assert_se(sd_journal_previous(j) == 0); /* no-op */ | |
232 | assert_se(sd_journal_previous(j) == 0); /* no-op */ | |
43e460a5 YW |
233 | test_check_numbers_down(j, 9); |
234 | sd_journal_close(j); | |
235 | ||
236 | /* Seek to tail, iterate up. */ | |
7a050b54 MV |
237 | assert_ret(sd_journal_open_directory(&j, t, 0)); |
238 | assert_ret(sd_journal_seek_tail(j)); | |
836809d1 | 239 | assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */ |
43e460a5 YW |
240 | test_check_numbers_up(j, 9); |
241 | sd_journal_close(j); | |
242 | ||
45689fd2 YW |
243 | /* Seek to tail twice, iterate up. */ |
244 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
245 | assert_ret(sd_journal_seek_tail(j)); | |
246 | assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */ | |
4aa33df8 YW |
247 | assert_ret(sd_journal_seek_tail(j)); |
248 | assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */ | |
45689fd2 YW |
249 | test_check_numbers_up(j, 9); |
250 | sd_journal_close(j); | |
251 | ||
43e460a5 YW |
252 | /* Seek to tail, move to next, then iterate up. */ |
253 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
254 | assert_ret(sd_journal_seek_tail(j)); | |
836809d1 YW |
255 | assert_se(sd_journal_next(j) == 0); /* no-op */ |
256 | assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry */ | |
43e460a5 | 257 | test_check_numbers_up(j, 9); |
7a050b54 MV |
258 | sd_journal_close(j); |
259 | ||
43e460a5 | 260 | /* Seek to tail, walk several steps, then iterate up. */ |
7a050b54 MV |
261 | assert_ret(sd_journal_open_directory(&j, t, 0)); |
262 | assert_ret(sd_journal_seek_tail(j)); | |
836809d1 YW |
263 | assert_se(sd_journal_next(j) == 0); /* no-op */ |
264 | assert_se(sd_journal_next(j) == 0); /* no-op */ | |
265 | assert_se(sd_journal_next(j) == 0); /* no-op */ | |
266 | assert_se(sd_journal_previous(j) == 1); /* pointing to the last entry. */ | |
267 | assert_se(sd_journal_next(j) == 0); /* no-op */ | |
268 | assert_se(sd_journal_next(j) == 0); /* no-op */ | |
43e460a5 YW |
269 | test_check_numbers_up(j, 9); |
270 | sd_journal_close(j); | |
271 | ||
272 | /* Seek to tail, skip to head, iterate down. */ | |
273 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
274 | assert_ret(sd_journal_seek_tail(j)); | |
836809d1 | 275 | assert_se(sd_journal_previous_skip(j, 9) == 9); /* pointing to the first entry. */ |
43e460a5 YW |
276 | test_check_numbers_down(j, 9); |
277 | sd_journal_close(j); | |
278 | ||
9a27ef09 | 279 | /* Seek to tail, skip to head in a more complex way, then iterate down. */ |
43e460a5 YW |
280 | assert_ret(sd_journal_open_directory(&j, t, 0)); |
281 | assert_ret(sd_journal_seek_tail(j)); | |
836809d1 YW |
282 | assert_se(sd_journal_next(j) == 0); |
283 | assert_se(sd_journal_previous_skip(j, 4) == 4); | |
284 | assert_se(sd_journal_previous_skip(j, 5) == 5); | |
285 | assert_se(sd_journal_previous(j) == 0); | |
286 | assert_se(sd_journal_previous_skip(j, 5) == 0); | |
287 | assert_se(sd_journal_next(j) == 1); | |
288 | assert_se(sd_journal_previous_skip(j, 5) == 1); | |
289 | assert_se(sd_journal_next(j) == 1); | |
290 | assert_se(sd_journal_next(j) == 1); | |
291 | assert_se(sd_journal_previous(j) == 1); | |
292 | assert_se(sd_journal_next(j) == 1); | |
293 | assert_se(sd_journal_next(j) == 1); | |
294 | assert_se(sd_journal_previous_skip(j, 5) == 3); | |
43e460a5 YW |
295 | test_check_numbers_down(j, 9); |
296 | sd_journal_close(j); | |
297 | ||
298 | /* Seek to head, skip to tail, iterate up. */ | |
299 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
300 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 | 301 | assert_se(sd_journal_next_skip(j, 9) == 9); |
43e460a5 | 302 | test_check_numbers_up(j, 9); |
7a050b54 MV |
303 | sd_journal_close(j); |
304 | ||
43e460a5 | 305 | /* Seek to head, skip to tail in a more complex way, then iterate up. */ |
7a050b54 MV |
306 | assert_ret(sd_journal_open_directory(&j, t, 0)); |
307 | assert_ret(sd_journal_seek_head(j)); | |
836809d1 YW |
308 | assert_se(sd_journal_previous(j) == 0); |
309 | assert_se(sd_journal_next_skip(j, 4) == 4); | |
310 | assert_se(sd_journal_next_skip(j, 5) == 5); | |
311 | assert_se(sd_journal_next(j) == 0); | |
312 | assert_se(sd_journal_next_skip(j, 5) == 0); | |
313 | assert_se(sd_journal_previous(j) == 1); | |
314 | assert_se(sd_journal_next_skip(j, 5) == 1); | |
315 | assert_se(sd_journal_previous(j) == 1); | |
316 | assert_se(sd_journal_previous(j) == 1); | |
317 | assert_se(sd_journal_next(j) == 1); | |
318 | assert_se(sd_journal_previous(j) == 1); | |
319 | assert_se(sd_journal_previous(j) == 1); | |
320 | assert_se(r = sd_journal_next_skip(j, 5) == 3); | |
43e460a5 | 321 | test_check_numbers_up(j, 9); |
7a050b54 MV |
322 | sd_journal_close(j); |
323 | ||
5cb24cd3 ZJS |
324 | log_info("Done..."); |
325 | ||
326 | if (arg_keep) | |
327 | log_info("Not removing %s", t); | |
328 | else { | |
8580d1f7 | 329 | journal_directory_vacuum(".", 3000000, 0, 0, NULL, true); |
5cb24cd3 | 330 | |
c6878637 | 331 | assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
5cb24cd3 ZJS |
332 | } |
333 | ||
334 | puts("------------------------------------------------------------"); | |
335 | } | |
336 | ||
68da8adf JJ |
337 | TEST(skip) { |
338 | test_skip_one(setup_sequential); | |
339 | test_skip_one(setup_interleaved); | |
340 | } | |
5cb24cd3 | 341 | |
7c1784db YW |
342 | static void test_boot_id_one(void (*setup)(void), size_t n_boots_expected) { |
343 | char t[] = "/var/tmp/journal-boot-id-XXXXXX"; | |
344 | sd_journal *j; | |
345 | _cleanup_free_ BootId *boots = NULL; | |
346 | size_t n_boots; | |
347 | ||
348 | mkdtemp_chdir_chattr(t); | |
349 | ||
350 | setup(); | |
351 | ||
352 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
353 | assert_se(journal_get_boots(j, &boots, &n_boots) >= 0); | |
354 | assert_se(boots); | |
355 | assert_se(n_boots == n_boots_expected); | |
356 | sd_journal_close(j); | |
357 | ||
358 | FOREACH_ARRAY(b, boots, n_boots) { | |
359 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
360 | assert_se(journal_find_boot_by_id(j, b->id) == 1); | |
361 | sd_journal_close(j); | |
362 | } | |
363 | ||
364 | for (int i = - (int) n_boots + 1; i <= (int) n_boots; i++) { | |
365 | sd_id128_t id; | |
366 | ||
367 | assert_ret(sd_journal_open_directory(&j, t, 0)); | |
368 | assert_se(journal_find_boot_by_offset(j, i, &id) == 1); | |
369 | if (i <= 0) | |
370 | assert_se(sd_id128_equal(id, boots[n_boots + i - 1].id)); | |
371 | else | |
372 | assert_se(sd_id128_equal(id, boots[i - 1].id)); | |
373 | sd_journal_close(j); | |
374 | } | |
375 | ||
376 | log_info("Done..."); | |
377 | ||
378 | if (arg_keep) | |
379 | log_info("Not removing %s", t); | |
380 | else { | |
381 | journal_directory_vacuum(".", 3000000, 0, 0, NULL, true); | |
382 | ||
383 | assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); | |
384 | } | |
385 | ||
386 | puts("------------------------------------------------------------"); | |
387 | } | |
388 | ||
389 | TEST(boot_id) { | |
390 | test_boot_id_one(setup_sequential, 3); | |
391 | } | |
392 | ||
c92f1ebe | 393 | static void test_sequence_numbers_one(void) { |
74fb5be6 | 394 | _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL; |
949082ac | 395 | char t[] = "/var/tmp/journal-seq-XXXXXX"; |
d02af6f3 | 396 | ManagedJournalFile *one, *two; |
5cb24cd3 ZJS |
397 | uint64_t seqnum = 0; |
398 | sd_id128_t seqnum_id; | |
399 | ||
74fb5be6 VC |
400 | m = mmap_cache_new(); |
401 | assert_se(m != NULL); | |
402 | ||
949082ac | 403 | mkdtemp_chdir_chattr(t); |
5cb24cd3 | 404 | |
49615dbd LP |
405 | assert_se(managed_journal_file_open(-1, "one.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, |
406 | UINT64_MAX, NULL, m, NULL, NULL, &one) == 0); | |
5cb24cd3 | 407 | |
7c1784db | 408 | append_number(one, 1, NULL, &seqnum); |
5cb24cd3 | 409 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 410 | assert_se(seqnum == 1); |
7c1784db | 411 | append_number(one, 2, NULL, &seqnum); |
5cb24cd3 | 412 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 413 | assert_se(seqnum == 2); |
5cb24cd3 | 414 | |
035b0f8f DDM |
415 | assert_se(one->file->header->state == STATE_ONLINE); |
416 | assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->machine_id)); | |
9204fc64 | 417 | assert_se(!sd_id128_equal(one->file->header->file_id, one->file->header->tail_entry_boot_id)); |
035b0f8f | 418 | assert_se(sd_id128_equal(one->file->header->file_id, one->file->header->seqnum_id)); |
5cb24cd3 | 419 | |
035b0f8f | 420 | memcpy(&seqnum_id, &one->file->header->seqnum_id, sizeof(sd_id128_t)); |
5cb24cd3 | 421 | |
49615dbd LP |
422 | assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR|O_CREAT, JOURNAL_COMPRESS, 0644, |
423 | UINT64_MAX, NULL, m, NULL, one, &two) == 0); | |
5cb24cd3 | 424 | |
035b0f8f DDM |
425 | assert_se(two->file->header->state == STATE_ONLINE); |
426 | assert_se(!sd_id128_equal(two->file->header->file_id, one->file->header->file_id)); | |
1fa2ebbe YW |
427 | assert_se(sd_id128_equal(two->file->header->machine_id, one->file->header->machine_id)); |
428 | assert_se(sd_id128_is_null(two->file->header->tail_entry_boot_id)); /* Not written yet. */ | |
429 | assert_se(sd_id128_equal(two->file->header->seqnum_id, one->file->header->seqnum_id)); | |
5cb24cd3 | 430 | |
7c1784db | 431 | append_number(two, 3, NULL, &seqnum); |
5cb24cd3 | 432 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 433 | assert_se(seqnum == 3); |
7c1784db | 434 | append_number(two, 4, NULL, &seqnum); |
5cb24cd3 | 435 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 436 | assert_se(seqnum == 4); |
5cb24cd3 | 437 | |
1fa2ebbe YW |
438 | /* Verify tail_entry_boot_id. */ |
439 | assert_se(sd_id128_equal(two->file->header->tail_entry_boot_id, one->file->header->tail_entry_boot_id)); | |
440 | ||
5cb24cd3 ZJS |
441 | test_close(two); |
442 | ||
7c1784db | 443 | append_number(one, 5, NULL, &seqnum); |
5cb24cd3 | 444 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 445 | assert_se(seqnum == 5); |
5cb24cd3 | 446 | |
7c1784db | 447 | append_number(one, 6, NULL, &seqnum); |
5cb24cd3 | 448 | printf("seqnum=%"PRIu64"\n", seqnum); |
0c0cdb06 | 449 | assert_se(seqnum == 6); |
5cb24cd3 ZJS |
450 | |
451 | test_close(one); | |
452 | ||
3a9ca230 NR |
453 | /* If the machine-id is not initialized, the header file verification |
454 | * (which happens when re-opening a journal file) will fail. */ | |
455 | if (sd_id128_get_machine(NULL) >= 0) { | |
456 | /* restart server */ | |
457 | seqnum = 0; | |
5cb24cd3 | 458 | |
3a9ca230 NR |
459 | assert_se(managed_journal_file_open(-1, "two.journal", O_RDWR, JOURNAL_COMPRESS, 0, |
460 | UINT64_MAX, NULL, m, NULL, NULL, &two) == 0); | |
5cb24cd3 | 461 | |
3a9ca230 | 462 | assert_se(sd_id128_equal(two->file->header->seqnum_id, seqnum_id)); |
5cb24cd3 | 463 | |
7c1784db | 464 | append_number(two, 7, NULL, &seqnum); |
3a9ca230 NR |
465 | printf("seqnum=%"PRIu64"\n", seqnum); |
466 | assert_se(seqnum == 5); | |
5cb24cd3 | 467 | |
3a9ca230 NR |
468 | /* So..., here we have the same seqnum in two files with the |
469 | * same seqnum_id. */ | |
5cb24cd3 | 470 | |
3a9ca230 NR |
471 | test_close(two); |
472 | } | |
5cb24cd3 ZJS |
473 | |
474 | log_info("Done..."); | |
475 | ||
476 | if (arg_keep) | |
477 | log_info("Not removing %s", t); | |
478 | else { | |
8580d1f7 | 479 | journal_directory_vacuum(".", 3000000, 0, 0, NULL, true); |
5cb24cd3 | 480 | |
c6878637 | 481 | assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
5cb24cd3 | 482 | } |
7a050b54 MV |
483 | } |
484 | ||
c92f1ebe DDM |
485 | TEST(sequence_numbers) { |
486 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1) >= 0); | |
487 | test_sequence_numbers_one(); | |
488 | ||
489 | assert_se(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1) >= 0); | |
490 | test_sequence_numbers_one(); | |
491 | } | |
492 | ||
68da8adf | 493 | static int intro(void) { |
d02af6f3 | 494 | /* managed_journal_file_open requires a valid machine id */ |
317bb217 ZJS |
495 | if (access("/etc/machine-id", F_OK) != 0) |
496 | return log_tests_skipped("/etc/machine-id not found"); | |
143bfdaf | 497 | |
68da8adf | 498 | arg_keep = saved_argc > 1; |
7a050b54 | 499 | |
68da8adf | 500 | return EXIT_SUCCESS; |
7a050b54 | 501 | } |
68da8adf JJ |
502 | |
503 | DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); |