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
34 #include "posixtest.h"
35 #include "tempfile.h"
36
37 struct testcase {
38 int prot;
39 int flags;
40 };
41
42 static struct testcase testcases[] = {
43 {.flags = MAP_SHARED,.prot = PROT_NONE},
44 {.flags = MAP_SHARED,.prot = PROT_READ},
45 {.flags = MAP_SHARED,.prot = PROT_WRITE},
46 {.flags = MAP_SHARED,.prot = PROT_EXEC},
47 {.flags = MAP_SHARED,.prot = PROT_READ | PROT_WRITE},
48 {.flags = MAP_SHARED,.prot = PROT_READ | PROT_EXEC},
49 {.flags = MAP_SHARED,.prot = PROT_EXEC | PROT_WRITE},
50 {.flags = MAP_SHARED,.prot = PROT_READ | PROT_WRITE | PROT_EXEC},
51
52 {.flags = MAP_PRIVATE,.prot = PROT_NONE},
53 {.flags = MAP_PRIVATE,.prot = PROT_READ},
54 {.flags = MAP_PRIVATE,.prot = PROT_WRITE},
55 {.flags = MAP_PRIVATE,.prot = PROT_EXEC},
56 {.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_WRITE},
57 {.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_EXEC},
58 {.flags = MAP_PRIVATE,.prot = PROT_EXEC | PROT_WRITE},
59 {.flags = MAP_PRIVATE,.prot = PROT_READ | PROT_WRITE | PROT_EXEC},
60 };
61
print_error(struct testcase * t,int saved_errno)62 static void print_error(struct testcase *t, int saved_errno)
63 {
64 printf("Combination of ");
65
66 if (t->prot == PROT_NONE)
67 printf("PROT_NONE ");
68
69 if (t->prot & PROT_READ)
70 printf("PROT_READ ");
71
72 if (t->prot & PROT_WRITE)
73 printf("PROT_WRITE ");
74
75 if (t->prot & PROT_EXEC)
76 printf("PROT_EXEC ");
77
78 switch (t->flags) {
79 case MAP_SHARED:
80 printf("with MAP_SHARED");
81 break;
82 case MAP_PRIVATE:
83 printf("with MAP_PRIVATE");
84 break;
85 }
86
87 printf(" has failed: %s\n", strerror(saved_errno));
88 }
89
main(void)90 int main(void)
91 {
92 char tmpfname[PATH_MAX];
93 void *pa;
94 size_t size = 1024;
95 int fd, fail = 0;
96 unsigned int i;
97
98 PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_5_1");
99 unlink(tmpfname);
100 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
101 if (fd == -1) {
102 printf("Error at open(): %s\n", strerror(errno));
103 return PTS_UNRESOLVED;
104 }
105 unlink(tmpfname);
106 if (ftruncate(fd, size) == -1) {
107 printf("Error at ftruncate(): %s\n", strerror(errno));
108 return PTS_UNRESOLVED;
109 }
110
111 for (i = 0; i < sizeof(testcases) / sizeof(*testcases); i++) {
112
113 pa = mmap(NULL, size, testcases[i].prot, testcases[i].flags, fd,
114 0);
115
116 if (pa == MAP_FAILED) {
117 if (errno != ENOTSUP) {
118 print_error(&testcases[i], errno);
119 fail++;
120 }
121 } else {
122 munmap(pa, size);
123 }
124 }
125
126 close(fd);
127
128 if (fail)
129 return PTS_FAIL;
130
131 printf("Test PASSED\n");
132 return PTS_PASS;
133 }
134