• 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 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 
31 #include "posixtest.h"
32 #include "tempfile.h"
33 
main(void)34 int main(void)
35 {
36 	char tmpfname[PATH_MAX];
37 	int total_size = 1024;
38 
39 	void *pa;
40 	size_t len = total_size;
41 	int fd, err = 0;
42 
43 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_27_1");
44 	unlink(tmpfname);
45 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
46 	if (fd == -1) {
47 		printf("Error at open(): %s\n", strerror(errno));
48 		return PTS_UNRESOLVED;
49 	}
50 
51 	/* Make sure the file is removed when it is closed */
52 	unlink(tmpfname);
53 
54 	if (ftruncate(fd, total_size) == -1) {
55 		printf("Error at ftruncate(): %s\n", strerror(errno));
56 		return PTS_UNRESOLVED;
57 	}
58 
59 	/* Trie to map file with MAP_PRIVATE */
60 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
61 	if (pa == MAP_FAILED) {
62 		if (errno != ENOTSUP) {
63 			printf("MAP_PRIVATE is not supported\n");
64 		} else {
65 			printf("MAP_PRIVATE failed with: %s\n",
66 			       strerror(errno));
67 			err++;
68 		}
69 	} else {
70 		printf("MAP_PRIVATE succeeded\n");
71 		munmap(pa, len);
72 	}
73 
74 	/* Now try to utilize MAP_FIXED */
75 	pa = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
76 	if (pa == MAP_FAILED) {
77 		printf("Error at mmap(): %s\n", strerror(errno));
78 		return PTS_UNRESOLVED;
79 	}
80 
81 	pa = mmap(pa, len / 2, PROT_READ, MAP_SHARED | MAP_FIXED, fd, 0);
82 
83 	if (pa == MAP_FAILED) {
84 		if (errno != ENOTSUP) {
85 			printf("MAP_FIXED is not supported\n");
86 		} else {
87 			printf("MAP_FIXED failed with: %s\n", strerror(errno));
88 			err++;
89 		}
90 	} else {
91 		printf("MAP_FIXED succeeded\n");
92 		munmap(pa, len);
93 	}
94 
95 	if (err)
96 		return PTS_FAIL;
97 
98 	printf("Test PASSED\n");
99 	return PTS_PASS;
100 }
101