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