Commit e8f0484b authored by Rusty Russell's avatar Rusty Russell

failtest: generic cleanup hooks

Each function in the history stores a cleanup function, rather than storing
extra structures.  In particular, we save writes and file offsets using
this.
parent 1605358b
This diff is collapsed.
......@@ -64,19 +64,21 @@ struct open_call {
const char *pathname;
int flags;
mode_t mode;
int dup_fd;
};
struct pipe_call {
int ret;
int fds[2];
bool closed[2];
};
struct read_call {
ssize_t ret;
off_t off;
int fd;
void *buf;
size_t count;
off_t off;
};
struct write_call {
......@@ -85,6 +87,10 @@ struct write_call {
const void *buf;
size_t count;
off_t off;
off_t old_filelen;
off_t saved_len;
void *saved_contents;
int dup_fd;
};
struct fcntl_call {
......@@ -121,6 +127,8 @@ struct failtest_call {
bool fail;
/* What we set errno to. */
int error;
/* How do we clean this up? */
void (*cleanup)(void *u);
/* The actual call data. */
union {
struct calloc_call calloc;
......
......@@ -17,6 +17,10 @@
#define realloc(ptr, size) \
failtest_realloc((ptr), (size), __FILE__, __LINE__)
#undef free
#define free(ptr) \
failtest_free(ptr)
/* Replacement of I/O. */
#include <sys/types.h>
#include <sys/stat.h>
......
......@@ -8,6 +8,7 @@ void *failtest_calloc(size_t nmemb, size_t size,
void *failtest_malloc(size_t size, const char *file, unsigned line);
void *failtest_realloc(void *ptr, size_t size,
const char *file, unsigned line);
void failtest_free(void *ptr);
int failtest_open(const char *pathname,
const char *file, unsigned line, ...);
int failtest_pipe(int pipefd[2], const char *file, unsigned line);
......
......@@ -45,8 +45,8 @@ int main(void)
realloc_call.ret = realloc(malloc_call.ret, 3);
realloc_call.ptr = malloc_call.ret;
realloc_call.size = 3;
call = add_history(FAILTEST_REALLOC, "run-history.c",
3, &realloc_call);
call = add_history(FAILTEST_REALLOC, "run-history.c", 3,
&realloc_call);
ok1(call->type == FAILTEST_REALLOC);
ok1(strcmp(call->file, "run-history.c") == 0);
ok1(call->line == 3);
......
......@@ -41,7 +41,7 @@ int main(void)
ok1(err == EACCES);
/* Clean up. */
close(fd);
failtest_close(fd);
close(pfd[0]);
close(pfd[1]);
......@@ -59,7 +59,7 @@ int main(void)
ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
ok1(strcmp(buf, "Hello world!") == 0);
/* Clean up. */
close(fd);
failtest_close(fd);
close(pfd[0]);
close(pfd[1]);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment