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