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  * When the implementation selects a
10  * value for pa, it never places a mapping at address 0.
11  *
12  * Test steps:
13  * This is not a good test. Cannot make sure (pa == 0) never happens.
14  * Repeat LOOP_NUM times mmap() and mnumap(),
15  * make sure pa will not equal 0.
16  */
17 
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <sys/wait.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <errno.h>
29 
30 #include "posixtest.h"
31 #include "tempfile.h"
32 
33 #define LOOP_NUM 100000
34 
main(void)35 int main(void)
36 {
37 	int rc;
38 	unsigned long cnt;
39 
40 	char tmpfname[PATH_MAX];
41 	long total_size;
42 
43 	void *pa;
44 	size_t size;
45 	int fd;
46 
47 	total_size = 1024;
48 	size = total_size;
49 
50 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_10_1");
51 	unlink(tmpfname);
52 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
53 	if (fd == -1) {
54 		printf("Error at open(): %s\n", strerror(errno));
55 		return PTS_UNRESOLVED;
56 	}
57 	unlink(tmpfname);
58 	if (ftruncate(fd, total_size) == -1) {
59 		printf("Error at ftruncate(): %s\n", strerror(errno));
60 		return PTS_UNRESOLVED;
61 	}
62 
63 	for (cnt = 0; cnt < LOOP_NUM; cnt++) {
64 		pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd,
65 			  0);
66 		if (pa == MAP_FAILED) {
67 			printf("Test FAILED: Error at mmap: %s\n",
68 			       strerror(errno));
69 			return PTS_FAIL;
70 		}
71 
72 		if (pa == NULL) {
73 			printf("Test FAILED:"
74 			       " mmap() map the file to 0 address "
75 			       "without setting MAP_FIXED\n");
76 			return PTS_FAIL;
77 		}
78 		rc = munmap(pa, size);
79 		if (rc != 0) {
80 			printf("Error at mnumap(): %s\n", strerror(errno));
81 			return PTS_UNRESOLVED;
82 		}
83 	}
84 
85 	close(fd);
86 	printf("Test PASSED\n");
87 	return PTS_PASS;
88 }
89