1 #include "../common/tdb_private.h"
2 #include "lock-tracking.h"
3 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
4 static ssize_t write_check(int fd, const void *buf, size_t count);
5 static int ftruncate_check(int fd, off_t length);
7 #define pwrite pwrite_check
8 #define write write_check
9 #define fcntl fcntl_with_lockcheck
10 #define ftruncate ftruncate_check
12 #include "../common/io.c"
13 #include "../common/tdb.c"
14 #include "../common/lock.c"
15 #include "../common/freelist.c"
16 #include "../common/traverse.c"
17 #include "../common/transaction.c"
18 #include "../common/error.c"
19 #include "../common/open.c"
20 #include "../common/check.c"
21 #include "../common/hash.c"
22 #include "tap-interface.h"
28 #include "external-agent.h"
36 static bool in_transaction;
37 static int target, current;
38 static jmp_buf jmpbuf;
39 #define TEST_DBNAME "run-die-during-transaction.tdb"
40 #define KEY_STRING "helloworld"
42 static void maybe_die(int fd)
44 if (in_transaction && current++ == target) {
49 static ssize_t pwrite_check(int fd,
50 const void *buf, size_t count, off_t offset)
56 ret = pwrite(fd, buf, count, offset);
64 static ssize_t write_check(int fd, const void *buf, size_t count)
70 ret = write(fd, buf, count);
78 static int ftruncate_check(int fd, off_t length)
84 ret = ftruncate(fd, length);
90 static bool test_death(enum operation op, struct agent *agent)
92 struct tdb_context *tdb = NULL;
94 enum agent_return ret;
95 int needed_recovery = 0;
100 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
101 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
103 if (setjmp(jmpbuf) != 0) {
104 /* We're partway through. Simulate our death. */
107 in_transaction = false;
109 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
112 else if (ret != FAILED) {
113 diag("Step %u agent NEEDS_RECOVERY = %s", current,
114 agent_return_name(ret));
118 ret = external_agent_operation(agent, op, KEY_STRING);
119 if (ret != SUCCESS) {
120 diag("Step %u op %s failed = %s", current,
122 agent_return_name(ret));
126 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
128 diag("Still needs recovery after step %u = %s",
129 current, agent_return_name(ret));
133 ret = external_agent_operation(agent, CHECK, "");
134 if (ret != SUCCESS) {
135 diag("Step %u check failed = %s", current,
136 agent_return_name(ret));
140 ret = external_agent_operation(agent, CLOSE, "");
141 if (ret != SUCCESS) {
142 diag("Step %u close failed = %s", current,
143 agent_return_name(ret));
147 /* Suppress logging as this tries to use closed fd. */
148 suppress_logging = true;
149 suppress_lockcheck = true;
151 suppress_logging = false;
152 suppress_lockcheck = false;
158 /* Put key for agent to fetch. */
159 key.dsize = strlen(KEY_STRING);
160 key.dptr = (void *)KEY_STRING;
161 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
164 /* This is the key we insert in transaction. */
167 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
169 errx(1, "Agent failed to open: %s", agent_return_name(ret));
171 ret = external_agent_operation(agent, FETCH, KEY_STRING);
173 errx(1, "Agent failed find key: %s", agent_return_name(ret));
175 in_transaction = true;
176 if (tdb_transaction_start(tdb) != 0)
179 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
182 if (tdb_transaction_commit(tdb) != 0)
185 in_transaction = false;
188 diag("Completed %u runs", current);
190 ret = external_agent_operation(agent, CLOSE, "");
191 if (ret != SUCCESS) {
192 diag("Step %u close failed = %s", current,
193 agent_return_name(ret));
197 ok1(needed_recovery);
198 ok1(locking_errors == 0);
199 ok1(forget_locking() == 0);
204 int main(int argc, char *argv[])
206 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
211 unlock_callback = maybe_die;
213 agent = prepare_external_agent();
215 err(1, "preparing agent");
217 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
218 diag("Testing %s after death", operation_name(ops[i]));
219 ok1(test_death(ops[i], agent));
222 return exit_status();