3 #include "lock-tracking.h"
4 #include "tap-interface.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 10
19 static void *allocated[MAX_ALLOCATIONS];
20 static unsigned max_alloc = 0;
22 static void *malloc_noleak(size_t len)
26 for (i = 0; i < MAX_ALLOCATIONS; i++)
28 allocated[i] = malloc(len);
31 diag("max_alloc: %i", max_alloc);
35 diag("Too many allocations!");
39 static void *realloc_noleak(void *p, size_t size)
43 for (i = 0; i < MAX_ALLOCATIONS; i++) {
44 if (allocated[i] == p) {
47 diag("max_alloc: %i", max_alloc);
49 return allocated[i] = realloc(p, size);
52 diag("Untracked realloc!");
56 static void free_noleak(void *p)
60 /* We don't catch asprintf, so don't complain if we miss one. */
61 for (i = 0; i < MAX_ALLOCATIONS; i++) {
62 if (allocated[i] == p) {
70 static void free_all(void)
74 for (i = 0; i < MAX_ALLOCATIONS; i++) {
80 #define malloc malloc_noleak
81 #define free free_noleak
82 #define realloc realloc_noleak
84 #include "ntdb-source.h"
96 #include <ccan/err/err.h>
98 #include "external-agent.h"
101 static bool in_transaction;
102 static int target, current;
103 static jmp_buf jmpbuf;
104 #define TEST_DBNAME "run-57-die-during-transaction.ntdb"
105 #define KEY_STRING "helloworld"
107 static void maybe_die(int fd)
109 if (in_transaction && current++ == target) {
114 static ssize_t pwrite_check(int fd,
115 const void *buf, size_t count, off_t offset)
121 ret = pwrite(fd, buf, count, offset);
129 static ssize_t write_check(int fd, const void *buf, size_t count)
135 ret = write(fd, buf, count);
143 static int ftruncate_check(int fd, off_t length)
149 ret = ftruncate(fd, length);
155 static bool test_death(enum operation op, struct agent *agent)
157 struct ntdb_context *ntdb = NULL;
159 enum agent_return ret;
160 int needed_recovery = 0;
162 current = target = 0;
165 ntdb = ntdb_open(TEST_DBNAME, NTDB_NOMMAP,
166 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
168 diag("Failed opening NTDB: %s", strerror(errno));
172 if (setjmp(jmpbuf) != 0) {
173 /* We're partway through. Simulate our death. */
174 close(ntdb->file->fd);
176 in_transaction = false;
178 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
181 else if (ret != FAILED) {
182 diag("Step %u agent NEEDS_RECOVERY = %s", current,
183 agent_return_name(ret));
187 ret = external_agent_operation(agent, op,
188 KEY_STRING "=" KEY_STRING);
189 if (ret != SUCCESS) {
190 diag("Step %u op %s failed = %s", current,
192 agent_return_name(ret));
196 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
198 diag("Still needs recovery after step %u = %s",
199 current, agent_return_name(ret));
203 ret = external_agent_operation(agent, CHECK, "");
204 if (ret != SUCCESS) {
205 diag("Step %u check failed = %s", current,
206 agent_return_name(ret));
210 ret = external_agent_operation(agent, CLOSE, "");
211 if (ret != SUCCESS) {
212 diag("Step %u close failed = %s", current,
213 agent_return_name(ret));
217 /* Suppress logging as this tries to use closed fd. */
218 suppress_logging = true;
219 suppress_lockcheck = true;
221 suppress_logging = false;
222 suppress_lockcheck = false;
229 /* Put key for agent to fetch. */
230 key = ntdb_mkdata(KEY_STRING, strlen(KEY_STRING));
231 if (ntdb_store(ntdb, key, key, NTDB_INSERT) != 0)
234 /* This is the key we insert in transaction. */
237 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
239 errx(1, "Agent failed to open: %s", agent_return_name(ret));
241 ret = external_agent_operation(agent, FETCH, KEY_STRING "=" KEY_STRING);
243 errx(1, "Agent failed find key: %s", agent_return_name(ret));
245 in_transaction = true;
246 if (ntdb_transaction_start(ntdb) != 0)
249 if (ntdb_store(ntdb, key, key, NTDB_INSERT) != 0)
252 if (ntdb_transaction_commit(ntdb) != 0)
255 in_transaction = false;
258 diag("Completed %u runs", current);
260 ret = external_agent_operation(agent, CLOSE, "");
261 if (ret != SUCCESS) {
262 diag("Step %u close failed = %s", current,
263 agent_return_name(ret));
267 ok1(needed_recovery);
268 ok1(locking_errors == 0);
269 ok1(forget_locking() == 0);
274 int main(int argc, char *argv[])
276 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
281 unlock_callback = maybe_die;
283 external_agent_free = free_noleak;
284 agent = prepare_external_agent();
286 err(1, "preparing agent");
288 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
289 diag("Testing %s after death", operation_name(ops[i]));
290 ok1(test_death(ops[i], agent));
293 free_external_agent(agent);
294 return exit_status();