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 { ...@@ -64,19 +64,21 @@ struct open_call {
const char *pathname; const char *pathname;
int flags; int flags;
mode_t mode; mode_t mode;
int dup_fd;
}; };
struct pipe_call { struct pipe_call {
int ret; int ret;
int fds[2]; int fds[2];
bool closed[2];
}; };
struct read_call { struct read_call {
ssize_t ret; ssize_t ret;
off_t off;
int fd; int fd;
void *buf; void *buf;
size_t count; size_t count;
off_t off;
}; };
struct write_call { struct write_call {
...@@ -85,6 +87,10 @@ struct write_call { ...@@ -85,6 +87,10 @@ struct write_call {
const void *buf; const void *buf;
size_t count; size_t count;
off_t off; off_t off;
off_t old_filelen;
off_t saved_len;
void *saved_contents;
int dup_fd;
}; };
struct fcntl_call { struct fcntl_call {
...@@ -121,6 +127,8 @@ struct failtest_call { ...@@ -121,6 +127,8 @@ struct failtest_call {
bool fail; bool fail;
/* What we set errno to. */ /* What we set errno to. */
int error; int error;
/* How do we clean this up? */
void (*cleanup)(void *u);
/* The actual call data. */ /* The actual call data. */
union { union {
struct calloc_call calloc; struct calloc_call calloc;
......
...@@ -17,6 +17,10 @@ ...@@ -17,6 +17,10 @@
#define realloc(ptr, size) \ #define realloc(ptr, size) \
failtest_realloc((ptr), (size), __FILE__, __LINE__) failtest_realloc((ptr), (size), __FILE__, __LINE__)
#undef free
#define free(ptr) \
failtest_free(ptr)
/* Replacement of I/O. */ /* Replacement of I/O. */
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
......
...@@ -8,6 +8,7 @@ void *failtest_calloc(size_t nmemb, size_t size, ...@@ -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_malloc(size_t size, const char *file, unsigned line);
void *failtest_realloc(void *ptr, size_t size, void *failtest_realloc(void *ptr, size_t size,
const char *file, unsigned line); const char *file, unsigned line);
void failtest_free(void *ptr);
int failtest_open(const char *pathname, int failtest_open(const char *pathname,
const char *file, unsigned line, ...); const char *file, unsigned line, ...);
int failtest_pipe(int pipefd[2], const char *file, unsigned line); int failtest_pipe(int pipefd[2], const char *file, unsigned line);
......
...@@ -45,8 +45,8 @@ int main(void) ...@@ -45,8 +45,8 @@ int main(void)
realloc_call.ret = realloc(malloc_call.ret, 3); realloc_call.ret = realloc(malloc_call.ret, 3);
realloc_call.ptr = malloc_call.ret; realloc_call.ptr = malloc_call.ret;
realloc_call.size = 3; realloc_call.size = 3;
call = add_history(FAILTEST_REALLOC, "run-history.c", call = add_history(FAILTEST_REALLOC, "run-history.c", 3,
3, &realloc_call); &realloc_call);
ok1(call->type == FAILTEST_REALLOC); ok1(call->type == FAILTEST_REALLOC);
ok1(strcmp(call->file, "run-history.c") == 0); ok1(strcmp(call->file, "run-history.c") == 0);
ok1(call->line == 3); ok1(call->line == 3);
......
...@@ -41,7 +41,7 @@ int main(void) ...@@ -41,7 +41,7 @@ int main(void)
ok1(err == EACCES); ok1(err == EACCES);
/* Clean up. */ /* Clean up. */
close(fd); failtest_close(fd);
close(pfd[0]); close(pfd[0]);
close(pfd[1]); close(pfd[1]);
...@@ -59,7 +59,7 @@ int main(void) ...@@ -59,7 +59,7 @@ int main(void)
ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!")); ok1(read(fd, buf, strlen("Hello world!")) == strlen("Hello world!"));
ok1(strcmp(buf, "Hello world!") == 0); ok1(strcmp(buf, "Hello world!") == 0);
/* Clean up. */ /* Clean up. */
close(fd); failtest_close(fd);
close(pfd[0]); close(pfd[0]);
close(pfd[1]); 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