s3:smb2_write: pass fsp->fnum to init_strict_lock_struct()
[Samba/gebeck_regimport.git] / lib / tdb2 / test / run-57-die-during-transaction.c
blob595d6ae23b3d4d8b669a5848d27387f957ca4c8a
1 #include "private.h"
2 #include <unistd.h>
3 #include "lock-tracking.h"
4 #include "tap-interface.h"
5 #include <stdlib.h>
6 #include <assert.h>
7 static ssize_t pwrite_check(int fd, const void *buf, size_t count, off_t offset);
8 static ssize_t write_check(int fd, const void *buf, size_t count);
9 static int ftruncate_check(int fd, off_t length);
11 #define pwrite pwrite_check
12 #define write write_check
13 #define fcntl fcntl_with_lockcheck
14 #define ftruncate ftruncate_check
16 /* There's a malloc inside transaction_setup_recovery, and valgrind complains
17 * when we longjmp and leak it. */
18 #define MAX_ALLOCATIONS 10
19 static void *allocated[MAX_ALLOCATIONS];
20 static unsigned max_alloc = 0;
22 static void *malloc_noleak(size_t len)
24 unsigned int i;
26 for (i = 0; i < MAX_ALLOCATIONS; i++)
27 if (!allocated[i]) {
28 allocated[i] = malloc(len);
29 if (i > max_alloc) {
30 max_alloc = i;
31 diag("max_alloc: %i", max_alloc);
33 return allocated[i];
35 diag("Too many allocations!");
36 abort();
39 static void *realloc_noleak(void *p, size_t size)
41 unsigned int i;
43 for (i = 0; i < MAX_ALLOCATIONS; i++) {
44 if (allocated[i] == p) {
45 if (i > max_alloc) {
46 max_alloc = i;
47 diag("max_alloc: %i", max_alloc);
49 return allocated[i] = realloc(p, size);
52 diag("Untracked realloc!");
53 abort();
56 static void free_noleak(void *p)
58 unsigned int i;
60 /* We don't catch asprintf, so don't complain if we miss one. */
61 for (i = 0; i < MAX_ALLOCATIONS; i++) {
62 if (allocated[i] == p) {
63 allocated[i] = NULL;
64 break;
67 free(p);
70 static void free_all(void)
72 unsigned int i;
74 for (i = 0; i < MAX_ALLOCATIONS; i++) {
75 free(allocated[i]);
76 allocated[i] = NULL;
80 #define malloc malloc_noleak
81 #define free free_noleak
82 #define realloc realloc_noleak
84 #include "tdb2-source.h"
86 #undef malloc
87 #undef free
88 #undef realloc
89 #undef write
90 #undef pwrite
91 #undef fcntl
92 #undef ftruncate
94 #include <stdbool.h>
95 #include <stdarg.h>
96 #include <setjmp.h>
97 #include "external-agent.h"
98 #include "logging.h"
100 static bool in_transaction;
101 static int target, current;
102 static jmp_buf jmpbuf;
103 #define TEST_DBNAME "run-57-die-during-transaction.tdb"
104 #define KEY_STRING "helloworld"
106 static void maybe_die(int fd)
108 if (in_transaction && current++ == target) {
109 longjmp(jmpbuf, 1);
113 static ssize_t pwrite_check(int fd,
114 const void *buf, size_t count, off_t offset)
116 ssize_t ret;
118 maybe_die(fd);
120 ret = pwrite(fd, buf, count, offset);
121 if (ret != count)
122 return ret;
124 maybe_die(fd);
125 return ret;
128 static ssize_t write_check(int fd, const void *buf, size_t count)
130 ssize_t ret;
132 maybe_die(fd);
134 ret = write(fd, buf, count);
135 if (ret != count)
136 return ret;
138 maybe_die(fd);
139 return ret;
142 static int ftruncate_check(int fd, off_t length)
144 int ret;
146 maybe_die(fd);
148 ret = ftruncate(fd, length);
150 maybe_die(fd);
151 return ret;
154 static bool test_death(enum operation op, struct agent *agent, int flags)
156 struct tdb_context *tdb = NULL;
157 TDB_DATA key;
158 enum agent_return ret;
159 int needed_recovery = 0;
161 current = target = 0;
162 reset:
163 unlink(TEST_DBNAME);
164 tdb = tdb_open(TEST_DBNAME, flags|TDB_NOMMAP,
165 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
166 if (!tdb) {
167 diag("Failed opening TDB: %s", strerror(errno));
168 return false;
171 if (setjmp(jmpbuf) != 0) {
172 /* We're partway through. Simulate our death. */
173 close(tdb->file->fd);
174 forget_locking();
175 in_transaction = false;
177 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
178 if (ret == SUCCESS)
179 needed_recovery++;
180 else if (ret != FAILED) {
181 diag("Step %u agent NEEDS_RECOVERY = %s", current,
182 agent_return_name(ret));
183 return false;
186 ret = external_agent_operation(agent, op, KEY_STRING);
187 if (ret != SUCCESS) {
188 diag("Step %u op %s failed = %s", current,
189 operation_name(op),
190 agent_return_name(ret));
191 return false;
194 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
195 if (ret != FAILED) {
196 diag("Still needs recovery after step %u = %s",
197 current, agent_return_name(ret));
198 return false;
201 ret = external_agent_operation(agent, CHECK, "");
202 if (ret != SUCCESS) {
203 diag("Step %u check failed = %s", current,
204 agent_return_name(ret));
205 return false;
208 ret = external_agent_operation(agent, CLOSE, "");
209 if (ret != SUCCESS) {
210 diag("Step %u close failed = %s", current,
211 agent_return_name(ret));
212 return false;
215 /* Suppress logging as this tries to use closed fd. */
216 suppress_logging = true;
217 suppress_lockcheck = true;
218 tdb_close(tdb);
219 suppress_logging = false;
220 suppress_lockcheck = false;
221 target++;
222 current = 0;
223 free_all();
224 goto reset;
227 /* Put key for agent to fetch. */
228 key = tdb_mkdata(KEY_STRING, strlen(KEY_STRING));
229 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
230 return false;
232 /* This is the key we insert in transaction. */
233 key.dsize--;
235 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
236 if (ret != SUCCESS)
237 errx(1, "Agent failed to open: %s", agent_return_name(ret));
239 ret = external_agent_operation(agent, FETCH, KEY_STRING);
240 if (ret != SUCCESS)
241 errx(1, "Agent failed find key: %s", agent_return_name(ret));
243 in_transaction = true;
244 if (tdb_transaction_start(tdb) != 0)
245 return false;
247 if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
248 return false;
250 if (tdb_transaction_commit(tdb) != 0)
251 return false;
253 in_transaction = false;
255 /* We made it! */
256 diag("Completed %u runs", current);
257 tdb_close(tdb);
258 ret = external_agent_operation(agent, CLOSE, "");
259 if (ret != SUCCESS) {
260 diag("Step %u close failed = %s", current,
261 agent_return_name(ret));
262 return false;
265 ok1(needed_recovery);
266 ok1(locking_errors == 0);
267 ok1(forget_locking() == 0);
268 locking_errors = 0;
269 return true;
272 int main(int argc, char *argv[])
274 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
275 struct agent *agent;
276 int i, flags;
278 plan_tests(24);
279 unlock_callback = maybe_die;
281 external_agent_free = free_noleak;
282 agent = prepare_external_agent();
283 if (!agent)
284 err(1, "preparing agent");
286 for (flags = TDB_DEFAULT; flags <= TDB_VERSION1; flags += TDB_VERSION1) {
287 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
288 diag("Testing %s after death", operation_name(ops[i]));
289 ok1(test_death(ops[i], agent, flags));
293 free_external_agent(agent);
294 return exit_status();