• 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  * 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 #define _XOPEN_SOURCE 600
32 
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <sys/mman.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/wait.h>
40 #include <fcntl.h>
41 #include <string.h>
42 #include <errno.h>
43 #include "posixtest.h"
44 
main(void)45 int main(void)
46 {
47 	char tmpfname[256];
48 	long page_size;
49 
50 	char *pa;
51 	size_t len;
52 	int fd;
53 
54 	pid_t child;
55 	int i, exit_val;
56 
57 	page_size = sysconf(_SC_PAGE_SIZE);
58 
59 	/* mmap will create a partial page */
60 	len = page_size / 2;
61 
62 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_5_%d", getpid());
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 		msync(pa, len, MS_SYNC);
96 		munmap(pa, len);
97 		close(fd);
98 		return PTS_PASS;
99 	case -1:
100 		printf("Error at fork(): %s\n", strerror(errno));
101 		return PTS_UNRESOLVED;
102 	default:
103 		break;
104 	}
105 
106 	wait(&exit_val);
107 	if (!(WIFEXITED(exit_val) && (WEXITSTATUS(exit_val) == PTS_PASS))) {
108 		unlink(tmpfname);
109 
110 		if (WIFEXITED(exit_val))
111 			return WEXITSTATUS(exit_val);
112 
113 		printf("Child exited abnormally\n");
114 		return PTS_UNRESOLVED;
115 	}
116 
117 	fd = open(tmpfname, O_RDWR, 0);
118 	unlink(tmpfname);
119 
120 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
121 	if (pa == MAP_FAILED) {
122 		printf("Error at 2nd mmap(): %s\n", strerror(errno));
123 		return PTS_FAIL;
124 	}
125 
126 	if (pa[len + 1] == 'b') {
127 		printf("Test FAILED: Modification of the partial page "
128 		       "at the end of an object is written out\n");
129 		return PTS_FAIL;
130 	}
131 	close(fd);
132 	munmap(pa, len);
133 
134 	printf("Test PASSED\n");
135 	return PTS_PASS;
136 }
137