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. */
120 in_transaction = false;
122 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
126 if (external_agent_operation(agent, op, "") != 1) {
127 diag("Step %u op failed", current);
131 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
133 diag("Still needs recovery after step %u", current);
137 if (external_agent_operation(agent, CHECK_KEEP_OPENED, "")
139 diag("Step %u check failed", current);
145 external_agent_operation(agent, CLOSE, "");
146 /* Suppress logging as this tries to use closed fd. */
147 suppress_logging = true;
148 suppress_lockcheck = true;
150 suppress_logging = false;
151 suppress_lockcheck = false;
158 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
159 O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
161 if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
162 errx(1, "Agent failed to open?");
164 if (tdb_transaction_start(tdb) != 0)
167 in_transaction = true;
168 key.dsize = strlen("hi");
169 key.dptr = (void *)"hi";
170 data.dptr = (void *)"world";
171 data.dsize = strlen("world");
173 if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
175 if (tdb_transaction_commit(tdb) != 0)
178 in_transaction = false;
181 diag("Completed %u runs", current);
183 external_agent_operation(agent, CLOSE, "");
185 ok1(needed_recovery);
186 ok1(locking_errors == 0);
187 ok1(forget_locking() == 0);
192 int main(int argc, char *argv[])
194 enum operation ops[] = { FETCH_KEEP_OPENED,
196 TRANSACTION_KEEP_OPENED };
201 unlock_callback = maybe_die;
203 agent = prepare_external_agent();
205 err(1, "preparing agent");
207 /* Nice ourselves down: we can't tell the difference between agent
208 * blocking on lock, and agent not scheduled. */
211 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
212 diag("Testing %s after death",
213 ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
214 : ops[i] == FETCH_KEEP_OPENED ? "fetch"
215 : ops[i] == STORE_KEEP_OPENED ? "store"
218 ok1(test_death(ops[i], agent));
221 return exit_status();