xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap09.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker 
3*49cdfc7eSAndroid Build Coastguard Worker /*
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2003
5*49cdfc7eSAndroid Build Coastguard Worker  * Author: by Paul Larson
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Verify that truncating a mmaped file works correctly.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * Use ftruncate to:
15*49cdfc7eSAndroid Build Coastguard Worker  *
16*49cdfc7eSAndroid Build Coastguard Worker  * 1. shrink the file while it is mapped
17*49cdfc7eSAndroid Build Coastguard Worker  * 2. grow the file while it is mapped
18*49cdfc7eSAndroid Build Coastguard Worker  * 3. zero the size of the file while it is mapped
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /* size of the test file = 64 KB */
24*49cdfc7eSAndroid Build Coastguard Worker #define MAPSIZE (64 * 1024)
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static int fd;
27*49cdfc7eSAndroid Build Coastguard Worker static char *maddr;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct test_case
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	off_t newsize;
32*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
33*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
34*49cdfc7eSAndroid Build Coastguard Worker 	{MAPSIZE - 8192, "ftruncate mmaped file to a smaller size"},
35*49cdfc7eSAndroid Build Coastguard Worker 	{MAPSIZE + 1024, "ftruncate mmaped file to a larger size"},
36*49cdfc7eSAndroid Build Coastguard Worker 	{0, "ftruncate mmaped file to 0 size"},
37*49cdfc7eSAndroid Build Coastguard Worker };
38*49cdfc7eSAndroid Build Coastguard Worker 
verify_mmap(unsigned int nr)39*49cdfc7eSAndroid Build Coastguard Worker static void verify_mmap(unsigned int nr)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcases[nr];
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_PASS(ftruncate(fd, tc->newsize), "%s", tc->desc);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("/tmp/mmaptest", O_RDWR | O_CREAT, 0666);
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	/* set the file to initial size */
51*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FTRUNCATE(fd, MAPSIZE);
52*49cdfc7eSAndroid Build Coastguard Worker 	maddr = SAFE_MMAP(0, MAPSIZE, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0);
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	/* fill up the file with A's */
55*49cdfc7eSAndroid Build Coastguard Worker 	memset(maddr, 'A', MAPSIZE);
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)58*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
59*49cdfc7eSAndroid Build Coastguard Worker {
60*49cdfc7eSAndroid Build Coastguard Worker 	if (maddr)
61*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_MUNMAP(maddr, MAPSIZE);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 	if (fd)
64*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
68*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
69*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
70*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_mmap,
71*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
72*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
73*49cdfc7eSAndroid Build Coastguard Worker };
74