1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "sd-journal.h"
8 #include "alloc-util.h"
9 #include "chattr-util.h"
10 #include "journal-file.h"
11 #include "journal-internal.h"
14 #include "parse-util.h"
21 static void verify_contents(sd_journal
*j
, unsigned skip
) {
27 SD_JOURNAL_FOREACH(j
) {
33 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
34 printf("cursor: %s\n", k
);
37 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
38 printf("\t%.*s\n", (int) l
, (const char*) d
);
40 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
41 assert_se(k
= strndup(d
, l
));
45 assert_se(safe_atou(k
+ 7, &u
) >= 0);
52 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
53 assert_se(sd_journal_test_cursor(j
, c
) > 0);
58 assert_se(i
== N_ENTRIES
);
61 int main(int argc
, char *argv
[]) {
62 JournalFile
*one
, *two
, *three
;
63 char t
[] = "/var/tmp/journal-stream-XXXXXX";
65 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
69 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
71 /* journal_file_open requires a valid machine id */
72 if (access("/etc/machine-id", F_OK
) != 0)
73 return log_tests_skipped("/etc/machine-id not found");
75 test_setup_logging(LOG_DEBUG
);
77 assert_se(mkdtemp(t
));
78 assert_se(chdir(t
) >= 0);
79 (void) chattr_path(t
, FS_NOCOW_FL
, FS_NOCOW_FL
, NULL
);
81 assert_se(journal_file_open(-1, "one.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &one
) == 0);
82 assert_se(journal_file_open(-1, "two.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &two
) == 0);
83 assert_se(journal_file_open(-1, "three.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &three
) == 0);
85 for (i
= 0; i
< N_ENTRIES
; i
++) {
88 struct iovec iovec
[2];
90 dual_timestamp_get(&ts
);
92 if (ts
.monotonic
<= previous_ts
.monotonic
)
93 ts
.monotonic
= previous_ts
.monotonic
+ 1;
95 if (ts
.realtime
<= previous_ts
.realtime
)
96 ts
.realtime
= previous_ts
.realtime
+ 1;
100 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
101 iovec
[0].iov_base
= p
;
102 iovec
[0].iov_len
= strlen(p
);
104 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
106 iovec
[1].iov_base
= q
;
107 iovec
[1].iov_len
= strlen(q
);
110 assert_se(journal_file_append_entry(three
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
113 assert_se(journal_file_append_entry(two
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
115 assert_se(journal_file_append_entry(one
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
122 (void) journal_file_close(one
);
123 (void) journal_file_close(two
);
124 (void) journal_file_close(three
);
126 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
128 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
129 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
130 _cleanup_free_
char *c
;
132 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
133 printf("\t%.*s\n", (int) l
, (const char*) data
);
135 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
136 assert_se(sd_journal_test_cursor(j
, c
) > 0);
139 SD_JOURNAL_FOREACH(j
) {
140 _cleanup_free_
char *c
;
142 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
143 printf("\t%.*s\n", (int) l
, (const char*) data
);
145 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
146 assert_se(sd_journal_test_cursor(j
, c
) > 0);
149 sd_journal_flush_matches(j
);
151 verify_contents(j
, 1);
153 printf("NEXT TEST\n");
154 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
156 assert_se(z
= journal_make_match_string(j
));
157 printf("resulting match expression is: %s\n", z
);
160 verify_contents(j
, 5);
162 printf("NEXT TEST\n");
163 sd_journal_flush_matches(j
);
164 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
165 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
166 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
167 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
169 assert_se(z
= journal_make_match_string(j
));
170 printf("resulting match expression is: %s\n", z
);
173 verify_contents(j
, 0);
175 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
176 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
177 printf("%.*s\n", (int) l
, (const char*) data
);
179 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);