1 /*
2 * Copyright (c) 2002, Intel Corporation. All rights reserved.
3 * Copyright (c) 2012, Cyril Hrubis <chrubis@suse.cz>
4 *
5 * This file is licensed under the GPL license. For the full content
6 * of this license, see the COPYING file at the top level of this
7 * source tree.
8 *
9 * The off argument is constrained to be aligned and sized
10 * according to the value returned by
11 * sysconf() when passed _SC_PAGESIZE or _SC_PAGE_SIZE.
12 *
13 * The mmap() function shall fail if: [EINVAL] The addr argument (if MAP_FIXED
14 * was specified) or off is not a multiple of the page size as returned by
15 * sysconf(), or is considered invalid by the implementation.
16 *
17 * Test Steps:
18 * 1. Set 'off' a value which is not a multiple of page size;
19 * 2. Call mmap() and get EINVAL;
20 *
21 */
22
23 #define _XOPEN_SOURCE 600
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <sys/mman.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <sys/wait.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <errno.h>
35 #include "posixtest.h"
36
main(void)37 int main(void)
38 {
39 char tmpfname[256];
40 long page_size;
41 long total_size;
42
43 void *pa;
44 size_t size;
45 int fd, saved_errno;
46 off_t off;
47
48 page_size = sysconf(_SC_PAGE_SIZE);
49 total_size = 3 * page_size;
50 size = page_size;
51
52 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_1_%d", getpid());
53 unlink(tmpfname);
54 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
55 if (fd == -1) {
56 printf("Error at open(): %s\n", strerror(errno));
57 return PTS_UNRESOLVED;
58 }
59 unlink(tmpfname);
60
61 if (ftruncate(fd, total_size) == -1) {
62 printf("Error at ftruncate(): %s\n", strerror(errno));
63 return PTS_UNRESOLVED;
64 }
65
66 /* This offset is considered illegal, not a multiple of page_size,
67 * unless the page_size is 1 byte, which is considered impossible.
68 */
69 off = page_size + 1;
70 errno = 0;
71 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
72
73 saved_errno = errno;
74
75 close(fd);
76 munmap(pa, size);
77
78 if (pa == MAP_FAILED && saved_errno == EINVAL) {
79 printf("Got EINVAL when 'off' is not multiple of page size\n");
80 printf("Test PASSED\n");
81 return PTS_PASS;
82 }
83
84 printf("Test FAILED: Did not get EINVAL"
85 " when 'off' is not a multiple of page size, get: %s\n",
86 strerror(saved_errno));
87
88 return PTS_FAIL;
89 }
90