dlinklist: Fix a typo
[Samba.git] / lib / tdb / test / run-die-during-transaction.c
blob9b9041552ed762701e05f397beaa1c2bb42235f0
1 #include "../common/tdb_private.h"
2 #include "lock-tracking.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 ftruncate_check(int fd, off_t length);
7 #define pwrite pwrite_check
8 #define write write_check
9 #define fcntl fcntl_with_lockcheck
10 #define ftruncate ftruncate_check
12 #include "../common/io.c"
13 #include "../common/tdb.c"
14 #include "../common/lock.c"
15 #include "../common/freelist.c"
16 #include "../common/traverse.c"
17 #include "../common/transaction.c"
18 #include "../common/error.c"
19 #include "../common/open.c"
20 #include "../common/check.c"
21 #include "../common/hash.c"
22 #include "tap-interface.h"
23 #include <stdlib.h>
24 #include <stdbool.h>
25 #include <stdarg.h>
26 #include <setjmp.h>
27 #include "external-agent.h"
28 #include "logging.h"
30 #undef write
31 #undef pwrite
32 #undef fcntl
33 #undef ftruncate
35 static bool in_transaction;
36 static int target, current;
37 static jmp_buf jmpbuf;
38 #define TEST_DBNAME "run-die-during-transaction.tdb"
39 #define KEY_STRING "helloworld"
41 static void maybe_die(int fd)
43 if (in_transaction && current++ == target) {
44 longjmp(jmpbuf, 1);
48 static ssize_t pwrite_check(int fd,
49 const void *buf, size_t count, off_t offset)
51 ssize_t ret;
53 maybe_die(fd);
55 ret = pwrite(fd, buf, count, offset);
56 if (ret != count)
57 return ret;
59 maybe_die(fd);
60 return ret;
63 static ssize_t write_check(int fd, const void *buf, size_t count)
65 ssize_t ret;
67 maybe_die(fd);
69 ret = write(fd, buf, count);
70 if (ret != count)
71 return ret;
73 maybe_die(fd);
74 return ret;
77 static int ftruncate_check(int fd, off_t length)
79 int ret;
81 maybe_die(fd);
83 ret = ftruncate(fd, length);
85 maybe_die(fd);
86 return ret;
89 static bool test_death(enum operation op, struct agent *agent)
91 struct tdb_context *tdb = NULL;
92 TDB_DATA key;
93 enum agent_return ret;
94 int needed_recovery = 0;
96 current = target = 0;
97 reset:
98 unlink(TEST_DBNAME);
99 tdb = tdb_open_ex(TEST_DBNAME, 1024, TDB_NOMMAP,
100 O_CREAT|O_TRUNC|O_RDWR, 0600, &taplogctx, NULL);
102 if (setjmp(jmpbuf) != 0) {
103 /* We're partway through. Simulate our death. */
104 close(tdb->fd);
105 forget_locking();
106 in_transaction = false;
108 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
109 if (ret == SUCCESS)
110 needed_recovery++;
111 else if (ret != FAILED) {
112 diag("Step %u agent NEEDS_RECOVERY = %s", current,
113 agent_return_name(ret));
114 return false;
117 ret = external_agent_operation(agent, op, KEY_STRING);
118 if (ret != SUCCESS) {
119 diag("Step %u op %s failed = %s", current,
120 operation_name(op),
121 agent_return_name(ret));
122 return false;
125 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
126 if (ret != FAILED) {
127 diag("Still needs recovery after step %u = %s",
128 current, agent_return_name(ret));
129 return false;
132 ret = external_agent_operation(agent, CHECK, "");
133 if (ret != SUCCESS) {
134 diag("Step %u check failed = %s", current,
135 agent_return_name(ret));
136 return false;
139 ret = external_agent_operation(agent, CLOSE, "");
140 if (ret != SUCCESS) {
141 diag("Step %u close failed = %s", current,
142 agent_return_name(ret));
143 return false;
146 /* Suppress logging as this tries to use closed fd. */
147 suppress_logging = true;
148 suppress_lockcheck = true;
149 tdb_close(tdb);
150 suppress_logging = false;
151 suppress_lockcheck = false;
152 target++;
153 current = 0;
154 goto reset;
157 /* Put key for agent to fetch. */
158 key.dsize = strlen(KEY_STRING);
159 key.dptr = discard_const_p(uint8_t, KEY_STRING);
160 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
161 return false;
163 /* This is the key we insert in transaction. */
164 key.dsize--;
166 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
167 if (ret != SUCCESS) {
168 fprintf(stderr, "Agent failed to open: %s\n",
169 agent_return_name(ret));
170 exit(1);
173 ret = external_agent_operation(agent, FETCH, KEY_STRING);
174 if (ret != SUCCESS) {
175 fprintf(stderr, "Agent failed find key: %s\n",
176 agent_return_name(ret));
177 exit(1);
180 in_transaction = true;
181 if (tdb_transaction_start(tdb) != 0)
182 return false;
184 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
185 return false;
187 if (tdb_transaction_commit(tdb) != 0)
188 return false;
190 in_transaction = false;
192 /* We made it! */
193 diag("Completed %u runs", current);
194 tdb_close(tdb);
195 ret = external_agent_operation(agent, CLOSE, "");
196 if (ret != SUCCESS) {
197 diag("Step %u close failed = %s", current,
198 agent_return_name(ret));
199 return false;
202 #ifdef HAVE_INCOHERENT_MMAP
203 /* This means we always mmap, which makes this test a noop. */
204 ok1(1);
205 #else
206 ok1(needed_recovery);
207 #endif
208 ok1(locking_errors == 0);
209 ok1(forget_locking() == 0);
210 locking_errors = 0;
211 return true;
214 int main(int argc, char *argv[])
216 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
217 struct agent *agent;
218 int i;
220 plan_tests(12);
221 unlock_callback = maybe_die;
223 agent = prepare_external_agent();
225 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
226 diag("Testing %s after death", operation_name(ops[i]));
227 ok1(test_death(ops[i], agent));
230 return exit_status();