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 mmap() function shall fail if:
10 * [ENOTSUP] MAP_FIXED or MAP_PRIVATE was specified
11 * in the flags argument and the
12 * implementation does not support this functionality.
13 * The implementation does not support the combination
14 * of accesses requested in the prot argument.
15 *
16 * Test Steps:
17 * 1. Try mmap with MAP_PRIVATE should either fail with ENOTSUP or SUCCEED
18 * 2. Try fixed mapping
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.h>
28 #include <string.h>
29 #include <errno.h>
30 #include "posixtest.h"
31
main(void)32 int main(void)
33 {
34 char tmpfname[256];
35 int total_size = 1024;
36
37 void *pa;
38 size_t len = total_size;
39 int fd, err = 0;
40
41 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_27_1_%d", getpid());
42 unlink(tmpfname);
43 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
44 if (fd == -1) {
45 printf("Error at open(): %s\n", strerror(errno));
46 return PTS_UNRESOLVED;
47 }
48
49 /* Make sure the file is removed when it is closed */
50 unlink(tmpfname);
51
52 if (ftruncate(fd, total_size) == -1) {
53 printf("Error at ftruncate(): %s\n", strerror(errno));
54 return PTS_UNRESOLVED;
55 }
56
57 /* Trie to map file with MAP_PRIVATE */
58 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
59 if (pa == MAP_FAILED) {
60 if (errno != ENOTSUP) {
61 printf("MAP_PRIVATE is not supported\n");
62 } else {
63 printf("MAP_PRIVATE failed with: %s\n",
64 strerror(errno));
65 err++;
66 }
67 } else {
68 printf("MAP_PRIVATE succeeded\n");
69 munmap(pa, len);
70 }
71
72 /* Now try to utilize MAP_FIXED */
73 pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
74 if (pa == MAP_FAILED) {
75 printf("Error at mmap(): %s\n", strerror(errno));
76 return PTS_UNRESOLVED;
77 }
78
79 pa = mmap(pa, len / 2, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0);
80
81 if (pa == MAP_FAILED) {
82 if (errno != ENOTSUP) {
83 printf("MAP_FIXED is not supported\n");
84 } else {
85 printf("MAP_FIXED failed with: %s\n", strerror(errno));
86 err++;
87 }
88 } else {
89 printf("MAP_FIXED succeeded\n");
90 munmap(pa, len);
91 }
92
93 if (err)
94 return PTS_FAIL;
95
96 printf("Test PASSED\n");
97 return PTS_PASS;
98 }
99