1 /*
2  * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
3  *
4  * This file is licensed under the GPL license.  For the full content
5  * of this license, see the COPYING file at the top level of this
6  * source tree.
7  *
8  * If len is zero, mmap() shall fail and no mapping shall be established.
9  */
10 
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <limits.h>
16 #include <sys/mman.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <sys/wait.h>
20 #include <fcntl.h>
21 #include <string.h>
22 #include <errno.h>
23 #include "posixtest.h"
24 
main(void)25 int main(void)
26 {
27 	char tmpfname[256];
28 
29 	void *pa;
30 	int fd;
31 
32 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_31_1_%d", getpid());
33 	unlink(tmpfname);
34 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
35 	if (fd == -1) {
36 		printf("Error at open(): %s\n", strerror(errno));
37 		return PTS_UNRESOLVED;
38 	}
39 	unlink(tmpfname);
40 
41 	pa = mmap(NULL, 0, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
42 	if (pa == MAP_FAILED && errno == EINVAL) {
43 		printf("Got EINVAL\n");
44 		printf("Test PASSED\n");
45 		close(fd);
46 		return PTS_PASS;
47 	}
48 
49 	if (pa == MAP_FAILED)
50 		printf("Test FAILED: Expected EINVAL got %s\n", strerror(errno));
51 	else
52 		printf("Test FAILED: mmap() succedded unexpectedly\n");
53 
54 	close(fd);
55 	munmap(pa, 0);
56 	return PTS_FAIL;
57 }
58