tdb: new test, cleanup old tests by centralizing lock tracking.
[ccan] / ccan / tdb / test / run-die-during-transaction.c
1 #define _XOPEN_SOURCE 500
2 #include <unistd.h>
3 #include "lock-tracking.h"
4 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
5 static ssize_t write_check(int fd, const void *buf, size_t count);
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_with_lockcheck
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 static void taplog(struct tdb_context *tdb,
43                    enum tdb_debug_level level,
44                    const char *fmt, ...)
45 {
46         va_list ap;
47         char line[200];
48
49         if (suppress_logging)
50                 return;
51
52         va_start(ap, fmt);
53         vsprintf(line, fmt, ap);
54         va_end(ap);
55
56         diag("%s", line);
57 }
58
59 static void maybe_die(int fd)
60 {
61         if (in_transaction && current++ == target) {
62                 longjmp(jmpbuf, 1);
63         }
64 }
65
66 static ssize_t pwrite_check(int fd,
67                             const void *buf, size_t count, off_t offset)
68 {
69         ssize_t ret;
70
71         maybe_die(fd);
72
73         ret = pwrite(fd, buf, count, offset);
74         if (ret != count)
75                 return ret;
76
77         maybe_die(fd);
78         return ret;
79 }
80
81 static ssize_t write_check(int fd, const void *buf, size_t count)
82 {
83         ssize_t ret;
84
85         maybe_die(fd);
86
87         ret = write(fd, buf, count);
88         if (ret != count)
89                 return ret;
90
91         maybe_die(fd);
92         return ret;
93 }
94
95 static int ftruncate_check(int fd, off_t length)
96 {
97         int ret;
98
99         maybe_die(fd);
100
101         ret = ftruncate(fd, length);
102
103         maybe_die(fd);
104         return ret;
105 }
106
107 static bool test_death(enum operation op, struct agent *agent)
108 {
109         struct tdb_context *tdb = NULL;
110         TDB_DATA key, data;
111         struct tdb_logging_context logctx = { taplog, NULL };
112         int needed_recovery = 0;
113
114         current = target = 0;
115 reset:
116         if (setjmp(jmpbuf) != 0) {
117                 /* We're partway through.  Simulate our death. */
118                 close(tdb->fd);
119                 in_transaction = false;
120
121                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
122                                              ""))
123                         needed_recovery++;
124
125                 if (external_agent_operation(agent, op, "") != 1) {
126                         diag("Step %u op failed", current);
127                         return false;
128                 }
129
130                 if (external_agent_operation(agent, NEEDS_RECOVERY_KEEP_OPENED,
131                                              "")) {
132                         diag("Still needs recovery after step %u", current);
133                         return false;
134                 }
135
136                 if (external_agent_operation(agent, CHECK_KEEP_OPENED, "")
137                     != 1) {
138                         diag("Step %u check failed", current);
139 #if 0
140                         return false;
141 #endif
142                 }
143
144                 external_agent_operation(agent, CLOSE, "");
145                 /* Suppress logging as this tries to use closed fd. */
146                 suppress_logging = true;
147                 tdb_close(tdb);
148                 suppress_logging = false;
149                 target++;
150                 current = 0;
151                 forget_locking();
152                 goto reset;
153         }
154
155         unlink(TEST_DBNAME);
156         tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
157                           O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
158
159         if (external_agent_operation(agent, KEEP_OPENED, TEST_DBNAME) != 0)
160                 errx(1, "Agent failed to open?");
161
162         if (tdb_transaction_start(tdb) != 0)
163                 return false;
164
165         in_transaction = true;
166         key.dsize = strlen("hi");
167         key.dptr = (void *)"hi";
168         data.dptr = (void *)"world";
169         data.dsize = strlen("world");
170
171         if (tdb_store(tdb, key, data, TDB_INSERT) != 0)
172                 return false;
173         if (tdb_transaction_commit(tdb) != 0)
174                 return false;
175
176         in_transaction = false;
177
178         /* We made it! */
179         diag("Completed %u runs", current);
180         tdb_close(tdb);
181         external_agent_operation(agent, CLOSE, "");
182
183         ok1(needed_recovery);
184         ok1(locking_errors == 0);
185         ok1(forget_locking() == 0);
186         locking_errors = 0;
187         return true;
188 }
189
190 int main(int argc, char *argv[])
191 {
192         enum operation ops[] = { FETCH_KEEP_OPENED,
193                                  STORE_KEEP_OPENED,
194                                  TRANSACTION_KEEP_OPENED };
195         struct agent *agent;
196         int i;
197
198         plan_tests(6);
199         unlock_callback = maybe_die;
200
201         agent = prepare_external_agent();
202         if (!agent)
203                 err(1, "preparing agent");
204
205         /* Nice ourselves down: we can't tell the difference between agent
206          * blocking on lock, and agent not scheduled. */
207         nice(15);
208
209         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
210                 diag("Testing %s after death",
211                      ops[i] == TRANSACTION_KEEP_OPENED ? "transaction"
212                      : ops[i] == FETCH_KEEP_OPENED ? "fetch"
213                      : ops[i] == STORE_KEEP_OPENED ? "store"
214                      : NULL);
215
216                 ok1(test_death(ops[i], agent));
217         }
218
219         return exit_status();
220 }