plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2003 |
| 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 | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * Test Name: mmap09 |
| 22 | * |
| 23 | * Test Description: |
| 24 | * Verify that truncating a mmaped file works correctly. |
| 25 | * |
| 26 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * ftruncate should be allowed to increase, decrease, or zero the |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 28 | * size of a file that has been mmaped |
| 29 | * |
| 30 | * Algorithm: |
| 31 | * Setup: |
| 32 | * Create file |
| 33 | * mmap the file |
| 34 | * fill it with data |
| 35 | * |
| 36 | * Test: |
| 37 | * Use ftruncate to shrink the file while it is mapped |
| 38 | * Use ftruncate to grow the file while it is mapped |
| 39 | * Use ftruncate to zero the size of the file while it is mapped |
| 40 | * |
| 41 | * Usage: <for command-line> |
| 42 | * mmap09 [-c n] [-f] [-i n] [-I x] [-P x] [-t] |
| 43 | * where, -c n : Run n copies concurrently. |
| 44 | * -f : Turn off functionality Testing. |
| 45 | * -i n : Execute test n times. |
| 46 | * -I x : Execute test for x seconds. |
| 47 | * -P x : Pause for x seconds between iterations. |
| 48 | * -t : Turn on syscall timing. |
| 49 | * |
| 50 | * HISTORY |
| 51 | * 04/2003 Written by Paul Larson |
| 52 | * |
| 53 | * RESTRICTIONS: |
| 54 | * None. |
| 55 | */ |
| 56 | #include <stdio.h> |
| 57 | #include <unistd.h> |
| 58 | #include <fcntl.h> |
| 59 | #include <errno.h> |
| 60 | #include <sys/mman.h> |
| 61 | #include <sys/types.h> |
| 62 | #include "test.h" |
| 63 | #include "usctest.h" |
| 64 | |
| 65 | void setup(); |
| 66 | void cleanup(); |
| 67 | |
| 68 | #define mapsize (1 << 14) |
| 69 | |
| 70 | char *TCID = "mmap09"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 71 | int TST_TOTAL = 3; |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 72 | int fd; |
| 73 | char *maddr; |
| 74 | |
| 75 | struct test_case_t { |
| 76 | off_t newsize; |
| 77 | char *desc; |
| 78 | } TC[] = { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 79 | { |
| 80 | mapsize - 8192, "ftruncate mmaped file to a smaller size"}, { |
| 81 | mapsize + 1024, "ftruncate mmaped file to a larger size"}, { |
| 82 | 0, "ftruncate mmaped file to 0 size"},}; |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 83 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 84 | int main(int argc, char **argv) |
| 85 | { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 86 | int lc; |
| 87 | int i; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 88 | char *msg; /* for parse_opts */ |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 89 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 90 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 91 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 92 | |
| 93 | setup(); |
| 94 | |
| 95 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 96 | Tst_count = 0; |
| 97 | for (i = 0; i < TST_TOTAL; i++) { |
| 98 | TEST(ftruncate(fd, TC[i].newsize)); |
| 99 | |
| 100 | if (TEST_RETURN == -1) { |
| 101 | TEST_ERROR_LOG(TEST_ERRNO); |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 102 | tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 103 | } else { |
vapier | 5fa775a | 2009-08-28 13:23:36 +0000 | [diff] [blame] | 104 | tst_resm(TPASS, "%s", TC[i].desc); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 109 | |
robbiew | faedd9e | 2003-04-15 19:08:59 +0000 | [diff] [blame] | 110 | cleanup(); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 111 | tst_exit(); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 112 | } |
| 113 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 114 | void setup() |
| 115 | { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 116 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 117 | |
| 118 | /* Pause if option was specified */ |
| 119 | TEST_PAUSE; |
| 120 | |
| 121 | tst_tmpdir(); |
| 122 | |
| 123 | if ((fd = open("mmaptest", O_RDWR | O_CREAT, 0666)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 124 | tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed"); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 125 | |
| 126 | /* ftruncate the file to 16k */ |
| 127 | if (ftruncate(fd, mapsize) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 128 | tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed"); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 129 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 130 | maddr = mmap(0, (size_t) mapsize, PROT_READ | PROT_WRITE, |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 131 | MAP_FILE | MAP_SHARED, fd, (off_t) 0); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 132 | if (maddr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 133 | tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed"); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 134 | |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 135 | /* fill up the file with A's */ |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 136 | memset(maddr, 'A', mapsize); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 137 | |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 138 | } |
| 139 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 140 | void cleanup() |
| 141 | { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 142 | TEST_CLEANUP; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 143 | munmap(maddr, (size_t) mapsize); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 144 | close(fd); |
| 145 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 146 | } |