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: mremap01 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that, mremap() succeeds when used to expand the existing |
| 25 | * virtual memory mapped region to the requested size where the |
| 26 | * virtual memory area was previously mapped to a file using mmap(). |
| 27 | * |
| 28 | * Expected Result: |
| 29 | * mremap() should succeed returning the address of new virtual memory area. |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 30 | * The expanded mapped memory region should be accessible and able to |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | * synchronize with the file. |
| 32 | * |
| 33 | * Algorithm: |
| 34 | * Setup: |
| 35 | * Setup signal handling. |
| 36 | * Create temporary directory. |
| 37 | * Pause for SIGUSR1 if option specified. |
| 38 | * |
| 39 | * Test: |
| 40 | * Loop if the proper options are given. |
| 41 | * Execute system call |
| 42 | * Check return code, if system call failed (return=-1) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 43 | * Log the errno and Issue a FAIL message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | * Otherwise, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 45 | * Verify the Functionality of system call |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | * if successful, |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 47 | * Issue Functionality-Pass message. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | * Otherwise, |
| 49 | * Issue Functionality-Fail message. |
| 50 | * Cleanup: |
| 51 | * Print errno log and/or timing stats if options given |
| 52 | * Delete the temporary directory created. |
| 53 | * |
| 54 | * Usage: <for command-line> |
| 55 | * mremap01 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 56 | * where, -c n : Run n copies concurrently. |
| 57 | * -f : Turn off functionality Testing. |
| 58 | * -i n : Execute test n times. |
| 59 | * -I x : Execute test for x seconds. |
| 60 | * -P x : Pause for x seconds between iterations. |
| 61 | * -t : Turn on syscall timing. |
| 62 | * |
| 63 | * HISTORY |
| 64 | * 07/2001 Ported by Wayne Boyer |
| 65 | * |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 66 | * 11/09/2001 Manoj Iyer (manjo@austin.ibm.com) |
| 67 | * Modified. |
| 68 | * - #include <linux/mman.h> should not be included as per man page for |
| 69 | * mremap, #include <sys/mman.h> alone should do the job. But inorder |
| 70 | * to include definition of MREMAP_MAYMOVE defined in bits/mman.h |
| 71 | * (included by sys/mman.h) __USE_GNU needs to be defined. |
| 72 | * There may be a more elegant way of doing this... |
| 73 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | * RESTRICTIONS: |
| 75 | * None. |
| 76 | */ |
| 77 | #include <unistd.h> |
| 78 | #include <errno.h> |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 79 | #define __USE_GNU |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | #include <sys/mman.h> |
iyermanoj | 874176f | 2001-11-09 07:07:02 +0000 | [diff] [blame] | 81 | #undef __USE_GNU |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | #include <fcntl.h> |
| 83 | |
| 84 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 85 | |
| 86 | #define TEMPFILE "mremapfile" |
| 87 | |
Cyril Hrubis | fdce7d5 | 2013-04-04 18:35:48 +0200 | [diff] [blame] | 88 | char *TCID = "mremap01"; |
| 89 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | char *addr; /* addr of memory mapped region */ |
| 91 | int memsize; /* memory mapped size */ |
| 92 | int newsize; /* new size of virtual memory block */ |
| 93 | int fildes; /* file descriptor for tempfile */ |
| 94 | |
| 95 | void setup(); /* Main setup function of test */ |
| 96 | void cleanup(); /* cleanup function for the test */ |
| 97 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 98 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 100 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 101 | int ind; /* counter variable */ |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 102 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 103 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 104 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 105 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 106 | tst_count = 0; |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 107 | |
| 108 | setup(); |
| 109 | |
| 110 | /* |
| 111 | * Call mremap to expand the existing mapped |
| 112 | * memory region (memsize) by newsize limits. |
| 113 | */ |
| 114 | addr = mremap(addr, memsize, newsize, MREMAP_MAYMOVE); |
| 115 | |
| 116 | /* Check for the return value of mremap() */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 117 | if (addr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 118 | tst_brkm(TFAIL | TERRNO, cleanup, "mremap failed"); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Attempt to initialize the expanded memory |
| 122 | * mapped region with data. If the map area |
| 123 | * was bad, we'd get SIGSEGV. |
| 124 | */ |
| 125 | for (ind = 0; ind < newsize; ind++) { |
| 126 | addr[ind] = (char)ind; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 129 | /* |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | * Memory mapped area is good. Now, attempt |
| 131 | * to synchronize the mapped memory region |
| 132 | * with the file. |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 133 | */ |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 134 | if (msync(addr, newsize, MS_SYNC) != 0) { |
| 135 | tst_resm(TFAIL | TERRNO, "msync failed to synch " |
| 136 | "mapped file"); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 137 | } else { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | tst_resm(TPASS, "Functionality of " |
| 139 | "mremap() is correct"); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 140 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 141 | |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 142 | cleanup(); |
| 143 | tst_exit(); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 144 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 145 | |
| 146 | /* |
| 147 | * void |
| 148 | * setup() - performs all ONE TIME setup for this test. |
| 149 | * |
| 150 | * Get system page size, Set the size of virtual memory area and the |
| 151 | * new size after resize, |
| 152 | * Creat a temporary directory and a file under it. |
| 153 | * Stratch the file size to the size of virtual memory area and |
| 154 | * write 1 byte (\0). Map the temporary file for the length of virtual |
| 155 | * memory (memsize) into memory. |
| 156 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 157 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 159 | int pagesz; /* system's page size */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 162 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | TEST_PAUSE; |
| 164 | |
| 165 | /* Get the system page size */ |
| 166 | if ((pagesz = getpagesize()) < 0) { |
Garrett Cooper | 5374050 | 2010-12-16 00:04:01 -0800 | [diff] [blame] | 167 | tst_brkm(TFAIL, NULL, |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 168 | "getpagesize failed to get system page size"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /* Get the size of virtual memory area to be mapped */ |
| 172 | memsize = (1000 * pagesz); |
| 173 | |
| 174 | /* Get the New size of virtual memory block after resize */ |
| 175 | newsize = (memsize * 2); |
| 176 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 177 | tst_tmpdir(); |
| 178 | |
| 179 | /* Creat a temporary file used for mapping */ |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 180 | if ((fildes = open(TEMPFILE, O_RDWR | O_CREAT, 0666)) < 0) |
| 181 | tst_brkm(TBROK | TERRNO, cleanup, "opening %s failed", |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 182 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 183 | |
| 184 | /* Stretch the file to the size of virtual memory area */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 185 | if (lseek(fildes, (off_t) memsize, SEEK_SET) != (off_t) memsize) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 186 | tst_brkm(TBROK | TERRNO, cleanup, |
| 187 | "lseeking to %d offset pos. failed", memsize); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* Write one byte data into temporary file */ |
| 191 | if (write(fildes, "\0", 1) != 1) { |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 192 | tst_brkm(TBROK, cleanup, "writing to %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 194 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 195 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | * Call mmap to map virtual memory (memsize bytes) from the |
| 197 | * beginning of temporary file (offset is 0) into memory. |
| 198 | */ |
| 199 | addr = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fildes, 0); |
| 200 | |
| 201 | /* Check for the return value of mmap() */ |
| 202 | if (addr == (char *)MAP_FAILED) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 203 | tst_brkm(TBROK, cleanup, "mmaping Failed on %s", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Stretch the file to newsize of virtual memory block */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | if (lseek(fildes, (off_t) newsize, SEEK_SET) != (off_t) newsize) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 208 | tst_brkm(TBROK, cleanup, "lseek() to %d offset pos. Failed, " |
| 209 | "error=%d : %s", newsize, errno, strerror(errno)); |
| 210 | } |
| 211 | |
| 212 | /* Write one byte data into temporary file */ |
| 213 | if (write(fildes, "\0", 1) != 1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 214 | tst_brkm(TBROK | TERRNO, cleanup, "writing to %s failed", |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 215 | TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * void |
| 221 | * cleanup() - performs all ONE TIME cleanup for this test at |
| 222 | * completion or premature exit. |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | * Unmap the mapped memory area done in the test. |
| 224 | * Close the temporary file. |
| 225 | * Remove the temporary directory. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 226 | */ |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 227 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 228 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 229 | |
| 230 | /* Unmap the mapped memory */ |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 231 | if (munmap(addr, newsize) != 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 232 | tst_brkm(TBROK | TERRNO, NULL, "munmap failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | |
| 234 | /* Close the temporary file */ |
| 235 | if (close(fildes) < 0) { |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 236 | tst_brkm(TBROK, NULL, "closing %s failed", TEMPFILE); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 237 | } |
| 238 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 239 | tst_rmdir(); |
Garrett Cooper | e5bcfc3 | 2010-12-19 10:25:43 -0800 | [diff] [blame] | 240 | } |