1 #define _XOPEN_SOURCE 500
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);
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_check
11 #define ftruncate ftruncate_check
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>
28 #include "external-transaction.h"
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"
45 static void taplog(struct tdb_context *tdb,
46 enum tdb_debug_level level,
53 vsprintf(line, fmt, ap);
59 static void save_file_contents(int fd)
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;
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;
76 static void check_for_agent(int fd, bool block)
81 if (!external_agent_operation_check(agent, block, &res))
85 err(1, "Agent failed open");
86 agent_pending = false;
88 if (!snapshot_uptodate)
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);
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");
108 static void check_file_contents(int fd)
111 check_for_agent(fd, false);
113 if (!agent_pending) {
114 save_file_contents(fd);
116 /* Ask agent to open file. */
117 external_agent_operation_start(agent,
119 OPEN_WITH_CLEAR_IF_FIRST :
122 agent_pending = true;
123 /* Hack: give it a chance to run. */
127 check_for_agent(fd, false);
130 static ssize_t pwrite_check(int fd,
131 const void *buf, size_t count, off_t offset)
136 check_file_contents(fd);
138 snapshot_uptodate = false;
139 ret = pwrite(fd, buf, count, offset);
144 check_file_contents(fd);
148 static ssize_t write_check(int fd, const void *buf, size_t count)
153 check_file_contents(fd);
155 snapshot_uptodate = false;
157 ret = write(fd, buf, count);
162 check_file_contents(fd);
166 /* This seems to be a macro for glibc... */
167 extern int fcntl(int fd, int cmd, ... /* arg */ );
169 static int fcntl_check(int fd, int cmd, ... /* arg */ )
175 if (cmd != F_SETLK && cmd != F_SETLKW) {
176 /* This may be totally bogus, but we don't know in general. */
178 arg3 = va_arg(ap, int);
181 return fcntl(fd, cmd, arg3);
185 fl = va_arg(ap, struct flock *);
188 ret = fcntl(fd, cmd, fl);
190 if (in_transaction && fl->l_type == F_UNLCK)
191 check_file_contents(fd);
195 static int ftruncate_check(int fd, off_t length)
200 check_file_contents(fd);
202 snapshot_uptodate = false;
204 ret = ftruncate(fd, length);
207 check_file_contents(fd);
211 int main(int argc, char *argv[])
213 struct tdb_logging_context logctx = { taplog, NULL };
214 const int flags[] = { TDB_DEFAULT,
217 TDB_CLEAR_IF_FIRST | TDB_NOMMAP };
219 struct tdb_context *tdb;
223 agent = prepare_external_agent();
225 err(1, "preparing agent");
227 /* Nice ourselves down: we can't tell the difference between agent
228 * blocking on lock, and agent not scheduled. */
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");
237 tdb = tdb_open_ex(TEST_DBNAME, 1024, flags[i],
238 O_CREAT|O_TRUNC|O_RDWR, 0600,
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");
249 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
250 ok1(tdb_transaction_commit(tdb) == 0);
252 check_for_agent(tdb->fd, true);
253 ok(errors == 0, "We had %u unexpected changes", errors);
258 return exit_status();