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