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