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/tap/tap.h>
29 #include "external-agent.h"
36 static bool in_transaction;
37 static bool suppress_logging;
38 static int target, current;
39 static jmp_buf jmpbuf;
40 #define TEST_DBNAME "run-die-during-transaction.tdb"
41 #define KEY_STRING "helloworld"
43 static void taplog(struct tdb_context *tdb,
44 enum tdb_debug_level level,
54 vsprintf(line, fmt, ap);
60 static void maybe_die(int fd)
62 if (in_transaction && current++ == target) {
67 static ssize_t pwrite_check(int fd,
68 const void *buf, size_t count, off_t offset)
74 ret = pwrite(fd, buf, count, offset);
82 static ssize_t write_check(int fd, const void *buf, size_t count)
88 ret = write(fd, buf, count);
96 static int ftruncate_check(int fd, off_t length)
102 ret = ftruncate(fd, length);
108 static bool test_death(enum operation op, struct agent *agent)
110 struct tdb_context *tdb = NULL;
112 struct tdb_logging_context logctx = { taplog, NULL };
113 enum agent_return ret;
114 int needed_recovery = 0;
116 current = target = 0;
118 if (setjmp(jmpbuf) != 0) {
119 /* We're partway through. Simulate our death. */
122 in_transaction = false;
124 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
127 else if (ret != FAILED) {
128 diag("Step %u agent NEEDS_RECOVERY = %s", current,
129 agent_return_name(ret));
133 ret = external_agent_operation(agent, op, KEY_STRING);
134 if (ret != SUCCESS) {
135 diag("Step %u op %s failed = %s", current,
137 agent_return_name(ret));
141 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
143 diag("Still needs recovery after step %u = %s",
144 current, agent_return_name(ret));
148 ret = external_agent_operation(agent, CHECK, "");
149 if (ret != SUCCESS) {
150 diag("Step %u check failed = %s", current,
151 agent_return_name(ret));
155 ret = external_agent_operation(agent, CLOSE, "");
156 if (ret != SUCCESS) {
157 diag("Step %u close failed = %s", current,
158 agent_return_name(ret));
162 /* Suppress logging as this tries to use closed fd. */
163 suppress_logging = true;
164 suppress_lockcheck = true;
166 suppress_logging = false;
167 suppress_lockcheck = false;
174 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
175 O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
177 /* Put key for agent to fetch. */
178 key.dsize = strlen(KEY_STRING);
179 key.dptr = (void *)KEY_STRING;
180 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
183 /* This is the key we insert in transaction. */
186 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
188 errx(1, "Agent failed to open: %s", agent_return_name(ret));
190 ret = external_agent_operation(agent, FETCH, KEY_STRING);
192 errx(1, "Agent failed find key: %s", agent_return_name(ret));
194 in_transaction = true;
195 if (tdb_transaction_start(tdb) != 0)
198 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
201 if (tdb_transaction_commit(tdb) != 0)
204 in_transaction = false;
207 diag("Completed %u runs", current);
209 ret = external_agent_operation(agent, CLOSE, "");
210 if (ret != SUCCESS) {
211 diag("Step %u close failed = %s", current,
212 agent_return_name(ret));
216 ok1(needed_recovery);
217 ok1(locking_errors == 0);
218 ok1(forget_locking() == 0);
223 int main(int argc, char *argv[])
225 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
230 unlock_callback = maybe_die;
232 agent = prepare_external_agent();
234 err(1, "preparing agent");
236 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
237 diag("Testing %s after death", operation_name(ops[i]));
238 ok1(test_death(ops[i], agent));
241 return exit_status();