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 #include "posixtest.h"
28
29 #define TNAME "munmap/3-1.c"
30
main(void)31 int main(void)
32 {
33 char tmpfname[256];
34 long file_size;
35
36 void *pa = NULL;
37 void *addr = NULL;
38 size_t len;
39 int flag;
40 int fd;
41 off_t off = 0;
42 int prot;
43
44 int page_size;
45
46 char *pa2;
47
48 page_size = sysconf(_SC_PAGE_SIZE);
49 file_size = 2 * page_size;
50
51 /* We hope to map 2 pages */
52 len = page_size + 1;
53
54 /* Create tmp file */
55 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_munmap_1_1_%d",
56 getpid());
57 unlink(tmpfname);
58 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
59 if (fd == -1) {
60 printf(TNAME " Error at open(): %s\n", strerror(errno));
61 exit(PTS_UNRESOLVED);
62 }
63 unlink(tmpfname);
64
65 if (ftruncate(fd, file_size) == -1) {
66 printf("Error at ftruncate: %s\n", strerror(errno));
67 exit(PTS_UNRESOLVED);
68 }
69
70 flag = MAP_SHARED;
71 prot = PROT_READ | PROT_WRITE;
72 pa = mmap(addr, len, prot, flag, fd, off);
73 if (pa == MAP_FAILED) {
74 printf("Test Unresolved: " TNAME " Error at mmap: %s\n",
75 strerror(errno));
76 exit(PTS_UNRESOLVED);
77 }
78
79 /* pa should be a multiple of page size */
80 pa2 = pa;
81
82 while (((unsigned long)pa2 % page_size) == 0)
83 pa2++;
84
85 close(fd);
86 if (munmap(pa2, len) == -1 && errno == EINVAL) {
87 printf("Got EINVAL\n");
88 printf("Test PASSED\n");
89 exit(PTS_PASS);
90 } else {
91 printf("Test FAILED: " TNAME " munmap returns: %s\n",
92 strerror(errno));
93 exit(PTS_FAIL);
94 }
95 }
96