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/tdb2/tdb.c>
14 #include <ccan/tdb2/free.c>
15 #include <ccan/tdb2/lock.c>
16 #include <ccan/tdb2/io.c>
17 #include <ccan/tdb2/hash.c>
18 #include <ccan/tdb2/check.c>
19 #include <ccan/tdb2/transaction.c>
20 #include <ccan/tap/tap.h>
26 #include "external-agent.h"
34 static bool in_transaction;
35 static int target, current;
36 static jmp_buf jmpbuf;
37 #define TEST_DBNAME "run-57-die-during-transaction.tdb"
38 #define KEY_STRING "helloworld"
40 static void maybe_die(int fd)
42 if (in_transaction && current++ == target) {
47 static ssize_t pwrite_check(int fd,
48 const void *buf, size_t count, off_t offset)
54 ret = pwrite(fd, buf, count, offset);
62 static ssize_t write_check(int fd, const void *buf, size_t count)
68 ret = write(fd, buf, count);
76 static int ftruncate_check(int fd, off_t length)
82 ret = ftruncate(fd, length);
88 static bool test_death(enum operation op, struct agent *agent)
90 struct tdb_context *tdb = NULL;
92 enum agent_return ret;
93 int needed_recovery = 0;
98 tdb = tdb_open(TEST_DBNAME, TDB_NOMMAP,
99 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
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;
156 /* Put key for agent to fetch. */
157 key.dsize = strlen(KEY_STRING);
158 key.dptr = (void *)KEY_STRING;
159 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
162 /* This is the key we insert in transaction. */
165 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
167 errx(1, "Agent failed to open: %s", agent_return_name(ret));
169 ret = external_agent_operation(agent, FETCH, KEY_STRING);
171 errx(1, "Agent failed find key: %s", agent_return_name(ret));
173 in_transaction = true;
174 if (tdb_transaction_start(tdb) != 0)
177 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
180 if (tdb_transaction_commit(tdb) != 0)
183 in_transaction = false;
186 diag("Completed %u runs", current);
188 ret = external_agent_operation(agent, CLOSE, "");
189 if (ret != SUCCESS) {
190 diag("Step %u close failed = %s", current,
191 agent_return_name(ret));
195 ok1(needed_recovery);
196 ok1(locking_errors == 0);
197 ok1(forget_locking() == 0);
202 int main(int argc, char *argv[])
204 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
209 unlock_callback = maybe_die;
211 agent = prepare_external_agent();
213 err(1, "preparing agent");
215 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
216 diag("Testing %s after death", operation_name(ops[i]));
217 ok1(test_death(ops[i], agent));
220 return exit_status();