3 #include "lock-tracking.h"
4 #include "tap-interface.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
)
26 for (i
= 0; i
< MAX_ALLOCATIONS
; i
++)
28 allocated
[i
] = malloc(len
);
31 diag("max_alloc: %i", max_alloc
);
35 diag("Too many allocations!");
39 static void *realloc_noleak(void *p
, size_t size
)
43 for (i
= 0; i
< MAX_ALLOCATIONS
; i
++) {
44 if (allocated
[i
] == p
) {
47 diag("max_alloc: %i", max_alloc
);
49 return allocated
[i
] = realloc(p
, size
);
52 diag("Untracked realloc!");
56 static void free_noleak(void *p
)
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
) {
70 static void free_all(void)
74 for (i
= 0; i
< MAX_ALLOCATIONS
; i
++) {
80 #define malloc malloc_noleak
81 #define free free_noleak
82 #define realloc realloc_noleak
84 #include "tdb2-source.h"
97 #include "external-agent.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
) {
113 static ssize_t
pwrite_check(int fd
,
114 const void *buf
, size_t count
, off_t offset
)
120 ret
= pwrite(fd
, buf
, count
, offset
);
128 static ssize_t
write_check(int fd
, const void *buf
, size_t count
)
134 ret
= write(fd
, buf
, count
);
142 static int ftruncate_check(int fd
, off_t length
)
148 ret
= ftruncate(fd
, length
);
154 static bool test_death(enum operation op
, struct agent
*agent
, int flags
)
156 struct tdb_context
*tdb
= NULL
;
158 enum agent_return ret
;
159 int needed_recovery
= 0;
161 current
= target
= 0;
164 tdb
= tdb_open(TEST_DBNAME
, flags
|TDB_NOMMAP
,
165 O_CREAT
|O_TRUNC
|O_RDWR
, 0600, &tap_log_attr
);
167 diag("Failed opening TDB: %s", strerror(errno
));
171 if (setjmp(jmpbuf
) != 0) {
172 /* We're partway through. Simulate our death. */
173 close(tdb
->file
->fd
);
175 in_transaction
= false;
177 ret
= external_agent_operation(agent
, NEEDS_RECOVERY
, "");
180 else if (ret
!= FAILED
) {
181 diag("Step %u agent NEEDS_RECOVERY = %s", current
,
182 agent_return_name(ret
));
186 ret
= external_agent_operation(agent
, op
, KEY_STRING
);
187 if (ret
!= SUCCESS
) {
188 diag("Step %u op %s failed = %s", current
,
190 agent_return_name(ret
));
194 ret
= external_agent_operation(agent
, NEEDS_RECOVERY
, "");
196 diag("Still needs recovery after step %u = %s",
197 current
, agent_return_name(ret
));
201 ret
= external_agent_operation(agent
, CHECK
, "");
202 if (ret
!= SUCCESS
) {
203 diag("Step %u check failed = %s", current
,
204 agent_return_name(ret
));
208 ret
= external_agent_operation(agent
, CLOSE
, "");
209 if (ret
!= SUCCESS
) {
210 diag("Step %u close failed = %s", current
,
211 agent_return_name(ret
));
215 /* Suppress logging as this tries to use closed fd. */
216 suppress_logging
= true;
217 suppress_lockcheck
= true;
219 suppress_logging
= false;
220 suppress_lockcheck
= false;
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)
232 /* This is the key we insert in transaction. */
235 ret
= external_agent_operation(agent
, OPEN
, TEST_DBNAME
);
237 errx(1, "Agent failed to open: %s", agent_return_name(ret
));
239 ret
= external_agent_operation(agent
, FETCH
, KEY_STRING
);
241 errx(1, "Agent failed find key: %s", agent_return_name(ret
));
243 in_transaction
= true;
244 if (tdb_transaction_start(tdb
) != 0)
247 if (tdb_store(tdb
, key
, key
, TDB_INSERT
) != 0)
250 if (tdb_transaction_commit(tdb
) != 0)
253 in_transaction
= false;
256 diag("Completed %u runs", current
);
258 ret
= external_agent_operation(agent
, CLOSE
, "");
259 if (ret
!= SUCCESS
) {
260 diag("Step %u close failed = %s", current
,
261 agent_return_name(ret
));
265 ok1(needed_recovery
);
266 ok1(locking_errors
== 0);
267 ok1(forget_locking() == 0);
272 int main(int argc
, char *argv
[])
274 enum operation ops
[] = { FETCH
, STORE
, TRANSACTION_START
};
279 unlock_callback
= maybe_die
;
281 external_agent_free
= free_noleak
;
282 agent
= prepare_external_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();