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