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-agent.h"
37 static bool in_transaction;
38 static int target, current;
39 static jmp_buf jmpbuf;
40 #define TEST_DBNAME "run-die-during-transaction.tdb"
41 #define KEY_STRING "helloworld"
43 static void maybe_die(int fd)
45 if (in_transaction && current++ == target) {
50 static ssize_t pwrite_check(int fd,
51 const void *buf, size_t count, off_t offset)
57 ret = pwrite(fd, buf, count, offset);
65 static ssize_t write_check(int fd, const void *buf, size_t count)
71 ret = write(fd, buf, count);
79 static int ftruncate_check(int fd, off_t length)
85 ret = ftruncate(fd, length);
91 static bool test_death(enum operation op, struct agent *agent)
93 struct tdb_context *tdb = NULL;
95 enum agent_return ret;
96 int needed_recovery = 0;
100 if (setjmp(jmpbuf) != 0) {
101 /* We're partway through. Simulate our death. */
104 in_transaction = false;
106 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
109 else if (ret != FAILED) {
110 diag("Step %u agent NEEDS_RECOVERY = %s", current,
111 agent_return_name(ret));
115 ret = external_agent_operation(agent, op, KEY_STRING);
116 if (ret != SUCCESS) {
117 diag("Step %u op %s failed = %s", current,
119 agent_return_name(ret));
123 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
125 diag("Still needs recovery after step %u = %s",
126 current, agent_return_name(ret));
130 ret = external_agent_operation(agent, CHECK, "");
131 if (ret != SUCCESS) {
132 diag("Step %u check failed = %s", current,
133 agent_return_name(ret));
137 ret = external_agent_operation(agent, CLOSE, "");
138 if (ret != SUCCESS) {
139 diag("Step %u close failed = %s", current,
140 agent_return_name(ret));
144 /* Suppress logging as this tries to use closed fd. */
145 suppress_logging = true;
146 suppress_lockcheck = true;
148 suppress_logging = false;
149 suppress_lockcheck = false;
156 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
157 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
159 /* Put key for agent to fetch. */
160 key.dsize = strlen(KEY_STRING);
161 key.dptr = (void *)KEY_STRING;
162 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
165 /* This is the key we insert in transaction. */
168 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
170 errx(1, "Agent failed to open: %s", agent_return_name(ret));
172 ret = external_agent_operation(agent, FETCH, KEY_STRING);
174 errx(1, "Agent failed find key: %s", agent_return_name(ret));
176 in_transaction = true;
177 if (tdb_transaction_start(tdb) != 0)
180 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
183 if (tdb_transaction_commit(tdb) != 0)
186 in_transaction = false;
189 diag("Completed %u runs", current);
191 ret = external_agent_operation(agent, CLOSE, "");
192 if (ret != SUCCESS) {
193 diag("Step %u close failed = %s", current,
194 agent_return_name(ret));
198 ok1(needed_recovery);
199 ok1(locking_errors == 0);
200 ok1(forget_locking() == 0);
205 int main(int argc, char *argv[])
207 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
212 unlock_callback = maybe_die;
214 agent = prepare_external_agent();
216 err(1, "preparing agent");
218 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
219 diag("Testing %s after death", operation_name(ops[i]));
220 ok1(test_death(ops[i], agent));
223 return exit_status();