1 #define _XOPEN_SOURCE 500
3 #include "lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
6 static int ftruncate_check(int fd, off_t length);
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_with_lockcheck
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>
29 #include "external-transaction.h"
36 static bool in_transaction;
37 static bool suppress_logging;
38 static int target, current;
39 static jmp_buf jmpbuf;
40 #define TEST_DBNAME "/tmp/test7.tdb"
42 static void taplog(struct tdb_context *tdb,
43 enum tdb_debug_level level,
53 vsprintf(line, fmt, ap);
59 static void maybe_die(int fd)
61 if (in_transaction && current++ == target) {
66 static ssize_t pwrite_check(int fd,
67 const void *buf, size_t count, off_t offset)
73 ret = pwrite(fd, buf, count, offset);
81 static ssize_t write_check(int fd, const void *buf, size_t count)
87 ret = write(fd, buf, count);
95 static int ftruncate_check(int fd, off_t length)
101 ret = ftruncate(fd, length);
107 static bool test_death(enum operation op, struct agent *agent)
109 struct tdb_context *tdb = NULL;
111 struct tdb_logging_context logctx = { taplog, NULL };
112 int needed_recovery = 0;
114 current = target = 0;
116 if (setjmp(jmpbuf) != 0) {
117 /* We're partway through. Simulate our death. */
119 in_transaction = false;
121 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
125 if (external_agent_operation(agent, op, "") != 1) {
126 diag("Step %u op failed", current);
130 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
132 diag("Still needs recovery after step %u", current);
136 if (external_agent_operation(agent, CHECK_KEEP_OPENED, "")
138 diag("Step %u check failed", current);
144 external_agent_operation(agent, CLOSE, "");
145 /* Suppress logging as this tries to use closed fd. */
146 suppress_logging = true;
148 suppress_logging = false;
156 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
157 O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
159 if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
160 errx(1, "Agent failed to open?");
162 if (tdb_transaction_start(tdb) != 0)
165 in_transaction = true;
166 key.dsize = strlen("hi");
167 key.dptr = (void *)"hi";
168 data.dptr = (void *)"world";
169 data.dsize = strlen("world");
171 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
173 if (tdb_transaction_commit(tdb) != 0)
176 in_transaction = false;
179 diag("Completed %u runs", current);
181 external_agent_operation(agent, CLOSE, "");
183 ok1(needed_recovery);
184 ok1(locking_errors == 0);
185 ok1(forget_locking() == 0);
190 int main(int argc, char *argv[])
192 enum operation ops[] = { FETCH_KEEP_OPENED,
194 TRANSACTION_KEEP_OPENED };
199 unlock_callback = maybe_die;
201 agent = prepare_external_agent();
203 err(1, "preparing agent");
205 /* Nice ourselves down: we can't tell the difference between agent
206 * blocking on lock, and agent not scheduled. */
209 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
210 diag("Testing %s after death",
211 ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
212 : ops[i] == FETCH_KEEP_OPENED ? "fetch"
213 : ops[i] == STORE_KEEP_OPENED ? "store"
216 ok1(test_death(ops[i], agent));
219 return exit_status();