]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
cbdca852 LP |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2012 Lennart Poettering | |
6 | ||
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. | |
11 | ||
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. | |
16 | ||
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/>. | |
19 | ***/ | |
20 | ||
cbdca852 | 21 | #include <fcntl.h> |
6bedfcbb | 22 | #include <unistd.h> |
cbdca852 | 23 | |
c6878637 | 24 | #include "sd-journal.h" |
6bedfcbb | 25 | |
b5efdb8a | 26 | #include "alloc-util.h" |
6bedfcbb LP |
27 | #include "journal-file.h" |
28 | #include "journal-internal.h" | |
cbdca852 | 29 | #include "log.h" |
0c0cdb06 | 30 | #include "macro.h" |
6bedfcbb | 31 | #include "parse-util.h" |
c6878637 | 32 | #include "rm-rf.h" |
6bedfcbb | 33 | #include "util.h" |
cbdca852 LP |
34 | |
35 | #define N_ENTRIES 200 | |
36 | ||
37 | static void verify_contents(sd_journal *j, unsigned skip) { | |
38 | unsigned i; | |
39 | ||
0c0cdb06 | 40 | assert_se(j); |
cbdca852 LP |
41 | |
42 | i = 0; | |
43 | SD_JOURNAL_FOREACH(j) { | |
44 | const void *d; | |
c6511e85 | 45 | char *k, *c; |
cbdca852 | 46 | size_t l; |
a7f7d1bd | 47 | unsigned u = 0; |
cbdca852 LP |
48 | |
49 | assert_se(sd_journal_get_cursor(j, &k) >= 0); | |
50 | printf("cursor: %s\n", k); | |
51 | free(k); | |
52 | ||
53 | assert_se(sd_journal_get_data(j, "MAGIC", &d, &l) >= 0); | |
54 | printf("\t%.*s\n", (int) l, (const char*) d); | |
55 | ||
56 | assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0); | |
57 | assert_se(k = strndup(d, l)); | |
58 | printf("\t%s\n", k); | |
59 | ||
60 | if (skip > 0) { | |
61 | assert_se(safe_atou(k + 7, &u) >= 0); | |
62 | assert_se(i == u); | |
63 | i += skip; | |
64 | } | |
65 | ||
66 | free(k); | |
c6511e85 LP |
67 | |
68 | assert_se(sd_journal_get_cursor(j, &c) >= 0); | |
69 | assert_se(sd_journal_test_cursor(j, c) > 0); | |
70 | free(c); | |
cbdca852 LP |
71 | } |
72 | ||
73 | if (skip > 0) | |
74 | assert_se(i == N_ENTRIES); | |
75 | } | |
76 | ||
77 | int main(int argc, char *argv[]) { | |
78 | JournalFile *one, *two, *three; | |
79 | char t[] = "/tmp/journal-stream-XXXXXX"; | |
80 | unsigned i; | |
4afd3348 | 81 | _cleanup_(sd_journal_closep) sd_journal *j = NULL; |
cbdca852 | 82 | char *z; |
3c1668da LP |
83 | const void *data; |
84 | size_t l; | |
98d2a534 | 85 | dual_timestamp previous_ts = DUAL_TIMESTAMP_NULL; |
cbdca852 | 86 | |
143bfdaf HHPF |
87 | /* journal_file_open requires a valid machine id */ |
88 | if (access("/etc/machine-id", F_OK) != 0) | |
89 | return EXIT_TEST_SKIP; | |
90 | ||
cbdca852 LP |
91 | log_set_max_level(LOG_DEBUG); |
92 | ||
93 | assert_se(mkdtemp(t)); | |
94 | assert_se(chdir(t) >= 0); | |
95 | ||
5d1ce257 LP |
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); | |
cbdca852 LP |
99 | |
100 | for (i = 0; i < N_ENTRIES; i++) { | |
101 | char *p, *q; | |
102 | dual_timestamp ts; | |
103 | struct iovec iovec[2]; | |
104 | ||
105 | dual_timestamp_get(&ts); | |
106 | ||
98d2a534 LP |
107 | if (ts.monotonic <= previous_ts.monotonic) |
108 | ts.monotonic = previous_ts.monotonic + 1; | |
109 | ||
110 | if (ts.realtime <= previous_ts.realtime) | |
111 | ts.realtime = previous_ts.realtime + 1; | |
112 | ||
113 | previous_ts = ts; | |
114 | ||
cbdca852 LP |
115 | assert_se(asprintf(&p, "NUMBER=%u", i) >= 0); |
116 | iovec[0].iov_base = p; | |
117 | iovec[0].iov_len = strlen(p); | |
118 | ||
119 | assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0); | |
120 | ||
121 | iovec[1].iov_base = q; | |
122 | iovec[1].iov_len = strlen(q); | |
123 | ||
124 | if (i % 10 == 0) | |
125 | assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0); | |
126 | else { | |
127 | if (i % 3 == 0) | |
128 | assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0); | |
129 | ||
130 | assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0); | |
131 | } | |
132 | ||
133 | free(p); | |
134 | free(q); | |
135 | } | |
136 | ||
69a3a6fd VC |
137 | (void) journal_file_close(one); |
138 | (void) journal_file_close(two); | |
139 | (void) journal_file_close(three); | |
cbdca852 LP |
140 | |
141 | assert_se(sd_journal_open_directory(&j, t, 0) >= 0); | |
142 | ||
143 | assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); | |
144 | SD_JOURNAL_FOREACH_BACKWARDS(j) { | |
7fd1b19b | 145 | _cleanup_free_ char *c; |
cbdca852 | 146 | |
3c1668da LP |
147 | assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); |
148 | printf("\t%.*s\n", (int) l, (const char*) data); | |
c6511e85 LP |
149 | |
150 | assert_se(sd_journal_get_cursor(j, &c) >= 0); | |
151 | assert_se(sd_journal_test_cursor(j, c) > 0); | |
cbdca852 LP |
152 | } |
153 | ||
154 | SD_JOURNAL_FOREACH(j) { | |
7fd1b19b | 155 | _cleanup_free_ char *c; |
cbdca852 | 156 | |
3c1668da LP |
157 | assert_se(sd_journal_get_data(j, "NUMBER", &data, &l) >= 0); |
158 | printf("\t%.*s\n", (int) l, (const char*) data); | |
c6511e85 LP |
159 | |
160 | assert_se(sd_journal_get_cursor(j, &c) >= 0); | |
161 | assert_se(sd_journal_test_cursor(j, c) > 0); | |
cbdca852 LP |
162 | } |
163 | ||
164 | sd_journal_flush_matches(j); | |
165 | ||
166 | verify_contents(j, 1); | |
167 | ||
168 | printf("NEXT TEST\n"); | |
169 | assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0); | |
170 | ||
171 | assert_se(z = journal_make_match_string(j)); | |
172 | printf("resulting match expression is: %s\n", z); | |
173 | free(z); | |
174 | ||
175 | verify_contents(j, 5); | |
176 | ||
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); | |
183 | ||
184 | assert_se(z = journal_make_match_string(j)); | |
185 | printf("resulting match expression is: %s\n", z); | |
186 | free(z); | |
187 | ||
188 | verify_contents(j, 0); | |
189 | ||
3c1668da LP |
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); | |
193 | ||
c6878637 | 194 | assert_se(rm_rf(t, REMOVE_ROOT|REMOVE_PHYSICAL) >= 0); |
cbdca852 LP |
195 | |
196 | return 0; | |
197 | } |