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/tdb/hash.c>
24 #include <ccan/tap/tap.h>
30 #include "external-agent.h"
38 static bool in_transaction;
39 static int target, current;
40 static jmp_buf jmpbuf;
41 #define TEST_DBNAME "run-die-during-transaction.tdb"
42 #define KEY_STRING "helloworld"
44 static void maybe_die(int fd)
46 if (in_transaction && current++ == target) {
51 static ssize_t pwrite_check(int fd,
52 const void *buf, size_t count, off_t offset)
58 ret = pwrite(fd, buf, count, offset);
66 static ssize_t write_check(int fd, const void *buf, size_t count)
72 ret = write(fd, buf, count);
80 static int ftruncate_check(int fd, off_t length)
86 ret = ftruncate(fd, length);
92 static bool test_death(enum operation op, struct agent *agent)
94 struct tdb_context *tdb = NULL;
96 enum agent_return ret;
97 int needed_recovery = 0;
101 if (setjmp(jmpbuf) != 0) {
102 /* We're partway through. Simulate our death. */
105 in_transaction = false;
107 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
110 else if (ret != FAILED) {
111 diag("Step %u agent NEEDS_RECOVERY = %s", current,
112 agent_return_name(ret));
116 ret = external_agent_operation(agent, op, KEY_STRING);
117 if (ret != SUCCESS) {
118 diag("Step %u op %s failed = %s", current,
120 agent_return_name(ret));
124 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
126 diag("Still needs recovery after step %u = %s",
127 current, agent_return_name(ret));
131 ret = external_agent_operation(agent, CHECK, "");
132 if (ret != SUCCESS) {
133 diag("Step %u check failed = %s", current,
134 agent_return_name(ret));
138 ret = external_agent_operation(agent, CLOSE, "");
139 if (ret != SUCCESS) {
140 diag("Step %u close failed = %s", current,
141 agent_return_name(ret));
145 /* Suppress logging as this tries to use closed fd. */
146 suppress_logging = true;
147 suppress_lockcheck = true;
149 suppress_logging = false;
150 suppress_lockcheck = false;
157 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
158 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
160 /* Put key for agent to fetch. */
161 key.dsize = strlen(KEY_STRING);
162 key.dptr = (void *)KEY_STRING;
163 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
166 /* This is the key we insert in transaction. */
169 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
171 errx(1, "Agent failed to open: %s", agent_return_name(ret));
173 ret = external_agent_operation(agent, FETCH, KEY_STRING);
175 errx(1, "Agent failed find key: %s", agent_return_name(ret));
177 in_transaction = true;
178 if (tdb_transaction_start(tdb) != 0)
181 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
184 if (tdb_transaction_commit(tdb) != 0)
187 in_transaction = false;
190 diag("Completed %u runs", current);
192 ret = external_agent_operation(agent, CLOSE, "");
193 if (ret != SUCCESS) {
194 diag("Step %u close failed = %s", current,
195 agent_return_name(ret));
199 ok1(needed_recovery);
200 ok1(locking_errors == 0);
201 ok1(forget_locking() == 0);
206 int main(int argc, char *argv[])
208 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
213 unlock_callback = maybe_die;
215 agent = prepare_external_agent();
217 err(1, "preparing agent");
219 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
220 diag("Testing %s after death", operation_name(ops[i]));
221 ok1(test_death(ops[i], agent));
224 return exit_status();