blob: ad4030bc11874e2f765f1d5bc9f1b47b703c7d38 [file] [log] [blame]
plars3266b452003-04-04 22:12:38 +00001/*
Cyril Hrubis5efee332013-06-04 20:14:58 +02002 * Copyright (c) International Business Machines Corp., 2003
plars3266b452003-04-04 22:12:38 +00003 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02004 * 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.
plars3266b452003-04-04 22:12:38 +00008 *
Cyril Hrubis5efee332013-06-04 20:14:58 +02009 * 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.
plars3266b452003-04-04 22:12:38 +000013 *
Cyril Hrubis5efee332013-06-04 20:14:58 +020014 * 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
plars3266b452003-04-04 22:12:38 +000017 */
18
19/*
plars3266b452003-04-04 22:12:38 +000020 * Test Description:
21 * Verify that truncating a mmaped file works correctly.
22 *
23 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000024 * ftruncate should be allowed to increase, decrease, or zero the
plars3266b452003-04-04 22:12:38 +000025 * size of a file that has been mmaped
26 *
plars3266b452003-04-04 22:12:38 +000027 * 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 *
plars3266b452003-04-04 22:12:38 +000032 * HISTORY
33 * 04/2003 Written by Paul Larson
plars3266b452003-04-04 22:12:38 +000034 */
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"
plars3266b452003-04-04 22:12:38 +000042
plars3266b452003-04-04 22:12:38 +000043#define mapsize (1 << 14)
44
45char *TCID = "mmap09";
subrata_modak56207ce2009-03-23 13:35:39 +000046int TST_TOTAL = 3;
plars3266b452003-04-04 22:12:38 +000047
Cyril Hrubis5efee332013-06-04 20:14:58 +020048static int fd;
49static char *maddr;
50
51static struct test_case_t {
plars3266b452003-04-04 22:12:38 +000052 off_t newsize;
53 char *desc;
54} TC[] = {
Cyril Hrubis5efee332013-06-04 20:14:58 +020055 {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
60static void setup(void);
61static void cleanup(void);
plars3266b452003-04-04 22:12:38 +000062
subrata_modak56207ce2009-03-23 13:35:39 +000063int main(int argc, char **argv)
64{
plars3266b452003-04-04 22:12:38 +000065 int lc;
66 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020067 const char *msg;
plars3266b452003-04-04 22:12:38 +000068
Garrett Cooper11d51042010-11-22 20:47:29 -080069 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
plars3266b452003-04-04 22:12:38 +000070 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars3266b452003-04-04 22:12:38 +000071
72 setup();
73
74 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080075 tst_count = 0;
plars3266b452003-04-04 22:12:38 +000076 for (i = 0; i < TST_TOTAL; i++) {
77 TEST(ftruncate(fd, TC[i].newsize));
78
79 if (TEST_RETURN == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +080080 tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc);
plars3266b452003-04-04 22:12:38 +000081 } else {
vapier5fa775a2009-08-28 13:23:36 +000082 tst_resm(TPASS, "%s", TC[i].desc);
plars3266b452003-04-04 22:12:38 +000083 }
84 }
85
86 }
subrata_modakbdbaec52009-02-26 12:14:51 +000087
robbiewfaedd9e2003-04-15 19:08:59 +000088 cleanup();
Garrett Cooper11d51042010-11-22 20:47:29 -080089 tst_exit();
plars3266b452003-04-04 22:12:38 +000090}
91
Cyril Hrubis5efee332013-06-04 20:14:58 +020092static void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000093{
plars3266b452003-04-04 22:12:38 +000094 tst_sig(NOFORK, DEF_HANDLER, cleanup);
95
plars3266b452003-04-04 22:12:38 +000096 TEST_PAUSE;
97
98 tst_tmpdir();
99
100 if ((fd = open("mmaptest", O_RDWR | O_CREAT, 0666)) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800101 tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed");
plars3266b452003-04-04 22:12:38 +0000102
103 /* ftruncate the file to 16k */
104 if (ftruncate(fd, mapsize) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105 tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
plars3266b452003-04-04 22:12:38 +0000106
Cyril Hrubis5efee332013-06-04 20:14:58 +0200107 maddr = mmap(0, mapsize, PROT_READ | PROT_WRITE,
108 MAP_FILE | MAP_SHARED, fd, 0);
plars3266b452003-04-04 22:12:38 +0000109 if (maddr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800110 tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed");
Garrett Cooper11d51042010-11-22 20:47:29 -0800111
plars3266b452003-04-04 22:12:38 +0000112 /* fill up the file with A's */
Garrett Cooper11d51042010-11-22 20:47:29 -0800113 memset(maddr, 'A', mapsize);
plars3266b452003-04-04 22:12:38 +0000114}
115
Cyril Hrubis5efee332013-06-04 20:14:58 +0200116static void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000117{
Cyril Hrubis5efee332013-06-04 20:14:58 +0200118 munmap(maddr, mapsize);
plars3266b452003-04-04 22:12:38 +0000119 close(fd);
120 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700121}