5f9d7f9f92d8600d69a3537bb11642e2777178c4
[ccan] / ccan / tdb / test / run-die-during-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
4 static ssize_t write_check(int fd, const void *buf, size_t count);
5 static int fcntl_check(int fd, int cmd, ... /* arg */ );
6 static int ftruncate_check(int fd, off_t length);
7
8 #define pwrite pwrite_check
9 #define write write_check
10 #define fcntl fcntl_check
11 #define ftruncate ftruncate_check
12
13 #include <ccan/tdb/tdb.h>
14 #include <ccan/tdb/io.c>
15 #include <ccan/tdb/tdb.c>
16 #include <ccan/tdb/lock.c>
17 #include <ccan/tdb/freelist.c>
18 #include <ccan/tdb/traverse.c>
19 #include <ccan/tdb/transaction.c>
20 #include <ccan/tdb/error.c>
21 #include <ccan/tdb/open.c>
22 #include <ccan/tdb/check.c>
23 #include <ccan/tap/tap.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <stdarg.h>
27 #include <err.h>
28 #include <setjmp.h>
29 #include "external-transaction.h"
30
31 #undef write
32 #undef pwrite
33 #undef fcntl
34 #undef ftruncate
35
36 static bool in_transaction;
37 static bool suppress_logging;
38 static int target, current;
39 static jmp_buf jmpbuf;
40 #define TEST_DBNAME "/tmp/test7.tdb"
41
42 /* We save the locks so we can reaquire them. */
43 struct lock {
44         struct lock *next;
45         off_t off;
46         unsigned int len;
47         int type;
48 };
49 static struct lock *locks;
50
51 static void taplog(struct tdb_context *tdb,
52                    enum tdb_debug_level level,
53                    const char *fmt, ...)
54 {
55         va_list ap;
56         char line[200];
57
58         if (suppress_logging)
59                 return;
60
61         va_start(ap, fmt);
62         vsprintf(line, fmt, ap);
63         va_end(ap);
64
65         diag("%s", line);
66 }
67
68 static void check_file_contents(int fd)
69 {
70         if (current++ == target) {
71                 longjmp(jmpbuf, 1);
72         }
73 }
74
75 static ssize_t pwrite_check(int fd,
76                             const void *buf, size_t count, off_t offset)
77 {
78         ssize_t ret;
79
80         if (in_transaction)
81                 check_file_contents(fd);
82
83         ret = pwrite(fd, buf, count, offset);
84         if (ret != count)
85                 return ret;
86
87         if (in_transaction)
88                 check_file_contents(fd);
89         return ret;
90 }
91
92 static ssize_t write_check(int fd, const void *buf, size_t count)
93 {
94         ssize_t ret;
95
96         if (in_transaction)
97                 check_file_contents(fd);
98
99         ret = write(fd, buf, count);
100         if (ret != count)
101                 return ret;
102
103         if (in_transaction)
104                 check_file_contents(fd);
105         return ret;
106 }
107
108 extern int fcntl(int fd, int cmd, ... /* arg */ );
109
110 static int fcntl_check(int fd, int cmd, ... /* arg */ )
111 {
112         va_list ap;
113         int ret, arg3;
114         struct flock *fl;
115
116         if (cmd != F_SETLK && cmd != F_SETLKW) {
117                 /* This may be totally bogus, but we don't know in general. */
118                 va_start(ap, cmd);
119                 arg3 = va_arg(ap, int);
120                 va_end(ap);
121
122                 return fcntl(fd, cmd, arg3);
123         }
124
125         va_start(ap, cmd);
126         fl = va_arg(ap, struct flock *);
127         va_end(ap);
128
129         ret = fcntl(fd, cmd, fl);
130         if (ret == 0) {
131                 if (fl->l_type == F_UNLCK) {
132                         struct lock **l;
133                         struct lock *old = NULL;
134                         
135                         for (l = &locks; *l; l = &(*l)->next) {
136                                 if ((*l)->off == fl->l_start
137                                     && (*l)->len == fl->l_len) {
138                                         old = *l;
139                                         *l = (*l)->next;
140                                         free(old);
141                                         break;
142                                 }
143                         }
144                         if (!old)
145                                 errx(1, "Unknown lock");
146                 } else {
147                         struct lock *new = malloc(sizeof *new);
148                         new->off = fl->l_start;
149                         new->len = fl->l_len;
150                         new->type = fl->l_type;
151                         new->next = locks;
152                         locks = new;
153                 }
154         }
155
156         if (in_transaction && fl->l_type == F_UNLCK)
157                 check_file_contents(fd);
158         return ret;
159 }
160
161 static int ftruncate_check(int fd, off_t length)
162 {
163         int ret;
164
165         if (in_transaction)
166                 check_file_contents(fd);
167
168         ret = ftruncate(fd, length);
169
170         if (in_transaction)
171                 check_file_contents(fd);
172         return ret;
173 }
174
175 static bool test_death(enum operation op, struct agent *agent)
176 {
177         struct tdb_context *tdb = NULL;
178         TDB_DATA key, data;
179         struct tdb_logging_context logctx = { taplog, NULL };
180         int needed_recovery = 0;
181
182         current = target = 0;
183 reset:
184         if (setjmp(jmpbuf) != 0) {
185                 /* We're partway through.  Simulate our death. */
186                 close(tdb->fd);
187                 in_transaction = false;
188
189                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
190                                              ""))
191                         needed_recovery++;
192
193                 if (external_agent_operation(agent, op, "") != 1) {
194                         diag("Step %u op failed", current);
195                         return false;
196                 }
197
198                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
199                                              "")) {
200                         diag("Still needs recovery after step %u", current);
201                         return false;
202                 }
203
204                 if (external_agent_operation(agent, CHECK_KEEP_OPENED, "")
205                     != 1) {
206                         diag("Step %u check failed", current);
207 #if 0
208                         return false;
209 #endif
210                 }
211
212                 external_agent_operation(agent, CLOSE, "");
213                 /* Suppress logging as this tries to use closed fd. */
214                 suppress_logging = true;
215                 tdb_close(tdb);
216                 suppress_logging = false;
217                 target++;
218                 current = 0;
219                 goto reset;
220         }
221
222         unlink(TEST_DBNAME);
223         tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
224                           O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
225
226         if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
227                 errx(1, "Agent failed to open?");
228
229         if (tdb_transaction_start(tdb) != 0)
230                 return false;
231
232         in_transaction = true;
233         key.dsize = strlen("hi");
234         key.dptr = (void *)"hi";
235         data.dptr = (void *)"world";
236         data.dsize = strlen("world");
237
238         if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
239                 return false;
240         if (tdb_transaction_commit(tdb) != 0)
241                 return false;
242
243         in_transaction = false;
244
245         /* We made it! */
246         diag("Completed %u runs", current);
247         tdb_close(tdb);
248         external_agent_operation(agent, CLOSE, "");
249
250         ok1(needed_recovery);
251         return true;
252 }
253
254 int main(int argc, char *argv[])
255 {
256         enum operation ops[] = { FETCH_KEEP_OPENED,
257                                  STORE_KEEP_OPENED,
258                                  TRANSACTION_KEEP_OPENED };
259         struct agent *agent;
260         int i;
261
262         plan_tests(6);
263         agent = prepare_external_agent();
264         if (!agent)
265                 err(1, "preparing agent");
266
267         /* Nice ourselves down: we can't tell the difference between agent
268          * blocking on lock, and agent not scheduled. */
269         nice(15);
270
271         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
272                 diag("Testing %s after death",
273                      ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
274                      : ops[i] == FETCH_KEEP_OPENED ? "fetch"
275                      : ops[i] == STORE_KEEP_OPENED ? "store"
276                      : NULL);
277
278                 ok1(test_death(ops[i], agent));
279         }
280
281         return exit_status();
282 }