1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2012 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include "sd-journal.h"
30 #include "journal-file.h"
31 #include "journal-internal.h"
35 static void verify_contents(sd_journal
*j
, unsigned skip
) {
41 SD_JOURNAL_FOREACH(j
) {
47 assert_se(sd_journal_get_cursor(j
, &k
) >= 0);
48 printf("cursor: %s\n", k
);
51 assert_se(sd_journal_get_data(j
, "MAGIC", &d
, &l
) >= 0);
52 printf("\t%.*s\n", (int) l
, (const char*) d
);
54 assert_se(sd_journal_get_data(j
, "NUMBER", &d
, &l
) >= 0);
55 assert_se(k
= strndup(d
, l
));
59 assert_se(safe_atou(k
+ 7, &u
) >= 0);
66 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
67 assert_se(sd_journal_test_cursor(j
, c
) > 0);
72 assert_se(i
== N_ENTRIES
);
75 int main(int argc
, char *argv
[]) {
76 JournalFile
*one
, *two
, *three
;
77 char t
[] = "/tmp/journal-stream-XXXXXX";
79 _cleanup_journal_close_ sd_journal
*j
= NULL
;
83 dual_timestamp previous_ts
= DUAL_TIMESTAMP_NULL
;
85 /* journal_file_open requires a valid machine id */
86 if (access("/etc/machine-id", F_OK
) != 0)
87 return EXIT_TEST_SKIP
;
89 log_set_max_level(LOG_DEBUG
);
91 assert_se(mkdtemp(t
));
92 assert_se(chdir(t
) >= 0);
94 assert_se(journal_file_open("one.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, &one
) == 0);
95 assert_se(journal_file_open("two.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, &two
) == 0);
96 assert_se(journal_file_open("three.journal", O_RDWR
|O_CREAT
, 0666, true, false, NULL
, NULL
, NULL
, &three
) == 0);
98 for (i
= 0; i
< N_ENTRIES
; i
++) {
101 struct iovec iovec
[2];
103 dual_timestamp_get(&ts
);
105 if (ts
.monotonic
<= previous_ts
.monotonic
)
106 ts
.monotonic
= previous_ts
.monotonic
+ 1;
108 if (ts
.realtime
<= previous_ts
.realtime
)
109 ts
.realtime
= previous_ts
.realtime
+ 1;
113 assert_se(asprintf(&p
, "NUMBER=%u", i
) >= 0);
114 iovec
[0].iov_base
= p
;
115 iovec
[0].iov_len
= strlen(p
);
117 assert_se(asprintf(&q
, "MAGIC=%s", i
% 5 == 0 ? "quux" : "waldo") >= 0);
119 iovec
[1].iov_base
= q
;
120 iovec
[1].iov_len
= strlen(q
);
123 assert_se(journal_file_append_entry(three
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
126 assert_se(journal_file_append_entry(two
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
128 assert_se(journal_file_append_entry(one
, &ts
, iovec
, 2, NULL
, NULL
, NULL
) == 0);
135 journal_file_close(one
);
136 journal_file_close(two
);
137 journal_file_close(three
);
139 assert_se(sd_journal_open_directory(&j
, t
, 0) >= 0);
141 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
142 SD_JOURNAL_FOREACH_BACKWARDS(j
) {
143 _cleanup_free_
char *c
;
145 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
146 printf("\t%.*s\n", (int) l
, (const char*) data
);
148 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
149 assert_se(sd_journal_test_cursor(j
, c
) > 0);
152 SD_JOURNAL_FOREACH(j
) {
153 _cleanup_free_
char *c
;
155 assert_se(sd_journal_get_data(j
, "NUMBER", &data
, &l
) >= 0);
156 printf("\t%.*s\n", (int) l
, (const char*) data
);
158 assert_se(sd_journal_get_cursor(j
, &c
) >= 0);
159 assert_se(sd_journal_test_cursor(j
, c
) > 0);
162 sd_journal_flush_matches(j
);
164 verify_contents(j
, 1);
166 printf("NEXT TEST\n");
167 assert_se(sd_journal_add_match(j
, "MAGIC=quux", 0) >= 0);
169 assert_se(z
= journal_make_match_string(j
));
170 printf("resulting match expression is: %s\n", z
);
173 verify_contents(j
, 5);
175 printf("NEXT TEST\n");
176 sd_journal_flush_matches(j
);
177 assert_se(sd_journal_add_match(j
, "MAGIC=waldo", 0) >= 0);
178 assert_se(sd_journal_add_match(j
, "NUMBER=10", 0) >= 0);
179 assert_se(sd_journal_add_match(j
, "NUMBER=11", 0) >= 0);
180 assert_se(sd_journal_add_match(j
, "NUMBER=12", 0) >= 0);
182 assert_se(z
= journal_make_match_string(j
));
183 printf("resulting match expression is: %s\n", z
);
186 verify_contents(j
, 0);
188 assert_se(sd_journal_query_unique(j
, "NUMBER") >= 0);
189 SD_JOURNAL_FOREACH_UNIQUE(j
, data
, l
)
190 printf("%.*s\n", (int) l
, (const char*) data
);
192 assert_se(rm_rf(t
, REMOVE_ROOT
|REMOVE_PHYSICAL
) >= 0);