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: mremap02 22f08c3bdfSopenharmony_ci * 23f08c3bdfSopenharmony_ci * Test Description: 24f08c3bdfSopenharmony_ci * Verify that, 25f08c3bdfSopenharmony_ci * mremap() fails when used to expand the existing virtual memory mapped 26f08c3bdfSopenharmony_ci * region to the requested size, if the virtual memory area previously 27f08c3bdfSopenharmony_ci * mapped was not page aligned or invalid argument specified. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * Expected Result: 30f08c3bdfSopenharmony_ci * mremap() should return -1 and set errno to EINVAL. 31f08c3bdfSopenharmony_ci * 32f08c3bdfSopenharmony_ci * Algorithm: 33f08c3bdfSopenharmony_ci * Setup: 34f08c3bdfSopenharmony_ci * Setup signal handling. 35f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 36f08c3bdfSopenharmony_ci * 37f08c3bdfSopenharmony_ci * Test: 38f08c3bdfSopenharmony_ci * Loop if the proper options are given. 39f08c3bdfSopenharmony_ci * Execute system call 40f08c3bdfSopenharmony_ci * Check return code, if system call failed (return=-1) 41f08c3bdfSopenharmony_ci * if errno set == expected errno 42f08c3bdfSopenharmony_ci * Issue sys call fails with expected return value and errno. 43f08c3bdfSopenharmony_ci * Otherwise, 44f08c3bdfSopenharmony_ci * Issue sys call fails with unexpected errno. 45f08c3bdfSopenharmony_ci * Otherwise, 46f08c3bdfSopenharmony_ci * Issue sys call returns unexpected value. 47f08c3bdfSopenharmony_ci * 48f08c3bdfSopenharmony_ci * Cleanup: 49f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 50f08c3bdfSopenharmony_ci * 51f08c3bdfSopenharmony_ci * Usage: <for command-line> 52f08c3bdfSopenharmony_ci * mremap02 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 53f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 54f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 55f08c3bdfSopenharmony_ci * -i n : Execute test n times. 56f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 57f08c3bdfSopenharmony_ci * -p x : Pause for x seconds between iterations. 58f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 59f08c3bdfSopenharmony_ci * 60f08c3bdfSopenharmony_ci * HISTORY 61f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 62f08c3bdfSopenharmony_ci * 63f08c3bdfSopenharmony_ci * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com) 64f08c3bdfSopenharmony_ci * Modified. 65f08c3bdfSopenharmony_ci * - #include <linux/mman.h> should not be included as per man page for 66f08c3bdfSopenharmony_ci * mremap, #include <sys/mman.h> alone should do the job. But inorder 67f08c3bdfSopenharmony_ci * to include definition of MREMAP_MAYMOVE defined in bits/mman.h 68f08c3bdfSopenharmony_ci * (included by sys/mman.h) __USE_GNU needs to be defined. 69f08c3bdfSopenharmony_ci * There may be a more elegant way of doing this... 70f08c3bdfSopenharmony_ci * 71f08c3bdfSopenharmony_ci * 72f08c3bdfSopenharmony_ci * RESTRICTIONS: 73f08c3bdfSopenharmony_ci * None. 74f08c3bdfSopenharmony_ci */ 75f08c3bdfSopenharmony_ci#define _GNU_SOURCE 76f08c3bdfSopenharmony_ci#include <errno.h> 77f08c3bdfSopenharmony_ci#include <unistd.h> 78f08c3bdfSopenharmony_ci#include <fcntl.h> 79f08c3bdfSopenharmony_ci#include <sys/mman.h> 80f08c3bdfSopenharmony_ci 81f08c3bdfSopenharmony_ci#include "test.h" 82f08c3bdfSopenharmony_ci 83f08c3bdfSopenharmony_cichar *TCID = "mremap02"; 84f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 85f08c3bdfSopenharmony_cichar *addr; /* addr of memory mapped region */ 86f08c3bdfSopenharmony_ciint memsize; /* memory mapped size */ 87f08c3bdfSopenharmony_ciint newsize; /* new size of virtual memory block */ 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_civoid setup(); /* Main setup function of test */ 90f08c3bdfSopenharmony_civoid cleanup(); /* cleanup function for the test */ 91f08c3bdfSopenharmony_ci 92f08c3bdfSopenharmony_ciint main(int ac, char **av) 93f08c3bdfSopenharmony_ci{ 94f08c3bdfSopenharmony_ci int lc; 95f08c3bdfSopenharmony_ci 96f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 97f08c3bdfSopenharmony_ci 98f08c3bdfSopenharmony_ci setup(); 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ci tst_count = 0; 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ci /* 105f08c3bdfSopenharmony_ci * Attempt to expand the existing mapped 106f08c3bdfSopenharmony_ci * memory region (memsize) by newsize limits using 107f08c3bdfSopenharmony_ci * mremap() should fail as old virtual address is not 108f08c3bdfSopenharmony_ci * page aligned. 109f08c3bdfSopenharmony_ci */ 110f08c3bdfSopenharmony_ci errno = 0; 111f08c3bdfSopenharmony_ci addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE); 112f08c3bdfSopenharmony_ci TEST_ERRNO = errno; 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci /* Check for the return value of mremap() */ 115f08c3bdfSopenharmony_ci if (addr != MAP_FAILED) { 116f08c3bdfSopenharmony_ci tst_resm(TFAIL, 117f08c3bdfSopenharmony_ci "mremap returned invalid value, expected: -1"); 118f08c3bdfSopenharmony_ci 119f08c3bdfSopenharmony_ci /* Unmap the mapped memory region */ 120f08c3bdfSopenharmony_ci if (munmap(addr, newsize) != 0) { 121f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "munmap fails to " 122f08c3bdfSopenharmony_ci "unmap the expanded memory region, " 123f08c3bdfSopenharmony_ci "error=%d", errno); 124f08c3bdfSopenharmony_ci } 125f08c3bdfSopenharmony_ci continue; 126f08c3bdfSopenharmony_ci } 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_ci if (errno == EINVAL) { 129f08c3bdfSopenharmony_ci tst_resm(TPASS, "mremap() Failed, 'invalid argument " 130f08c3bdfSopenharmony_ci "specified' - errno %d", TEST_ERRNO); 131f08c3bdfSopenharmony_ci } else { 132f08c3bdfSopenharmony_ci tst_resm(TFAIL, "mremap() Failed, " 133f08c3bdfSopenharmony_ci "'Unexpected errno %d", TEST_ERRNO); 134f08c3bdfSopenharmony_ci } 135f08c3bdfSopenharmony_ci } 136f08c3bdfSopenharmony_ci 137f08c3bdfSopenharmony_ci cleanup(); 138f08c3bdfSopenharmony_ci tst_exit(); 139f08c3bdfSopenharmony_ci 140f08c3bdfSopenharmony_ci} 141f08c3bdfSopenharmony_ci 142f08c3bdfSopenharmony_ci/* 143f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test. 144f08c3bdfSopenharmony_ci * 145f08c3bdfSopenharmony_ci * Get system page size, Set the size of virtual memory area and the 146f08c3bdfSopenharmony_ci * new size after resize, Set the virtual memory address such that it 147f08c3bdfSopenharmony_ci * is not aligned. 148f08c3bdfSopenharmony_ci */ 149f08c3bdfSopenharmony_civoid setup(void) 150f08c3bdfSopenharmony_ci{ 151f08c3bdfSopenharmony_ci 152f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_ci TEST_PAUSE; 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci /* Get the system page size */ 157f08c3bdfSopenharmony_ci if ((memsize = getpagesize()) < 0) { 158f08c3bdfSopenharmony_ci tst_brkm(TFAIL, NULL, 159f08c3bdfSopenharmony_ci "getpagesize() fails to get system page size"); 160f08c3bdfSopenharmony_ci } 161f08c3bdfSopenharmony_ci 162f08c3bdfSopenharmony_ci /* Get the New size of virtual memory block after resize */ 163f08c3bdfSopenharmony_ci newsize = (memsize * 2); 164f08c3bdfSopenharmony_ci 165f08c3bdfSopenharmony_ci /* Set the old virtual memory address */ 166f08c3bdfSopenharmony_ci addr = (char *)(addr + (memsize - 1)); 167f08c3bdfSopenharmony_ci} 168f08c3bdfSopenharmony_ci 169f08c3bdfSopenharmony_ci/* 170f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at 171f08c3bdfSopenharmony_ci * completion or premature exit. 172f08c3bdfSopenharmony_ci */ 173f08c3bdfSopenharmony_civoid cleanup(void) 174f08c3bdfSopenharmony_ci{ 175f08c3bdfSopenharmony_ci 176f08c3bdfSopenharmony_ci /* Exit the program */ 177f08c3bdfSopenharmony_ci 178f08c3bdfSopenharmony_ci} 179