Skip to content

Commit 6e07f8b

Browse files
committed
Add mmap_write.
1 parent 3830ce9 commit 6e07f8b

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
mmap_read: mmap_read.c
22
gcc -o mmap_read mmap_read.c -I.
33

4+
mmap_write: mmap_write.c
5+
gcc -o mmap_write mmap_write.c -I.
6+
47
pread: pread.c
58
gcc -o pread pread.c -I.
69

710
pwrite: pwrite.c
811
gcc -o pwrite pwrite.c -I.
912

10-
all: pread pwrite mmap_read
13+
all: pread pwrite mmap_read mmap_write

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def read_corruption_config():
174174

175175
def setup_and_run_test(config, results_writer, do_corruption):
176176
error_block = (config['offset'], 1) #TODO(Wesley) multi-section errors
177-
test_commands = ['./mmap_read', './pread', './pwrite']
177+
test_commands = ['./mmap_read', './mmap_write', './pread', './pwrite']
178178
for command in test_commands:
179179
tmp_image_path = make_tmpfile(config['image'], config['md5sum'])
180180

mmap_write.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <errno.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <stdint.h>
6+
#include <sys/mman.h>
7+
#include <sys/types.h>
8+
#include <fcntl.h>
9+
10+
#define SIZE 445
11+
12+
int main(int argc, char *argv[])
13+
{
14+
char *memblock;
15+
int fd;
16+
17+
if (argc != 2) {
18+
printf("Expected 1 argument (filename), found %d\n", argc-1);
19+
return 1;
20+
}
21+
22+
fd = open(argv[1], O_RDONLY);
23+
if (fd < 0) {
24+
printf("open fail %s\n", strerror(errno));
25+
return fd;
26+
}
27+
28+
memblock = mmap(NULL, SIZE, PROT_WRITE, MAP_PRIVATE, fd, 0);
29+
if (memblock == MAP_FAILED) {
30+
perror("mmap");
31+
return 2;
32+
}
33+
34+
memblock[0] = '\0';
35+
// Without use of this call there is no guarantee that changes are
36+
// written back before munmap(2) is called
37+
if (msync(memblock, 1, MS_SYNC)) {
38+
perror("msync");
39+
return 3;
40+
}
41+
42+
if (munmap(memblock, SIZE)) {
43+
perror("munmap");
44+
return 4;
45+
}
46+
return 0;
47+
}

0 commit comments

Comments
 (0)