blob: 2d6c1d071e6508fe9e227b8319a398ff35b2f2da [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * Test Name: mremap02
22 *
23 * Test Description:
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * Verify that,
25 * mremap() fails when used to expand the existing virtual memory mapped
26 * region to the requested size, if the virtual memory area previously
plars865695b2001-08-27 22:15:12 +000027 * mapped was not page aligned or invalid argument specified.
28 *
29 * Expected Result:
30 * mremap() should return -1 and set errno to EINVAL.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Pause for SIGUSR1 if option specified.
36 *
37 * Test:
38 * Loop if the proper options are given.
39 * Execute system call
40 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000041 * if errno set == expected errno
42 * Issue sys call fails with expected return value and errno.
43 * Otherwise,
plars865695b2001-08-27 22:15:12 +000044 * Issue sys call fails with unexpected errno.
45 * Otherwise,
46 * Issue sys call returns unexpected value.
47 *
48 * Cleanup:
49 * Print errno log and/or timing stats if options given
50 *
51 * Usage: <for command-line>
52 * mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
53 * where, -c n : Run n copies concurrently.
54 * -e : Turn on errno logging.
55 * -i n : Execute test n times.
56 * -I x : Execute test for x seconds.
57 * -p x : Pause for x seconds between iterations.
58 * -t : Turn on syscall timing.
59 *
60 * HISTORY
61 * 07/2001 Ported by Wayne Boyer
62 *
iyermanoj874176f2001-11-09 07:07:02 +000063 * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
64 * Modified.
65 * - #include <linux/mman.h> should not be included as per man page for
66 * mremap, #include <sys/mman.h> alone should do the job. But inorder
67 * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
68 * (included by sys/mman.h) __USE_GNU needs to be defined.
69 * There may be a more elegant way of doing this...
70 *
71 *
plars865695b2001-08-27 22:15:12 +000072 * RESTRICTIONS:
73 * None.
74 */
75#include <errno.h>
76#include <unistd.h>
77#include <fcntl.h>
iyermanoj874176f2001-11-09 07:07:02 +000078#define __USE_GNU
plars865695b2001-08-27 22:15:12 +000079#include <sys/mman.h>
iyermanoj874176f2001-11-09 07:07:02 +000080#undef __USE_GNU
plars865695b2001-08-27 22:15:12 +000081
82#include "test.h"
83#include "usctest.h"
84
subrata_modak56207ce2009-03-23 13:35:39 +000085char *TCID = "mremap02"; /* Test program identifier. */
86int TST_TOTAL = 1; /* Total number of test cases. */
plars865695b2001-08-27 22:15:12 +000087extern int Tst_count; /* Test Case counter for tst_* routines */
88char *addr; /* addr of memory mapped region */
89int memsize; /* memory mapped size */
90int newsize; /* new size of virtual memory block */
subrata_modak56207ce2009-03-23 13:35:39 +000091int exp_enos[] = { EINVAL, 0 };
plars865695b2001-08-27 22:15:12 +000092
93void setup(); /* Main setup function of test */
94void cleanup(); /* cleanup function for the test */
95
subrata_modak56207ce2009-03-23 13:35:39 +000096int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000097{
98 int lc; /* loop counter */
99 char *msg; /* message returned from parse_opts */
subrata_modakbdbaec52009-02-26 12:14:51 +0000100
plars865695b2001-08-27 22:15:12 +0000101 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800102 msg = parse_opts(ac, av, NULL, NULL);
103 if (msg != NULL) {
Garrett Cooper60fa8012010-11-22 13:50:58 -0800104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000105 }
106
107 /* Perform global setup for test */
108 setup();
109
110 /* set the expected errnos... */
111 TEST_EXP_ENOS(exp_enos);
112
113 /* Check looping state if -i option given */
114 for (lc = 0; TEST_LOOPING(lc); lc++) {
115
116 /* Reset Tst_count in case we are looping. */
subrata_modak56207ce2009-03-23 13:35:39 +0000117 Tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000118
subrata_modak4bb656a2009-02-26 12:02:09 +0000119 /*
120 * Attempt to expand the existing mapped
plars865695b2001-08-27 22:15:12 +0000121 * memory region (memsize) by newsize limits using
122 * mremap() should fail as old virtual address is not
123 * page aligned.
124 */
robbiewd5c21122002-03-22 16:22:40 +0000125 errno = 0;
126 addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
127 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000128
129 /* Check for the return value of mremap() */
robbiewd5c21122002-03-22 16:22:40 +0000130 if (addr != MAP_FAILED) {
plars865695b2001-08-27 22:15:12 +0000131 tst_resm(TFAIL,
132 "mremap returned invalid value, expected: -1");
133
134 /* Unmap the mapped memory region */
robbiewd5c21122002-03-22 16:22:40 +0000135 if (munmap(addr, newsize) != 0) {
plars865695b2001-08-27 22:15:12 +0000136 tst_brkm(TBROK, cleanup, "munmap fails to "
137 "unmap the expanded memory region, "
138 "error=%d", errno);
139 }
140 continue;
141 }
142
143 TEST_ERROR_LOG(TEST_ERRNO);
144
145 if (errno == EINVAL) {
146 tst_resm(TPASS, "mremap() Failed, 'invalid argument "
147 "specified' - errno %d", TEST_ERRNO);
148 } else {
149 tst_resm(TFAIL, "mremap() Failed, "
150 "'Unexpected errno %d", TEST_ERRNO);
151 }
subrata_modak56207ce2009-03-23 13:35:39 +0000152 } /* End of TEST_LOOPING */
153
plars865695b2001-08-27 22:15:12 +0000154 /* Call cleanup() to undo setup done for the test. */
155 cleanup();
156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 /*NOTREACHED*/ return 0;
robbiew09830082003-03-26 23:18:32 +0000158
subrata_modak56207ce2009-03-23 13:35:39 +0000159} /* End main */
plars865695b2001-08-27 22:15:12 +0000160
161/*
162 * setup() - performs all ONE TIME setup for this test.
163 *
164 * Get system page size, Set the size of virtual memory area and the
165 * new size after resize, Set the virtual memory address such that it
166 * is not aligned.
167 */
subrata_modak56207ce2009-03-23 13:35:39 +0000168void setup()
plars865695b2001-08-27 22:15:12 +0000169{
170 /* capture signals */
171 tst_sig(FORK, DEF_HANDLER, cleanup);
172
173 /* Pause if that option was specified */
174 TEST_PAUSE;
175
176 /* Get the system page size */
177 if ((memsize = getpagesize()) < 0) {
178 tst_brkm(TFAIL, tst_exit,
179 "getpagesize() fails to get system page size");
180 }
181
182 /* Get the New size of virtual memory block after resize */
183 newsize = (memsize * 2);
184
185 /* Set the old virtual memory address */
186 addr = (char *)(addr + (memsize - 1));
187}
188
189/*
190 * cleanup() - performs all ONE TIME cleanup for this test at
191 * completion or premature exit.
192 */
subrata_modak56207ce2009-03-23 13:35:39 +0000193void cleanup()
plars865695b2001-08-27 22:15:12 +0000194{
195 /*
196 * print timing stats if that option was specified.
197 * print errno log if that option was specified.
198 */
199 TEST_CLEANUP;
200
201 /* Exit the program */
202 tst_exit();
203}