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