1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * Test Name: mremap04
22*49cdfc7eSAndroid Build Coastguard Worker *
23*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker * Verify that,
25*49cdfc7eSAndroid Build Coastguard Worker * mremap() fails when used to expand the existing virtual memory mapped
26*49cdfc7eSAndroid Build Coastguard Worker * region to the requested size, if the memory area cannot be expanded at
27*49cdfc7eSAndroid Build Coastguard Worker * the current virtual address and MREMAP_MAYMOVE flag not set.
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * Expected Result:
30*49cdfc7eSAndroid Build Coastguard Worker * mremap() should return -1 and set errno to ENOMEM.
31*49cdfc7eSAndroid Build Coastguard Worker *
32*49cdfc7eSAndroid Build Coastguard Worker * Algorithm:
33*49cdfc7eSAndroid Build Coastguard Worker * Setup:
34*49cdfc7eSAndroid Build Coastguard Worker * Setup signal handling.
35*49cdfc7eSAndroid Build Coastguard Worker * Create temporary directory.
36*49cdfc7eSAndroid Build Coastguard Worker * Pause for SIGUSR1 if option specified.
37*49cdfc7eSAndroid Build Coastguard Worker *
38*49cdfc7eSAndroid Build Coastguard Worker * Test:
39*49cdfc7eSAndroid Build Coastguard Worker * Loop if the proper options are given.
40*49cdfc7eSAndroid Build Coastguard Worker * Execute system call
41*49cdfc7eSAndroid Build Coastguard Worker * Check return code, if system call failed (return=-1)
42*49cdfc7eSAndroid Build Coastguard Worker * if errno set == expected errno
43*49cdfc7eSAndroid Build Coastguard Worker * Issue sys call failed with expected return value and errno.
44*49cdfc7eSAndroid Build Coastguard Worker * Otherwise,
45*49cdfc7eSAndroid Build Coastguard Worker * Issue sys call failed with unexpected errno.
46*49cdfc7eSAndroid Build Coastguard Worker * Otherwise,
47*49cdfc7eSAndroid Build Coastguard Worker * Issue sys call returns unexpected value.
48*49cdfc7eSAndroid Build Coastguard Worker *
49*49cdfc7eSAndroid Build Coastguard Worker * Cleanup:
50*49cdfc7eSAndroid Build Coastguard Worker * Print errno log and/or timing stats if options given
51*49cdfc7eSAndroid Build Coastguard Worker * Delete the temporary directory(s)/file(s) created.
52*49cdfc7eSAndroid Build Coastguard Worker *
53*49cdfc7eSAndroid Build Coastguard Worker * Usage: <for command-line>
54*49cdfc7eSAndroid Build Coastguard Worker * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t]
55*49cdfc7eSAndroid Build Coastguard Worker * where, -c n : Run n copies concurrently.
56*49cdfc7eSAndroid Build Coastguard Worker * -e : Turn on errno logging.
57*49cdfc7eSAndroid Build Coastguard Worker * -i n : Execute test n times.
58*49cdfc7eSAndroid Build Coastguard Worker * -I x : Execute test for x seconds.
59*49cdfc7eSAndroid Build Coastguard Worker * -p x : Pause for x seconds between iterations.
60*49cdfc7eSAndroid Build Coastguard Worker * -t : Turn on syscall timing.
61*49cdfc7eSAndroid Build Coastguard Worker *
62*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
63*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
64*49cdfc7eSAndroid Build Coastguard Worker *
65*49cdfc7eSAndroid Build Coastguard Worker * 11/09/2001 Manoj Iyer ([email protected])
66*49cdfc7eSAndroid Build Coastguard Worker * Modified.
67*49cdfc7eSAndroid Build Coastguard Worker * - #include <linux/mman.h> should not be included as per man page for
68*49cdfc7eSAndroid Build Coastguard Worker * mremap, #include <sys/mman.h> alone should do the job. But inorder
69*49cdfc7eSAndroid Build Coastguard Worker * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
70*49cdfc7eSAndroid Build Coastguard Worker * (included by sys/mman.h) __USE_GNU needs to be defined.
71*49cdfc7eSAndroid Build Coastguard Worker * There may be a more elegant way of doing this...
72*49cdfc7eSAndroid Build Coastguard Worker *
73*49cdfc7eSAndroid Build Coastguard Worker * 26/02/2008 Renaud Lottiaux ([email protected])
74*49cdfc7eSAndroid Build Coastguard Worker * - Fix concurrency issue. Use a shm key from getipckey instead of
75*49cdfc7eSAndroid Build Coastguard Worker * a fixed hard-coded value.
76*49cdfc7eSAndroid Build Coastguard Worker *
77*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS:
78*49cdfc7eSAndroid Build Coastguard Worker * None.
79*49cdfc7eSAndroid Build Coastguard Worker */
80*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
81*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
82*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
83*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
84*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ipc.h>
85*49cdfc7eSAndroid Build Coastguard Worker #include <sys/shm.h>
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker #define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mremap04";
92*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
93*49cdfc7eSAndroid Build Coastguard Worker char *addr; /* addr of memory mapped region */
94*49cdfc7eSAndroid Build Coastguard Worker char *shmaddr; /* pointer to shared memory segment */
95*49cdfc7eSAndroid Build Coastguard Worker int shmid; /* shared memory identifier. */
96*49cdfc7eSAndroid Build Coastguard Worker int memsize; /* memory mapped size */
97*49cdfc7eSAndroid Build Coastguard Worker int newsize; /* new size of virtual memory block */
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker void setup(); /* Main setup function of test */
100*49cdfc7eSAndroid Build Coastguard Worker void cleanup(); /* cleanup function for the test */
101*49cdfc7eSAndroid Build Coastguard Worker
102*49cdfc7eSAndroid Build Coastguard Worker extern int getipckey();
103*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)104*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker int lc;
107*49cdfc7eSAndroid Build Coastguard Worker
108*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker setup();
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
113*49cdfc7eSAndroid Build Coastguard Worker
114*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker /*
117*49cdfc7eSAndroid Build Coastguard Worker * Attempt to expand the existing shared
118*49cdfc7eSAndroid Build Coastguard Worker * memory region of newsize by newsize limits
119*49cdfc7eSAndroid Build Coastguard Worker * using mremap() should fail as specified
120*49cdfc7eSAndroid Build Coastguard Worker * memory area already locked and MREMAP_MAYMOVE
121*49cdfc7eSAndroid Build Coastguard Worker * flag unset.
122*49cdfc7eSAndroid Build Coastguard Worker */
123*49cdfc7eSAndroid Build Coastguard Worker errno = 0;
124*49cdfc7eSAndroid Build Coastguard Worker addr = mremap(shmaddr, memsize, newsize, 0);
125*49cdfc7eSAndroid Build Coastguard Worker TEST_ERRNO = errno;
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker /* Check for the return value of mremap() */
128*49cdfc7eSAndroid Build Coastguard Worker if (addr != MAP_FAILED) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
130*49cdfc7eSAndroid Build Coastguard Worker "mremap returned invalid value, expected: -1");
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker /* Unmap the mapped memory region */
133*49cdfc7eSAndroid Build Coastguard Worker if (munmap(addr, newsize) != 0) {
134*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup, "munmap failed to "
135*49cdfc7eSAndroid Build Coastguard Worker "unmap the expanded memory region, "
136*49cdfc7eSAndroid Build Coastguard Worker "error=%d", errno);
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker continue;
139*49cdfc7eSAndroid Build Coastguard Worker }
140*49cdfc7eSAndroid Build Coastguard Worker
141*49cdfc7eSAndroid Build Coastguard Worker if (TEST_ERRNO == ENOMEM) {
142*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "mremap() failed, "
143*49cdfc7eSAndroid Build Coastguard Worker "'MREMAP_MAYMOVE flag unset', "
144*49cdfc7eSAndroid Build Coastguard Worker "errno %d", TEST_ERRNO);
145*49cdfc7eSAndroid Build Coastguard Worker } else {
146*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "mremap() failed, "
147*49cdfc7eSAndroid Build Coastguard Worker "Unexpected errno %d", TEST_ERRNO);
148*49cdfc7eSAndroid Build Coastguard Worker }
149*49cdfc7eSAndroid Build Coastguard Worker }
150*49cdfc7eSAndroid Build Coastguard Worker
151*49cdfc7eSAndroid Build Coastguard Worker cleanup();
152*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker }
155*49cdfc7eSAndroid Build Coastguard Worker
156*49cdfc7eSAndroid Build Coastguard Worker /*
157*49cdfc7eSAndroid Build Coastguard Worker * setup() - performs all ONE TIME setup for this test.
158*49cdfc7eSAndroid Build Coastguard Worker *
159*49cdfc7eSAndroid Build Coastguard Worker * Get system page size, Set the size of virtual memory area and the
160*49cdfc7eSAndroid Build Coastguard Worker * newsize after resize,
161*49cdfc7eSAndroid Build Coastguard Worker * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE
162*49cdfc7eSAndroid Build Coastguard Worker * by using shmget() which returns a shared memory identifier associated
163*49cdfc7eSAndroid Build Coastguard Worker * with the created shared memory segment.
164*49cdfc7eSAndroid Build Coastguard Worker * Call shmat() to attach the shared memory segment to the data segment of the
165*49cdfc7eSAndroid Build Coastguard Worker * calling process. The segment is attached at the first available address as
166*49cdfc7eSAndroid Build Coastguard Worker * selected by the system.
167*49cdfc7eSAndroid Build Coastguard Worker */
setup(void)168*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
169*49cdfc7eSAndroid Build Coastguard Worker {
170*49cdfc7eSAndroid Build Coastguard Worker key_t shmkey;
171*49cdfc7eSAndroid Build Coastguard Worker
172*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
173*49cdfc7eSAndroid Build Coastguard Worker
174*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
177*49cdfc7eSAndroid Build Coastguard Worker
178*49cdfc7eSAndroid Build Coastguard Worker /* Get the system page size */
179*49cdfc7eSAndroid Build Coastguard Worker if ((memsize = getpagesize()) < 0) {
180*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, NULL,
181*49cdfc7eSAndroid Build Coastguard Worker "getpagesize() failed to get system page size");
182*49cdfc7eSAndroid Build Coastguard Worker }
183*49cdfc7eSAndroid Build Coastguard Worker
184*49cdfc7eSAndroid Build Coastguard Worker /* Get the New size of virtual memory block after resize */
185*49cdfc7eSAndroid Build Coastguard Worker newsize = (memsize * 2);
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker /* get an IPC resource key */
188*49cdfc7eSAndroid Build Coastguard Worker shmkey = getipckey();
189*49cdfc7eSAndroid Build Coastguard Worker
190*49cdfc7eSAndroid Build Coastguard Worker /*
191*49cdfc7eSAndroid Build Coastguard Worker * Create a shared memory segment represented by SHMKEY of
192*49cdfc7eSAndroid Build Coastguard Worker * specified size 'newsize' and mode permissions 'SHM_MODE'.
193*49cdfc7eSAndroid Build Coastguard Worker */
194*49cdfc7eSAndroid Build Coastguard Worker shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE);
195*49cdfc7eSAndroid Build Coastguard Worker if (shmid == -1) {
196*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, NULL, "shmget() Failed to create a shared "
197*49cdfc7eSAndroid Build Coastguard Worker "memory, error:%d", errno);
198*49cdfc7eSAndroid Build Coastguard Worker }
199*49cdfc7eSAndroid Build Coastguard Worker
200*49cdfc7eSAndroid Build Coastguard Worker /*
201*49cdfc7eSAndroid Build Coastguard Worker * Attach the shared memory segment associated with the shared
202*49cdfc7eSAndroid Build Coastguard Worker * memory identifier specified by "shmid" to the data segment of
203*49cdfc7eSAndroid Build Coastguard Worker * the calling process at the first available address as selected
204*49cdfc7eSAndroid Build Coastguard Worker * by the system.
205*49cdfc7eSAndroid Build Coastguard Worker */
206*49cdfc7eSAndroid Build Coastguard Worker shmaddr = shmat(shmid, NULL, 0);
207*49cdfc7eSAndroid Build Coastguard Worker if (shmaddr == (void *)-1) {
208*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared "
209*49cdfc7eSAndroid Build Coastguard Worker "memory, error:%d", errno);
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker }
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker /*
214*49cdfc7eSAndroid Build Coastguard Worker * cleanup() - performs all ONE TIME cleanup for this test at
215*49cdfc7eSAndroid Build Coastguard Worker * completion or premature exit.
216*49cdfc7eSAndroid Build Coastguard Worker * Detach the shared memory segment and remove the shared memory
217*49cdfc7eSAndroid Build Coastguard Worker * identifier associated with the shared memory.
218*49cdfc7eSAndroid Build Coastguard Worker */
cleanup(void)219*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
220*49cdfc7eSAndroid Build Coastguard Worker {
221*49cdfc7eSAndroid Build Coastguard Worker
222*49cdfc7eSAndroid Build Coastguard Worker /*
223*49cdfc7eSAndroid Build Coastguard Worker * Detach the shared memory segment attached to
224*49cdfc7eSAndroid Build Coastguard Worker * the calling process's data segment
225*49cdfc7eSAndroid Build Coastguard Worker */
226*49cdfc7eSAndroid Build Coastguard Worker if (shmdt(shmaddr) < 0) {
227*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared "
228*49cdfc7eSAndroid Build Coastguard Worker "memory, error:%d", errno);
229*49cdfc7eSAndroid Build Coastguard Worker }
230*49cdfc7eSAndroid Build Coastguard Worker
231*49cdfc7eSAndroid Build Coastguard Worker /*
232*49cdfc7eSAndroid Build Coastguard Worker * Remove the shared memory identifier associated with
233*49cdfc7eSAndroid Build Coastguard Worker * the shared memory segment and destroy the shared memory
234*49cdfc7eSAndroid Build Coastguard Worker * segment.
235*49cdfc7eSAndroid Build Coastguard Worker */
236*49cdfc7eSAndroid Build Coastguard Worker if (shmctl(shmid, IPC_RMID, 0) < 0) {
237*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared "
238*49cdfc7eSAndroid Build Coastguard Worker "memory, error:%d", errno);
239*49cdfc7eSAndroid Build Coastguard Worker }
240*49cdfc7eSAndroid Build Coastguard Worker
241*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
242*49cdfc7eSAndroid Build Coastguard Worker
243*49cdfc7eSAndroid Build Coastguard Worker /* Exit the program */
244*49cdfc7eSAndroid Build Coastguard Worker
245*49cdfc7eSAndroid Build Coastguard Worker }
246