1 /* SPDX-License-Identifier: LGPL-2.1+ */
6 #include "sd-journal.h"
8 #include "alloc-util.h"
9 #include "journal-file.h"
10 #include "journal-internal.h"
13 #include "parse-util.h"
19 static void verify_contents(sd_journal
*j
, unsigned skip
) {
25 SD_JOURNAL_FOREACH(j
) {
31 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
32 printf("cursor: %s\n", k
);
35 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
36 printf("\t%.*s\n", (int) l
, (const char*) d
);
38 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
39 assert_se(k
= strndup(d
, l
));
43 assert_se(safe_atou(k
+ 7, &u
) >= 0);
50 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
51 assert_se(sd_journal_test_cursor(j
, c
) > 0);
56 assert_se(i
== N_ENTRIES
);
59 int main(int argc
, char *argv
[]) {
60 JournalFile
*one
, *two
, *three
;
61 char t
[] = "/tmp/journal-stream-XXXXXX";
63 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
67 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
69 /* journal_file_open requires a valid machine id */
70 if (access("/etc/machine-id", F_OK
) != 0)
71 return EXIT_TEST_SKIP
;
73 log_set_max_level(LOG_DEBUG
);
75 assert_se(mkdtemp(t
));
76 assert_se(chdir(t
) >= 0);
78 assert_se(journal_file_open(-1, "one.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &one
) == 0);
79 assert_se(journal_file_open(-1, "two.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &two
) == 0);
80 assert_se(journal_file_open(-1, "three.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &three
) == 0);
82 for (i
= 0; i
< N_ENTRIES
; i
++) {
85 struct iovec iovec
[2];
87 dual_timestamp_get(&ts
);
89 if (ts
.monotonic
<= previous_ts
.monotonic
)
90 ts
.monotonic
= previous_ts
.monotonic
+ 1;
92 if (ts
.realtime
<= previous_ts
.realtime
)
93 ts
.realtime
= previous_ts
.realtime
+ 1;
97 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
98 iovec
[0].iov_base
= p
;
99 iovec
[0].iov_len
= strlen(p
);
101 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
103 iovec
[1].iov_base
= q
;
104 iovec
[1].iov_len
= strlen(q
);
107 assert_se(journal_file_append_entry(three
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
110 assert_se(journal_file_append_entry(two
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
112 assert_se(journal_file_append_entry(one
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
119 (void) journal_file_close(one
);
120 (void) journal_file_close(two
);
121 (void) journal_file_close(three
);
123 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
125 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
126 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
127 _cleanup_free_
char *c
;
129 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
130 printf("\t%.*s\n", (int) l
, (const char*) data
);
132 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
133 assert_se(sd_journal_test_cursor(j
, c
) > 0);
136 SD_JOURNAL_FOREACH(j
) {
137 _cleanup_free_
char *c
;
139 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
140 printf("\t%.*s\n", (int) l
, (const char*) data
);
142 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
143 assert_se(sd_journal_test_cursor(j
, c
) > 0);
146 sd_journal_flush_matches(j
);
148 verify_contents(j
, 1);
150 printf("NEXT TEST\n");
151 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
153 assert_se(z
= journal_make_match_string(j
));
154 printf("resulting match expression is: %s\n", z
);
157 verify_contents(j
, 5);
159 printf("NEXT TEST\n");
160 sd_journal_flush_matches(j
);
161 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
162 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
163 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
164 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
166 assert_se(z
= journal_make_match_string(j
));
167 printf("resulting match expression is: %s\n", z
);
170 verify_contents(j
, 0);
172 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
173 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
174 printf("%.*s\n", (int) l
, (const char*) data
);
176 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);