• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
35 #include "posixtest.h"
36 #include "tempfile.h"
37 
main(void)38 int main(void)
39 {
40 	char tmpfname[PATH_MAX];
41 	long page_size;
42 	long total_size;
43 
44 	void *pa;
45 	size_t size;
46 	int fd, saved_errno;
47 	off_t off;
48 
49 	page_size = sysconf(_SC_PAGE_SIZE);
50 	total_size = 3 * page_size;
51 	size = page_size;
52 
53 	PTS_GET_TMP_FILENAME(tmpfname, "pts_mmap_11_1");
54 	unlink(tmpfname);
55 	fd = open(tmpfname, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
56 	if (fd == -1) {
57 		printf("Error at open(): %s\n", strerror(errno));
58 		return PTS_UNRESOLVED;
59 	}
60 	unlink(tmpfname);
61 
62 	if (ftruncate(fd, total_size) == -1) {
63 		printf("Error at ftruncate(): %s\n", strerror(errno));
64 		return PTS_UNRESOLVED;
65 	}
66 
67 	/* This offset is considered illegal, not a multiple of page_size,
68 	 * unless the page_size is 1 byte, which is considered impossible.
69 	 */
70 	off = page_size + 1;
71 	errno = 0;
72 	pa = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, off);
73 
74 	saved_errno = errno;
75 
76 	close(fd);
77 	munmap(pa, size);
78 
79 	if (pa == MAP_FAILED && saved_errno == EINVAL) {
80 		printf("Got EINVAL when 'off' is not multiple of page size\n");
81 		printf("Test PASSED\n");
82 		return PTS_PASS;
83 	}
84 
85 	printf("Test FAILED: Did not get EINVAL"
86 	       " when 'off' is not a multiple of page size, get: %s\n",
87 	       strerror(saved_errno));
88 
89 	return PTS_FAIL;
90 }
91