1865695bbc89088b9526ea9045410e5afb70a985cplars/*
2865695bbc89088b9526ea9045410e5afb70a985cplars *
3865695bbc89088b9526ea9045410e5afb70a985cplars *   Copyright (c) International Business Machines  Corp., 2001
4865695bbc89088b9526ea9045410e5afb70a985cplars *
5865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is free software;  you can redistribute it and/or modify
6865695bbc89088b9526ea9045410e5afb70a985cplars *   it under the terms of the GNU General Public License as published by
7865695bbc89088b9526ea9045410e5afb70a985cplars *   the Free Software Foundation; either version 2 of the License, or
8865695bbc89088b9526ea9045410e5afb70a985cplars *   (at your option) any later version.
9865695bbc89088b9526ea9045410e5afb70a985cplars *
10865695bbc89088b9526ea9045410e5afb70a985cplars *   This program is distributed in the hope that it will be useful,
11865695bbc89088b9526ea9045410e5afb70a985cplars *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12865695bbc89088b9526ea9045410e5afb70a985cplars *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13865695bbc89088b9526ea9045410e5afb70a985cplars *   the GNU General Public License for more details.
14865695bbc89088b9526ea9045410e5afb70a985cplars *
15865695bbc89088b9526ea9045410e5afb70a985cplars *   You should have received a copy of the GNU General Public License
16865695bbc89088b9526ea9045410e5afb70a985cplars *   along with this program;  if not, write to the Free Software
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18865695bbc89088b9526ea9045410e5afb70a985cplars */
19865695bbc89088b9526ea9045410e5afb70a985cplars
20865695bbc89088b9526ea9045410e5afb70a985cplars/*
21865695bbc89088b9526ea9045410e5afb70a985cplars * Test Name: mremap01
22865695bbc89088b9526ea9045410e5afb70a985cplars *
23865695bbc89088b9526ea9045410e5afb70a985cplars * Test Description:
24865695bbc89088b9526ea9045410e5afb70a985cplars *  Verify that, mremap() succeeds when used to expand the existing
25865695bbc89088b9526ea9045410e5afb70a985cplars *  virtual memory mapped region to the requested size where the
26865695bbc89088b9526ea9045410e5afb70a985cplars *  virtual memory area was previously mapped to a file using mmap().
27865695bbc89088b9526ea9045410e5afb70a985cplars *
28865695bbc89088b9526ea9045410e5afb70a985cplars * Expected Result:
29865695bbc89088b9526ea9045410e5afb70a985cplars *  mremap() should succeed returning the address of new virtual memory area.
304bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak *  The expanded mapped memory region should be accessible and able to
31865695bbc89088b9526ea9045410e5afb70a985cplars *  synchronize with the file.
32865695bbc89088b9526ea9045410e5afb70a985cplars *
33865695bbc89088b9526ea9045410e5afb70a985cplars * Algorithm:
34865695bbc89088b9526ea9045410e5afb70a985cplars *  Setup:
35865695bbc89088b9526ea9045410e5afb70a985cplars *   Setup signal handling.
36865695bbc89088b9526ea9045410e5afb70a985cplars *   Create temporary directory.
37865695bbc89088b9526ea9045410e5afb70a985cplars *   Pause for SIGUSR1 if option specified.
38865695bbc89088b9526ea9045410e5afb70a985cplars *
39865695bbc89088b9526ea9045410e5afb70a985cplars *  Test:
40865695bbc89088b9526ea9045410e5afb70a985cplars *   Loop if the proper options are given.
41865695bbc89088b9526ea9045410e5afb70a985cplars *   Execute system call
42865695bbc89088b9526ea9045410e5afb70a985cplars *   Check return code, if system call failed (return=-1)
4356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Log the errno and Issue a FAIL message.
44865695bbc89088b9526ea9045410e5afb70a985cplars *   Otherwise,
4556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	Verify the Functionality of system call
46865695bbc89088b9526ea9045410e5afb70a985cplars *      if successful,
4756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *		Issue Functionality-Pass message.
48865695bbc89088b9526ea9045410e5afb70a985cplars *      Otherwise,
49865695bbc89088b9526ea9045410e5afb70a985cplars *		Issue Functionality-Fail message.
50865695bbc89088b9526ea9045410e5afb70a985cplars *  Cleanup:
51865695bbc89088b9526ea9045410e5afb70a985cplars *   Print errno log and/or timing stats if options given
52865695bbc89088b9526ea9045410e5afb70a985cplars *   Delete the temporary directory created.
53865695bbc89088b9526ea9045410e5afb70a985cplars *
54865695bbc89088b9526ea9045410e5afb70a985cplars * Usage:  <for command-line>
55865695bbc89088b9526ea9045410e5afb70a985cplars *  mremap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
56865695bbc89088b9526ea9045410e5afb70a985cplars *     where,  -c n : Run n copies concurrently.
57865695bbc89088b9526ea9045410e5afb70a985cplars *             -f   : Turn off functionality Testing.
58865695bbc89088b9526ea9045410e5afb70a985cplars *	       -i n : Execute test n times.
59865695bbc89088b9526ea9045410e5afb70a985cplars *	       -I x : Execute test for x seconds.
60865695bbc89088b9526ea9045410e5afb70a985cplars *	       -P x : Pause for x seconds between iterations.
61865695bbc89088b9526ea9045410e5afb70a985cplars *	       -t   : Turn on syscall timing.
62865695bbc89088b9526ea9045410e5afb70a985cplars *
63865695bbc89088b9526ea9045410e5afb70a985cplars * HISTORY
64865695bbc89088b9526ea9045410e5afb70a985cplars *	07/2001 Ported by Wayne Boyer
65865695bbc89088b9526ea9045410e5afb70a985cplars *
66874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *      11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
67874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	Modified.
68874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	- #include <linux/mman.h> should not be included as per man page for
69874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	  mremap, #include <sys/mman.h> alone should do the job. But inorder
70874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	  to include definition of MREMAP_MAYMOVE defined in bits/mman.h
71874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	  (included by sys/mman.h) __USE_GNU needs to be defined.
72874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *	  There may be a more elegant way of doing this...
73874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj *
74865695bbc89088b9526ea9045410e5afb70a985cplars * RESTRICTIONS:
75865695bbc89088b9526ea9045410e5afb70a985cplars *  None.
76865695bbc89088b9526ea9045410e5afb70a985cplars */
77865695bbc89088b9526ea9045410e5afb70a985cplars#include <unistd.h>
78865695bbc89088b9526ea9045410e5afb70a985cplars#include <errno.h>
79874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj#define __USE_GNU
80865695bbc89088b9526ea9045410e5afb70a985cplars#include <sys/mman.h>
81874176f950d2ee54ec3b28246157138830ffc0d9iyermanoj#undef __USE_GNU
82865695bbc89088b9526ea9045410e5afb70a985cplars#include <fcntl.h>
83865695bbc89088b9526ea9045410e5afb70a985cplars
84865695bbc89088b9526ea9045410e5afb70a985cplars#include "test.h"
85865695bbc89088b9526ea9045410e5afb70a985cplars
86865695bbc89088b9526ea9045410e5afb70a985cplars#define TEMPFILE	"mremapfile"
87865695bbc89088b9526ea9045410e5afb70a985cplars
88fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubischar *TCID = "mremap01";
89fdce7d5e2a219d201a2b0e3bab6b61b01ec1d716Cyril Hrubisint TST_TOTAL = 1;
90865695bbc89088b9526ea9045410e5afb70a985cplarschar *addr;			/* addr of memory mapped region */
91865695bbc89088b9526ea9045410e5afb70a985cplarsint memsize;			/* memory mapped size */
92865695bbc89088b9526ea9045410e5afb70a985cplarsint newsize;			/* new size of virtual memory block */
93865695bbc89088b9526ea9045410e5afb70a985cplarsint fildes;			/* file descriptor for tempfile */
94865695bbc89088b9526ea9045410e5afb70a985cplars
95865695bbc89088b9526ea9045410e5afb70a985cplarsvoid setup();			/* Main setup function of test */
96865695bbc89088b9526ea9045410e5afb70a985cplarsvoid cleanup();			/* cleanup function for the test */
97865695bbc89088b9526ea9045410e5afb70a985cplars
9856207cec7732e09c216c751c0b5f88a242bacae6subrata_modakint main(int ac, char **av)
99865695bbc89088b9526ea9045410e5afb70a985cplars{
100865695bbc89088b9526ea9045410e5afb70a985cplars	int ind;		/* counter variable */
101bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
102d6d11d08678aac1ed2c370ea8e42e5f45aea07beCyril Hrubis	tst_parse_opts(ac, av, NULL, NULL);
103865695bbc89088b9526ea9045410e5afb70a985cplars
104d59a659cd639ca2780b00049d102acd2a783d585Caspar Zhang	tst_count = 0;
105865695bbc89088b9526ea9045410e5afb70a985cplars
1067d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	setup();
1077d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper
1087d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	/*
1097d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	 * Call mremap to expand the existing mapped
1107d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	 * memory region (memsize) by newsize limits.
1117d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	 */
1127d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
113865695bbc89088b9526ea9045410e5afb70a985cplars
1147d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	/* Check for the return value of mremap() */
115e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	if (addr == MAP_FAILED)
116354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TFAIL | TERRNO, cleanup, "mremap failed");
117e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis
118e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	/*
119e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * Attempt to initialize the expanded memory
120e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * mapped region with data. If the map area
121e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * was bad, we'd get SIGSEGV.
122e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 */
123e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	for (ind = 0; ind < newsize; ind++) {
124e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		addr[ind] = (char)ind;
1257d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	}
126865695bbc89088b9526ea9045410e5afb70a985cplars
1277d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	/*
128e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * Memory mapped area is good. Now, attempt
129e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * to synchronize the mapped memory region
130e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	 * with the file.
1317d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	 */
132e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis	if (msync(addr, newsize, MS_SYNC) != 0) {
133e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		tst_resm(TFAIL | TERRNO, "msync failed to synch "
134e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 "mapped file");
1357d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	} else {
136e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis		tst_resm(TPASS, "Functionality of "
137e38b961c385192f0d804914b77bd590734b42e75Cyril Hrubis			 "mremap() is correct");
1382c28215423293e443469a07ae7011135d058b671Garrett Cooper	}
139865695bbc89088b9526ea9045410e5afb70a985cplars
1407d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	cleanup();
1417d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	tst_exit();
1422c28215423293e443469a07ae7011135d058b671Garrett Cooper}
143865695bbc89088b9526ea9045410e5afb70a985cplars
144865695bbc89088b9526ea9045410e5afb70a985cplars/*
145865695bbc89088b9526ea9045410e5afb70a985cplars * void
146865695bbc89088b9526ea9045410e5afb70a985cplars * setup() - performs all ONE TIME setup for this test.
147865695bbc89088b9526ea9045410e5afb70a985cplars *
148865695bbc89088b9526ea9045410e5afb70a985cplars * Get system page size, Set the size of virtual memory area and the
149865695bbc89088b9526ea9045410e5afb70a985cplars * new size after resize,
150865695bbc89088b9526ea9045410e5afb70a985cplars * Creat a temporary directory and a file under it.
151865695bbc89088b9526ea9045410e5afb70a985cplars * Stratch the file size to the size of virtual memory area and
152865695bbc89088b9526ea9045410e5afb70a985cplars * write 1 byte (\0). Map the temporary file for the length of virtual
153865695bbc89088b9526ea9045410e5afb70a985cplars * memory (memsize) into memory.
154865695bbc89088b9526ea9045410e5afb70a985cplars */
155c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid setup(void)
156865695bbc89088b9526ea9045410e5afb70a985cplars{
15756207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	int pagesz;		/* system's page size */
158865695bbc89088b9526ea9045410e5afb70a985cplars
159865695bbc89088b9526ea9045410e5afb70a985cplars	tst_sig(NOFORK, DEF_HANDLER, cleanup);
160865695bbc89088b9526ea9045410e5afb70a985cplars
161865695bbc89088b9526ea9045410e5afb70a985cplars	TEST_PAUSE;
162865695bbc89088b9526ea9045410e5afb70a985cplars
163865695bbc89088b9526ea9045410e5afb70a985cplars	/* Get the system page size */
164865695bbc89088b9526ea9045410e5afb70a985cplars	if ((pagesz = getpagesize()) < 0) {
16553740500924f6439623a8ac256b5be2d6c59ed1fGarrett Cooper		tst_brkm(TFAIL, NULL,
1667d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper			 "getpagesize failed to get system page size");
167865695bbc89088b9526ea9045410e5afb70a985cplars	}
168865695bbc89088b9526ea9045410e5afb70a985cplars
169865695bbc89088b9526ea9045410e5afb70a985cplars	/* Get the size of virtual memory area to be mapped */
170865695bbc89088b9526ea9045410e5afb70a985cplars	memsize = (1000 * pagesz);
171865695bbc89088b9526ea9045410e5afb70a985cplars
172865695bbc89088b9526ea9045410e5afb70a985cplars	/* Get the New size of virtual memory block after resize */
173865695bbc89088b9526ea9045410e5afb70a985cplars	newsize = (memsize * 2);
174865695bbc89088b9526ea9045410e5afb70a985cplars
175865695bbc89088b9526ea9045410e5afb70a985cplars	tst_tmpdir();
176865695bbc89088b9526ea9045410e5afb70a985cplars
177865695bbc89088b9526ea9045410e5afb70a985cplars	/* Creat a temporary file used for mapping */
178354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
179354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
1807d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper			 TEMPFILE);
181865695bbc89088b9526ea9045410e5afb70a985cplars
182865695bbc89088b9526ea9045410e5afb70a985cplars	/* Stretch the file to the size of virtual memory area */
18356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (lseek(fildes, (off_t) memsize, SEEK_SET) != (off_t) memsize) {
184354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup,
185354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			 "lseeking to %d offset pos. failed", memsize);
186865695bbc89088b9526ea9045410e5afb70a985cplars	}
187865695bbc89088b9526ea9045410e5afb70a985cplars
188865695bbc89088b9526ea9045410e5afb70a985cplars	/* Write one byte data into temporary file */
189865695bbc89088b9526ea9045410e5afb70a985cplars	if (write(fildes, "\0", 1) != 1) {
1907d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper		tst_brkm(TBROK, cleanup, "writing to %s failed", TEMPFILE);
191865695bbc89088b9526ea9045410e5afb70a985cplars	}
192bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
1934bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	/*
194865695bbc89088b9526ea9045410e5afb70a985cplars	 * Call mmap to map virtual memory (memsize bytes) from the
195865695bbc89088b9526ea9045410e5afb70a985cplars	 * beginning of temporary file (offset is 0) into memory.
196865695bbc89088b9526ea9045410e5afb70a985cplars	 */
197865695bbc89088b9526ea9045410e5afb70a985cplars	addr = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fildes, 0);
198865695bbc89088b9526ea9045410e5afb70a985cplars
199865695bbc89088b9526ea9045410e5afb70a985cplars	/* Check for the return value of mmap() */
200865695bbc89088b9526ea9045410e5afb70a985cplars	if (addr == (char *)MAP_FAILED) {
201354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK, cleanup, "mmaping Failed on %s", TEMPFILE);
202865695bbc89088b9526ea9045410e5afb70a985cplars	}
203865695bbc89088b9526ea9045410e5afb70a985cplars
204865695bbc89088b9526ea9045410e5afb70a985cplars	/* Stretch the file to newsize of virtual memory block */
20556207cec7732e09c216c751c0b5f88a242bacae6subrata_modak	if (lseek(fildes, (off_t) newsize, SEEK_SET) != (off_t) newsize) {
206865695bbc89088b9526ea9045410e5afb70a985cplars		tst_brkm(TBROK, cleanup, "lseek() to %d offset pos. Failed, "
207865695bbc89088b9526ea9045410e5afb70a985cplars			 "error=%d : %s", newsize, errno, strerror(errno));
208865695bbc89088b9526ea9045410e5afb70a985cplars	}
209865695bbc89088b9526ea9045410e5afb70a985cplars
210865695bbc89088b9526ea9045410e5afb70a985cplars	/* Write one byte data into temporary file */
211865695bbc89088b9526ea9045410e5afb70a985cplars	if (write(fildes, "\0", 1) != 1) {
212354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, cleanup, "writing to %s failed",
2137d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper			 TEMPFILE);
214865695bbc89088b9526ea9045410e5afb70a985cplars	}
215865695bbc89088b9526ea9045410e5afb70a985cplars}
216865695bbc89088b9526ea9045410e5afb70a985cplars
217865695bbc89088b9526ea9045410e5afb70a985cplars/*
218865695bbc89088b9526ea9045410e5afb70a985cplars * void
219865695bbc89088b9526ea9045410e5afb70a985cplars * cleanup() - performs all ONE TIME cleanup for this test at
220865695bbc89088b9526ea9045410e5afb70a985cplars *             completion or premature exit.
22156207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	       Unmap the mapped memory area done in the test.
22256207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	       Close the temporary file.
22356207cec7732e09c216c751c0b5f88a242bacae6subrata_modak *	       Remove the temporary directory.
224865695bbc89088b9526ea9045410e5afb70a985cplars */
225c57fba5535abf457e33dd7a986b6c512d95cdef6Mike Frysingervoid cleanup(void)
226865695bbc89088b9526ea9045410e5afb70a985cplars{
227865695bbc89088b9526ea9045410e5afb70a985cplars
228865695bbc89088b9526ea9045410e5afb70a985cplars	/* Unmap the mapped memory */
2297d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper	if (munmap(addr, newsize) != 0)
230354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tst_brkm(TBROK | TERRNO, NULL, "munmap failed");
231865695bbc89088b9526ea9045410e5afb70a985cplars
232865695bbc89088b9526ea9045410e5afb70a985cplars	/* Close the temporary file */
233865695bbc89088b9526ea9045410e5afb70a985cplars	if (close(fildes) < 0) {
2347d0a4a57fbcd47f72b67c08df532e8ef47f6fdaeGarrett Cooper		tst_brkm(TBROK, NULL, "closing %s failed", TEMPFILE);
235865695bbc89088b9526ea9045410e5afb70a985cplars	}
236865695bbc89088b9526ea9045410e5afb70a985cplars
237865695bbc89088b9526ea9045410e5afb70a985cplars	tst_rmdir();
238e5bcfc37aece4f4747e1a59451fa7ea66f640516Garrett Cooper}
239