• 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 prot shall be either PROT_NONE or the bitwise-inclusive OR of
10  * one or more of the other flags in the following table, defined in the
11  * <sys/mman.h> header.
12  * PROT_READ Data can be read.
13  * PROT_WRITE Data can be written.
14  * PROT_EXEC Data can be executed.
15  * PROT_NONE Data cannot be accessed.
16  * If an implementation cannot support the combination of access types
17  * specified by prot, the call to mmap() shall fail.
18  *
19  * Test Steps:
20  * 1. call mmap() for all combinations permitted by POSIX
21  * 2. each should either succed or fail with ENOTSUP
22  */
23 
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <errno.h>
33 #include "posixtest.h"
34 
35 struct testcase {
36 	int prot;
37 	int flags;
38 };
39 
40 struct testcase testcases[] = {
41 	{.flags = MAP_SHARED,.prot = PROT_NONE},
42 	{.flags = MAP_SHARED,.prot = PROT_READ},
43 	{.flags = MAP_SHARED,.prot = PROT_WRITE},
44 	{.flags = MAP_SHARED,.prot = PROT_EXEC},
45 	{.flags = MAP_SHARED,.prot = PROT_READ | PROT_WRITE},
46 	{.flags = MAP_SHARED,.prot = PROT_READ | PROT_EXEC},
47 	{.flags = MAP_SHARED,.prot = PROT_EXEC | PROT_WRITE},
48 	{.flags = MAP_SHARED,.prot = PROT_READ | PROT_WRITE | PROT_EXEC},
49 
50 	{.flags = MAP_PRIVATE,.prot = PROT_NONE},
51 	{.flags = MAP_PRIVATE,.prot = PROT_READ},
52 	{.flags = MAP_PRIVATE,.prot = PROT_WRITE},
53 	{.flags = MAP_PRIVATE,.prot = PROT_EXEC},
54 	{.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_WRITE},
55 	{.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_EXEC},
56 	{.flags = MAP_PRIVATE,.prot = PROT_EXEC | PROT_WRITE},
57 	{.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_WRITE | PROT_EXEC},
58 };
59 
print_error(struct testcase * t,int saved_errno)60 static void print_error(struct testcase *t, int saved_errno)
61 {
62 	printf("Combination of ");
63 
64 	if (t->prot == PROT_NONE)
65 		printf("PROT_NONE ");
66 
67 	if (t->prot & PROT_READ)
68 		printf("PROT_READ ");
69 
70 	if (t->prot & PROT_WRITE)
71 		printf("PROT_WRITE ");
72 
73 	if (t->prot & PROT_EXEC)
74 		printf("PROT_EXEC ");
75 
76 	switch (t->flags) {
77 	case MAP_SHARED:
78 		printf("with MAP_SHARED");
79 		break;
80 	case MAP_PRIVATE:
81 		printf("with MAP_PRIVATE");
82 		break;
83 	}
84 
85 	printf(" has failed: %s\n", strerror(saved_errno));
86 }
87 
main(void)88 int main(void)
89 {
90 	char tmpfname[256];
91 	void *pa;
92 	size_t size = 1024;
93 	int fd, fail = 0;
94 	unsigned int i;
95 
96 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_5_1_%d", getpid());
97 	unlink(tmpfname);
98 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
99 	if (fd == -1) {
100 		printf("Error at open(): %s\n", strerror(errno));
101 		return PTS_UNRESOLVED;
102 	}
103 	unlink(tmpfname);
104 	if (ftruncate(fd, size) == -1) {
105 		printf("Error at ftruncate(): %s\n", strerror(errno));
106 		return PTS_UNRESOLVED;
107 	}
108 
109 	for (i = 0; i < sizeof(testcases) / sizeof(*testcases); i++) {
110 
111 		pa = mmap(NULL, size, testcases[i].prot, testcases[i].flags, fd,
112 			  0);
113 
114 		if (pa == MAP_FAILED) {
115 			if (errno != ENOTSUP) {
116 				print_error(&testcases[i], errno);
117 				fail++;
118 			}
119 		} else {
120 			munmap(pa, size);
121 		}
122 	}
123 
124 	close(fd);
125 
126 	if (fail)
127 		return PTS_FAIL;
128 
129 	printf("Test PASSED\n");
130 	return PTS_PASS;
131 }
132