7e61b4818c97133ecb119addb975c7cf70daf3ce
[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;
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                         return false;
208                 }
209
210                 external_agent_operation(agent, CLOSE, "");
211                 /* Suppress logging as this tries to use closed fd. */
212                 suppress_logging = true;
213                 tdb_close(tdb);
214                 suppress_logging = false;
215                 target++;
216                 current = 0;
217                 goto reset;
218         }
219
220         unlink(TEST_DBNAME);
221         tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
222                           O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
223
224         if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
225                 errx(1, "Agent failed to open?");
226
227         if (tdb_transaction_start(tdb) != 0)
228                 return false;
229
230         in_transaction = true;
231         key.dsize = strlen("hi");
232         key.dptr = (void *)"hi";
233         data.dptr = (void *)"world";
234         data.dsize = strlen("world");
235
236         if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
237                 return false;
238         if (tdb_transaction_commit(tdb) != 0)
239                 return false;
240
241         in_transaction = false;
242
243         /* We made it! */
244         diag("Completed %u runs", current);
245         tdb_close(tdb);
246         external_agent_operation(agent, CLOSE, "");
247
248         ok1(needed_recovery);
249         return true;
250 }
251
252 int main(int argc, char *argv[])
253 {
254         enum operation ops[] = { FETCH_KEEP_OPENED,
255                                  STORE_KEEP_OPENED,
256                                  TRANSACTION_KEEP_OPENED };
257         struct agent *agent;
258         int i;
259
260         plan_tests(6);
261         agent = prepare_external_agent();
262         if (!agent)
263                 err(1, "preparing agent");
264
265         /* Nice ourselves down: we can't tell the difference between agent
266          * blocking on lock, and agent not scheduled. */
267         nice(15);
268
269         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
270                 diag("Testing %s after death",
271                      ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
272                      : ops[i] == FETCH_KEEP_OPENED ? "fetch"
273                      : ops[i] == STORE_KEEP_OPENED ? "store"
274                      : NULL);
275
276                 ok1(test_death(ops[i], agent));
277         }
278
279         return exit_status();
280 }