• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2002, Intel Corporation. All rights reserved.
3  * This file is licensed under the GPL license.  For the full content
4  * of this license, see the COPYING file at the top level of this
5  * source tree.
6  *
7  * The munmap() function shall fail if:
8  * [EINVAL] The len argument is 0.
9  *
10  */
11 
12 
13 #include <pthread.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/mman.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/wait.h>
21 #include <fcntl.h>
22 #include <string.h>
23 #include <errno.h>
24 #include "posixtest.h"
25 
26 #define TNAME "munmap/9-1.c"
27 
main(void)28 int main(void)
29 {
30 	char tmpfname[256];
31 	long file_size;
32 
33 	void *pa = NULL;
34 	void *addr = NULL;
35 	size_t len;
36 	int flag;
37 	int fd;
38 	off_t off = 0;
39 	int prot;
40 
41 	int page_size;
42 
43 	page_size = sysconf(_SC_PAGE_SIZE);
44 	file_size = 2 * page_size;
45 
46 	/* We hope to map 2 pages */
47 	len = page_size + 1;
48 
49 	/* Create tmp file */
50 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d",
51 		 getpid());
52 	unlink(tmpfname);
53 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
54 	if (fd == -1) {
55 		printf(TNAME " Error at open(): %s\n", strerror(errno));
56 		exit(PTS_UNRESOLVED);
57 	}
58 	unlink(tmpfname);
59 
60 	if (ftruncate(fd, file_size) == -1) {
61 		printf("Error at ftruncate: %s\n", strerror(errno));
62 		exit(PTS_UNRESOLVED);
63 	}
64 
65 	flag = MAP_SHARED;
66 	prot = PROT_READ | PROT_WRITE;
67 	pa = mmap(addr, len, prot, flag, fd, off);
68 	if (pa == MAP_FAILED) {
69 		printf("Test Unresolved: " TNAME " Error at mmap: %s\n",
70 		       strerror(errno));
71 		exit(PTS_UNRESOLVED);
72 	}
73 
74 	close(fd);
75 
76 	/* Set len as 0 */
77 	if (munmap(pa, 0) == -1 && errno == EINVAL) {
78 		printf("Get EINVAL when len=0\n");
79 		printf("Test PASSED\n");
80 		exit(PTS_PASS);
81 	} else {
82 		printf("Test Fail: Expect EINVAL while get %s\n",
83 		       strerror(errno));
84 		return PTS_FAIL;
85 	}
86 }
87