|
14 | 14 | #include <fcntl.h> |
15 | 15 | #include <stdio.h> |
16 | 16 | #include <errno.h> |
| 17 | +#include <assert.h> |
17 | 18 |
|
18 | 19 | static int tests = 0, fails = 0; |
19 | 20 |
|
@@ -81,6 +82,34 @@ static int check_mapping(void *addr, size_t size, unsigned flags, int fd, |
81 | 82 | return 0; |
82 | 83 | } |
83 | 84 |
|
| 85 | +static int test_mmap_with_file_removed() |
| 86 | +{ |
| 87 | + char file_template[30]; |
| 88 | + snprintf(file_template, sizeof(file_template) / sizeof(char), "%s/mmap-file-deleted.XXXXXX", "/tmp"); |
| 89 | + |
| 90 | + auto fd1 = mkstemp(file_template); |
| 91 | + assert(fd1 != -1); |
| 92 | + |
| 93 | + assert(unlink(file_template) >= 0); |
| 94 | + size_t buf_size = 131072; |
| 95 | + assert(ftruncate(fd1, buf_size) >= 0); |
| 96 | + |
| 97 | + auto buf = (char *) mmap(NULL, buf_size, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd1, 0); |
| 98 | + assert(buf != MAP_FAILED); |
| 99 | + |
| 100 | + auto fd2 = open(file_template, O_RDWR); |
| 101 | + close(fd2); |
| 102 | + |
| 103 | + auto urandom = fopen("/dev/urandom", "rb"); |
| 104 | + assert(urandom != NULL); |
| 105 | + assert(fread(buf, 1, buf_size, urandom) == buf_size); |
| 106 | + |
| 107 | + close(fd1); |
| 108 | + report(munmap(buf,buf_size) == 0, "test_mmap_with_file_removed succeeded"); |
| 109 | + |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
84 | 113 | int main(int argc, char *argv[]) |
85 | 114 | { |
86 | 115 | auto fd = open("/tmp/mmap-file-test", O_CREAT|O_TRUNC|O_RDWR, 0666); |
@@ -143,6 +172,8 @@ int main(int argc, char *argv[]) |
143 | 172 | report(munmap(b, 4096) == 0, "munmap temporary mapping"); |
144 | 173 | report(close(fd) == 0, "close again"); |
145 | 174 |
|
| 175 | + test_mmap_with_file_removed(); |
| 176 | + |
146 | 177 | // TODO: map an append-only file with prot asking for PROT_WRITE, mmap should return EACCES. |
147 | 178 | // TODO: map a file under a fs mounted with the flag NO_EXEC and prot asked for PROT_EXEC (expect EPERM). |
148 | 179 |
|
|
0 commit comments