Debian Codegraph @

src/libsystemd/sd-journal/test-journal-flush.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <stdlib.h>
5
6 #include "sd-journal.h"
7
8 #include "alloc-util.h"
9 #include "argv-util.h"
10 #include "chattr-util.h"
11 #include "dirent-util.h"
12 #include "fd-util.h"
13 #include "journal-file-util.h"
14 #include "journal-internal.h"
15 #include "logs-show.h"
16 #include "output-mode.h"
17 #include "path-util.h"
18 #include "rm-rf.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "tests.h"
22 #include "time-util.h"
23 #include "tmpfile-util.h"
24
25 static int open_archive_file(sd_journal **ret) {
26 _cleanup_closedir_ DIR *d = NULL;
27 _cleanup_close_ int newest_fd = -EBADF;
28 unsigned long long newest_realtime = 0;
29 bool newest_is_system = false;
30 sd_id128_t machine_id;
31 const char *p;
32 int r;
33
34 r = sd_id128_get_machine(&machine_id);
35 if (r < 0)
36 return r;
37
38 p = strjoina("/var/log/journal/", SD_ID128_TO_STRING(machine_id), "/");
39
40 d = opendir(p);
41 if (!d)
42 return -errno;
43
44 FOREACH_DIRENT_ALL(de, d, return -errno) {
45 unsigned long long realtime;
46 bool is_system;
47 size_t q;
48 int fd;
49
50 if (!dirent_is_file_with_suffix(de, ".journal"))
51 continue;
52
53 is_system = startswith(de->d_name, "system@");
54 if (newest_is_system && !is_system)
55 continue;
56
57 q = strlen(de->d_name);
58
59 if (q < 1 + 32 + 1 + 16 + 1 + 16 + 8)
60 continue;
61
62 if (de->d_name[q-8-16-1] != '-' ||
63 de->d_name[q-8-16-1-16-1] != '-' ||
64 de->d_name[q-8-16-1-16-1-32-1] != '@')
65 continue;
66
67 if (sscanf(de->d_name + q-8-16, "%16llx.journal", &realtime) != 1)
68 continue;
69
70 if (newest_realtime >= realtime)
71 continue;
72
73 fd = openat(dirfd(d), de->d_name, O_CLOEXEC | O_NONBLOCK | O_RDONLY);
74 if (fd < 0) {
75 log_info_errno(errno, "Failed to open /var/log/journal/%s, ignoring: %m", de->d_name);
76 continue;
77 }
78
79 close_and_replace(newest_fd, fd);
80 newest_realtime = realtime;
81 newest_is_system = is_system;
82 }
83
84 if (newest_fd < 0)
85 return log_info_errno(SYNTHETIC_ERRNO(ENOENT), "No archive journal found.");
86
87 r = sd_journal_open_files_fd(ret, &newest_fd, 1, SD_JOURNAL_ASSUME_IMMUTABLE);
88
89 _cleanup_free_ char *path = NULL;
90 (void) fd_get_path(newest_fd, &path);
91
92 if (r < 0)
93 log_info_errno(r, "Failed to open %s, ignoring: %m", strna(path));
94 else {
95 log_info("Opened %s.", strna(path));
96 TAKE_FD(newest_fd);
97 }
98
99 return r;
100 }
101
102 static void test_journal_flush_one(int argc, char *argv[]) {
103 _cleanup_(mmap_cache_unrefp) MMapCache *m = NULL;
104 _cleanup_free_ char *fn = NULL;
105 _cleanup_(rm_rf_physical_and_freep) char *dn = NULL;
106 _cleanup_(journal_file_offline_closep) JournalFile *new_journal = NULL;
107 _cleanup_(sd_journal_closep) sd_journal *j = NULL;
108 unsigned n, limit;
109 int r;
110
111 ASSERT_NOT_NULL(m = mmap_cache_new());
112 ASSERT_OK(mkdtemp_malloc("/var/tmp/test-journal-flush.XXXXXX", &dn));
113 (void) chattr_path(dn, FS_NOCOW_FL, FS_NOCOW_FL);
114
115 ASSERT_NOT_NULL(fn = path_join(dn, "test.journal"));
116
117 ASSERT_OK(journal_file_open(-EBADF, fn, O_CREAT|O_RDWR, 0, 0644, 0, NULL, m, NULL, &new_journal));
118
119 if (argc > 1)
120 r = sd_journal_open_files(&j, (const char **) strv_skip(argv, 1), SD_JOURNAL_ASSUME_IMMUTABLE);
121 else {
122 r = open_archive_file(&j);
123 if (r < 0)
124 r = sd_journal_open(&j, SD_JOURNAL_ASSUME_IMMUTABLE);
125 }
126 ASSERT_OK_ZERO(r);
127
128 sd_journal_set_data_threshold(j, 0);
129
130 n = 0;
131 limit = slow_tests_enabled() ? 10000 : 1000;
132 SD_JOURNAL_FOREACH(j) {
133 Object *o;
134 JournalFile *f;
135
136 f = j->current_file;
137 ASSERT_TRUE(f && f->current_offset > 0);
138
139 r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
140 if (r < 0)
141 log_error_errno(r, "journal_file_move_to_object failed: %m");
142 ASSERT_OK(r);
143
144 r = journal_file_copy_entry(f, new_journal, o, f->current_offset, NULL, NULL);
145 if (r < 0)
146 log_warning_errno(r, "journal_file_copy_entry failed: %m");
147 ASSERT_TRUE(r >= 0 ||
148 IN_SET(r, -EBADMSG, /* corrupted file */
149 -EPROTONOSUPPORT, /* unsupported compression */
150 -EIO, /* file rotated */
151 -EREMCHG)); /* clock rollback */
152
153 if (++n >= limit)
154 break;
155 }
156
157 if (n == 0)
158 return (void) log_tests_skipped("No journal entry found");
159
160 /* Open the new journal before archiving and offlining the file. */
161 sd_journal_close(j);
162 ASSERT_OK(sd_journal_open_directory(&j, dn, SD_JOURNAL_ASSUME_IMMUTABLE));
163
164 /* Read the online journal. */
165 ASSERT_OK(sd_journal_seek_tail(j));
166 ASSERT_OK_POSITIVE(sd_journal_step_one(j, 0));
167 printf("current_journal: %s (%i)\n", j->current_file->path, j->current_file->fd);
168 ASSERT_OK(show_journal_entry(stdout, j, OUTPUT_EXPORT, 0, 0, NULL, NULL, NULL, &(dual_timestamp) {}, &(sd_id128_t) {}));
169
170 uint64_t p;
171 ASSERT_OK(journal_file_tail_end_by_mmap(j->current_file, &p));
172 for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) {
173 Object *o;
174
175 r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o);
176 ASSERT_TRUE(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL));
177 }
178
179 /* Archive and offline file. */
180 ASSERT_OK(journal_file_archive(new_journal, NULL));
181 ASSERT_OK(journal_file_set_offline(new_journal, /* wait= */ true));
182
183 /* Read the archived and offline journal. */
184 for (uint64_t q = ALIGN64(p + 1); q < (uint64_t) j->current_file->last_stat.st_size; q = ALIGN64(q + 1)) {
185 Object *o;
186
187 r = journal_file_move_to_object(j->current_file, OBJECT_UNUSED, q, &o);
188 ASSERT_TRUE(IN_SET(r, -EBADMSG, -EADDRNOTAVAIL, -EIDRM));
189 }
190 }
191
192 TEST(journal_flush) {
193 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "0", 1));
194 test_journal_flush_one(saved_argc, saved_argv);
195 }
196
197 TEST(journal_flush_compact) {
198 ASSERT_OK_ERRNO(setenv("SYSTEMD_JOURNAL_COMPACT", "1", 1));
199 test_journal_flush_one(saved_argc, saved_argv);
200 }
201
202 DEFINE_TEST_MAIN(LOG_INFO);