tdb2: use config.h instead of setting _XOPEN_SOURCE etc.
[ccan] / ccan / tdb2 / test / run-57-die-during-transaction.c
1 #include "config.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 <ccan/tdb2/tdb.c>
62 #include <ccan/tdb2/free.c>
63 #include <ccan/tdb2/lock.c>
64 #include <ccan/tdb2/io.c>
65 #include <ccan/tdb2/hash.c>
66 #include <ccan/tdb2/check.c>
67 #include <ccan/tdb2/transaction.c>
68 #undef malloc
69 #undef free
70 #undef write
71 #undef pwrite
72 #undef fcntl
73 #undef ftruncate
74
75 #include <stdbool.h>
76 #include <stdarg.h>
77 #include <err.h>
78 #include <setjmp.h>
79 #include "external-agent.h"
80 #include "logging.h"
81
82 static bool in_transaction;
83 static int target, current;
84 static jmp_buf jmpbuf;
85 #define TEST_DBNAME "run-57-die-during-transaction.tdb"
86 #define KEY_STRING "helloworld"
87
88 static void maybe_die(int fd)
89 {
90         if (in_transaction && current++ == target) {
91                 longjmp(jmpbuf, 1);
92         }
93 }
94
95 static ssize_t pwrite_check(int fd,
96                             const void *buf, size_t count, off_t offset)
97 {
98         ssize_t ret;
99
100         maybe_die(fd);
101
102         ret = pwrite(fd, buf, count, offset);
103         if (ret != count)
104                 return ret;
105
106         maybe_die(fd);
107         return ret;
108 }
109
110 static ssize_t write_check(int fd, const void *buf, size_t count)
111 {
112         ssize_t ret;
113
114         maybe_die(fd);
115
116         ret = write(fd, buf, count);
117         if (ret != count)
118                 return ret;
119
120         maybe_die(fd);
121         return ret;
122 }
123
124 static int ftruncate_check(int fd, off_t length)
125 {
126         int ret;
127
128         maybe_die(fd);
129
130         ret = ftruncate(fd, length);
131
132         maybe_die(fd);
133         return ret;
134 }
135
136 static bool test_death(enum operation op, struct agent *agent)
137 {
138         struct tdb_context *tdb = NULL;
139         TDB_DATA key;
140         enum agent_return ret;
141         int needed_recovery = 0;
142
143         current = target = 0;
144 reset:
145         unlink(TEST_DBNAME);
146         tdb = tdb_open(TEST_DBNAME, TDB_NOMMAP,
147                        O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
148         if (!tdb) {
149                 diag("Failed opening TDB: %s", strerror(errno));
150                 return false;
151         }
152
153         if (setjmp(jmpbuf) != 0) {
154                 /* We're partway through.  Simulate our death. */
155                 close(tdb->fd);
156                 forget_locking();
157                 in_transaction = false;
158
159                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
160                 if (ret == SUCCESS)
161                         needed_recovery++;
162                 else if (ret != FAILED) {
163                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
164                              agent_return_name(ret));
165                         return false;
166                 }
167
168                 ret = external_agent_operation(agent, op, KEY_STRING);
169                 if (ret != SUCCESS) {
170                         diag("Step %u op %s failed = %s", current,
171                              operation_name(op),
172                              agent_return_name(ret));
173                         return false;
174                 }
175
176                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
177                 if (ret != FAILED) {
178                         diag("Still needs recovery after step %u = %s",
179                              current, agent_return_name(ret));
180                         return false;
181                 }
182
183                 ret = external_agent_operation(agent, CHECK, "");
184                 if (ret != SUCCESS) {
185                         diag("Step %u check failed = %s", current,
186                              agent_return_name(ret));
187                         return false;
188                 }
189
190                 ret = external_agent_operation(agent, CLOSE, "");
191                 if (ret != SUCCESS) {
192                         diag("Step %u close failed = %s", current,
193                              agent_return_name(ret));
194                         return false;
195                 }
196
197                 /* Suppress logging as this tries to use closed fd. */
198                 suppress_logging = true;
199                 suppress_lockcheck = true;
200                 tdb_close(tdb);
201                 suppress_logging = false;
202                 suppress_lockcheck = false;
203                 target++;
204                 current = 0;
205                 free_all();
206                 goto reset;
207         }
208
209         /* Put key for agent to fetch. */
210         key.dsize = strlen(KEY_STRING);
211         key.dptr = (void *)KEY_STRING;
212         if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
213                 return false;
214
215         /* This is the key we insert in transaction. */
216         key.dsize--;
217
218         ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
219         if (ret != SUCCESS)
220                 errx(1, "Agent failed to open: %s", agent_return_name(ret));
221
222         ret = external_agent_operation(agent, FETCH, KEY_STRING);
223         if (ret != SUCCESS)
224                 errx(1, "Agent failed find key: %s", agent_return_name(ret));
225
226         in_transaction = true;
227         if (tdb_transaction_start(tdb) != 0)
228                 return false;
229
230         if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
231                 return false;
232
233         if (tdb_transaction_commit(tdb) != 0)
234                 return false;
235
236         in_transaction = false;
237
238         /* We made it! */
239         diag("Completed %u runs", current);
240         tdb_close(tdb);
241         ret = external_agent_operation(agent, CLOSE, "");
242         if (ret != SUCCESS) {
243                 diag("Step %u close failed = %s", current,
244                      agent_return_name(ret));
245                 return false;
246         }
247
248         ok1(needed_recovery);
249         ok1(locking_errors == 0);
250         ok1(forget_locking() == 0);
251         locking_errors = 0;
252         return true;
253 }
254
255 int main(int argc, char *argv[])
256 {
257         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
258         struct agent *agent;
259         int i;
260
261         plan_tests(12);
262         unlock_callback = maybe_die;
263
264         agent = prepare_external_agent();
265         if (!agent)
266                 err(1, "preparing agent");
267
268         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
269                 diag("Testing %s after death", operation_name(ops[i]));
270                 ok1(test_death(ops[i], agent));
271         }
272
273         free_external_agent(agent);
274         return exit_status();
275 }