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 read the file using read()
27 * and check that change wasn't written.
28 */
29
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <sys/mman.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <sys/wait.h>
38 #include <fcntl.h>
39 #include <string.h>
40 #include <errno.h>
41
42 #include "posixtest.h"
43 #include "tempfile.h"
44
main(void)45 int main(void)
46 {
47 char tmpfname[PATH_MAX];
48 long page_size;
49
50 char *pa, ch;
51 ssize_t len;
52 int fd;
53
54 pid_t child;
55 int i, exit_val, ret, size;
56
57 page_size = sysconf(_SC_PAGE_SIZE);
58
59 /* mmap will create a partial page */
60 len = page_size / 2;
61
62 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_11_6");
63 child = fork();
64 switch (child) {
65 case 0:
66 /* Create shared object */
67 unlink(tmpfname);
68 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL,
69 S_IRUSR | S_IWUSR);
70 if (fd == -1) {
71 printf("Error at open(): %s\n", strerror(errno));
72 return PTS_UNRESOLVED;
73 }
74 if (ftruncate(fd, len) == -1) {
75 printf("Error at ftruncate(): %s\n", strerror(errno));
76 return PTS_UNRESOLVED;
77 }
78
79 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
80 if (pa == MAP_FAILED) {
81 printf("Error at mmap(): %s\n", strerror(errno));
82 return PTS_FAIL;
83 }
84
85 /* Check the patial page is ZERO filled */
86 for (i = len; i < page_size; i++)
87 if (pa[i] != 0) {
88 printf("Test FAILED: The partial page at the "
89 "end of an object is not zero-filled\n");
90 return PTS_FAIL;
91 }
92
93 /* Write the partial page */
94 pa[len + 1] = 'b';
95 munmap(pa, len);
96 close(fd);
97 return PTS_PASS;
98 case -1:
99 printf("Error at fork(): %s\n", strerror(errno));
100 return PTS_UNRESOLVED;
101 default:
102 break;
103 }
104
105 wait(&exit_val);
106 if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
107 unlink(tmpfname);
108
109 if (WIFEXITED(exit_val))
110 return WEXITSTATUS(exit_val);
111
112 printf("Child exited abnormally\n");
113 return PTS_UNRESOLVED;
114 }
115
116 fd = open(tmpfname, O_RDWR, 0);
117 unlink(tmpfname);
118
119 size = 0;
120
121 while ((ret = read(fd, &ch, 1)) == 1) {
122
123 if (ch != 0) {
124 printf("File is not zeroed\n");
125 return PTS_FAIL;
126 }
127
128 size += ret;
129 }
130
131 if (ret == -1) {
132 printf("Error at read(): %s\n", strerror(errno));
133 return PTS_UNRESOLVED;
134 }
135
136 if (size != len) {
137 printf("File has wrong size\n");
138 return PTS_UNRESOLVED;
139 }
140
141 close(fd);
142
143 printf("Test PASSED\n");
144 return PTS_PASS;
145 }
146