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