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"
28 static bool in_transaction;
29 static int target, current;
30 static jmp_buf jmpbuf;
31 #define TEST_DBNAME "run-die-during-transaction.tdb1"
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 tdb_context *tdb = NULL;
86 enum agent_return ret;
87 int needed_recovery = 0;
88 union tdb_attribute hsize;
90 hsize.base.attr = TDB_ATTRIBUTE_TDB1_HASHSIZE;
91 hsize.base.next = &tap_log_attr;
92 hsize.tdb1_hashsize.hsize = 1024;
97 tdb = tdb_open(TEST_DBNAME, TDB_VERSION1|TDB_NOMMAP,
98 O_CREAT|O_TRUNC|O_RDWR, 0600, &hsize);
100 if (setjmp(jmpbuf) != 0) {
101 /* We're partway through. Simulate our death. */
102 close(tdb->file->fd);
104 in_transaction = false;
106 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
109 else if (ret != FAILED) {
110 diag("Step %u agent NEEDS_RECOVERY = %s", current,
111 agent_return_name1(ret));
115 ret = external_agent_operation1(agent, op, KEY_STRING);
116 if (ret != SUCCESS) {
117 diag("Step %u op %s failed = %s", current,
119 agent_return_name1(ret));
123 ret = external_agent_operation1(agent, NEEDS_RECOVERY, "");
125 diag("Still needs recovery after step %u = %s",
126 current, agent_return_name1(ret));
130 ret = external_agent_operation1(agent, CHECK, "");
131 if (ret != SUCCESS) {
132 diag("Step %u check failed = %s", current,
133 agent_return_name1(ret));
137 ret = external_agent_operation1(agent, CLOSE, "");
138 if (ret != SUCCESS) {
139 diag("Step %u close failed = %s", current,
140 agent_return_name1(ret));
144 /* Suppress logging as this tries to use closed fd. */
145 suppress_logging = true;
146 suppress_lockcheck1 = true;
148 suppress_logging = false;
149 suppress_lockcheck1 = false;
155 /* Put key for agent to fetch. */
156 key.dsize = strlen(KEY_STRING);
157 key.dptr = (void *)KEY_STRING;
158 if (tdb1_store(tdb, key, key, TDB_INSERT) != 0)
161 /* This is the key we insert in transaction. */
164 ret = external_agent_operation1(agent, OPEN, TEST_DBNAME);
166 errx(1, "Agent failed to open: %s", agent_return_name1(ret));
168 ret = external_agent_operation1(agent, FETCH, KEY_STRING);
170 errx(1, "Agent failed find key: %s", agent_return_name1(ret));
172 in_transaction = true;
173 if (tdb1_transaction_start(tdb) != 0)
176 if (tdb1_store(tdb, key, key, TDB_INSERT) != 0)
179 if (tdb1_transaction_commit(tdb) != 0)
182 in_transaction = false;
185 diag("Completed %u runs", current);
187 ret = external_agent_operation1(agent, CLOSE, "");
188 if (ret != SUCCESS) {
189 diag("Step %u close failed = %s", current,
190 agent_return_name1(ret));
194 ok1(needed_recovery);
195 ok1(locking_errors1 == 0);
196 ok1(forget_locking1() == 0);
201 int main(int argc, char *argv[])
203 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
208 unlock_callback1 = maybe_die;
210 agent = prepare_external_agent1();
212 err(1, "preparing agent");
214 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
215 diag("Testing %s after death", operation_name1(ops[i]));
216 ok1(test_death(ops[i], agent));
219 return exit_status();