1 #include <ccan/tdb2/private.h>
3 #include "tdb1-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_lockcheck1
11 #define ftruncate ftruncate_check
13 #include "tdb2-source.h"
14 #include <ccan/tap/tap.h>
20 #include "tdb1-external-agent.h"
21 #include "tdb1-logging.h"
28 static bool in_transaction;
29 static int target, current;
30 static jmp_buf jmpbuf;
31 #define TEST_DBNAME "run-die-during-transaction.tdb"
32 #define KEY_STRING "helloworld"
34 static void maybe_die(int fd)
36 if (in_transaction && current++ == target) {
41 static ssize_t pwrite_check(int fd,
42 const void *buf, size_t count, off_t offset)
48 ret = pwrite(fd, buf, count, offset);
56 static ssize_t write_check(int fd, const void *buf, size_t count)
62 ret = write(fd, buf, count);
70 static int ftruncate_check(int fd, off_t length)
76 ret = ftruncate(fd, length);
82 static bool test_death(enum operation op, struct agent *agent)
84 struct tdb1_context *tdb = NULL;
86 enum agent_return ret;
87 int needed_recovery = 0;
92 tdb = tdb1_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
93 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
95 if (setjmp(jmpbuf) != 0) {
96 /* We're partway through. Simulate our death. */
99 in_transaction = false;
101 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
104 else if (ret != FAILED) {
105 diag("Step %u agent NEEDS_RECOVERY = %s", current,
106 agent_return_name1(ret));
110 ret = external_agent_operation1(agent, op, KEY_STRING);
111 if (ret != SUCCESS) {
112 diag("Step %u op %s failed = %s", current,
114 agent_return_name1(ret));
118 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
120 diag("Still needs recovery after step %u = %s",
121 current, agent_return_name1(ret));
125 ret = external_agent_operation1(agent, CHECK, "");
126 if (ret != SUCCESS) {
127 diag("Step %u check failed = %s", current,
128 agent_return_name1(ret));
132 ret = external_agent_operation1(agent, CLOSE, "");
133 if (ret != SUCCESS) {
134 diag("Step %u close failed = %s", current,
135 agent_return_name1(ret));
139 /* Suppress logging as this tries to use closed fd. */
140 suppress_logging = true;
141 suppress_lockcheck1 = true;
143 suppress_logging = false;
144 suppress_lockcheck1 = false;
150 /* Put key for agent to fetch. */
151 key.dsize = strlen(KEY_STRING);
152 key.dptr = (void *)KEY_STRING;
153 if (tdb1_store(tdb, key, key, TDB_INSERT) != 0)
156 /* This is the key we insert in transaction. */
159 ret = external_agent_operation1(agent, OPEN, TEST_DBNAME);
161 errx(1, "Agent failed to open: %s", agent_return_name1(ret));
163 ret = external_agent_operation1(agent, FETCH, KEY_STRING);
165 errx(1, "Agent failed find key: %s", agent_return_name1(ret));
167 in_transaction = true;
168 if (tdb1_transaction_start(tdb) != 0)
171 if (tdb1_store(tdb, key, key, TDB_INSERT) != 0)
174 if (tdb1_transaction_commit(tdb) != 0)
177 in_transaction = false;
180 diag("Completed %u runs", current);
182 ret = external_agent_operation1(agent, CLOSE, "");
183 if (ret != SUCCESS) {
184 diag("Step %u close failed = %s", current,
185 agent_return_name1(ret));
189 ok1(needed_recovery);
190 ok1(locking_errors1 == 0);
191 ok1(forget_locking1() == 0);
196 int main(int argc, char *argv[])
198 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
203 unlock_callback1 = maybe_die;
205 agent = prepare_external_agent1();
207 err(1, "preparing agent");
209 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
210 diag("Testing %s after death", operation_name1(ops[i]));
211 ok1(test_death(ops[i], agent));
214 return exit_status();