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
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include <sys/mman.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/wait.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include "posixtest.h"
35
main(void)36 int main(void)
37 {
38 char tmpfname[256];
39 long page_size;
40 long total_size;
41
42 void *pa;
43 size_t size;
44 int fd, saved_errno;
45 off_t off;
46
47 page_size = sysconf(_SC_PAGE_SIZE);
48 total_size = 3 * page_size;
49 size = page_size;
50
51 snprintf(tmpfname, sizeof(tmpfname), "/tmp/pts_mmap_11_1_%d", getpid());
52 unlink(tmpfname);
53 fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
54 if (fd == -1) {
55 printf("Error at open(): %s\n", strerror(errno));
56 return PTS_UNRESOLVED;
57 }
58 unlink(tmpfname);
59
60 if (ftruncate(fd, total_size) == -1) {
61 printf("Error at ftruncate(): %s\n", strerror(errno));
62 return PTS_UNRESOLVED;
63 }
64
65 /* This offset is considered illegal, not a multiple of page_size,
66 * unless the page_size is 1 byte, which is considered impossible.
67 */
68 off = page_size + 1;
69 errno = 0;
70 pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
71
72 saved_errno = errno;
73
74 close(fd);
75 munmap(pa, size);
76
77 if (pa == MAP_FAILED && saved_errno == EINVAL) {
78 printf("Got EINVAL when 'off' is not multiple of page size\n");
79 printf("Test PASSED\n");
80 return PTS_PASS;
81 }
82
83 printf("Test FAILED: Did not get EINVAL"
84 " when 'off' is not a multiple of page size, get: %s\n",
85 strerror(saved_errno));
86
87 return PTS_FAIL;
88 }
89