xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap001.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2000 Juan Quintela <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *                    Aaron Laffin <[email protected]>
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
6*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License
7*49cdfc7eSAndroid Build Coastguard Worker  * as published by the Free Software Foundation; either version 2
8*49cdfc7eSAndroid Build Coastguard Worker  * of the License, or (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18*49cdfc7eSAndroid Build Coastguard Worker  *
19*49cdfc7eSAndroid Build Coastguard Worker  * mmap001.c - Tests mmapping a big file and writing it once
20*49cdfc7eSAndroid Build Coastguard Worker  */
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
23*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
24*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
25*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
27*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
28*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmap001";
34*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 5;
35*49cdfc7eSAndroid Build Coastguard Worker static char *filename = NULL;
36*49cdfc7eSAndroid Build Coastguard Worker static int m_opt = 0;
37*49cdfc7eSAndroid Build Coastguard Worker static char *m_copt;
38*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)39*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
40*49cdfc7eSAndroid Build Coastguard Worker {
41*49cdfc7eSAndroid Build Coastguard Worker 	free(filename);
42*49cdfc7eSAndroid Build Coastguard Worker 
43*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
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 	char buf[1024];
49*49cdfc7eSAndroid Build Coastguard Worker 	/*
50*49cdfc7eSAndroid Build Coastguard Worker 	 * setup a default signal hander and a
51*49cdfc7eSAndroid Build Coastguard Worker 	 * temporary working directory.
52*49cdfc7eSAndroid Build Coastguard Worker 	 */
53*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	snprintf(buf, 1024, "testfile.%d", getpid());
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 	if ((filename = strdup(buf)) == NULL) {
62*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TBROK | TERRNO, cleanup, "strdup failed");
63*49cdfc7eSAndroid Build Coastguard Worker 	}
64*49cdfc7eSAndroid Build Coastguard Worker 
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
help(void)67*49cdfc7eSAndroid Build Coastguard Worker static void help(void)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	printf("  -m x    size of mmap in pages (default 1000)\n");
70*49cdfc7eSAndroid Build Coastguard Worker }
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker /*
73*49cdfc7eSAndroid Build Coastguard Worker  * add the -m option whose parameter is the
74*49cdfc7eSAndroid Build Coastguard Worker  * pages that should be mapped.
75*49cdfc7eSAndroid Build Coastguard Worker  */
76*49cdfc7eSAndroid Build Coastguard Worker option_t options[] = {
77*49cdfc7eSAndroid Build Coastguard Worker 	{"m:", &m_opt, &m_copt},
78*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, NULL, NULL}
79*49cdfc7eSAndroid Build Coastguard Worker };
80*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])81*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
82*49cdfc7eSAndroid Build Coastguard Worker {
83*49cdfc7eSAndroid Build Coastguard Worker 	char *array;
84*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
85*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int i;
86*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
87*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int pages, memsize;
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, options, help);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	if (m_opt) {
92*49cdfc7eSAndroid Build Coastguard Worker 		memsize = pages = atoi(m_copt);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 		if (memsize < 1) {
95*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK, cleanup, "Invalid arg for -m: %s",
96*49cdfc7eSAndroid Build Coastguard Worker 				 m_copt);
97*49cdfc7eSAndroid Build Coastguard Worker 		}
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker 		memsize *= getpagesize();	/* N PAGES */
100*49cdfc7eSAndroid Build Coastguard Worker 
101*49cdfc7eSAndroid Build Coastguard Worker 	} else {
102*49cdfc7eSAndroid Build Coastguard Worker 		/*
103*49cdfc7eSAndroid Build Coastguard Worker 		 * default size 1000 pages;
104*49cdfc7eSAndroid Build Coastguard Worker 		 */
105*49cdfc7eSAndroid Build Coastguard Worker 		memsize = pages = 1000;
106*49cdfc7eSAndroid Build Coastguard Worker 		memsize *= getpagesize();
107*49cdfc7eSAndroid Build Coastguard Worker 	}
108*49cdfc7eSAndroid Build Coastguard Worker 
109*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "mmap()ing file of %u pages or %u bytes", pages,
110*49cdfc7eSAndroid Build Coastguard Worker 		 memsize);
111*49cdfc7eSAndroid Build Coastguard Worker 
112*49cdfc7eSAndroid Build Coastguard Worker 	setup();
113*49cdfc7eSAndroid Build Coastguard Worker 
114*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
115*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 		fd = open(filename, O_RDWR | O_CREAT, 0666);
118*49cdfc7eSAndroid Build Coastguard Worker 		if ((fd == -1))
119*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup,
120*49cdfc7eSAndroid Build Coastguard Worker 				 "opening %s failed", filename);
121*49cdfc7eSAndroid Build Coastguard Worker 
122*49cdfc7eSAndroid Build Coastguard Worker 		if (lseek(fd, memsize, SEEK_SET) != memsize) {
123*49cdfc7eSAndroid Build Coastguard Worker 			TEST_ERRNO = errno;
124*49cdfc7eSAndroid Build Coastguard Worker 			close(fd);
125*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TTERRNO, cleanup, "lseek failed");
126*49cdfc7eSAndroid Build Coastguard Worker 		}
127*49cdfc7eSAndroid Build Coastguard Worker 
128*49cdfc7eSAndroid Build Coastguard Worker 		if (write(fd, "\0", 1) != 1) {
129*49cdfc7eSAndroid Build Coastguard Worker 			TEST_ERRNO = errno;
130*49cdfc7eSAndroid Build Coastguard Worker 			close(fd);
131*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TTERRNO, cleanup,
132*49cdfc7eSAndroid Build Coastguard Worker 				 "writing to %s failed", filename);
133*49cdfc7eSAndroid Build Coastguard Worker 		}
134*49cdfc7eSAndroid Build Coastguard Worker 
135*49cdfc7eSAndroid Build Coastguard Worker 		array = mmap(0, memsize, PROT_WRITE, MAP_SHARED, fd, 0);
136*49cdfc7eSAndroid Build Coastguard Worker 		if (array == MAP_FAILED) {
137*49cdfc7eSAndroid Build Coastguard Worker 			TEST_ERRNO = errno;
138*49cdfc7eSAndroid Build Coastguard Worker 			close(fd);
139*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TTERRNO, cleanup,
140*49cdfc7eSAndroid Build Coastguard Worker 				 "mmapping %s failed", filename);
141*49cdfc7eSAndroid Build Coastguard Worker 		} else {
142*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "mmap() completed successfully.");
143*49cdfc7eSAndroid Build Coastguard Worker 		}
144*49cdfc7eSAndroid Build Coastguard Worker 
145*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "touching mmaped memory");
146*49cdfc7eSAndroid Build Coastguard Worker 
147*49cdfc7eSAndroid Build Coastguard Worker 		for (i = 0; i < memsize; i++) {
148*49cdfc7eSAndroid Build Coastguard Worker 			array[i] = (char)i;
149*49cdfc7eSAndroid Build Coastguard Worker 		}
150*49cdfc7eSAndroid Build Coastguard Worker 
151*49cdfc7eSAndroid Build Coastguard Worker 		/*
152*49cdfc7eSAndroid Build Coastguard Worker 		 * seems that if the map area was bad, we'd get SEGV,
153*49cdfc7eSAndroid Build Coastguard Worker 		 * hence we can indicate a PASS.
154*49cdfc7eSAndroid Build Coastguard Worker 		 */
155*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS,
156*49cdfc7eSAndroid Build Coastguard Worker 			 "we're still here, mmaped area must be good");
157*49cdfc7eSAndroid Build Coastguard Worker 
158*49cdfc7eSAndroid Build Coastguard Worker 		TEST(msync(array, memsize, MS_SYNC));
159*49cdfc7eSAndroid Build Coastguard Worker 
160*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
161*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO,
162*49cdfc7eSAndroid Build Coastguard Worker 				 "synchronizing mmapped page failed");
163*49cdfc7eSAndroid Build Coastguard Worker 		} else {
164*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS,
165*49cdfc7eSAndroid Build Coastguard Worker 				 "synchronizing mmapped page passed");
166*49cdfc7eSAndroid Build Coastguard Worker 		}
167*49cdfc7eSAndroid Build Coastguard Worker 
168*49cdfc7eSAndroid Build Coastguard Worker 		TEST(munmap(array, memsize));
169*49cdfc7eSAndroid Build Coastguard Worker 
170*49cdfc7eSAndroid Build Coastguard Worker 		if (TEST_RETURN == -1) {
171*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TTERRNO,
172*49cdfc7eSAndroid Build Coastguard Worker 				 "munmapping %s failed", filename);
173*49cdfc7eSAndroid Build Coastguard Worker 		} else {
174*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "munmapping %s successful", filename);
175*49cdfc7eSAndroid Build Coastguard Worker 		}
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 		close(fd);
178*49cdfc7eSAndroid Build Coastguard Worker 		unlink(filename);
179*49cdfc7eSAndroid Build Coastguard Worker 
180*49cdfc7eSAndroid Build Coastguard Worker 	}
181*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
182*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
183*49cdfc7eSAndroid Build Coastguard Worker }
184