plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2003 |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 3 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 8 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 13 | * |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | /* |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 20 | * Test Description: |
| 21 | * Verify that truncating a mmaped file works correctly. |
| 22 | * |
| 23 | * Expected Result: |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 24 | * ftruncate should be allowed to increase, decrease, or zero the |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 25 | * size of a file that has been mmaped |
| 26 | * |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 27 | * Test: |
| 28 | * Use ftruncate to shrink the file while it is mapped |
| 29 | * Use ftruncate to grow the file while it is mapped |
| 30 | * Use ftruncate to zero the size of the file while it is mapped |
| 31 | * |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 32 | * HISTORY |
| 33 | * 04/2003 Written by Paul Larson |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 34 | */ |
| 35 | #include <stdio.h> |
| 36 | #include <unistd.h> |
| 37 | #include <fcntl.h> |
| 38 | #include <errno.h> |
| 39 | #include <sys/mman.h> |
| 40 | #include <sys/types.h> |
| 41 | #include "test.h" |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 42 | |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 43 | #define mapsize (1 << 14) |
| 44 | |
| 45 | char *TCID = "mmap09"; |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 46 | int TST_TOTAL = 3; |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 47 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 48 | static int fd; |
| 49 | static char *maddr; |
| 50 | |
| 51 | static struct test_case_t { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 52 | off_t newsize; |
| 53 | char *desc; |
| 54 | } TC[] = { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 55 | {mapsize - 8192, "ftruncate mmaped file to a smaller size"}, |
| 56 | {mapsize + 1024, "ftruncate mmaped file to a larger size"}, |
| 57 | {0, "ftruncate mmaped file to 0 size"}, |
| 58 | }; |
| 59 | |
| 60 | static void setup(void); |
| 61 | static void cleanup(void); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 62 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | int main(int argc, char **argv) |
| 64 | { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 65 | int lc; |
| 66 | int i; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 67 | const char *msg; |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 68 | |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 69 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 70 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 71 | |
| 72 | setup(); |
| 73 | |
| 74 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 75 | tst_count = 0; |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 76 | for (i = 0; i < TST_TOTAL; i++) { |
| 77 | TEST(ftruncate(fd, TC[i].newsize)); |
| 78 | |
| 79 | if (TEST_RETURN == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 80 | tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 81 | } else { |
vapier | 5fa775a | 2009-08-28 13:23:36 +0000 | [diff] [blame] | 82 | tst_resm(TPASS, "%s", TC[i].desc); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 87 | |
robbiew | faedd9e | 2003-04-15 19:08:59 +0000 | [diff] [blame] | 88 | cleanup(); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 89 | tst_exit(); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 92 | static void setup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 93 | { |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 94 | tst_sig(NOFORK, DEF_HANDLER, cleanup); |
| 95 | |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 96 | TEST_PAUSE; |
| 97 | |
| 98 | tst_tmpdir(); |
| 99 | |
| 100 | if ((fd = open("mmaptest", O_RDWR | O_CREAT, 0666)) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 101 | tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed"); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 102 | |
| 103 | /* ftruncate the file to 16k */ |
| 104 | if (ftruncate(fd, mapsize) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 105 | tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed"); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 106 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 107 | maddr = mmap(0, mapsize, PROT_READ | PROT_WRITE, |
| 108 | MAP_FILE | MAP_SHARED, fd, 0); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 109 | if (maddr == MAP_FAILED) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 110 | tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed"); |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 111 | |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 112 | /* fill up the file with A's */ |
Garrett Cooper | 11d5104 | 2010-11-22 20:47:29 -0800 | [diff] [blame] | 113 | memset(maddr, 'A', mapsize); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 116 | static void cleanup(void) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 117 | { |
Cyril Hrubis | 5efee33 | 2013-06-04 20:14:58 +0200 | [diff] [blame] | 118 | munmap(maddr, mapsize); |
plars | 3266b45 | 2003-04-04 22:12:38 +0000 | [diff] [blame] | 119 | close(fd); |
| 120 | tst_rmdir(); |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 121 | } |