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