• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
4  *
5  * This file is licensed under the GPL license.  For the full content
6  * of this license, see the COPYING file at the top level of this
7  * source tree.
8  *
9  * The mapping established by mmap() shall replace any previous
10  * mappings for those whole pages containing any part of the address
11  * space of the process starting at pa and continuing for len bytes.
12  *
13  * Test Steps:
14  * 1. Set the size of the file to be mapped as (2 * _SC_PAGE_SIZE);
15  * 2. Map size = (_SC_PAGE_SIZE + 2) bytes into memory,
16  *    setting the content as 'a'. The mapped address is pa.
17  * 2. Map size2 = (_SC_PAGE_SIZE + 1) bytes into memory, starting at the same
18  *    address as the first mmap, i.e. pa, using MAP_FIXED flag.
19  *    Setting the cotent as 'b'
20  * 3. Test whether byte *(pa + size) is 'b'.
21  */
22 
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 <fcntl.h>
30 #include <string.h>
31 #include <errno.h>
32 #include "posixtest.h"
33 
34 #ifdef MAP_FIXED
main(void)35 int main(void)
36 {
37 	char tmpfname[256];
38 	char tmpfname2[256];
39 	char *data;
40 	long total_size;
41 	long page_size;
42 
43 	void *pa;
44 	size_t size;
45 	int fd;
46 
47 	void *pa2;
48 	size_t size2;
49 	int fd2;
50 
51 	char *ch;
52 
53 	page_size = sysconf(_SC_PAGE_SIZE);
54 	size = page_size + 2;
55 	size2 = page_size + 1;
56 
57 	/* Size of the file */
58 	total_size = 2 * page_size;
59 
60 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_3_1_%d_1",
61 		 getpid());
62 	snprintf(tmpfname2, sizeof(tmpfname2), "/tmp/pts_mmap_3_1_%d_2",
63 		 getpid());
64 
65 	unlink(tmpfname);
66 	unlink(tmpfname2);
67 
68 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
69 	fd2 = open(tmpfname2, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
70 	if (fd == -1 || fd2 == -1) {
71 		printf("Error at open(): %s\n", strerror(errno));
72 		return PTS_UNRESOLVED;
73 	}
74 
75 	unlink(tmpfname);
76 	unlink(tmpfname2);
77 
78 	data = malloc(total_size);
79 
80 	memset(data, 'a', total_size);
81 	if (write(fd, data, total_size) != total_size) {
82 		printf("Error at write(), fd: %s\n", strerror(errno));
83 		return PTS_UNRESOLVED;
84 	}
85 
86 	memset(data, 'b', total_size);
87 	if (write(fd2, data, total_size) != total_size) {
88 		printf("Error at write(), fd1: %s\n", strerror(errno));
89 		return PTS_UNRESOLVED;
90 	}
91 
92 	free(data);
93 
94 	/* Map first file */
95 	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
96 	if (pa == MAP_FAILED) {
97 		printf("Error at mmap: %s\n", strerror(errno));
98 		return PTS_FAIL;
99 	}
100 
101 	ch = pa + size;
102 	if (*ch != 'a') {
103 		printf("Test Fail: The file is not mapped correctly\n");
104 		return PTS_FAIL;
105 	}
106 
107 	/* Replace orginal mapping */
108 	pa2 =
109 	    mmap(pa, size2, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, fd2,
110 		 0);
111 	if (pa2 == MAP_FAILED) {
112 		printf("Error at 2nd mmap: %s\n", strerror(errno));
113 		return PTS_FAIL;
114 	}
115 
116 	if (pa2 != pa) {
117 		printf("Error at mmap, the second mmap does not replaced the"
118 		       " first mapping\n");
119 		return PTS_FAIL;
120 	}
121 
122 	ch = pa2 + size;
123 	if (*ch != 'b') {
124 		printf("The original mapped page has not been replaced\n");
125 		return PTS_FAIL;
126 	}
127 
128 	close(fd);
129 	close(fd2);
130 	munmap(pa, size);
131 	munmap(pa2, size2);
132 	printf("Test PASSED\n");
133 	return PTS_PASS;
134 }
135 #else
main(void)136 int main(void)
137 {
138 	printf("MAP_FIXED was not defined at the time of compilation\n");
139 	return PTS_UNRESOLVED;
140 }
141 #endif /* MAP_FIXED */
142