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 implementation shall require that addr be a multiple
8 * of the page size {PAGESIZE}.
9 *
10 * Test step:
11 * Try to call unmap, with addr NOT a multiple of page size.
12 * Should get EINVAL.
13 */
14
15
16 #include <pthread.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <sys/mman.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/wait.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <errno.h>
27
28 #include "posixtest.h"
29 #include "tempfile.h"
30
31 #define TNAME "munmap/3-1.c"
32
main(void)33 int main(void)
34 {
35 char tmpfname[PATH_MAX];
36 long file_size;
37
38 void *pa = NULL;
39 void *addr = NULL;
40 size_t len;
41 int flag;
42 int fd;
43 off_t off = 0;
44 int prot;
45
46 int page_size;
47
48 char *pa2;
49
50 page_size = sysconf(_SC_PAGE_SIZE);
51 file_size = 2 * page_size;
52
53 /* We hope to map 2 pages */
54 len = page_size + 1;
55
56 /* Create tmp file */
57 PTS_GET_TMP_FILENAME(tmpfname, "pts_munmap_3_1");
58 unlink(tmpfname);
59 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
60 if (fd == -1) {
61 printf(TNAME " Error at open(): %s\n", strerror(errno));
62 exit(PTS_UNRESOLVED);
63 }
64 unlink(tmpfname);
65
66 if (ftruncate(fd, file_size) == -1) {
67 printf("Error at ftruncate: %s\n", strerror(errno));
68 exit(PTS_UNRESOLVED);
69 }
70
71 flag = MAP_SHARED;
72 prot = PROT_READ | PROT_WRITE;
73 pa = mmap(addr, len, prot, flag, fd, off);
74 if (pa == MAP_FAILED) {
75 printf("Test Unresolved: " TNAME " Error at mmap: %s\n",
76 strerror(errno));
77 exit(PTS_UNRESOLVED);
78 }
79
80 /* pa should be a multiple of page size */
81 pa2 = pa;
82
83 while (((unsigned long)pa2 % page_size) == 0)
84 pa2++;
85
86 close(fd);
87 if (munmap(pa2, len) == -1 && errno == EINVAL) {
88 printf("Got EINVAL\n");
89 printf("Test PASSED\n");
90 exit(PTS_PASS);
91 } else {
92 printf("Test FAILED: " TNAME " munmap returns: %s\n",
93 strerror(errno));
94 exit(PTS_FAIL);
95 }
96 }
97