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