• 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 #define _XOPEN_SOURCE 600
13 
14 #include <pthread.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <sys/mman.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <sys/wait.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <errno.h>
25 #include "posixtest.h"
26 
27 #define TNAME "munmap/9-1.c"
28 
main(void)29 int main(void)
30 {
31 	char tmpfname[256];
32 	long file_size;
33 
34 	void *pa = NULL;
35 	void *addr = NULL;
36 	size_t len;
37 	int flag;
38 	int fd;
39 	off_t off = 0;
40 	int prot;
41 
42 	int page_size;
43 
44 	page_size = sysconf(_SC_PAGE_SIZE);
45 	file_size = 2 * page_size;
46 
47 	/* We hope to map 2 pages */
48 	len = page_size + 1;
49 
50 	/* Create tmp file */
51 	snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d",
52 		 getpid());
53 	unlink(tmpfname);
54 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
55 	if (fd == -1) {
56 		printf(TNAME " Error at open(): %s\n", strerror(errno));
57 		exit(PTS_UNRESOLVED);
58 	}
59 	unlink(tmpfname);
60 
61 	if (ftruncate(fd, file_size) == -1) {
62 		printf("Error at ftruncate: %s\n", strerror(errno));
63 		exit(PTS_UNRESOLVED);
64 	}
65 
66 	flag = MAP_SHARED;
67 	prot = PROT_READ | PROT_WRITE;
68 	pa = mmap(addr, len, prot, flag, fd, off);
69 	if (pa == MAP_FAILED) {
70 		printf("Test Unresolved: " TNAME " Error at mmap: %s\n",
71 		       strerror(errno));
72 		exit(PTS_UNRESOLVED);
73 	}
74 
75 	close(fd);
76 
77 	/* Set len as 0 */
78 	if (munmap(pa, 0) == -1 && errno == EINVAL) {
79 		printf("Get EINVAL when len=0\n");
80 		printf("Test PASSED\n");
81 		exit(PTS_PASS);
82 	} else {
83 		printf("Test Fail: Expect EINVAL while get %s\n",
84 		       strerror(errno));
85 		return PTS_FAIL;
86 	}
87 }
88