e622395537afb8b85429fd7894ea5bb5021389d3
[ccan] / ccan / tdb2 / test / run-57-die-during-transaction.c
1 #include <ccan/tdb2/private.h>
2 #include <unistd.h>
3 #include "lock-tracking.h"
4 #include <ccan/tap/tap.h>
5 #include <stdlib.h>
6 #include <assert.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);
10
11 #define pwrite pwrite_check
12 #define write write_check
13 #define fcntl fcntl_with_lockcheck
14 #define ftruncate ftruncate_check
15
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];
20
21 static void *malloc_noleak(size_t len)
22 {
23         unsigned int i;
24
25         for (i = 0; i < MAX_ALLOCATIONS; i++)
26                 if (!allocated[i]) {
27                         allocated[i] = malloc(len);
28                         return allocated[i];
29                 }
30         diag("Too many allocations!");
31         abort();
32 }
33
34 static void free_noleak(void *p)
35 {
36         unsigned int i;
37
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) {
41                         allocated[i] = NULL;
42                         break;
43                 }
44         }
45         free(p);
46 }
47
48 static void free_all(void)
49 {
50         unsigned int i;
51
52         for (i = 0; i < MAX_ALLOCATIONS; i++) {
53                 free(allocated[i]);
54                 allocated[i] = NULL;
55         }
56 }
57
58 #define malloc malloc_noleak
59 #define free free_noleak
60
61 #include "tdb2-source.h"
62
63 #undef malloc
64 #undef free
65 #undef write
66 #undef pwrite
67 #undef fcntl
68 #undef ftruncate
69
70 #include <stdbool.h>
71 #include <stdarg.h>
72 #include <err.h>
73 #include <setjmp.h>
74 #include "external-agent.h"
75 #include "logging.h"
76
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"
82
83 static void maybe_die(int fd)
84 {
85         if (in_transaction && current++ == target) {
86                 longjmp(jmpbuf, 1);
87         }
88 }
89
90 static ssize_t pwrite_check(int fd,
91                             const void *buf, size_t count, off_t offset)
92 {
93         ssize_t ret;
94
95         maybe_die(fd);
96
97         ret = pwrite(fd, buf, count, offset);
98         if (ret != count)
99                 return ret;
100
101         maybe_die(fd);
102         return ret;
103 }
104
105 static ssize_t write_check(int fd, const void *buf, size_t count)
106 {
107         ssize_t ret;
108
109         maybe_die(fd);
110
111         ret = write(fd, buf, count);
112         if (ret != count)
113                 return ret;
114
115         maybe_die(fd);
116         return ret;
117 }
118
119 static int ftruncate_check(int fd, off_t length)
120 {
121         int ret;
122
123         maybe_die(fd);
124
125         ret = ftruncate(fd, length);
126
127         maybe_die(fd);
128         return ret;
129 }
130
131 static bool test_death(enum operation op, struct agent *agent)
132 {
133         struct tdb_context *tdb = NULL;
134         TDB_DATA key;
135         enum agent_return ret;
136         int needed_recovery = 0;
137
138         current = target = 0;
139 reset:
140         unlink(TEST_DBNAME);
141         tdb = tdb_open(TEST_DBNAME, TDB_NOMMAP,
142                        O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
143         if (!tdb) {
144                 diag("Failed opening TDB: %s", strerror(errno));
145                 return false;
146         }
147
148         if (setjmp(jmpbuf) != 0) {
149                 /* We're partway through.  Simulate our death. */
150                 close(tdb->file->fd);
151                 forget_locking();
152                 in_transaction = false;
153
154                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
155                 if (ret == SUCCESS)
156                         needed_recovery++;
157                 else if (ret != FAILED) {
158                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
159                              agent_return_name(ret));
160                         return false;
161                 }
162
163                 ret = external_agent_operation(agent, op, KEY_STRING);
164                 if (ret != SUCCESS) {
165                         diag("Step %u op %s failed = %s", current,
166                              operation_name(op),
167                              agent_return_name(ret));
168                         return false;
169                 }
170
171                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
172                 if (ret != FAILED) {
173                         diag("Still needs recovery after step %u = %s",
174                              current, agent_return_name(ret));
175                         return false;
176                 }
177
178                 ret = external_agent_operation(agent, CHECK, "");
179                 if (ret != SUCCESS) {
180                         diag("Step %u check failed = %s", current,
181                              agent_return_name(ret));
182                         return false;
183                 }
184
185                 ret = external_agent_operation(agent, CLOSE, "");
186                 if (ret != SUCCESS) {
187                         diag("Step %u close failed = %s", current,
188                              agent_return_name(ret));
189                         return false;
190                 }
191
192                 /* Suppress logging as this tries to use closed fd. */
193                 suppress_logging = true;
194                 suppress_lockcheck = true;
195                 tdb_close(tdb);
196                 suppress_logging = false;
197                 suppress_lockcheck = false;
198                 target++;
199                 current = 0;
200                 free_all();
201                 goto reset;
202         }
203
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)
207                 return false;
208
209         /* This is the key we insert in transaction. */
210         key.dsize--;
211
212         ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
213         if (ret != SUCCESS)
214                 errx(1, "Agent failed to open: %s", agent_return_name(ret));
215
216         ret = external_agent_operation(agent, FETCH, KEY_STRING);
217         if (ret != SUCCESS)
218                 errx(1, "Agent failed find key: %s", agent_return_name(ret));
219
220         in_transaction = true;
221         if (tdb_transaction_start(tdb) != 0)
222                 return false;
223
224         if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
225                 return false;
226
227         if (tdb_transaction_commit(tdb) != 0)
228                 return false;
229
230         in_transaction = false;
231
232         /* We made it! */
233         diag("Completed %u runs", current);
234         tdb_close(tdb);
235         ret = external_agent_operation(agent, CLOSE, "");
236         if (ret != SUCCESS) {
237                 diag("Step %u close failed = %s", current,
238                      agent_return_name(ret));
239                 return false;
240         }
241
242         ok1(needed_recovery);
243         ok1(locking_errors == 0);
244         ok1(forget_locking() == 0);
245         locking_errors = 0;
246         return true;
247 }
248
249 int main(int argc, char *argv[])
250 {
251         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
252         struct agent *agent;
253         int i;
254
255         plan_tests(12);
256         unlock_callback = maybe_die;
257
258         agent = prepare_external_agent();
259         if (!agent)
260                 err(1, "preparing agent");
261
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));
265         }
266
267         free_external_agent(agent);
268         return exit_status();
269 }