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 * Implementation performs mapping operations over whole pages.
10 * Thus, while the argument len
11 * need not meet a size or alignment constraint,
12 * the implementation shall include, in any mapping
13 * operation, any partial page specified by the range [pa,pa+len).
14 * The system shall always zero-fill any partial page at the end of an object.
15 * Further, the system shall never write out any modified portions of
16 * the last page of an object which are beyond its end.
17 *
18 * Test Steps:
19 * 1. Create a process, in this process:
20 * a. map a file with size of 1/2 * page_size,
21 * set len = 1/2 * page_size
22 * b. Read the partial page beyond the object size.
23 * Make sure the partial page is zero-filled;
24 * c. Modify a byte in the partial page, then un-map the and close the
25 * file descriptor.
26 * 2. Wait for the child proces to exit, then
27 * Map the file again,
28 * read the byte from the position modified at step 1-c and check.
29 */
30
31
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/mman.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/wait.h>
39 #include <fcntl.h>
40 #include <string.h>
41 #include <errno.h>
42 #include <limits.h>
43 #ifdef __linux__
44 #include <sys/vfs.h>
45 #endif
46
47 #include "posixtest.h"
48 #include "tempfile.h"
49
50 #define TYPE_TMPFS_MAGIC 0x01021994
51
main(void)52 int main(void)
53 {
54 char tmpfname[PATH_MAX];
55 long page_size;
56
57 char *pa;
58 size_t len;
59 int fd;
60
61 pid_t child;
62 int i, exit_val;
63
64 page_size = sysconf(_SC_PAGE_SIZE);
65
66 /* mmap will create a partial page */
67 len = page_size / 2;
68
69 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_11_4");
70 #ifdef __linux__
71 struct statfs buf;
72
73 if (statfs(pts_get_tmpdir(), &buf)) {
74 printf("Error at statfs(): %s\n", strerror(errno));
75 return PTS_UNRESOLVED;
76 }
77
78 if (buf.f_type == TYPE_TMPFS_MAGIC) {
79 printf("From mmap(2) manpage, skip known bug on tmpfs\n");
80 return PTS_UNTESTED;
81 }
82 #endif
83
84 child = fork();
85 switch (child) {
86 case 0:
87 /* Create shared object */
88 unlink(tmpfname);
89 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
90 S_IRUSR | S_IWUSR);
91 if (fd == -1) {
92 printf("Error at open(): %s\n", strerror(errno));
93 return PTS_UNRESOLVED;
94 }
95 if (ftruncate(fd, len) == -1) {
96 printf("Error at ftruncate(): %s\n", strerror(errno));
97 return PTS_UNRESOLVED;
98 }
99
100 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
101 if (pa == MAP_FAILED) {
102 printf("Error at mmap(): %s\n", strerror(errno));
103 return PTS_FAIL;
104 }
105
106 /* Check the patial page is ZERO filled */
107 for (i = len; i < page_size; i++)
108 if (pa[i] != 0) {
109 printf("Test FAILED: The partial page at the "
110 "end of an object is not zero-filled\n");
111 return PTS_FAIL;
112 }
113
114 /* Write the partial page */
115 pa[len + 1] = 'b';
116 msync(pa, len, MS_SYNC);
117 munmap(pa, len);
118 close(fd);
119 return PTS_PASS;
120 case -1:
121 printf("Error at fork(): %s\n", strerror(errno));
122 return PTS_UNRESOLVED;
123 default:
124 break;
125 }
126
127 wait(&exit_val);
128 if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
129 unlink(tmpfname);
130
131 if (WIFEXITED(exit_val))
132 return WEXITSTATUS(exit_val);
133
134 printf("Child exited abnormally\n");
135 return PTS_UNRESOLVED;
136 }
137
138 fd = open(tmpfname, O_RDWR, 0);
139 unlink(tmpfname);
140
141 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
142 if (pa == MAP_FAILED) {
143 printf("Error at 2nd mmap(): %s\n", strerror(errno));
144 return PTS_FAIL;
145 }
146
147 if (pa[len + 1] == 'b') {
148 printf("Test FAILED: Modification of the partial page "
149 "at the end of an object is written out\n");
150 return PTS_FAIL;
151 }
152 close(fd);
153 munmap(pa, len);
154
155 printf("Test PASSED\n");
156 return PTS_PASS;
157 }
158