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