Raise the level of a debug.
[Samba.git] / lib / ntdb / test / run-57-die-during-transaction.c
blob32f781e4a89585f63765f65e51adf8e4b0af0fb5
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(x) free_noleak(x)
82 #define realloc realloc_noleak
84 #include "ntdb-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 <ccan/err/err.h>
97 #include <setjmp.h>
98 #include "external-agent.h"
99 #include "logging.h"
101 static bool in_transaction;
102 static int target, current;
103 static jmp_buf jmpbuf;
104 #define TEST_DBNAME "run-57-die-during-transaction.ntdb"
105 #define KEY_STRING "helloworld"
106 #define DATA_STRING "Helloworld"
108 static void maybe_die(int fd)
110 if (in_transaction && current++ == target) {
111 longjmp(jmpbuf, 1);
115 static ssize_t pwrite_check(int fd,
116 const void *buf, size_t count, off_t offset)
118 ssize_t ret;
120 maybe_die(fd);
122 ret = pwrite(fd, buf, count, offset);
123 if (ret != count)
124 return ret;
126 maybe_die(fd);
127 return ret;
130 static ssize_t write_check(int fd, const void *buf, size_t count)
132 ssize_t ret;
134 maybe_die(fd);
136 ret = write(fd, buf, count);
137 if (ret != count)
138 return ret;
140 maybe_die(fd);
141 return ret;
144 static int ftruncate_check(int fd, off_t length)
146 int ret;
148 maybe_die(fd);
150 ret = ftruncate(fd, length);
152 maybe_die(fd);
153 return ret;
156 static bool test_death(enum operation op, struct agent *agent,
157 bool pre_create_recovery)
159 struct ntdb_context *ntdb = NULL;
160 NTDB_DATA key, data;
161 enum agent_return ret;
162 int needed_recovery = 0;
164 current = target = 0;
165 /* Big long data to force a change. */
166 data = ntdb_mkdata(DATA_STRING, strlen(DATA_STRING));
168 reset:
169 unlink(TEST_DBNAME);
170 ntdb = ntdb_open(TEST_DBNAME, NTDB_NOMMAP|MAYBE_NOSYNC,
171 O_CREAT|O_TRUNC|O_RDWR, 0600, &tap_log_attr);
172 if (!ntdb) {
173 diag("Failed opening NTDB: %s", strerror(errno));
174 return false;
177 if (setjmp(jmpbuf) != 0) {
178 /* We're partway through. Simulate our death. */
179 close(ntdb->file->fd);
180 forget_locking();
181 in_transaction = false;
183 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
184 if (ret == SUCCESS)
185 needed_recovery++;
186 else if (ret != FAILED) {
187 diag("Step %u agent NEEDS_RECOVERY = %s", current,
188 agent_return_name(ret));
189 return false;
192 /* Could be key, or data. */
193 ret = external_agent_operation(agent, op,
194 KEY_STRING "=" KEY_STRING);
195 if (ret != SUCCESS) {
196 ret = external_agent_operation(agent, op,
197 KEY_STRING
198 "=" DATA_STRING);
200 if (ret != SUCCESS) {
201 diag("Step %u op %s failed = %s", current,
202 operation_name(op),
203 agent_return_name(ret));
204 return false;
207 ret = external_agent_operation(agent, NEEDS_RECOVERY, "");
208 if (ret != FAILED) {
209 diag("Still needs recovery after step %u = %s",
210 current, agent_return_name(ret));
211 return false;
214 ret = external_agent_operation(agent, CHECK, "");
215 if (ret != SUCCESS) {
216 diag("Step %u check failed = %s", current,
217 agent_return_name(ret));
218 return false;
221 ret = external_agent_operation(agent, CLOSE, "");
222 if (ret != SUCCESS) {
223 diag("Step %u close failed = %s", current,
224 agent_return_name(ret));
225 return false;
228 /* Suppress logging as this tries to use closed fd. */
229 suppress_logging = true;
230 suppress_lockcheck = true;
231 ntdb_close(ntdb);
232 suppress_logging = false;
233 suppress_lockcheck = false;
234 target++;
235 current = 0;
236 free_all();
237 goto reset;
240 /* Put key for agent to fetch. */
241 key = ntdb_mkdata(KEY_STRING, strlen(KEY_STRING));
243 if (pre_create_recovery) {
244 /* Using a transaction now means we allocate the recovery
245 * area immediately. That makes the later transaction smaller
246 * and thus tickles a bug we had. */
247 if (ntdb_transaction_start(ntdb) != 0)
248 return false;
250 if (ntdb_store(ntdb, key, key, NTDB_INSERT) != 0)
251 return false;
252 if (pre_create_recovery) {
253 if (ntdb_transaction_commit(ntdb) != 0)
254 return false;
257 /* This is the key we insert in transaction. */
258 key.dsize--;
260 ret = external_agent_operation(agent, OPEN, TEST_DBNAME);
261 if (ret != SUCCESS)
262 errx(1, "Agent failed to open: %s", agent_return_name(ret));
264 ret = external_agent_operation(agent, FETCH, KEY_STRING "=" KEY_STRING);
265 if (ret != SUCCESS)
266 errx(1, "Agent failed find key: %s", agent_return_name(ret));
268 in_transaction = true;
269 if (ntdb_transaction_start(ntdb) != 0)
270 return false;
272 if (ntdb_store(ntdb, key, data, NTDB_INSERT) != 0)
273 return false;
275 if (ntdb_transaction_commit(ntdb) != 0)
276 return false;
278 in_transaction = false;
280 /* We made it! */
281 diag("Completed %u runs", current);
282 ntdb_close(ntdb);
283 ret = external_agent_operation(agent, CLOSE, "");
284 if (ret != SUCCESS) {
285 diag("Step %u close failed = %s", current,
286 agent_return_name(ret));
287 return false;
290 ok1(needed_recovery);
291 ok1(locking_errors == 0);
292 ok1(forget_locking() == 0);
293 locking_errors = 0;
294 return true;
297 int main(int argc, char *argv[])
299 enum operation ops[] = { FETCH, STORE, TRANSACTION_START };
300 struct agent *agent;
301 int i, j;
303 plan_tests(24);
304 unlock_callback = maybe_die;
306 external_agent_free = free_noleak;
307 agent = prepare_external_agent();
308 if (!agent)
309 err(1, "preparing agent");
311 for (j = 0; j < 2; j++) {
312 for (i = 0; i < sizeof(ops)/sizeof(ops[0]); i++) {
313 diag("Testing %s after death (%s recovery area)",
314 operation_name(ops[i]), j ? "with" : "without");
315 ok1(test_death(ops[i], agent, j));
319 free_external_agent(agent);
320 return exit_status();