]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/journal/test-journal-stream.c
journald: initial version of FSPRG hookup
[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 <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;
42 char *k;
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);
64 }
65
66 if (skip > 0)
67 assert_se(i == N_ENTRIES);
68 }
69
70 int main(int argc, char *argv[]) {
71 JournalFile *one, *two, *three;
72 char t[] = "/tmp/journal-stream-XXXXXX";
73 unsigned i;
74 sd_journal *j;
75 char *z;
76
77 log_set_max_level(LOG_DEBUG);
78
79 assert_se(mkdtemp(t));
80 assert_se(chdir(t) >= 0);
81
82 assert_se(journal_file_open("one.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, &one) == 0);
83 assert_se(journal_file_open("two.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, &two) == 0);
84 assert_se(journal_file_open("three.journal", O_RDWR|O_CREAT, 0666, true, false, NULL, NULL, &three) == 0);
85
86 for (i = 0; i < N_ENTRIES; i++) {
87 char *p, *q;
88 dual_timestamp ts;
89 struct iovec iovec[2];
90
91 dual_timestamp_get(&ts);
92
93 assert_se(asprintf(&p, "NUMBER=%u", i) >= 0);
94 iovec[0].iov_base = p;
95 iovec[0].iov_len = strlen(p);
96
97 assert_se(asprintf(&q, "MAGIC=%s", i % 5 == 0 ? "quux" : "waldo") >= 0);
98
99 iovec[1].iov_base = q;
100 iovec[1].iov_len = strlen(q);
101
102 if (i % 10 == 0)
103 assert_se(journal_file_append_entry(three, &ts, iovec, 2, NULL, NULL, NULL) == 0);
104 else {
105 if (i % 3 == 0)
106 assert_se(journal_file_append_entry(two, &ts, iovec, 2, NULL, NULL, NULL) == 0);
107
108 assert_se(journal_file_append_entry(one, &ts, iovec, 2, NULL, NULL, NULL) == 0);
109 }
110
111 free(p);
112 free(q);
113 }
114
115 journal_file_close(one);
116 journal_file_close(two);
117 journal_file_close(three);
118
119 assert_se(sd_journal_open_directory(&j, t, 0) >= 0);
120
121 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
122 SD_JOURNAL_FOREACH_BACKWARDS(j) {
123 const void *d;
124 size_t l;
125
126 assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
127 printf("\t%.*s\n", (int) l, (const char*) d);
128 }
129
130 SD_JOURNAL_FOREACH(j) {
131 const void *d;
132 size_t l;
133
134 assert_se(sd_journal_get_data(j, "NUMBER", &d, &l) >= 0);
135 printf("\t%.*s\n", (int) l, (const char*) d);
136 }
137
138 sd_journal_flush_matches(j);
139
140 verify_contents(j, 1);
141
142 printf("NEXT TEST\n");
143 assert_se(sd_journal_add_match(j, "MAGIC=quux", 0) >= 0);
144
145 assert_se(z = journal_make_match_string(j));
146 printf("resulting match expression is: %s\n", z);
147 free(z);
148
149 verify_contents(j, 5);
150
151 printf("NEXT TEST\n");
152 sd_journal_flush_matches(j);
153 assert_se(sd_journal_add_match(j, "MAGIC=waldo", 0) >= 0);
154 assert_se(sd_journal_add_match(j, "NUMBER=10", 0) >= 0);
155 assert_se(sd_journal_add_match(j, "NUMBER=11", 0) >= 0);
156 assert_se(sd_journal_add_match(j, "NUMBER=12", 0) >= 0);
157
158 assert_se(z = journal_make_match_string(j));
159 printf("resulting match expression is: %s\n", z);
160 free(z);
161
162 verify_contents(j, 0);
163
164 sd_journal_close(j);
165
166 assert_se(rm_rf_dangerous(t, false, true, false) >= 0);
167
168 return 0;
169 }