blob: 61861e3c0b1d8a86067632ef36f21809b72228cb [file] [log] [blame]
plars3266b452003-04-04 22:12:38 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars3266b452003-04-04 22:12:38 +000018 */
19
20/*
21 * Test Name: mmap09
22 *
23 * Test Description:
24 * Verify that truncating a mmaped file works correctly.
25 *
26 * Expected Result:
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * ftruncate should be allowed to increase, decrease, or zero the
plars3266b452003-04-04 22:12:38 +000028 * 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
65void setup();
66void cleanup();
67
68#define mapsize (1 << 14)
69
70char *TCID = "mmap09";
subrata_modak56207ce2009-03-23 13:35:39 +000071int TST_TOTAL = 3;
plars3266b452003-04-04 22:12:38 +000072int fd;
73char *maddr;
74
75struct test_case_t {
76 off_t newsize;
77 char *desc;
78} TC[] = {
subrata_modak56207ce2009-03-23 13:35:39 +000079 {
80 mapsize - 8192, "ftruncate mmaped file to a smaller size"}, {
81 mapsize + 1024, "ftruncate mmaped file to a larger size"}, {
820, "ftruncate mmaped file to 0 size"},};
plars3266b452003-04-04 22:12:38 +000083
subrata_modak56207ce2009-03-23 13:35:39 +000084int main(int argc, char **argv)
85{
plars3266b452003-04-04 22:12:38 +000086 int lc;
87 int i;
subrata_modak56207ce2009-03-23 13:35:39 +000088 char *msg; /* for parse_opts */
plars3266b452003-04-04 22:12:38 +000089
Garrett Cooper11d51042010-11-22 20:47:29 -080090 if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL)
plars3266b452003-04-04 22:12:38 +000091 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars3266b452003-04-04 22:12:38 +000092
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 Gao354ebb42012-12-07 10:10:04 +0800102 tst_resm(TFAIL | TTERRNO, "%s", TC[i].desc);
plars3266b452003-04-04 22:12:38 +0000103 } else {
vapier5fa775a2009-08-28 13:23:36 +0000104 tst_resm(TPASS, "%s", TC[i].desc);
plars3266b452003-04-04 22:12:38 +0000105 }
106 }
107
108 }
subrata_modakbdbaec52009-02-26 12:14:51 +0000109
robbiewfaedd9e2003-04-15 19:08:59 +0000110 cleanup();
Garrett Cooper11d51042010-11-22 20:47:29 -0800111 tst_exit();
plars3266b452003-04-04 22:12:38 +0000112}
113
subrata_modak56207ce2009-03-23 13:35:39 +0000114void setup()
115{
plars3266b452003-04-04 22:12:38 +0000116 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 Gao354ebb42012-12-07 10:10:04 +0800124 tst_brkm(TFAIL | TERRNO, cleanup, "opening mmaptest failed");
plars3266b452003-04-04 22:12:38 +0000125
126 /* ftruncate the file to 16k */
127 if (ftruncate(fd, mapsize) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 tst_brkm(TFAIL | TERRNO, cleanup, "ftruncate file failed");
plars3266b452003-04-04 22:12:38 +0000129
subrata_modak56207ce2009-03-23 13:35:39 +0000130 maddr = mmap(0, (size_t) mapsize, PROT_READ | PROT_WRITE,
Wanlong Gao354ebb42012-12-07 10:10:04 +0800131 MAP_FILE | MAP_SHARED, fd, (off_t) 0);
plars3266b452003-04-04 22:12:38 +0000132 if (maddr == MAP_FAILED)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800133 tst_brkm(TFAIL | TERRNO, cleanup, "mmapping mmaptest failed");
Garrett Cooper11d51042010-11-22 20:47:29 -0800134
plars3266b452003-04-04 22:12:38 +0000135 /* fill up the file with A's */
Garrett Cooper11d51042010-11-22 20:47:29 -0800136 memset(maddr, 'A', mapsize);
plars3266b452003-04-04 22:12:38 +0000137
plars3266b452003-04-04 22:12:38 +0000138}
139
subrata_modak56207ce2009-03-23 13:35:39 +0000140void cleanup()
141{
plars3266b452003-04-04 22:12:38 +0000142 TEST_CLEANUP;
subrata_modak56207ce2009-03-23 13:35:39 +0000143 munmap(maddr, (size_t) mapsize);
plars3266b452003-04-04 22:12:38 +0000144 close(fd);
145 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700146}