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