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
25 #include "posixtest.h"
26 #include "tempfile.h"
27
28 #define TNAME "munmap/9-1.c"
29
main(void)30 int main(void)
31 {
32 char tmpfname[PATH_MAX];
33 long file_size;
34
35 void *pa = NULL;
36 void *addr = NULL;
37 size_t len;
38 int flag;
39 int fd;
40 off_t off = 0;
41 int prot;
42
43 int page_size;
44
45 page_size = sysconf(_SC_PAGE_SIZE);
46 file_size = 2 * page_size;
47
48 /* We hope to map 2 pages */
49 len = page_size + 1;
50
51 /* Create tmp file */
52 PTS_GET_TMP_FILENAME(tmpfname, "pts_munmap_9_1");
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