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