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: mremap04 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 memory area cannot be expanded at 27f08c3bdfSopenharmony_ci * the current virtual address and MREMAP_MAYMOVE flag not set. 28f08c3bdfSopenharmony_ci * 29f08c3bdfSopenharmony_ci * Expected Result: 30f08c3bdfSopenharmony_ci * mremap() should return -1 and set errno to ENOMEM. 31f08c3bdfSopenharmony_ci * 32f08c3bdfSopenharmony_ci * Algorithm: 33f08c3bdfSopenharmony_ci * Setup: 34f08c3bdfSopenharmony_ci * Setup signal handling. 35f08c3bdfSopenharmony_ci * Create temporary directory. 36f08c3bdfSopenharmony_ci * Pause for SIGUSR1 if option specified. 37f08c3bdfSopenharmony_ci * 38f08c3bdfSopenharmony_ci * Test: 39f08c3bdfSopenharmony_ci * Loop if the proper options are given. 40f08c3bdfSopenharmony_ci * Execute system call 41f08c3bdfSopenharmony_ci * Check return code, if system call failed (return=-1) 42f08c3bdfSopenharmony_ci * if errno set == expected errno 43f08c3bdfSopenharmony_ci * Issue sys call failed with expected return value and errno. 44f08c3bdfSopenharmony_ci * Otherwise, 45f08c3bdfSopenharmony_ci * Issue sys call failed with unexpected errno. 46f08c3bdfSopenharmony_ci * Otherwise, 47f08c3bdfSopenharmony_ci * Issue sys call returns unexpected value. 48f08c3bdfSopenharmony_ci * 49f08c3bdfSopenharmony_ci * Cleanup: 50f08c3bdfSopenharmony_ci * Print errno log and/or timing stats if options given 51f08c3bdfSopenharmony_ci * Delete the temporary directory(s)/file(s) created. 52f08c3bdfSopenharmony_ci * 53f08c3bdfSopenharmony_ci * Usage: <for command-line> 54f08c3bdfSopenharmony_ci * mremap04 [-c n] [-e] [-i n] [-I x] [-P x] [-t] 55f08c3bdfSopenharmony_ci * where, -c n : Run n copies concurrently. 56f08c3bdfSopenharmony_ci * -e : Turn on errno logging. 57f08c3bdfSopenharmony_ci * -i n : Execute test n times. 58f08c3bdfSopenharmony_ci * -I x : Execute test for x seconds. 59f08c3bdfSopenharmony_ci * -p x : Pause for x seconds between iterations. 60f08c3bdfSopenharmony_ci * -t : Turn on syscall timing. 61f08c3bdfSopenharmony_ci * 62f08c3bdfSopenharmony_ci * HISTORY 63f08c3bdfSopenharmony_ci * 07/2001 Ported by Wayne Boyer 64f08c3bdfSopenharmony_ci * 65f08c3bdfSopenharmony_ci * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com) 66f08c3bdfSopenharmony_ci * Modified. 67f08c3bdfSopenharmony_ci * - #include <linux/mman.h> should not be included as per man page for 68f08c3bdfSopenharmony_ci * mremap, #include <sys/mman.h> alone should do the job. But inorder 69f08c3bdfSopenharmony_ci * to include definition of MREMAP_MAYMOVE defined in bits/mman.h 70f08c3bdfSopenharmony_ci * (included by sys/mman.h) __USE_GNU needs to be defined. 71f08c3bdfSopenharmony_ci * There may be a more elegant way of doing this... 72f08c3bdfSopenharmony_ci * 73f08c3bdfSopenharmony_ci * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com) 74f08c3bdfSopenharmony_ci * - Fix concurrency issue. Use a shm key from getipckey instead of 75f08c3bdfSopenharmony_ci * a fixed hard-coded value. 76f08c3bdfSopenharmony_ci * 77f08c3bdfSopenharmony_ci * RESTRICTIONS: 78f08c3bdfSopenharmony_ci * None. 79f08c3bdfSopenharmony_ci */ 80f08c3bdfSopenharmony_ci#define _GNU_SOURCE 81f08c3bdfSopenharmony_ci#include <errno.h> 82f08c3bdfSopenharmony_ci#include <unistd.h> 83f08c3bdfSopenharmony_ci#include <sys/mman.h> 84f08c3bdfSopenharmony_ci#include <sys/ipc.h> 85f08c3bdfSopenharmony_ci#include <sys/shm.h> 86f08c3bdfSopenharmony_ci 87f08c3bdfSopenharmony_ci#include "test.h" 88f08c3bdfSopenharmony_ci 89f08c3bdfSopenharmony_ci#define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */ 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_cichar *TCID = "mremap04"; 92f08c3bdfSopenharmony_ciint TST_TOTAL = 1; 93f08c3bdfSopenharmony_cichar *addr; /* addr of memory mapped region */ 94f08c3bdfSopenharmony_cichar *shmaddr; /* pointer to shared memory segment */ 95f08c3bdfSopenharmony_ciint shmid; /* shared memory identifier. */ 96f08c3bdfSopenharmony_ciint memsize; /* memory mapped size */ 97f08c3bdfSopenharmony_ciint newsize; /* new size of virtual memory block */ 98f08c3bdfSopenharmony_ci 99f08c3bdfSopenharmony_civoid setup(); /* Main setup function of test */ 100f08c3bdfSopenharmony_civoid cleanup(); /* cleanup function for the test */ 101f08c3bdfSopenharmony_ci 102f08c3bdfSopenharmony_ciextern int getipckey(); 103f08c3bdfSopenharmony_ci 104f08c3bdfSopenharmony_ciint main(int ac, char **av) 105f08c3bdfSopenharmony_ci{ 106f08c3bdfSopenharmony_ci int lc; 107f08c3bdfSopenharmony_ci 108f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 109f08c3bdfSopenharmony_ci 110f08c3bdfSopenharmony_ci setup(); 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); lc++) { 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci tst_count = 0; 115f08c3bdfSopenharmony_ci 116f08c3bdfSopenharmony_ci /* 117f08c3bdfSopenharmony_ci * Attempt to expand the existing shared 118f08c3bdfSopenharmony_ci * memory region of newsize by newsize limits 119f08c3bdfSopenharmony_ci * using mremap() should fail as specified 120f08c3bdfSopenharmony_ci * memory area already locked and MREMAP_MAYMOVE 121f08c3bdfSopenharmony_ci * flag unset. 122f08c3bdfSopenharmony_ci */ 123f08c3bdfSopenharmony_ci errno = 0; 124f08c3bdfSopenharmony_ci addr = mremap(shmaddr, memsize, newsize, 0); 125f08c3bdfSopenharmony_ci TEST_ERRNO = errno; 126f08c3bdfSopenharmony_ci 127f08c3bdfSopenharmony_ci /* Check for the return value of mremap() */ 128f08c3bdfSopenharmony_ci if (addr != MAP_FAILED) { 129f08c3bdfSopenharmony_ci tst_resm(TFAIL, 130f08c3bdfSopenharmony_ci "mremap returned invalid value, expected: -1"); 131f08c3bdfSopenharmony_ci 132f08c3bdfSopenharmony_ci /* Unmap the mapped memory region */ 133f08c3bdfSopenharmony_ci if (munmap(addr, newsize) != 0) { 134f08c3bdfSopenharmony_ci tst_brkm(TFAIL, cleanup, "munmap failed to " 135f08c3bdfSopenharmony_ci "unmap the expanded memory region, " 136f08c3bdfSopenharmony_ci "error=%d", errno); 137f08c3bdfSopenharmony_ci } 138f08c3bdfSopenharmony_ci continue; 139f08c3bdfSopenharmony_ci } 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci if (TEST_ERRNO == ENOMEM) { 142f08c3bdfSopenharmony_ci tst_resm(TPASS, "mremap() failed, " 143f08c3bdfSopenharmony_ci "'MREMAP_MAYMOVE flag unset', " 144f08c3bdfSopenharmony_ci "errno %d", TEST_ERRNO); 145f08c3bdfSopenharmony_ci } else { 146f08c3bdfSopenharmony_ci tst_resm(TFAIL, "mremap() failed, " 147f08c3bdfSopenharmony_ci "Unexpected errno %d", TEST_ERRNO); 148f08c3bdfSopenharmony_ci } 149f08c3bdfSopenharmony_ci } 150f08c3bdfSopenharmony_ci 151f08c3bdfSopenharmony_ci cleanup(); 152f08c3bdfSopenharmony_ci tst_exit(); 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_ci} 155f08c3bdfSopenharmony_ci 156f08c3bdfSopenharmony_ci/* 157f08c3bdfSopenharmony_ci * setup() - performs all ONE TIME setup for this test. 158f08c3bdfSopenharmony_ci * 159f08c3bdfSopenharmony_ci * Get system page size, Set the size of virtual memory area and the 160f08c3bdfSopenharmony_ci * newsize after resize, 161f08c3bdfSopenharmony_ci * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE 162f08c3bdfSopenharmony_ci * by using shmget() which returns a shared memory identifier associated 163f08c3bdfSopenharmony_ci * with the created shared memory segment. 164f08c3bdfSopenharmony_ci * Call shmat() to attach the shared memory segment to the data segment of the 165f08c3bdfSopenharmony_ci * calling process. The segment is attached at the first available address as 166f08c3bdfSopenharmony_ci * selected by the system. 167f08c3bdfSopenharmony_ci */ 168f08c3bdfSopenharmony_civoid setup(void) 169f08c3bdfSopenharmony_ci{ 170f08c3bdfSopenharmony_ci key_t shmkey; 171f08c3bdfSopenharmony_ci 172f08c3bdfSopenharmony_ci tst_sig(FORK, DEF_HANDLER, cleanup); 173f08c3bdfSopenharmony_ci 174f08c3bdfSopenharmony_ci TEST_PAUSE; 175f08c3bdfSopenharmony_ci 176f08c3bdfSopenharmony_ci tst_tmpdir(); 177f08c3bdfSopenharmony_ci 178f08c3bdfSopenharmony_ci /* Get the system page size */ 179f08c3bdfSopenharmony_ci if ((memsize = getpagesize()) < 0) { 180f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, 181f08c3bdfSopenharmony_ci "getpagesize() failed to get system page size"); 182f08c3bdfSopenharmony_ci } 183f08c3bdfSopenharmony_ci 184f08c3bdfSopenharmony_ci /* Get the New size of virtual memory block after resize */ 185f08c3bdfSopenharmony_ci newsize = (memsize * 2); 186f08c3bdfSopenharmony_ci 187f08c3bdfSopenharmony_ci /* get an IPC resource key */ 188f08c3bdfSopenharmony_ci shmkey = getipckey(); 189f08c3bdfSopenharmony_ci 190f08c3bdfSopenharmony_ci /* 191f08c3bdfSopenharmony_ci * Create a shared memory segment represented by SHMKEY of 192f08c3bdfSopenharmony_ci * specified size 'newsize' and mode permissions 'SHM_MODE'. 193f08c3bdfSopenharmony_ci */ 194f08c3bdfSopenharmony_ci shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE); 195f08c3bdfSopenharmony_ci if (shmid == -1) { 196f08c3bdfSopenharmony_ci tst_brkm(TBROK, NULL, "shmget() Failed to create a shared " 197f08c3bdfSopenharmony_ci "memory, error:%d", errno); 198f08c3bdfSopenharmony_ci } 199f08c3bdfSopenharmony_ci 200f08c3bdfSopenharmony_ci /* 201f08c3bdfSopenharmony_ci * Attach the shared memory segment associated with the shared 202f08c3bdfSopenharmony_ci * memory identifier specified by "shmid" to the data segment of 203f08c3bdfSopenharmony_ci * the calling process at the first available address as selected 204f08c3bdfSopenharmony_ci * by the system. 205f08c3bdfSopenharmony_ci */ 206f08c3bdfSopenharmony_ci shmaddr = shmat(shmid, NULL, 0); 207f08c3bdfSopenharmony_ci if (shmaddr == (void *)-1) { 208f08c3bdfSopenharmony_ci tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared " 209f08c3bdfSopenharmony_ci "memory, error:%d", errno); 210f08c3bdfSopenharmony_ci } 211f08c3bdfSopenharmony_ci} 212f08c3bdfSopenharmony_ci 213f08c3bdfSopenharmony_ci/* 214f08c3bdfSopenharmony_ci * cleanup() - performs all ONE TIME cleanup for this test at 215f08c3bdfSopenharmony_ci * completion or premature exit. 216f08c3bdfSopenharmony_ci * Detach the shared memory segment and remove the shared memory 217f08c3bdfSopenharmony_ci * identifier associated with the shared memory. 218f08c3bdfSopenharmony_ci */ 219f08c3bdfSopenharmony_civoid cleanup(void) 220f08c3bdfSopenharmony_ci{ 221f08c3bdfSopenharmony_ci 222f08c3bdfSopenharmony_ci /* 223f08c3bdfSopenharmony_ci * Detach the shared memory segment attached to 224f08c3bdfSopenharmony_ci * the calling process's data segment 225f08c3bdfSopenharmony_ci */ 226f08c3bdfSopenharmony_ci if (shmdt(shmaddr) < 0) { 227f08c3bdfSopenharmony_ci tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared " 228f08c3bdfSopenharmony_ci "memory, error:%d", errno); 229f08c3bdfSopenharmony_ci } 230f08c3bdfSopenharmony_ci 231f08c3bdfSopenharmony_ci /* 232f08c3bdfSopenharmony_ci * Remove the shared memory identifier associated with 233f08c3bdfSopenharmony_ci * the shared memory segment and destroy the shared memory 234f08c3bdfSopenharmony_ci * segment. 235f08c3bdfSopenharmony_ci */ 236f08c3bdfSopenharmony_ci if (shmctl(shmid, IPC_RMID, 0) < 0) { 237f08c3bdfSopenharmony_ci tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared " 238f08c3bdfSopenharmony_ci "memory, error:%d", errno); 239f08c3bdfSopenharmony_ci } 240f08c3bdfSopenharmony_ci 241f08c3bdfSopenharmony_ci tst_rmdir(); 242f08c3bdfSopenharmony_ci 243f08c3bdfSopenharmony_ci /* Exit the program */ 244f08c3bdfSopenharmony_ci 245f08c3bdfSopenharmony_ci} 246