]> git.ozlabs.org Git - ccan/commitdiff
failtest: catch mmap.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 29 Nov 2011 22:42:11 +0000 (09:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 29 Nov 2011 22:42:11 +0000 (09:12 +1030)
mmap can also fail on out-of-memory, and for the coming change to the
way we save and restore files we want to know about them anyway.

ccan/failtest/failtest.c
ccan/failtest/failtest.h
ccan/failtest/failtest_override.h
ccan/failtest/failtest_proto.h
ccan/failtest/failtest_undo.h

index be5234f7a90453ac8ec313a287b3dd4594667431..1dad0864aa372fa3b7ead07a081f9e125f192bbc 100644 (file)
@@ -13,6 +13,7 @@
 #include <sys/wait.h>
 #include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/mman.h>
 #include <signal.h>
 #include <assert.h>
 #include <ccan/time/time.h>
@@ -96,7 +97,7 @@ static unsigned int lock_num = 0;
 
 static pid_t orig_pid;
 
-static const char info_to_arg[] = "mceoxprwf";
+static const char info_to_arg[] = "mceoxprwfa";
 
 /* Dummy call used for failtest_undo wrappers. */
 static struct failtest_call unrecorded_call;
@@ -860,6 +861,30 @@ int failtest_open(const char *pathname,
        return p->u.open.ret;
 }
 
+void *failtest_mmap(void *addr, size_t length, int prot, int flags,
+                   int fd, off_t offset, const char *file, unsigned line)
+{
+       struct failtest_call *p;
+       struct mmap_call call;
+
+       call.addr = addr;
+       call.length = length;
+       call.prot = prot;
+       call.flags = flags;
+       call.offset = offset;
+       call.fd = fd;
+
+       p = add_history(FAILTEST_MMAP, file, line, &call);
+       if (should_fail(p)) {
+               p->u.mmap.ret = MAP_FAILED;
+               p->error = ENOMEM;
+       } else {
+               p->u.mmap.ret = mmap(addr, length, prot, flags, fd, offset);
+       }
+       errno = p->error;
+       return p->u.mmap.ret;
+}
+
 static void cleanup_pipe(struct pipe_call *call)
 {
        if (!call->closed[0])
index f473f14f110f7b8095daafbe2f10a6a8ec7e2897..b304917bc6b0eece40039d6390f91c4020117811 100644 (file)
@@ -47,6 +47,7 @@ enum failtest_call_type {
        FAILTEST_READ,
        FAILTEST_WRITE,
        FAILTEST_FCNTL,
+       FAILTEST_MMAP,
 };
 
 struct calloc_call {
@@ -110,6 +111,16 @@ struct fcntl_call {
        } arg;
 };
 
+struct mmap_call {
+       void *ret;
+       void *addr;
+       size_t length;
+       int prot;
+       int flags;
+       int fd;
+       off_t offset;
+};
+
 /**
  * struct failtest_call - description of a call redirected to failtest module
  * @type: the call type
@@ -151,6 +162,7 @@ struct failtest_call {
                struct read_call read;
                struct write_call write;
                struct fcntl_call fcntl;
+               struct mmap_call mmap;
        } u;
 };
 
index eee45ff17682332720285763255ee2a57e0ca435..7178afd43424038f195a9b7da08252369f8a4873 100644 (file)
@@ -29,6 +29,7 @@
 /* Replacement of I/O. */
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/mman.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 #undef fcntl
 #define fcntl(fd, ...) failtest_fcntl((fd), __FILE__, __LINE__, __VA_ARGS__)
 
+#undef mmap
+#define mmap(addr, length, prot, flags, fd, offset)                    \
+       failtest_mmap((addr), (length), (prot), (flags), (fd), (offset), \
+                     __FILE__, __LINE__)
+
 /* Replacement of getpid (since failtest will fork). */
 #undef getpid
 #define getpid() failtest_getpid(__FILE__, __LINE__)
index d4967a3e51f8573d3136a9ed9f7ecd0c2f006d0a..58cdf5bd1d543cfd210e0c2b948bdc279d4331e7 100644 (file)
@@ -21,6 +21,8 @@ ssize_t failtest_pread(int fd, void *buf, size_t count, off_t offset,
                       const char *file, unsigned line);
 ssize_t failtest_pwrite(int fd, const void *buf, size_t count, off_t offset,
                        const char *file, unsigned line);
+void *failtest_mmap(void *addr, size_t length, int prot, int flags,
+                   int fd, off_t offset, const char *file, unsigned line);
 int failtest_close(int fd, const char *file, unsigned line);
 int failtest_fcntl(int fd, const char *file, unsigned line, int cmd, ...);
 pid_t failtest_getpid(const char *file, unsigned line);
index ff5e47b970730ee65559a1aff6be68712a175303..e42a992d93e273494df04a840e965de3bc11a9bd 100644 (file)
 #define write(fd, buf, count) \
        failtest_write((fd), (buf), (count), NULL, 0)
 
+#undef mmap
+#define mmap(addr, length, prot, flags, fd, offset) \
+       failtest_mmap((addr), (length), (prot), (flags), (fd), (offset), NULL, 0)
+
 #undef close
 #define close(fd) failtest_close(fd)