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