1 #include <ccan/tdb2/private.h>
3 #include "lock-tracking.h"
4 #include <ccan/tap/tap.h>
7 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
8 static ssize_t write_check(int fd, const void *buf, size_t count);
9 static int ftruncate_check(int fd, off_t length);
11 #define pwrite pwrite_check
12 #define write write_check
13 #define fcntl fcntl_with_lockcheck
14 #define ftruncate ftruncate_check
16 /* There's a malloc inside transaction_setup_recovery, and valgrind complains
17 * when we longjmp and leak it. */
18 #define MAX_ALLOCATIONS 200
19 static void *allocated[MAX_ALLOCATIONS];
21 static void *malloc_noleak(size_t len)
25 for (i = 0; i < MAX_ALLOCATIONS; i++)
27 allocated[i] = malloc(len);
30 diag("Too many allocations!");
34 static void free_noleak(void *p)
38 /* We don't catch realloc, so don't care if we miss one. */
39 for (i = 0; i < MAX_ALLOCATIONS; i++) {
40 if (allocated[i] == p) {
48 static void free_all(void)
52 for (i = 0; i < MAX_ALLOCATIONS; i++) {
58 #define malloc malloc_noleak
59 #define free free_noleak
61 #include "tdb2-source.h"
74 #include "external-agent.h"
77 static bool in_transaction;
78 static int target, current;
79 static jmp_buf jmpbuf;
80 #define TEST_DBNAME "run-57-die-during-transaction.tdb"
81 #define KEY_STRING "helloworld"
83 static void maybe_die(int fd)
85 if (in_transaction && current++ == target) {
90 static ssize_t pwrite_check(int fd,
91 const void *buf, size_t count, off_t offset)
97 ret = pwrite(fd, buf, count, offset);
105 static ssize_t write_check(int fd, const void *buf, size_t count)
111 ret = write(fd, buf, count);
119 static int ftruncate_check(int fd, off_t length)
125 ret = ftruncate(fd, length);
131 static bool test_death(enum operation op, struct agent *agent)
133 struct tdb_context *tdb = NULL;
135 enum agent_return ret;
136 int needed_recovery = 0;
138 current = target = 0;
141 tdb = tdb_open(TEST_DBNAME, TDB_NOMMAP,
142 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
144 diag("Failed opening TDB: %s", strerror(errno));
148 if (setjmp(jmpbuf) != 0) {
149 /* We're partway through. Simulate our death. */
150 close(tdb->file->fd);
152 in_transaction = false;
154 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
157 else if (ret != FAILED) {
158 diag("Step %u agent NEEDS_RECOVERY = %s", current,
159 agent_return_name(ret));
163 ret = external_agent_operation(agent, op, KEY_STRING);
164 if (ret != SUCCESS) {
165 diag("Step %u op %s failed = %s", current,
167 agent_return_name(ret));
171 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
173 diag("Still needs recovery after step %u = %s",
174 current, agent_return_name(ret));
178 ret = external_agent_operation(agent, CHECK, "");
179 if (ret != SUCCESS) {
180 diag("Step %u check failed = %s", current,
181 agent_return_name(ret));
185 ret = external_agent_operation(agent, CLOSE, "");
186 if (ret != SUCCESS) {
187 diag("Step %u close failed = %s", current,
188 agent_return_name(ret));
192 /* Suppress logging as this tries to use closed fd. */
193 suppress_logging = true;
194 suppress_lockcheck = true;
196 suppress_logging = false;
197 suppress_lockcheck = false;
204 /* Put key for agent to fetch. */
205 key = tdb_mkdata(KEY_STRING, strlen(KEY_STRING));
206 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
209 /* This is the key we insert in transaction. */
212 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
214 errx(1, "Agent failed to open: %s", agent_return_name(ret));
216 ret = external_agent_operation(agent, FETCH, KEY_STRING);
218 errx(1, "Agent failed find key: %s", agent_return_name(ret));
220 in_transaction = true;
221 if (tdb_transaction_start(tdb) != 0)
224 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
227 if (tdb_transaction_commit(tdb) != 0)
230 in_transaction = false;
233 diag("Completed %u runs", current);
235 ret = external_agent_operation(agent, CLOSE, "");
236 if (ret != SUCCESS) {
237 diag("Step %u close failed = %s", current,
238 agent_return_name(ret));
242 ok1(needed_recovery);
243 ok1(locking_errors == 0);
244 ok1(forget_locking() == 0);
249 int main(int argc, char *argv[])
251 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
256 unlock_callback = maybe_die;
258 agent = prepare_external_agent();
260 err(1, "preparing agent");
262 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
263 diag("Testing %s after death", operation_name(ops[i]));
264 ok1(test_death(ops[i], agent));
267 free_external_agent(agent);
268 return exit_status();