]> git.ozlabs.org Git - ccan/blob - ccan/tdb/test/run-die-during-transaction.c
tdb: rewrite external agent for testing.
[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-agent.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 "run-die-during-transaction.tdb"
41 #define KEY_STRING "helloworld"
42
43 static void taplog(struct tdb_context *tdb,
44                    enum tdb_debug_level level,
45                    const char *fmt, ...)
46 {
47         va_list ap;
48         char line[200];
49
50         if (suppress_logging)
51                 return;
52
53         va_start(ap, fmt);
54         vsprintf(line, fmt, ap);
55         va_end(ap);
56
57         diag("%s", line);
58 }
59
60 static void maybe_die(int fd)
61 {
62         if (in_transaction && current++ == target) {
63                 longjmp(jmpbuf, 1);
64         }
65 }
66
67 static ssize_t pwrite_check(int fd,
68                             const void *buf, size_t count, off_t offset)
69 {
70         ssize_t ret;
71
72         maybe_die(fd);
73
74         ret = pwrite(fd, buf, count, offset);
75         if (ret != count)
76                 return ret;
77
78         maybe_die(fd);
79         return ret;
80 }
81
82 static ssize_t write_check(int fd, const void *buf, size_t count)
83 {
84         ssize_t ret;
85
86         maybe_die(fd);
87
88         ret = write(fd, buf, count);
89         if (ret != count)
90                 return ret;
91
92         maybe_die(fd);
93         return ret;
94 }
95
96 static int ftruncate_check(int fd, off_t length)
97 {
98         int ret;
99
100         maybe_die(fd);
101
102         ret = ftruncate(fd, length);
103
104         maybe_die(fd);
105         return ret;
106 }
107
108 static bool test_death(enum operation op, struct agent *agent)
109 {
110         struct tdb_context *tdb = NULL;
111         TDB_DATA key;
112         struct tdb_logging_context logctx = { taplog, NULL };
113         enum agent_return ret;
114         int needed_recovery = 0;
115
116         current = target = 0;
117 reset:
118         if (setjmp(jmpbuf) != 0) {
119                 /* We're partway through.  Simulate our death. */
120                 close(tdb->fd);
121                 forget_locking();
122                 in_transaction = false;
123
124                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
125                 if (ret == SUCCESS)
126                         needed_recovery++;
127                 else if (ret != FAILED) {
128                         diag("Step %u agent NEEDS_RECOVERY = %s", current,
129                              agent_return_name(ret));
130                         return false;
131                 }
132
133                 ret = external_agent_operation(agent, op, KEY_STRING);
134                 if (ret != SUCCESS) {
135                         diag("Step %u op %s failed = %s", current,
136                              operation_name(op),
137                              agent_return_name(ret));
138                         return false;
139                 }
140
141                 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
142                 if (ret != FAILED) {
143                         diag("Still needs recovery after step %u = %s",
144                              current, agent_return_name(ret));
145                         return false;
146                 }
147
148                 ret = external_agent_operation(agent, CHECK, "");
149                 if (ret != SUCCESS) {
150                         diag("Step %u check failed = %s", current,
151                              agent_return_name(ret));
152                         return false;
153                 }
154
155                 ret = external_agent_operation(agent, CLOSE, "");
156                 if (ret != SUCCESS) {
157                         diag("Step %u close failed = %s", current,
158                              agent_return_name(ret));
159                         return false;
160                 }
161
162                 /* Suppress logging as this tries to use closed fd. */
163                 suppress_logging = true;
164                 suppress_lockcheck = true;
165                 tdb_close(tdb);
166                 suppress_logging = false;
167                 suppress_lockcheck = false;
168                 target++;
169                 current = 0;
170                 goto reset;
171         }
172
173         unlink(TEST_DBNAME);
174         tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
175                           O_CREAT|O_TRUNC|O_RDWR, 0600, &logctx, NULL);
176
177         /* Put key for agent to fetch. */
178         key.dsize = strlen(KEY_STRING);
179         key.dptr = (void *)KEY_STRING;
180         if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
181                 return false;
182
183         /* This is the key we insert in transaction. */
184         key.dsize--;
185
186         ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
187         if (ret != SUCCESS)
188                 errx(1, "Agent failed to open: %s", agent_return_name(ret));
189
190         ret = external_agent_operation(agent, FETCH, KEY_STRING);
191         if (ret != SUCCESS)
192                 errx(1, "Agent failed find key: %s", agent_return_name(ret));
193
194         in_transaction = true;
195         if (tdb_transaction_start(tdb) != 0)
196                 return false;
197
198         if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
199                 return false;
200
201         if (tdb_transaction_commit(tdb) != 0)
202                 return false;
203
204         in_transaction = false;
205
206         /* We made it! */
207         diag("Completed %u runs", current);
208         tdb_close(tdb);
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         ok1(needed_recovery);
217         ok1(locking_errors == 0);
218         ok1(forget_locking() == 0);
219         locking_errors = 0;
220         return true;
221 }
222
223 int main(int argc, char *argv[])
224 {
225         enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
226         struct agent *agent;
227         int i;
228
229         plan_tests(12);
230         unlock_callback = maybe_die;
231
232         agent = prepare_external_agent();
233         if (!agent)
234                 err(1, "preparing agent");
235
236         for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
237                 diag("Testing %s after death", operation_name(ops[i]));
238                 ok1(test_death(ops[i], agent));
239         }
240
241         return exit_status();
242 }