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: munmap01
22f08c3bdfSopenharmony_ci *
23f08c3bdfSopenharmony_ci * Test Description:
24f08c3bdfSopenharmony_ci *  Verify that, munmap call will succeed to unmap a mapped file or
25f08c3bdfSopenharmony_ci *  anonymous shared memory region from the calling process's address space
26f08c3bdfSopenharmony_ci *  and after successful completion of munmap, the unmapped region is no
27f08c3bdfSopenharmony_ci *  longer accessible.
28f08c3bdfSopenharmony_ci *
29f08c3bdfSopenharmony_ci * Expected Result:
30f08c3bdfSopenharmony_ci *  munmap call should succeed to unmap a mapped file or anonymous shared
31f08c3bdfSopenharmony_ci *  memory region from the process's address space and it returns with a
32f08c3bdfSopenharmony_ci *  value 0, further reference to the unmapped region should result in a
33f08c3bdfSopenharmony_ci *  segmentation fault (SIGSEGV).
34f08c3bdfSopenharmony_ci *
35f08c3bdfSopenharmony_ci * Algorithm:
36f08c3bdfSopenharmony_ci *  Setup:
37f08c3bdfSopenharmony_ci *   Setup signal handling.
38f08c3bdfSopenharmony_ci *   Create temporary directory.
39f08c3bdfSopenharmony_ci *   Pause for SIGUSR1 if option specified.
40f08c3bdfSopenharmony_ci *
41f08c3bdfSopenharmony_ci *  Test:
42f08c3bdfSopenharmony_ci *   Loop if the proper options are given.
43f08c3bdfSopenharmony_ci *   Execute system call
44f08c3bdfSopenharmony_ci *   Check return code, if system call failed (return=-1)
45f08c3bdfSopenharmony_ci *   	Log the errno and Issue a FAIL message.
46f08c3bdfSopenharmony_ci *   Otherwise,
47f08c3bdfSopenharmony_ci *   	Verify the Functionality of system call
48f08c3bdfSopenharmony_ci *      if successful,
49f08c3bdfSopenharmony_ci *      	Issue Functionality-Pass message.
50f08c3bdfSopenharmony_ci *      Otherwise,
51f08c3bdfSopenharmony_ci *		Issue Functionality-Fail message.
52f08c3bdfSopenharmony_ci *  Cleanup:
53f08c3bdfSopenharmony_ci *   Print errno log and/or timing stats if options given
54f08c3bdfSopenharmony_ci *   Delete the temporary directory created.
55f08c3bdfSopenharmony_ci *
56f08c3bdfSopenharmony_ci * Usage:  <for command-line>
57f08c3bdfSopenharmony_ci *  munmap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
58f08c3bdfSopenharmony_ci *     where,  -c n : Run n copies concurrently.
59f08c3bdfSopenharmony_ci *             -f   : Turn off functionality Testing.
60f08c3bdfSopenharmony_ci *	       -i n : Execute test n times.
61f08c3bdfSopenharmony_ci *	       -I x : Execute test for x seconds.
62f08c3bdfSopenharmony_ci *	       -P x : Pause for x seconds between iterations.
63f08c3bdfSopenharmony_ci *	       -t   : Turn on syscall timing.
64f08c3bdfSopenharmony_ci *
65f08c3bdfSopenharmony_ci * HISTORY
66f08c3bdfSopenharmony_ci *	07/2001 Ported by Wayne Boyer
67f08c3bdfSopenharmony_ci *
68f08c3bdfSopenharmony_ci * RESTRICTIONS:
69f08c3bdfSopenharmony_ci *  None.$
70f08c3bdfSopenharmony_ci */
71f08c3bdfSopenharmony_ci#include <errno.h>
72f08c3bdfSopenharmony_ci#include <unistd.h>
73f08c3bdfSopenharmony_ci#include <fcntl.h>
74f08c3bdfSopenharmony_ci#include <sys/stat.h>
75f08c3bdfSopenharmony_ci#include <sys/mman.h>
76f08c3bdfSopenharmony_ci
77f08c3bdfSopenharmony_ci#include "test.h"
78f08c3bdfSopenharmony_ci#include "safe_macros.h"
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci#define TEMPFILE	"mmapfile"
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_cichar *TCID = "munmap01";
83f08c3bdfSopenharmony_ciint TST_TOTAL = 1;
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_cichar *addr;			/* addr of memory mapped region */
86f08c3bdfSopenharmony_ciint fildes;			/* file descriptor for tempfile */
87f08c3bdfSopenharmony_ciunsigned int map_len;		/* length of the region to be mapped */
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_civoid setup();			/* Main setup function of test */
90f08c3bdfSopenharmony_civoid cleanup();			/* cleanup function for the test */
91f08c3bdfSopenharmony_civoid sig_handler();		/* signal catching function */
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ciint main(int ac, char **av)
94f08c3bdfSopenharmony_ci{
95f08c3bdfSopenharmony_ci	int lc;
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
98f08c3bdfSopenharmony_ci
99f08c3bdfSopenharmony_ci	for (lc = 0; TEST_LOOPING(lc); lc++) {
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_ci		tst_count = 0;
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci		setup();
104f08c3bdfSopenharmony_ci
105f08c3bdfSopenharmony_ci		/*
106f08c3bdfSopenharmony_ci		 * Call munmap to unmap the mapped region of the
107f08c3bdfSopenharmony_ci		 * temporary file from the calling process's address space.
108f08c3bdfSopenharmony_ci		 */
109f08c3bdfSopenharmony_ci		TEST(munmap(addr, map_len));
110f08c3bdfSopenharmony_ci
111f08c3bdfSopenharmony_ci		/* Check for the return value of munmap() */
112f08c3bdfSopenharmony_ci		if (TEST_RETURN == -1) {
113f08c3bdfSopenharmony_ci			tst_resm(TFAIL, "munmap() fails, errno=%d : %s",
114f08c3bdfSopenharmony_ci				 TEST_ERRNO, strerror(TEST_ERRNO));
115f08c3bdfSopenharmony_ci			continue;
116f08c3bdfSopenharmony_ci		}
117f08c3bdfSopenharmony_ci#ifdef UCLINUX
118f08c3bdfSopenharmony_ci		/*
119f08c3bdfSopenharmony_ci		 * No SIGSEGV on uClinux since
120f08c3bdfSopenharmony_ci		 * MMU not implemented on uClinux
121f08c3bdfSopenharmony_ci		 */
122f08c3bdfSopenharmony_ci		tst_resm(TPASS, "call succedded");
123f08c3bdfSopenharmony_ci#else
124f08c3bdfSopenharmony_ci		/*
125f08c3bdfSopenharmony_ci		 * Check whether further reference is possible
126f08c3bdfSopenharmony_ci		 * to the unmapped memory region by writing
127f08c3bdfSopenharmony_ci		 * to the first byte of region with
128f08c3bdfSopenharmony_ci		 * some arbitrary number.
129f08c3bdfSopenharmony_ci		 */
130f08c3bdfSopenharmony_ci		*addr = 50;
131f08c3bdfSopenharmony_ci
132f08c3bdfSopenharmony_ci		/* This message is printed if no SIGSEGV */
133f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "process succeeds to refer unmapped "
134f08c3bdfSopenharmony_ci			 "memory region");
135f08c3bdfSopenharmony_ci#endif
136f08c3bdfSopenharmony_ci
137f08c3bdfSopenharmony_ci		cleanup();
138f08c3bdfSopenharmony_ci
139f08c3bdfSopenharmony_ci	}
140f08c3bdfSopenharmony_ci	tst_exit();
141f08c3bdfSopenharmony_ci}
142f08c3bdfSopenharmony_ci
143f08c3bdfSopenharmony_ci/*
144f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test.
145f08c3bdfSopenharmony_ci *
146f08c3bdfSopenharmony_ci * Set up signal handler to catch SIGSEGV.
147f08c3bdfSopenharmony_ci * Get system page size, create a temporary file for reading/writing,
148f08c3bdfSopenharmony_ci * write one byte data into it, map the open file for the specified
149f08c3bdfSopenharmony_ci * map length.
150f08c3bdfSopenharmony_ci */
151f08c3bdfSopenharmony_civoid setup(void)
152f08c3bdfSopenharmony_ci{
153f08c3bdfSopenharmony_ci	size_t page_sz;
154f08c3bdfSopenharmony_ci
155f08c3bdfSopenharmony_ci	tst_sig(NOFORK, DEF_HANDLER, cleanup);
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci	/* call signal function to trap the signal generated */
158f08c3bdfSopenharmony_ci	if (signal(SIGSEGV, sig_handler) == SIG_ERR) {
159f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "signal fails to catch signal");
160f08c3bdfSopenharmony_ci	}
161f08c3bdfSopenharmony_ci
162f08c3bdfSopenharmony_ci	TEST_PAUSE;
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ci	/* Get the system page size */
165f08c3bdfSopenharmony_ci	page_sz = getpagesize();
166f08c3bdfSopenharmony_ci
167f08c3bdfSopenharmony_ci	/*
168f08c3bdfSopenharmony_ci	 * Get the length of the open file to be mapped into process
169f08c3bdfSopenharmony_ci	 * address space.
170f08c3bdfSopenharmony_ci	 */
171f08c3bdfSopenharmony_ci	map_len = 3 * page_sz;
172f08c3bdfSopenharmony_ci
173f08c3bdfSopenharmony_ci	tst_tmpdir();
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci	/* Creat a temporary file used for mapping */
176f08c3bdfSopenharmony_ci	if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) {
177f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "open() on %s Failed, errno=%d : %s",
178f08c3bdfSopenharmony_ci			 TEMPFILE, errno, strerror(errno));
179f08c3bdfSopenharmony_ci	}
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci	/*
182f08c3bdfSopenharmony_ci	 * move the file pointer to maplength position from the beginning
183f08c3bdfSopenharmony_ci	 * of the file.
184f08c3bdfSopenharmony_ci	 */
185f08c3bdfSopenharmony_ci	SAFE_LSEEK(cleanup, fildes, map_len, SEEK_SET);
186f08c3bdfSopenharmony_ci
187f08c3bdfSopenharmony_ci	/* Write one byte into temporary file */
188f08c3bdfSopenharmony_ci	if (write(fildes, "a", 1) != 1) {
189f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "write() on %s Failed, errno=%d : %s",
190f08c3bdfSopenharmony_ci			 TEMPFILE, errno, strerror(errno));
191f08c3bdfSopenharmony_ci	}
192f08c3bdfSopenharmony_ci
193f08c3bdfSopenharmony_ci	/*
194f08c3bdfSopenharmony_ci	 * map the open file 'TEMPFILE' from its beginning up to the maplength
195f08c3bdfSopenharmony_ci	 * into the calling process's address space at the system choosen
196f08c3bdfSopenharmony_ci	 * with read/write permissions to the mapped region.
197f08c3bdfSopenharmony_ci	 */
198f08c3bdfSopenharmony_ci#ifdef UCLINUX
199f08c3bdfSopenharmony_ci	/* MAP_SHARED is not implemented on uClinux */
200f08c3bdfSopenharmony_ci	addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
201f08c3bdfSopenharmony_ci		    MAP_FILE | MAP_PRIVATE, fildes, 0);
202f08c3bdfSopenharmony_ci#else
203f08c3bdfSopenharmony_ci	addr = mmap(0, map_len, PROT_READ | PROT_WRITE,
204f08c3bdfSopenharmony_ci		    MAP_FILE | MAP_SHARED, fildes, 0);
205f08c3bdfSopenharmony_ci#endif
206f08c3bdfSopenharmony_ci
207f08c3bdfSopenharmony_ci	/* check for the return value of mmap system call */
208f08c3bdfSopenharmony_ci	if (addr == (char *)MAP_FAILED) {
209f08c3bdfSopenharmony_ci		tst_brkm(TBROK, cleanup, "mmap() Failed on %s, errno=%d : %s",
210f08c3bdfSopenharmony_ci			 TEMPFILE, errno, strerror(errno));
211f08c3bdfSopenharmony_ci	}
212f08c3bdfSopenharmony_ci
213f08c3bdfSopenharmony_ci}
214f08c3bdfSopenharmony_ci
215f08c3bdfSopenharmony_ci/*
216f08c3bdfSopenharmony_ci * sig_handler() - signal catching function.
217f08c3bdfSopenharmony_ci *   This function is used to trap the signal generated when tried to read or
218f08c3bdfSopenharmony_ci *   write to the memory mapped region which is already detached from the
219f08c3bdfSopenharmony_ci *   calling process address space.
220f08c3bdfSopenharmony_ci *   this function is invoked when SIGSEGV generated and it calls test
221f08c3bdfSopenharmony_ci *   cleanup function and exit the program.
222f08c3bdfSopenharmony_ci */
223f08c3bdfSopenharmony_civoid sig_handler(void)
224f08c3bdfSopenharmony_ci{
225f08c3bdfSopenharmony_ci	tst_resm(TPASS, "Functionality of munmap() successful");
226f08c3bdfSopenharmony_ci
227f08c3bdfSopenharmony_ci	/* Invoke test cleanup function and exit */
228f08c3bdfSopenharmony_ci	cleanup();
229f08c3bdfSopenharmony_ci
230f08c3bdfSopenharmony_ci	tst_exit();
231f08c3bdfSopenharmony_ci}
232f08c3bdfSopenharmony_ci
233f08c3bdfSopenharmony_ci/*
234f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at
235f08c3bdfSopenharmony_ci *             completion or premature exit.
236f08c3bdfSopenharmony_ci *  	       Close the temporary file.
237f08c3bdfSopenharmony_ci *  	       Remove the temporary directory.
238f08c3bdfSopenharmony_ci */
239f08c3bdfSopenharmony_civoid cleanup(void)
240f08c3bdfSopenharmony_ci{
241f08c3bdfSopenharmony_ci
242f08c3bdfSopenharmony_ci	/* Close the temporary file */
243f08c3bdfSopenharmony_ci	if (close(fildes) < 0) {
244f08c3bdfSopenharmony_ci		tst_brkm(TFAIL, NULL, "close() on %s Failed, errno=%d : %s",
245f08c3bdfSopenharmony_ci			 TEMPFILE, errno, strerror(errno));
246f08c3bdfSopenharmony_ci	}
247f08c3bdfSopenharmony_ci
248f08c3bdfSopenharmony_ci	tst_rmdir();
249f08c3bdfSopenharmony_ci}
250