3 #include "lock-tracking.h"
5 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
6 static ssize_t write_check(int fd, const void *buf, size_t count);
7 static int ftruncate_check(int fd, off_t length);
9 #define pwrite pwrite_check
10 #define write write_check
11 #define fcntl fcntl_with_lockcheck
12 #define ftruncate ftruncate_check
14 #include <ccan/tdb2/tdb.c>
15 #include <ccan/tdb2/free.c>
16 #include <ccan/tdb2/lock.c>
17 #include <ccan/tdb2/io.c>
18 #include <ccan/tdb2/hash.c>
19 #include <ccan/tdb2/check.c>
20 #include <ccan/tdb2/transaction.c>
21 #include <ccan/tap/tap.h>
26 #include "external-agent.h"
29 static struct agent *agent;
31 static int errors = 0;
32 #define TEST_DBNAME "run-56-open-during-transaction.tdb"
39 static bool is_same(const char *snapshot, const char *latest, off_t len)
43 for (i = 0; i < len; i++) {
44 if (snapshot[i] != latest[i])
50 static bool compare_file(int fd, const char *snapshot, off_t snapshot_len)
55 /* over-length read serves as length check. */
56 contents = malloc(snapshot_len+1);
57 same = pread(fd, contents, snapshot_len+1, 0) == snapshot_len
58 && is_same(snapshot, contents, snapshot_len);
63 static void check_file_intact(int fd)
65 enum agent_return ret;
70 contents = malloc(st.st_size);
71 if (pread(fd, contents, st.st_size, 0) != st.st_size) {
77 /* Ask agent to open file. */
78 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
80 /* It's OK to open it, but it must not have changed! */
81 if (!compare_file(fd, contents, st.st_size)) {
82 diag("Agent changed file after opening %s",
83 agent_return_name(ret));
88 ret = external_agent_operation(agent, CLOSE, NULL);
90 diag("Agent failed to close tdb: %s",
91 agent_return_name(ret));
94 } else if (ret != WOULD_HAVE_BLOCKED) {
95 diag("Agent opening file gave %s",
96 agent_return_name(ret));
103 static void after_unlock(int fd)
106 check_file_intact(fd);
109 static ssize_t pwrite_check(int fd,
110 const void *buf, size_t count, off_t offset)
113 check_file_intact(fd);
115 return pwrite(fd, buf, count, offset);
118 static ssize_t write_check(int fd, const void *buf, size_t count)
121 check_file_intact(fd);
123 return write(fd, buf, count);
126 static int ftruncate_check(int fd, off_t length)
129 check_file_intact(fd);
131 return ftruncate(fd, length);
135 int main(int argc, char *argv[])
137 const int flags[] = { TDB_DEFAULT,
140 TDB_CONVERT | TDB_NOMMAP };
142 struct tdb_context *tdb;
146 agent = prepare_external_agent();
148 err(1, "preparing agent");
150 unlock_callback = after_unlock;
151 for (i = 0; i < sizeof(flags)/sizeof(flags[0]); i++) {
152 diag("Test with %s and %s\n",
153 (flags[i] & TDB_CONVERT) ? "CONVERT" : "DEFAULT",
154 (flags[i] & TDB_NOMMAP) ? "no mmap" : "mmap");
156 tdb = tdb_open(TEST_DBNAME, flags[i],
157 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
161 ok1(tdb_transaction_start(tdb) == 0);
162 key.dsize = strlen("hi");
163 key.dptr = (void *)"hi";
164 data.dptr = (void *)"world";
165 data.dsize = strlen("world");
167 ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
168 ok1(tdb_transaction_commit(tdb) == 0);
169 ok(!errors, "We had %u open errors", errors);
175 return exit_status();