• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * This file is licensed under the GPL license.  For the full content
4  * of this license, see the COPYING file at the top level of this
5  * source tree.
6  *
7  *
8  * If a mapping to be removed was private, any modifications
9  * made in this address range shall be discarded.
10  *
11  * Test Step:
12  * 1. mmap a file into memory. Set flag as MAP_PRIVATE;
13  * 2. Modify the mapped memory, and call msync to try to synchronize the change with
14  * 	  the file;
15  * 3. munmap the mapped memory;
16  * 4. mmap the same file again into memory;
17  * 5. If the modification in step 2 appears in the mapped memory, then fail,
18  *    otherwise pass.
19  */
20 
21 
22 #include <pthread.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/wait.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <errno.h>
33 
34 #include "posixtest.h"
35 #include "tempfile.h"
36 
37 #define TNAME "munmap/4-1.c"
38 
main(void)39 int main(void)
40 {
41 	int rc;
42 
43 	char tmpfname[PATH_MAX];
44 	char *data;
45 	int total_size = 1024;
46 
47 	void *pa = NULL;
48 	void *addr = NULL;
49 	size_t size = total_size;
50 	int flag;
51 	int fd;
52 	off_t off = 0;
53 	int prot;
54 
55 	char *ch;
56 
57 	PTS_GET_TMP_FILENAME(tmpfname, "pts_munmap_4_1");
58 	unlink(tmpfname);
59 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
60 	if (fd == -1) {
61 		printf(TNAME " Error at open(): %s\n", strerror(errno));
62 		exit(PTS_UNRESOLVED);
63 	}
64 	unlink(tmpfname);
65 
66 	data = malloc(total_size);
67 	memset(data, 'a', total_size);
68 	if (write(fd, data, total_size) != total_size) {
69 		printf(TNAME "Error at write(): %s\n", strerror(errno));
70 		exit(PTS_UNRESOLVED);
71 	}
72 	free(data);
73 
74 	prot = PROT_READ | PROT_WRITE;
75 	flag = MAP_PRIVATE;
76 	pa = mmap(addr, size, prot, flag, fd, off);
77 	if (pa == MAP_FAILED) {
78 		printf("Test Fail: " TNAME " Error at mmap: %s\n",
79 		       strerror(errno));
80 		exit(PTS_FAIL);
81 	}
82 
83 	ch = pa;
84 	*ch = 'b';
85 
86 	/* Flush changes back to the file */
87 
88 	if ((rc = msync(pa, size, MS_SYNC)) != 0) {
89 		printf(TNAME " Error at msync(): %s\n", strerror(rc));
90 		exit(PTS_UNRESOLVED);
91 	}
92 
93 	munmap(pa, size);
94 
95 	/* Mmap again */
96 
97 	pa = mmap(addr, size, prot, flag, fd, off);
98 	if (pa == MAP_FAILED) {
99 		printf("Test Fail: " TNAME " Error at 2nd mmap: %s\n",
100 		       strerror(errno));
101 		exit(PTS_FAIL);
102 	}
103 
104 	ch = pa;
105 	if (*ch == 'b') {
106 		printf("Test FAIL\n");
107 		exit(PTS_FAIL);
108 	}
109 
110 	close(fd);
111 	printf("Write referece is discarded when setting MAP_RPIVATE\n");
112 	printf("Test PASSED\n");
113 	return PTS_PASS;
114 }
115