blob: f6c15cbd126a46fb790df77f58a5672cadac2bfc [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: mremap01
22 *
23 * Test Description:
24 * Verify that, mremap() succeeds when used to expand the existing
25 * virtual memory mapped region to the requested size where the
26 * virtual memory area was previously mapped to a file using mmap().
27 *
28 * Expected Result:
29 * mremap() should succeed returning the address of new virtual memory area.
subrata_modak4bb656a2009-02-26 12:02:09 +000030 * The expanded mapped memory region should be accessible and able to
plars865695b2001-08-27 22:15:12 +000031 * synchronize with the file.
32 *
33 * Algorithm:
34 * Setup:
35 * Setup signal handling.
36 * Create temporary directory.
37 * Pause for SIGUSR1 if option specified.
38 *
39 * Test:
40 * Loop if the proper options are given.
41 * Execute system call
42 * Check return code, if system call failed (return=-1)
subrata_modak56207ce2009-03-23 13:35:39 +000043 * Log the errno and Issue a FAIL message.
plars865695b2001-08-27 22:15:12 +000044 * Otherwise,
subrata_modak56207ce2009-03-23 13:35:39 +000045 * Verify the Functionality of system call
plars865695b2001-08-27 22:15:12 +000046 * if successful,
subrata_modak56207ce2009-03-23 13:35:39 +000047 * Issue Functionality-Pass message.
plars865695b2001-08-27 22:15:12 +000048 * Otherwise,
49 * Issue Functionality-Fail message.
50 * Cleanup:
51 * Print errno log and/or timing stats if options given
52 * Delete the temporary directory created.
53 *
54 * Usage: <for command-line>
55 * mremap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
56 * where, -c n : Run n copies concurrently.
57 * -f : Turn off functionality Testing.
58 * -i n : Execute test n times.
59 * -I x : Execute test for x seconds.
60 * -P x : Pause for x seconds between iterations.
61 * -t : Turn on syscall timing.
62 *
63 * HISTORY
64 * 07/2001 Ported by Wayne Boyer
65 *
iyermanoj874176f2001-11-09 07:07:02 +000066 * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com)
67 * Modified.
68 * - #include <linux/mman.h> should not be included as per man page for
69 * mremap, #include <sys/mman.h> alone should do the job. But inorder
70 * to include definition of MREMAP_MAYMOVE defined in bits/mman.h
71 * (included by sys/mman.h) __USE_GNU needs to be defined.
72 * There may be a more elegant way of doing this...
73 *
plars865695b2001-08-27 22:15:12 +000074 * RESTRICTIONS:
75 * None.
76 */
77#include <unistd.h>
78#include <errno.h>
iyermanoj874176f2001-11-09 07:07:02 +000079#define __USE_GNU
plars865695b2001-08-27 22:15:12 +000080#include <sys/mman.h>
iyermanoj874176f2001-11-09 07:07:02 +000081#undef __USE_GNU
plars865695b2001-08-27 22:15:12 +000082#include <fcntl.h>
83
84#include "test.h"
plars865695b2001-08-27 22:15:12 +000085
86#define TEMPFILE "mremapfile"
87
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020088char *TCID = "mremap01";
89int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000090char *addr; /* addr of memory mapped region */
91int memsize; /* memory mapped size */
92int newsize; /* new size of virtual memory block */
93int fildes; /* file descriptor for tempfile */
94
95void setup(); /* Main setup function of test */
96void cleanup(); /* cleanup function for the test */
97
subrata_modak56207ce2009-03-23 13:35:39 +000098int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000099{
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200100 const char *msg;
plars865695b2001-08-27 22:15:12 +0000101 int ind; /* counter variable */
subrata_modakbdbaec52009-02-26 12:14:51 +0000102
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800103 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -0800104 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800105
Caspar Zhangd59a6592013-03-07 14:59:12 +0800106 tst_count = 0;
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800107
108 setup();
109
110 /*
111 * Call mremap to expand the existing mapped
112 * memory region (memsize) by newsize limits.
113 */
114 addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE);
115
116 /* Check for the return value of mremap() */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200117 if (addr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800118 tst_brkm(TFAIL | TERRNO, cleanup, "mremap failed");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200119
120 /*
121 * Attempt to initialize the expanded memory
122 * mapped region with data. If the map area
123 * was bad, we'd get SIGSEGV.
124 */
125 for (ind = 0; ind < newsize; ind++) {
126 addr[ind] = (char)ind;
plars865695b2001-08-27 22:15:12 +0000127 }
128
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800129 /*
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 * Memory mapped area is good. Now, attempt
131 * to synchronize the mapped memory region
132 * with the file.
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800133 */
Cyril Hrubise38b9612014-06-02 17:20:57 +0200134 if (msync(addr, newsize, MS_SYNC) != 0) {
135 tst_resm(TFAIL | TERRNO, "msync failed to synch "
136 "mapped file");
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800137 } else {
Cyril Hrubise38b9612014-06-02 17:20:57 +0200138 tst_resm(TPASS, "Functionality of "
139 "mremap() is correct");
Garrett Cooper2c282152010-12-16 00:55:50 -0800140 }
plars865695b2001-08-27 22:15:12 +0000141
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800142 cleanup();
143 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -0800144}
plars865695b2001-08-27 22:15:12 +0000145
146/*
147 * void
148 * setup() - performs all ONE TIME setup for this test.
149 *
150 * Get system page size, Set the size of virtual memory area and the
151 * new size after resize,
152 * Creat a temporary directory and a file under it.
153 * Stratch the file size to the size of virtual memory area and
154 * write 1 byte (\0). Map the temporary file for the length of virtual
155 * memory (memsize) into memory.
156 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400157void setup(void)
plars865695b2001-08-27 22:15:12 +0000158{
subrata_modak56207ce2009-03-23 13:35:39 +0000159 int pagesz; /* system's page size */
plars865695b2001-08-27 22:15:12 +0000160
plars865695b2001-08-27 22:15:12 +0000161 tst_sig(NOFORK, DEF_HANDLER, cleanup);
162
plars865695b2001-08-27 22:15:12 +0000163 TEST_PAUSE;
164
165 /* Get the system page size */
166 if ((pagesz = getpagesize()) < 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800167 tst_brkm(TFAIL, NULL,
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800168 "getpagesize failed to get system page size");
plars865695b2001-08-27 22:15:12 +0000169 }
170
171 /* Get the size of virtual memory area to be mapped */
172 memsize = (1000 * pagesz);
173
174 /* Get the New size of virtual memory block after resize */
175 newsize = (memsize * 2);
176
plars865695b2001-08-27 22:15:12 +0000177 tst_tmpdir();
178
179 /* Creat a temporary file used for mapping */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0)
181 tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed",
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800182 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000183
184 /* Stretch the file to the size of virtual memory area */
subrata_modak56207ce2009-03-23 13:35:39 +0000185 if (lseek(fildes, (off_t) memsize, SEEK_SET) != (off_t) memsize) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800186 tst_brkm(TBROK | TERRNO, cleanup,
187 "lseeking to %d offset pos. failed", memsize);
plars865695b2001-08-27 22:15:12 +0000188 }
189
190 /* Write one byte data into temporary file */
191 if (write(fildes, "\0", 1) != 1) {
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800192 tst_brkm(TBROK, cleanup, "writing to %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000193 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000194
subrata_modak4bb656a2009-02-26 12:02:09 +0000195 /*
plars865695b2001-08-27 22:15:12 +0000196 * Call mmap to map virtual memory (memsize bytes) from the
197 * beginning of temporary file (offset is 0) into memory.
198 */
199 addr = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fildes, 0);
200
201 /* Check for the return value of mmap() */
202 if (addr == (char *)MAP_FAILED) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800203 tst_brkm(TBROK, cleanup, "mmaping Failed on %s", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000204 }
205
206 /* Stretch the file to newsize of virtual memory block */
subrata_modak56207ce2009-03-23 13:35:39 +0000207 if (lseek(fildes, (off_t) newsize, SEEK_SET) != (off_t) newsize) {
plars865695b2001-08-27 22:15:12 +0000208 tst_brkm(TBROK, cleanup, "lseek() to %d offset pos. Failed, "
209 "error=%d : %s", newsize, errno, strerror(errno));
210 }
211
212 /* Write one byte data into temporary file */
213 if (write(fildes, "\0", 1) != 1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 tst_brkm(TBROK | TERRNO, cleanup, "writing to %s failed",
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800215 TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000216 }
217}
218
219/*
220 * void
221 * cleanup() - performs all ONE TIME cleanup for this test at
222 * completion or premature exit.
subrata_modak56207ce2009-03-23 13:35:39 +0000223 * Unmap the mapped memory area done in the test.
224 * Close the temporary file.
225 * Remove the temporary directory.
plars865695b2001-08-27 22:15:12 +0000226 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400227void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000228{
plars865695b2001-08-27 22:15:12 +0000229
230 /* Unmap the mapped memory */
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800231 if (munmap(addr, newsize) != 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800232 tst_brkm(TBROK | TERRNO, NULL, "munmap failed");
plars865695b2001-08-27 22:15:12 +0000233
234 /* Close the temporary file */
235 if (close(fildes) < 0) {
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800236 tst_brkm(TBROK, NULL, "closing %s failed", TEMPFILE);
plars865695b2001-08-27 22:15:12 +0000237 }
238
plars865695b2001-08-27 22:15:12 +0000239 tst_rmdir();
Garrett Coopere5bcfc32010-12-19 10:25:43 -0800240}