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