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