1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2012 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "sd-journal.h"
26 #include "alloc-util.h"
27 #include "journal-file.h"
28 #include "journal-internal.h"
31 #include "parse-util.h"
37 static void verify_contents(sd_journal
*j
, unsigned skip
) {
43 SD_JOURNAL_FOREACH(j
) {
49 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
50 printf("cursor: %s\n", k
);
53 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
54 printf("\t%.*s\n", (int) l
, (const char*) d
);
56 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
57 assert_se(k
= strndup(d
, l
));
61 assert_se(safe_atou(k
+ 7, &u
) >= 0);
68 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
69 assert_se(sd_journal_test_cursor(j
, c
) > 0);
74 assert_se(i
== N_ENTRIES
);
77 int main(int argc
, char *argv
[]) {
78 JournalFile
*one
, *two
, *three
;
79 char t
[] = "/tmp/journal-stream-XXXXXX";
81 _cleanup_(sd_journal_closep
) sd_journal
*j
= NULL
;
85 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
87 /* journal_file_open requires a valid machine id */
88 if (access("/etc/machine-id", F_OK
) != 0)
89 return EXIT_TEST_SKIP
;
91 log_set_max_level(LOG_DEBUG
);
93 assert_se(mkdtemp(t
));
94 assert_se(chdir(t
) >= 0);
96 assert_se(journal_file_open(-1, "one.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, NULL
, &one
) == 0);
97 assert_se(journal_file_open(-1, "two.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, NULL
, &two
) == 0);
98 assert_se(journal_file_open(-1, "three.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, NULL
, &three
) == 0);
100 for (i
= 0; i
< N_ENTRIES
; i
++) {
103 struct iovec iovec
[2];
105 dual_timestamp_get(&ts
);
107 if (ts
.monotonic
<= previous_ts
.monotonic
)
108 ts
.monotonic
= previous_ts
.monotonic
+ 1;
110 if (ts
.realtime
<= previous_ts
.realtime
)
111 ts
.realtime
= previous_ts
.realtime
+ 1;
115 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
116 iovec
[0].iov_base
= p
;
117 iovec
[0].iov_len
= strlen(p
);
119 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
121 iovec
[1].iov_base
= q
;
122 iovec
[1].iov_len
= strlen(q
);
125 assert_se(journal_file_append_entry(three
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
128 assert_se(journal_file_append_entry(two
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
130 assert_se(journal_file_append_entry(one
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
137 (void) journal_file_close(one
);
138 (void) journal_file_close(two
);
139 (void) journal_file_close(three
);
141 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
143 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
144 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
145 _cleanup_free_
char *c
;
147 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
148 printf("\t%.*s\n", (int) l
, (const char*) data
);
150 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
151 assert_se(sd_journal_test_cursor(j
, c
) > 0);
154 SD_JOURNAL_FOREACH(j
) {
155 _cleanup_free_
char *c
;
157 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
158 printf("\t%.*s\n", (int) l
, (const char*) data
);
160 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
161 assert_se(sd_journal_test_cursor(j
, c
) > 0);
164 sd_journal_flush_matches(j
);
166 verify_contents(j
, 1);
168 printf("NEXT TEST\n");
169 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
171 assert_se(z
= journal_make_match_string(j
));
172 printf("resulting match expression is: %s\n", z
);
175 verify_contents(j
, 5);
177 printf("NEXT TEST\n");
178 sd_journal_flush_matches(j
);
179 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
180 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
181 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
182 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
184 assert_se(z
= journal_make_match_string(j
));
185 printf("resulting match expression is: %s\n", z
);
188 verify_contents(j
, 0);
190 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
191 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
192 printf("%.*s\n", (int) l
, (const char*) data
);
194 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);