Browse Source

failtest: record close events

We trap them, might as well put them in history.  This also makes tracking
open file descriptors more robust.
Rusty Russell 15 years ago
parent
commit
c96ab569e7

+ 13 - 2
ccan/failtest/failtest.c

@@ -56,7 +56,7 @@ static pid_t lock_owner;
 static struct lock_info *locks = NULL;
 static struct lock_info *locks = NULL;
 static unsigned int lock_num = 0;
 static unsigned int lock_num = 0;
 
 
-static const char info_to_arg[] = "mceoprwf";
+static const char info_to_arg[] = "mceoxprwf";
 
 
 /* Dummy call used for failtest_undo wrappers. */
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
 static struct failtest_call unrecorded_call;
@@ -825,9 +825,20 @@ add_lock(struct lock_info *locks, int fd, off_t start, off_t end, int type)
 }
 }
 
 
 /* We trap this so we can record it: we don't fail it. */
 /* We trap this so we can record it: we don't fail it. */
-int failtest_close(int fd)
+int failtest_close(int fd, const char *file, unsigned line)
 {
 {
 	int i;
 	int i;
+	struct close_call call;
+	struct failtest_call *p;
+
+	call.fd = fd;
+	p = add_history(FAILTEST_CLOSE, file, line, &call);
+	p->fail = false;
+
+	/* Consume close from failpath. */
+	if (failpath)
+		if (should_fail(p))
+			abort();
 
 
 	if (fd < 0)
 	if (fd < 0)
 		return close(fd);
 		return close(fd);

+ 6 - 0
ccan/failtest/failtest.h

@@ -36,6 +36,7 @@ enum failtest_call_type {
 	FAILTEST_CALLOC,
 	FAILTEST_CALLOC,
 	FAILTEST_REALLOC,
 	FAILTEST_REALLOC,
 	FAILTEST_OPEN,
 	FAILTEST_OPEN,
+	FAILTEST_CLOSE,
 	FAILTEST_PIPE,
 	FAILTEST_PIPE,
 	FAILTEST_READ,
 	FAILTEST_READ,
 	FAILTEST_WRITE,
 	FAILTEST_WRITE,
@@ -66,6 +67,10 @@ struct open_call {
 	mode_t mode;
 	mode_t mode;
 };
 };
 
 
+struct close_call {
+	int fd;
+};
+
 struct pipe_call {
 struct pipe_call {
 	int ret;
 	int ret;
 	int fds[2];
 	int fds[2];
@@ -130,6 +135,7 @@ struct failtest_call {
 		struct malloc_call malloc;
 		struct malloc_call malloc;
 		struct realloc_call realloc;
 		struct realloc_call realloc;
 		struct open_call open;
 		struct open_call open;
+		struct close_call close;
 		struct pipe_call pipe;
 		struct pipe_call pipe;
 		struct read_call read;
 		struct read_call read;
 		struct write_call write;
 		struct write_call write;

+ 1 - 1
ccan/failtest/failtest_override.h

@@ -52,7 +52,7 @@
 	failtest_pwrite((fd), (buf), (count), (off), __FILE__, __LINE__)
 	failtest_pwrite((fd), (buf), (count), (off), __FILE__, __LINE__)
 
 
 #undef close
 #undef close
-#define close(fd) failtest_close(fd)
+#define close(fd) failtest_close(fd, __FILE__, __LINE__)
 
 
 #undef fcntl
 #undef fcntl
 #define fcntl(fd, ...) failtest_fcntl((fd), __FILE__, __LINE__, __VA_ARGS__)
 #define fcntl(fd, ...) failtest_fcntl((fd), __FILE__, __LINE__, __VA_ARGS__)

+ 1 - 1
ccan/failtest/failtest_proto.h

@@ -20,6 +20,6 @@ ssize_t failtest_pread(int fd, void *buf, size_t count, off_t offset,
 		       const char *file, unsigned line);
 		       const char *file, unsigned line);
 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
 			const char *file, unsigned line);
 			const char *file, unsigned line);
-int failtest_close(int fd);
+int failtest_close(int fd, const char *file, unsigned line);
 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...);
 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...);
 #endif /* CCAN_FAILTEST_PROTO_H */
 #endif /* CCAN_FAILTEST_PROTO_H */

+ 2 - 2
ccan/failtest/test/run-open.c

@@ -41,7 +41,7 @@ int main(void)
 	ok1(err == EACCES);
 	ok1(err == EACCES);
 
 
 	/* Clean up. */
 	/* Clean up. */
-	failtest_close(fd);
+	failtest_close(fd, "run-open.c", 1);
 	close(pfd[0]);
 	close(pfd[0]);
 	close(pfd[1]);
 	close(pfd[1]);
 
 
@@ -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. */
-	failtest_close(fd);
+	failtest_close(fd, "run-open.c", 1);
 	close(pfd[0]);
 	close(pfd[0]);
 	close(pfd[1]);
 	close(pfd[1]);