1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
11 #include "sd-journal.h"
13 #include "alloc-util.h"
14 #include "journal-file.h"
15 #include "journal-internal.h"
18 #include "parse-util.h"
24 static void verify_contents(sd_journal
*j
, unsigned skip
) {
30 SD_JOURNAL_FOREACH(j
) {
36 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
37 printf("cursor: %s\n", k
);
40 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
41 printf("\t%.*s\n", (int) l
, (const char*) d
);
43 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
44 assert_se(k
= strndup(d
, l
));
48 assert_se(safe_atou(k
+ 7, &u
) >= 0);
55 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
56 assert_se(sd_journal_test_cursor(j
, c
) > 0);
61 assert_se(i
== N_ENTRIES
);
64 int main(int argc
, char *argv
[]) {
65 JournalFile
*one
, *two
, *three
;
66 char t
[] = "/tmp/journal-stream-XXXXXX";
68 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
72 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
74 /* journal_file_open requires a valid machine id */
75 if (access("/etc/machine-id", F_OK
) != 0)
76 return EXIT_TEST_SKIP
;
78 log_set_max_level(LOG_DEBUG
);
80 assert_se(mkdtemp(t
));
81 assert_se(chdir(t
) >= 0);
83 assert_se(journal_file_open(-1, "one.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &one
) == 0);
84 assert_se(journal_file_open(-1, "two.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &two
) == 0);
85 assert_se(journal_file_open(-1, "three.journal", O_RDWR
|O_CREAT
, 0666, true, (uint64_t) -1, false, NULL
, NULL
, NULL
, NULL
, &three
) == 0);
87 for (i
= 0; i
< N_ENTRIES
; i
++) {
90 struct iovec iovec
[2];
92 dual_timestamp_get(&ts
);
94 if (ts
.monotonic
<= previous_ts
.monotonic
)
95 ts
.monotonic
= previous_ts
.monotonic
+ 1;
97 if (ts
.realtime
<= previous_ts
.realtime
)
98 ts
.realtime
= previous_ts
.realtime
+ 1;
102 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
103 iovec
[0].iov_base
= p
;
104 iovec
[0].iov_len
= strlen(p
);
106 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
108 iovec
[1].iov_base
= q
;
109 iovec
[1].iov_len
= strlen(q
);
112 assert_se(journal_file_append_entry(three
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
115 assert_se(journal_file_append_entry(two
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
117 assert_se(journal_file_append_entry(one
, &ts
, NULL
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
124 (void) journal_file_close(one
);
125 (void) journal_file_close(two
);
126 (void) journal_file_close(three
);
128 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
130 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
131 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
132 _cleanup_free_
char *c
;
134 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
135 printf("\t%.*s\n", (int) l
, (const char*) data
);
137 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
138 assert_se(sd_journal_test_cursor(j
, c
) > 0);
141 SD_JOURNAL_FOREACH(j
) {
142 _cleanup_free_
char *c
;
144 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
145 printf("\t%.*s\n", (int) l
, (const char*) data
);
147 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
148 assert_se(sd_journal_test_cursor(j
, c
) > 0);
151 sd_journal_flush_matches(j
);
153 verify_contents(j
, 1);
155 printf("NEXT TEST\n");
156 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
158 assert_se(z
= journal_make_match_string(j
));
159 printf("resulting match expression is: %s\n", z
);
162 verify_contents(j
, 5);
164 printf("NEXT TEST\n");
165 sd_journal_flush_matches(j
);
166 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
167 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
168 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
169 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
171 assert_se(z
= journal_make_match_string(j
));
172 printf("resulting match expression is: %s\n", z
);
175 verify_contents(j
, 0);
177 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
178 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
179 printf("%.*s\n", (int) l
, (const char*) data
);
181 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);