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: mremap04 |
| 22 | * |
| 23 | * Test Description: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * Verify that, |
| 25 | * mremap() fails when used to expand the existing virtual memory mapped |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 42 | * if errno set == expected errno |
| 43 | * Issue sys call failed with expected return value and errno. |
| 44 | * Otherwise, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 | * |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 65 | * 11/09/2001 Manoj Iyer (manjo@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 | * |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 73 | * 26/02/2008 Renaud Lottiaux (Renaud.Lottiaux@kerlabs.com) |
| 74 | * - Fix concurrency issue. Use a shm key from getipckey instead of |
| 75 | * a fixed hard-coded value. |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 76 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | * RESTRICTIONS: |
| 78 | * None. |
| 79 | */ |
| 80 | #include <errno.h> |
| 81 | #include <unistd.h> |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 82 | #define __USE_GNU |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | #include <sys/mman.h> |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 84 | #undef __USE_GNU |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | #include <sys/ipc.h> |
| 86 | #include <sys/shm.h> |
| 87 | |
| 88 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 89 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | #define SHM_MODE (SHM_R | SHM_W) /* mode permissions of shared memory */ |
| 91 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 92 | char *TCID = "mremap04"; |
| 93 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 */ |
Cyril Hrubis | 605fa33 | 2015-02-04 13:11:20 +0100 | [diff] [blame] | 98 | int newsize; /* new size of virtual memory block */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | |
| 100 | void setup(); /* Main setup function of test */ |
| 101 | void cleanup(); /* cleanup function for the test */ |
| 102 | |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 103 | extern int getipckey(); |
| 104 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 105 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 106 | { |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 107 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 108 | const char *msg; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 109 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 110 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 111 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 112 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | setup(); |
| 114 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 115 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 116 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 117 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 118 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 119 | /* |
| 120 | * Attempt to expand the existing shared |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 121 | * memory region of newsize by newsize limits |
| 122 | * using mremap() should fail as specified |
| 123 | * memory area already locked and MREMAP_MAYMOVE |
| 124 | * flag unset. |
| 125 | */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 126 | errno = 0; |
| 127 | addr = mremap(shmaddr, memsize, newsize, 0); |
| 128 | TEST_ERRNO = errno; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
| 130 | /* Check for the return value of mremap() */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 131 | if (addr != MAP_FAILED) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | tst_resm(TFAIL, |
| 133 | "mremap returned invalid value, expected: -1"); |
| 134 | |
| 135 | /* Unmap the mapped memory region */ |
robbiew | d5c2112 | 2002-03-22 16:22:40 +0000 | [diff] [blame] | 136 | if (munmap(addr, newsize) != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 137 | tst_brkm(TFAIL, cleanup, "munmap failed to " |
| 138 | "unmap the expanded memory region, " |
| 139 | "error=%d", errno); |
| 140 | } |
| 141 | continue; |
| 142 | } |
| 143 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 144 | if (TEST_ERRNO == ENOMEM) { |
| 145 | tst_resm(TPASS, "mremap() failed, " |
| 146 | "'MREMAP_MAYMOVE flag unset', " |
| 147 | "errno %d", TEST_ERRNO); |
| 148 | } else { |
| 149 | tst_resm(TFAIL, "mremap() failed, " |
| 150 | "Unexpected errno %d", TEST_ERRNO); |
| 151 | } |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 152 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 153 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | cleanup(); |
Garrett Cooper | 1e6f5a6 | 2010-12-19 09:58:10 -0800 | [diff] [blame] | 155 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 157 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | |
| 159 | /* |
| 160 | * setup() - performs all ONE TIME setup for this test. |
| 161 | * |
| 162 | * Get system page size, Set the size of virtual memory area and the |
| 163 | * newsize after resize, |
| 164 | * Create a named shared memory segment SHMKEY of newsize and mode SHM_MODE |
| 165 | * by using shmget() which returns a shared memory identifier associated |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 166 | * with the created shared memory segment. |
| 167 | * Call shmat() to attach the shared memory segment to the data segment of the |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 168 | * calling process. The segment is attached at the first available address as |
| 169 | * selected by the system. |
| 170 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 171 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 172 | { |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 173 | key_t shmkey; |
| 174 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 175 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 176 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | TEST_PAUSE; |
| 178 | |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 179 | tst_tmpdir(); |
| 180 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 181 | /* Get the system page size */ |
| 182 | if ((memsize = getpagesize()) < 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 183 | tst_brkm(TBROK, NULL, |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 184 | "getpagesize() failed to get system page size"); |
| 185 | } |
| 186 | |
| 187 | /* Get the New size of virtual memory block after resize */ |
| 188 | newsize = (memsize * 2); |
| 189 | |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 190 | /* get an IPC resource key */ |
| 191 | shmkey = getipckey(); |
| 192 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | /* |
| 194 | * Create a shared memory segment represented by SHMKEY of |
| 195 | * specified size 'newsize' and mode permissions 'SHM_MODE'. |
| 196 | */ |
subrata_modak | ae6cabc | 2008-02-27 14:47:11 +0000 | [diff] [blame] | 197 | shmid = shmget(shmkey, newsize, IPC_CREAT | SHM_MODE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | if (shmid == -1) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 199 | tst_brkm(TBROK, NULL, "shmget() Failed to create a shared " |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 200 | "memory, error:%d", errno); |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Attach the shared memory segment associated with the shared |
| 205 | * memory identifier specified by "shmid" to the data segment of |
| 206 | * the calling process at the first available address as selected |
| 207 | * by the system. |
| 208 | */ |
Cyril Hrubis | d2db480 | 2014-09-24 17:08:17 +0200 | [diff] [blame] | 209 | shmaddr = shmat(shmid, NULL, 0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 210 | if (shmaddr == (void *)-1) { |
| 211 | tst_brkm(TBROK, cleanup, "shmat() Failed to attach shared " |
| 212 | "memory, error:%d", errno); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 218 | * completion or premature exit. |
| 219 | * Detach the shared memory segment and remove the shared memory |
| 220 | * identifier associated with the shared memory. |
| 221 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 222 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 223 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 224 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 225 | /* |
| 226 | * Detach the shared memory segment attached to |
| 227 | * the calling process's data segment |
| 228 | */ |
| 229 | if (shmdt(shmaddr) < 0) { |
| 230 | tst_brkm(TFAIL, NULL, "shmdt() Failed to detach shared " |
| 231 | "memory, error:%d", errno); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Remove the shared memory identifier associated with |
| 236 | * the shared memory segment and destroy the shared memory |
| 237 | * segment. |
| 238 | */ |
| 239 | if (shmctl(shmid, IPC_RMID, 0) < 0) { |
| 240 | tst_brkm(TFAIL, NULL, "shmctl() Failed to remove shared " |
| 241 | "memory, error:%d", errno); |
| 242 | } |
| 243 | |
subrata_modak | 4464067 | 2008-06-03 13:05:45 +0000 | [diff] [blame] | 244 | tst_rmdir(); |
| 245 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 246 | /* Exit the program */ |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 247 | |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 248 | } |