xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mremap/mremap01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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: mremap01
22*49cdfc7eSAndroid Build Coastguard Worker  *
23*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
24*49cdfc7eSAndroid Build Coastguard Worker  *  Verify that, mremap() succeeds when used to expand the existing
25*49cdfc7eSAndroid Build Coastguard Worker  *  virtual memory mapped region to the requested size where the
26*49cdfc7eSAndroid Build Coastguard Worker  *  virtual memory area was previously mapped to a file using mmap().
27*49cdfc7eSAndroid Build Coastguard Worker  *
28*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
29*49cdfc7eSAndroid Build Coastguard Worker  *  mremap() should succeed returning the address of new virtual memory area.
30*49cdfc7eSAndroid Build Coastguard Worker  *  The expanded mapped memory region should be accessible and able to
31*49cdfc7eSAndroid Build Coastguard Worker  *  synchronize with the file.
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  * Algorithm:
34*49cdfc7eSAndroid Build Coastguard Worker  *  Setup:
35*49cdfc7eSAndroid Build Coastguard Worker  *   Setup signal handling.
36*49cdfc7eSAndroid Build Coastguard Worker  *   Create temporary directory.
37*49cdfc7eSAndroid Build Coastguard Worker  *   Pause for SIGUSR1 if option specified.
38*49cdfc7eSAndroid Build Coastguard Worker  *
39*49cdfc7eSAndroid Build Coastguard Worker  *  Test:
40*49cdfc7eSAndroid Build Coastguard Worker  *   Loop if the proper options are given.
41*49cdfc7eSAndroid Build Coastguard Worker  *   Execute system call
42*49cdfc7eSAndroid Build Coastguard Worker  *   Check return code, if system call failed (return=-1)
43*49cdfc7eSAndroid Build Coastguard Worker  *	Log the errno and Issue a FAIL message.
44*49cdfc7eSAndroid Build Coastguard Worker  *   Otherwise,
45*49cdfc7eSAndroid Build Coastguard Worker  *	Verify the Functionality of system call
46*49cdfc7eSAndroid Build Coastguard Worker  *      if successful,
47*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Pass message.
48*49cdfc7eSAndroid Build Coastguard Worker  *      Otherwise,
49*49cdfc7eSAndroid Build Coastguard Worker  *		Issue Functionality-Fail message.
50*49cdfc7eSAndroid Build Coastguard Worker  *  Cleanup:
51*49cdfc7eSAndroid Build Coastguard Worker  *   Print errno log and/or timing stats if options given
52*49cdfc7eSAndroid Build Coastguard Worker  *   Delete the temporary directory created.
53*49cdfc7eSAndroid Build Coastguard Worker  *
54*49cdfc7eSAndroid Build Coastguard Worker  * Usage:  <for command-line>
55*49cdfc7eSAndroid Build Coastguard Worker  *  mremap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
56*49cdfc7eSAndroid Build Coastguard Worker  *     where,  -c n : Run n copies concurrently.
57*49cdfc7eSAndroid Build Coastguard Worker  *             -f   : Turn off functionality Testing.
58*49cdfc7eSAndroid Build Coastguard Worker  *	       -i n : Execute test n times.
59*49cdfc7eSAndroid Build Coastguard Worker  *	       -I x : Execute test for x seconds.
60*49cdfc7eSAndroid Build Coastguard Worker  *	       -P x : Pause for x seconds between iterations.
61*49cdfc7eSAndroid Build Coastguard Worker  *	       -t   : Turn on syscall timing.
62*49cdfc7eSAndroid Build Coastguard Worker  *
63*49cdfc7eSAndroid Build Coastguard Worker  * HISTORY
64*49cdfc7eSAndroid Build Coastguard Worker  *	07/2001 Ported by Wayne Boyer
65*49cdfc7eSAndroid Build Coastguard Worker  *
66*49cdfc7eSAndroid Build Coastguard Worker  *      11/09/2001 Manoj Iyer ([email protected])
67*49cdfc7eSAndroid Build Coastguard Worker  *	Modified.
68*49cdfc7eSAndroid Build Coastguard Worker  *	- #include <linux/mman.h> should not be included as per man page for
69*49cdfc7eSAndroid Build Coastguard Worker  *	  mremap, #include <sys/mman.h> alone should do the job. But inorder
70*49cdfc7eSAndroid Build Coastguard Worker  *	  to include definition of MREMAP_MAYMOVE defined in bits/mman.h
71*49cdfc7eSAndroid Build Coastguard Worker  *	  (included by sys/mman.h) __USE_GNU needs to be defined.
72*49cdfc7eSAndroid Build Coastguard Worker  *	  There may be a more elegant way of doing this...
73*49cdfc7eSAndroid Build Coastguard Worker  *
74*49cdfc7eSAndroid Build Coastguard Worker  * RESTRICTIONS:
75*49cdfc7eSAndroid Build Coastguard Worker  *  None.
76*49cdfc7eSAndroid Build Coastguard Worker  */
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
79*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
80*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
81*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
82*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
85*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE	"mremapfile"
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mremap01";
90*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
91*49cdfc7eSAndroid Build Coastguard Worker char *addr;			/* addr of memory mapped region */
92*49cdfc7eSAndroid Build Coastguard Worker int memsize;			/* memory mapped size */
93*49cdfc7eSAndroid Build Coastguard Worker int newsize;			/* new size of virtual memory block */
94*49cdfc7eSAndroid Build Coastguard Worker int fildes;			/* file descriptor for tempfile */
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker void setup();			/* Main setup function of test */
97*49cdfc7eSAndroid Build Coastguard Worker void cleanup();			/* cleanup function for the test */
98*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)99*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
100*49cdfc7eSAndroid Build Coastguard Worker {
101*49cdfc7eSAndroid Build Coastguard Worker 	int ind;		/* counter variable */
102*49cdfc7eSAndroid Build Coastguard Worker 
103*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
104*49cdfc7eSAndroid Build Coastguard Worker 
105*49cdfc7eSAndroid Build Coastguard Worker 	tst_count = 0;
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker 	setup();
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	/*
110*49cdfc7eSAndroid Build Coastguard Worker 	 * Call mremap to expand the existing mapped
111*49cdfc7eSAndroid Build Coastguard Worker 	 * memory region (memsize) by newsize limits.
112*49cdfc7eSAndroid Build Coastguard Worker 	 */
113*49cdfc7eSAndroid Build Coastguard Worker 	addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
114*49cdfc7eSAndroid Build Coastguard Worker 
115*49cdfc7eSAndroid Build Coastguard Worker 	/* Check for the return value of mremap() */
116*49cdfc7eSAndroid Build Coastguard Worker 	if (addr == MAP_FAILED)
117*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TERRNO, cleanup, "mremap failed");
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	/*
120*49cdfc7eSAndroid Build Coastguard Worker 	 * Attempt to initialize the expanded memory
121*49cdfc7eSAndroid Build Coastguard Worker 	 * mapped region with data. If the map area
122*49cdfc7eSAndroid Build Coastguard Worker 	 * was bad, we'd get SIGSEGV.
123*49cdfc7eSAndroid Build Coastguard Worker 	 */
124*49cdfc7eSAndroid Build Coastguard Worker 	for (ind = 0; ind < newsize; ind++) {
125*49cdfc7eSAndroid Build Coastguard Worker 		addr[ind] = (char)ind;
126*49cdfc7eSAndroid Build Coastguard Worker 	}
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 	/*
129*49cdfc7eSAndroid Build Coastguard Worker 	 * Memory mapped area is good. Now, attempt
130*49cdfc7eSAndroid Build Coastguard Worker 	 * to synchronize the mapped memory region
131*49cdfc7eSAndroid Build Coastguard Worker 	 * with the file.
132*49cdfc7eSAndroid Build Coastguard Worker 	 */
133*49cdfc7eSAndroid Build Coastguard Worker 	if (msync(addr, newsize, MS_SYNC) != 0) {
134*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL | TERRNO, "msync failed to synch "
135*49cdfc7eSAndroid Build Coastguard Worker 			 "mapped file");
136*49cdfc7eSAndroid Build Coastguard Worker 	} else {
137*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Functionality of "
138*49cdfc7eSAndroid Build Coastguard Worker 			 "mremap() is correct");
139*49cdfc7eSAndroid Build Coastguard Worker 	}
140*49cdfc7eSAndroid Build Coastguard Worker 
141*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
142*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker /*
146*49cdfc7eSAndroid Build Coastguard Worker  * void
147*49cdfc7eSAndroid Build Coastguard Worker  * setup() - performs all ONE TIME setup for this test.
148*49cdfc7eSAndroid Build Coastguard Worker  *
149*49cdfc7eSAndroid Build Coastguard Worker  * Get system page size, Set the size of virtual memory area and the
150*49cdfc7eSAndroid Build Coastguard Worker  * new size after resize,
151*49cdfc7eSAndroid Build Coastguard Worker  * Creat a temporary directory and a file under it.
152*49cdfc7eSAndroid Build Coastguard Worker  * Stratch the file size to the size of virtual memory area and
153*49cdfc7eSAndroid Build Coastguard Worker  * write 1 byte (\0). Map the temporary file for the length of virtual
154*49cdfc7eSAndroid Build Coastguard Worker  * memory (memsize) into memory.
155*49cdfc7eSAndroid Build Coastguard Worker  */
setup(void)156*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
157*49cdfc7eSAndroid Build Coastguard Worker {
158*49cdfc7eSAndroid Build Coastguard Worker 	int pagesz;		/* system's page size */
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
161*49cdfc7eSAndroid Build Coastguard Worker 
162*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
163*49cdfc7eSAndroid Build Coastguard Worker 
164*49cdfc7eSAndroid Build Coastguard Worker 	/* Get the system page size */
165*49cdfc7eSAndroid Build Coastguard Worker 	if ((pagesz = getpagesize()) < 0) {
166*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL, NULL,
167*49cdfc7eSAndroid Build Coastguard Worker 			 "getpagesize failed to get system page size");
168*49cdfc7eSAndroid Build Coastguard Worker 	}
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 	/* Get the size of virtual memory area to be mapped */
171*49cdfc7eSAndroid Build Coastguard Worker 	memsize = (1000 * pagesz);
172*49cdfc7eSAndroid Build Coastguard Worker 
173*49cdfc7eSAndroid Build Coastguard Worker 	/* Get the New size of virtual memory block after resize */
174*49cdfc7eSAndroid Build Coastguard Worker 	newsize = (memsize * 2);
175*49cdfc7eSAndroid Build Coastguard Worker 
176*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
177*49cdfc7eSAndroid Build Coastguard Worker 
178*49cdfc7eSAndroid Build Coastguard Worker 	/* Creat a temporary file used for mapping */
179*49cdfc7eSAndroid Build Coastguard Worker 	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
180*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
181*49cdfc7eSAndroid Build Coastguard Worker 			 TEMPFILE);
182*49cdfc7eSAndroid Build Coastguard Worker 
183*49cdfc7eSAndroid Build Coastguard Worker 	/* Stretch the file to the size of virtual memory area */
184*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fildes, (off_t) memsize, SEEK_SET) != (off_t) memsize) {
185*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup,
186*49cdfc7eSAndroid Build Coastguard Worker 			 "lseeking to %d offset pos. failed", memsize);
187*49cdfc7eSAndroid Build Coastguard Worker 	}
188*49cdfc7eSAndroid Build Coastguard Worker 
189*49cdfc7eSAndroid Build Coastguard Worker 	/* Write one byte data into temporary file */
190*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fildes, "\0", 1) != 1) {
191*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "writing to %s failed", TEMPFILE);
192*49cdfc7eSAndroid Build Coastguard Worker 	}
193*49cdfc7eSAndroid Build Coastguard Worker 
194*49cdfc7eSAndroid Build Coastguard Worker 	/*
195*49cdfc7eSAndroid Build Coastguard Worker 	 * Call mmap to map virtual memory (memsize bytes) from the
196*49cdfc7eSAndroid Build Coastguard Worker 	 * beginning of temporary file (offset is 0) into memory.
197*49cdfc7eSAndroid Build Coastguard Worker 	 */
198*49cdfc7eSAndroid Build Coastguard Worker 	addr = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fildes, 0);
199*49cdfc7eSAndroid Build Coastguard Worker 
200*49cdfc7eSAndroid Build Coastguard Worker 	/* Check for the return value of mmap() */
201*49cdfc7eSAndroid Build Coastguard Worker 	if (addr == (char *)MAP_FAILED) {
202*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "mmaping Failed on %s", TEMPFILE);
203*49cdfc7eSAndroid Build Coastguard Worker 	}
204*49cdfc7eSAndroid Build Coastguard Worker 
205*49cdfc7eSAndroid Build Coastguard Worker 	/* Stretch the file to newsize of virtual memory block */
206*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fildes, (off_t) newsize, SEEK_SET) != (off_t) newsize) {
207*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK, cleanup, "lseek() to %d offset pos. Failed, "
208*49cdfc7eSAndroid Build Coastguard Worker 			 "error=%d : %s", newsize, errno, strerror(errno));
209*49cdfc7eSAndroid Build Coastguard Worker 	}
210*49cdfc7eSAndroid Build Coastguard Worker 
211*49cdfc7eSAndroid Build Coastguard Worker 	/* Write one byte data into temporary file */
212*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fildes, "\0", 1) != 1) {
213*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "writing to %s failed",
214*49cdfc7eSAndroid Build Coastguard Worker 			 TEMPFILE);
215*49cdfc7eSAndroid Build Coastguard Worker 	}
216*49cdfc7eSAndroid Build Coastguard Worker }
217*49cdfc7eSAndroid Build Coastguard Worker 
218*49cdfc7eSAndroid Build Coastguard Worker /*
219*49cdfc7eSAndroid Build Coastguard Worker  * void
220*49cdfc7eSAndroid Build Coastguard Worker  * cleanup() - performs all ONE TIME cleanup for this test at
221*49cdfc7eSAndroid Build Coastguard Worker  *             completion or premature exit.
222*49cdfc7eSAndroid Build Coastguard Worker  *	       Unmap the mapped memory area done in the test.
223*49cdfc7eSAndroid Build Coastguard Worker  *	       Close the temporary file.
224*49cdfc7eSAndroid Build Coastguard Worker  *	       Remove the temporary directory.
225*49cdfc7eSAndroid Build Coastguard Worker  */
cleanup(void)226*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
227*49cdfc7eSAndroid Build Coastguard Worker {
228*49cdfc7eSAndroid Build Coastguard Worker 
229*49cdfc7eSAndroid Build Coastguard Worker 	/* Unmap the mapped memory */
230*49cdfc7eSAndroid Build Coastguard Worker 	if (munmap(addr, newsize) != 0)
231*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, NULL, "munmap failed");
232*49cdfc7eSAndroid Build Coastguard Worker 
233*49cdfc7eSAndroid Build Coastguard Worker 	/* Close the temporary file */
234*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(NULL, fildes);
235*49cdfc7eSAndroid Build Coastguard Worker 
236*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
237*49cdfc7eSAndroid Build Coastguard Worker }
238