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