blob: 748988ad842219c2c9a154d0173f780c14adfd4a [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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * Test Name: mremap04
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
plars865695b2001-08-27 22:15:12 +000026 * region to the requested size, if the memory area cannot be expanded at
27 * the current virtual address and MREMAP_MAYMOVE flag not set.
28 *
29 * Expected Result:
30 * mremap() should return -1 and set errno to ENOMEM.
31 *
32 * Algorithm:
33 * Setup:
34 * Setup signal handling.
35 * Create temporary directory.
36 * Pause for SIGUSR1 if option specified.
37 *
38 * Test:
39 * Loop if the proper options are given.
40 * Execute system call
41 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000042 * if errno set == expected errno
43 * Issue sys call failed with expected return value and errno.
44 * Otherwise,
plars865695b2001-08-27 22:15:12 +000045 * Issue sys call failed with unexpected errno.
46 * Otherwise,
47 * Issue sys call returns unexpected value.
48 *
49 * Cleanup:
50 * Print errno log and/or timing stats if options given
51 * Delete the temporary directory(s)/file(s) created.
52 *
53 * Usage: <for command-line>
54 * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
55 * where, -c n : Run n copies concurrently.
56 * -e : Turn on errno logging.
57 * -i n : Execute test n times.
58 * -I x : Execute test for x seconds.
59 * -p x : Pause for x seconds between iterations.
60 * -t : Turn on syscall timing.
61 *
62 * HISTORY
63 * 07/2001 Ported by Wayne Boyer
64 *
iyermanoj874176f2001-11-09 07:07:02 +000065 * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
66 * Modified.
67 * - #include <linux/mman.h> should not be included as per man page for
68 * mremap, #include <sys/mman.h> alone should do the job. But inorder
69 * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
70 * (included by sys/mman.h) __USE_GNU needs to be defined.
71 * There may be a more elegant way of doing this...
72 *
subrata_modakae6cabc2008-02-27 14:47:11 +000073 * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com)
74 * - Fix concurrency issue. Use a shm key from getipckey instead of
75 * a fixed hard-coded value.
iyermanoj874176f2001-11-09 07:07:02 +000076 *
plars865695b2001-08-27 22:15:12 +000077 * RESTRICTIONS:
78 * None.
79 */
80#include <errno.h>
81#include <unistd.h>
iyermanoj874176f2001-11-09 07:07:02 +000082#define __USE_GNU
plars865695b2001-08-27 22:15:12 +000083#include <sys/mman.h>
iyermanoj874176f2001-11-09 07:07:02 +000084#undef __USE_GNU
plars865695b2001-08-27 22:15:12 +000085#include <sys/ipc.h>
86#include <sys/shm.h>
87
88#include "test.h"
plars865695b2001-08-27 22:15:12 +000089
plars865695b2001-08-27 22:15:12 +000090#define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */
91
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020092char *TCID = "mremap04";
93int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000094char *addr; /* addr of memory mapped region */
95char *shmaddr; /* pointer to shared memory segment */
96int shmid; /* shared memory identifier. */
97int memsize; /* memory mapped size */
Cyril Hrubis605fa332015-02-04 13:11:20 +010098int newsize; /* new size of virtual memory block */
plars865695b2001-08-27 22:15:12 +000099
100void setup(); /* Main setup function of test */
101void cleanup(); /* cleanup function for the test */
102
subrata_modakae6cabc2008-02-27 14:47:11 +0000103extern int getipckey();
104
subrata_modak56207ce2009-03-23 13:35:39 +0000105int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +0000106{
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200107 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200108 const char *msg;
subrata_modakbdbaec52009-02-26 12:14:51 +0000109
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800110 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800111 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +0000112
plars865695b2001-08-27 22:15:12 +0000113 setup();
114
plars865695b2001-08-27 22:15:12 +0000115 for (lc = 0; TEST_LOOPING(lc); lc++) {
116
Caspar Zhangd59a6592013-03-07 14:59:12 +0800117 tst_count = 0;
plars865695b2001-08-27 22:15:12 +0000118
subrata_modak4bb656a2009-02-26 12:02:09 +0000119 /*
120 * Attempt to expand the existing shared
plars865695b2001-08-27 22:15:12 +0000121 * memory region of newsize by newsize limits
122 * using mremap() should fail as specified
123 * memory area already locked and MREMAP_MAYMOVE
124 * flag unset.
125 */
robbiewd5c21122002-03-22 16:22:40 +0000126 errno = 0;
127 addr = mremap(shmaddr, memsize, newsize, 0);
128 TEST_ERRNO = errno;
plars865695b2001-08-27 22:15:12 +0000129
130 /* Check for the return value of mremap() */
robbiewd5c21122002-03-22 16:22:40 +0000131 if (addr != MAP_FAILED) {
plars865695b2001-08-27 22:15:12 +0000132 tst_resm(TFAIL,
133 "mremap returned invalid value, expected: -1");
134
135 /* Unmap the mapped memory region */
robbiewd5c21122002-03-22 16:22:40 +0000136 if (munmap(addr, newsize) != 0) {
plars865695b2001-08-27 22:15:12 +0000137 tst_brkm(TFAIL, cleanup, "munmap failed to "
138 "unmap the expanded memory region, "
139 "error=%d", errno);
140 }
141 continue;
142 }
143
plars865695b2001-08-27 22:15:12 +0000144 if (TEST_ERRNO == ENOMEM) {
145 tst_resm(TPASS, "mremap() failed, "
146 "'MREMAP_MAYMOVE flag unset', "
147 "errno %d", TEST_ERRNO);
148 } else {
149 tst_resm(TFAIL, "mremap() failed, "
150 "Unexpected errno %d", TEST_ERRNO);
151 }
Garrett Cooper2c282152010-12-16 00:55:50 -0800152 }
subrata_modak56207ce2009-03-23 13:35:39 +0000153
plars865695b2001-08-27 22:15:12 +0000154 cleanup();
Garrett Cooper1e6f5a62010-12-19 09:58:10 -0800155 tst_exit();
plars865695b2001-08-27 22:15:12 +0000156
Garrett Cooper2c282152010-12-16 00:55:50 -0800157}
plars865695b2001-08-27 22:15:12 +0000158
159/*
160 * setup() - performs all ONE TIME setup for this test.
161 *
162 * Get system page size, Set the size of virtual memory area and the
163 * newsize after resize,
164 * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
165 * by using shmget() which returns a shared memory identifier associated
subrata_modak4bb656a2009-02-26 12:02:09 +0000166 * with the created shared memory segment.
167 * Call shmat() to attach the shared memory segment to the data segment of the
plars865695b2001-08-27 22:15:12 +0000168 * calling process. The segment is attached at the first available address as
169 * selected by the system.
170 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400171void setup(void)
plars865695b2001-08-27 22:15:12 +0000172{
subrata_modakae6cabc2008-02-27 14:47:11 +0000173 key_t shmkey;
174
plars865695b2001-08-27 22:15:12 +0000175 tst_sig(FORK, DEF_HANDLER, cleanup);
176
plars865695b2001-08-27 22:15:12 +0000177 TEST_PAUSE;
178
subrata_modakae6cabc2008-02-27 14:47:11 +0000179 tst_tmpdir();
180
plars865695b2001-08-27 22:15:12 +0000181 /* Get the system page size */
182 if ((memsize = getpagesize()) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800183 tst_brkm(TBROK, NULL,
plars865695b2001-08-27 22:15:12 +0000184 "getpagesize() failed to get system page size");
185 }
186
187 /* Get the New size of virtual memory block after resize */
188 newsize = (memsize * 2);
189
subrata_modakae6cabc2008-02-27 14:47:11 +0000190 /* get an IPC resource key */
191 shmkey = getipckey();
192
plars865695b2001-08-27 22:15:12 +0000193 /*
194 * Create a shared memory segment represented by SHMKEY of
195 * specified size 'newsize' and mode permissions 'SHM_MODE'.
196 */
subrata_modakae6cabc2008-02-27 14:47:11 +0000197 shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
plars865695b2001-08-27 22:15:12 +0000198 if (shmid == -1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800199 tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
plars865695b2001-08-27 22:15:12 +0000200 "memory, error:%d", errno);
201 }
202
203 /*
204 * Attach the shared memory segment associated with the shared
205 * memory identifier specified by "shmid" to the data segment of
206 * the calling process at the first available address as selected
207 * by the system.
208 */
Cyril Hrubisd2db4802014-09-24 17:08:17 +0200209 shmaddr = shmat(shmid, NULL, 0);
plars865695b2001-08-27 22:15:12 +0000210 if (shmaddr == (void *)-1) {
211 tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
212 "memory, error:%d", errno);
213 }
214}
215
216/*
217 * cleanup() - performs all ONE TIME cleanup for this test at
218 * completion or premature exit.
219 * Detach the shared memory segment and remove the shared memory
220 * identifier associated with the shared memory.
221 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400222void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000223{
plars865695b2001-08-27 22:15:12 +0000224
plars865695b2001-08-27 22:15:12 +0000225 /*
226 * Detach the shared memory segment attached to
227 * the calling process's data segment
228 */
229 if (shmdt(shmaddr) < 0) {
230 tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
231 "memory, error:%d", errno);
232 }
233
234 /*
235 * Remove the shared memory identifier associated with
236 * the shared memory segment and destroy the shared memory
237 * segment.
238 */
239 if (shmctl(shmid, IPC_RMID, 0) < 0) {
240 tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
241 "memory, error:%d", errno);
242 }
243
subrata_modak44640672008-06-03 13:05:45 +0000244 tst_rmdir();
245
plars865695b2001-08-27 22:15:12 +0000246 /* Exit the program */
Garrett Cooper2c282152010-12-16 00:55:50 -0800247
Chris Dearmanec6edca2012-10-17 19:54:01 -0700248}