]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-open-during-transaction.c
27356e785f6b00c59539887b5e82ae1a641c77a6
[ccan] / ccan / tdb / test / run-open-during-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
4 static ssize_t write_check(int fd, const void *buf, size_t count);
5 static int fcntl_check(int fd, int cmd, ... /* arg */ );
6 static int ftruncate_check(int fd, off_t length);
7
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_check
11 #define ftruncate ftruncate_check
12
13 #include <ccan/tdb/tdb.h>
14 #include <ccan/tdb/io.c>
15 #include <ccan/tdb/tdb.c>
16 #include <ccan/tdb/lock.c>
17 #include <ccan/tdb/freelist.c>
18 #include <ccan/tdb/traverse.c>
19 #include <ccan/tdb/transaction.c>
20 #include <ccan/tdb/error.c>
21 #include <ccan/tdb/open.c>
22 #include <ccan/tdb/check.c>
23 #include <ccan/tap/tap.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <err.h>
28 #include "external-transaction.h"
29
30 static struct agent *agent;
31 static bool agent_pending;
32 static bool in_transaction;
33 static int errors = 0;
34 static bool snapshot_uptodate;
35 static char *snapshot;
36 static off_t snapshot_len;
37 static bool clear_if_first;
38 #define TEST_DBNAME "/tmp/test7.tdb"
39
40 #undef write
41 #undef pwrite
42 #undef fcntl
43 #undef ftruncate
44
45 static void taplog(struct tdb_context *tdb,
46                    enum tdb_debug_level level,
47                    const char *fmt, ...)
48 {
49         va_list ap;
50         char line[200];
51
52         va_start(ap, fmt);
53         vsprintf(line, fmt, ap);
54         va_end(ap);
55
56         diag("%s", line);
57 }
58
59 static void save_file_contents(int fd)
60 {
61         struct stat st;
62         int res;
63
64         /* Save copy of file. */
65         stat(TEST_DBNAME, &st);
66         if (snapshot_len != st.st_size) {
67                 snapshot = realloc(snapshot, st.st_size * 2);
68                 snapshot_len = st.st_size;
69         }
70         res = pread(fd, snapshot, snapshot_len, 0);
71         if (res != snapshot_len)
72                 err(1, "Reading %zu bytes = %u", (size_t)snapshot_len, res);
73         snapshot_uptodate = true;
74 }
75
76 static void check_for_agent(int fd, bool block)
77 {
78         struct stat st;
79         int res;
80
81         if (!external_agent_operation_check(agent, block, &res))
82                 return;
83
84         if (res != 1)
85                 err(1, "Agent failed open");
86         agent_pending = false;
87
88         if (!snapshot_uptodate)
89                 return;
90
91         stat(TEST_DBNAME, &st);
92         if (st.st_size != snapshot_len) {
93                 diag("Other open changed size from %zu -> %zu",
94                      (size_t)snapshot_len, (size_t)st.st_size);
95                 errors++;
96                 return;
97         }
98
99         if (pread(fd, snapshot+snapshot_len, snapshot_len, 0) != snapshot_len)
100                 err(1, "Reading %zu bytes", (size_t)snapshot_len);
101         if (memcmp(snapshot, snapshot+snapshot_len, snapshot_len) != 0) {
102                 diag("File changed");
103                 errors++;
104                 return;
105         }
106 }
107
108 static void check_file_contents(int fd)
109 {
110         if (agent_pending)
111                 check_for_agent(fd, false);
112
113         if (!agent_pending) {
114                 save_file_contents(fd);
115
116                 /* Ask agent to open file. */
117                 external_agent_operation_start(agent,
118                                                clear_if_first ?
119                                                OPEN_WITH_CLEAR_IF_FIRST :
120                                                OPEN,
121                                                TEST_DBNAME);
122                 agent_pending = true;
123                 /* Hack: give it a chance to run. */
124                 sleep(0);
125         }
126
127         check_for_agent(fd, false);
128 }
129
130 static ssize_t pwrite_check(int fd,
131                             const void *buf, size_t count, off_t offset)
132 {
133         ssize_t ret;
134
135         if (in_transaction)
136                 check_file_contents(fd);
137
138         snapshot_uptodate = false;
139         ret = pwrite(fd, buf, count, offset);
140         if (ret != count)
141                 return ret;
142
143         if (in_transaction)
144                 check_file_contents(fd);
145         return ret;
146 }
147
148 static ssize_t write_check(int fd, const void *buf, size_t count)
149 {
150         ssize_t ret;
151
152         if (in_transaction)
153                 check_file_contents(fd);
154
155         snapshot_uptodate = false;
156
157         ret = write(fd, buf, count);
158         if (ret != count)
159                 return ret;
160
161         if (in_transaction)
162                 check_file_contents(fd);
163         return ret;
164 }
165
166 /* This seems to be a macro for glibc... */
167 extern int fcntl(int fd, int cmd, ... /* arg */ );
168
169 static int fcntl_check(int fd, int cmd, ... /* arg */ )
170 {
171         va_list ap;
172         int ret, arg3;
173         struct flock *fl;
174
175         if (cmd != F_SETLK && cmd != F_SETLKW) {
176                 /* This may be totally bogus, but we don't know in general. */
177                 va_start(ap, cmd);
178                 arg3 = va_arg(ap, int);
179                 va_end(ap);
180
181                 return fcntl(fd, cmd, arg3);
182         }
183
184         va_start(ap, cmd);
185         fl = va_arg(ap, struct flock *);
186         va_end(ap);
187
188         ret = fcntl(fd, cmd, fl);
189
190         if (in_transaction && fl->l_type == F_UNLCK)
191                 check_file_contents(fd);
192         return ret;
193 }
194
195 static int ftruncate_check(int fd, off_t length)
196 {
197         int ret;
198
199         if (in_transaction)
200                 check_file_contents(fd);
201
202         snapshot_uptodate = false;
203
204         ret = ftruncate(fd, length);
205
206         if (in_transaction)
207                 check_file_contents(fd);
208         return ret;
209 }
210
211 int main(int argc, char *argv[])
212 {
213         struct tdb_logging_context logctx = { taplog, NULL };
214         const int flags[] = { TDB_DEFAULT,
215                               TDB_CLEAR_IF_FIRST,
216                               TDB_NOMMAP, 
217                               TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
218         int i;
219         struct tdb_context *tdb;
220         TDB_DATA key, data;
221
222         plan_tests(20);
223         agent = prepare_external_agent();
224         if (!agent)
225                 err(1, "preparing agent");
226
227         /* Nice ourselves down: we can't tell the difference between agent
228          * blocking on lock, and agent not scheduled. */
229         nice(15);
230
231         for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
232                 clear_if_first = (flags[i] & TDB_CLEAR_IF_FIRST);
233                 diag("Test with %s and %s\n",
234                      clear_if_first ? "CLEAR" : "DEFAULT",
235                      (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
236                 unlink(TEST_DBNAME);
237                 tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
238                                   O_CREAT|O_TRUNC|O_RDWR, 0600,
239                                   &logctx, NULL);
240                 ok1(tdb);
241
242                 ok1(tdb_transaction_start(tdb) == 0);
243                 in_transaction = true;
244                 key.dsize = strlen("hi");
245                 key.dptr = (void *)"hi";
246                 data.dptr = (void *)"world";
247                 data.dsize = strlen("world");
248
249                 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
250                 ok1(tdb_transaction_commit(tdb) == 0);
251                 if (agent_pending)
252                         check_for_agent(tdb->fd, true);
253                 ok(errors == 0, "We had %u unexpected changes", errors);
254
255                 tdb_close(tdb);
256         }
257
258         return exit_status();
259 }