xref: /aosp_15_r20/external/ltp/testcases/kernel/mem/mmapstress/mmapstress05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /* IBM Corporation */
2*49cdfc7eSAndroid Build Coastguard Worker /* 01/02/2003	Port to LTP	[email protected]*/
3*49cdfc7eSAndroid Build Coastguard Worker /* 06/30/2001	Port to Linux	[email protected] */
4*49cdfc7eSAndroid Build Coastguard Worker /*
5*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2003
6*49cdfc7eSAndroid Build Coastguard Worker  *
7*49cdfc7eSAndroid Build Coastguard Worker  *
8*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
9*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
10*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
11*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
14*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
15*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
16*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
17*49cdfc7eSAndroid Build Coastguard Worker  *
18*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
19*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
20*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21*49cdfc7eSAndroid Build Coastguard Worker  */
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /* mfile_insque:
24*49cdfc7eSAndroid Build Coastguard Worker  *	This test mmaps a portion of a file, and then mmaps the next
25*49cdfc7eSAndroid Build Coastguard Worker  *	portion of the file in front of the virtual space containing the
26*49cdfc7eSAndroid Build Coastguard Worker  *	original mmap.  It then mmaps the preceding portion of the file behind
27*49cdfc7eSAndroid Build Coastguard Worker  *	the original mmap.  None of the mmaps can be concatenated.
28*49cdfc7eSAndroid Build Coastguard Worker  */
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
36*49cdfc7eSAndroid Build Coastguard Worker /* #include <sys/pte.h> */
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker /*****	LTP Port	*****/
39*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
40*49cdfc7eSAndroid Build Coastguard Worker /*****	**	**	*****/
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker #ifndef MMU_NARROWPTEPG
43*49cdfc7eSAndroid Build Coastguard Worker #define MMU_NARROWPTEPG	1024
44*49cdfc7eSAndroid Build Coastguard Worker #endif
45*49cdfc7eSAndroid Build Coastguard Worker 
46*49cdfc7eSAndroid Build Coastguard Worker /*****	LTP Port	*****/
47*49cdfc7eSAndroid Build Coastguard Worker #define FAILED 0
48*49cdfc7eSAndroid Build Coastguard Worker #define PASSED 1
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker int local_flag = PASSED;
51*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmapstress05";	//mfile_insque
52*49cdfc7eSAndroid Build Coastguard Worker FILE *temp;
53*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker int anyfail();
56*49cdfc7eSAndroid Build Coastguard Worker void ok_exit();
57*49cdfc7eSAndroid Build Coastguard Worker /*****	**	**	*****/
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker #define ERROR(M)	(void)fprintf(stderr, "%s:  errno = %d; " M "\n", \
60*49cdfc7eSAndroid Build Coastguard Worker 				progname, errno);
61*49cdfc7eSAndroid Build Coastguard Worker #define CLEAN	(void)close(fd); \
62*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(mmapaddr+pagesize, pagesize) == -1) { \
63*49cdfc7eSAndroid Build Coastguard Worker 			ERROR("munmap failed"); \
64*49cdfc7eSAndroid Build Coastguard Worker 		} \
65*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(mmapaddr, pagesize) == -1) { \
66*49cdfc7eSAndroid Build Coastguard Worker 			ERROR("munmap failed"); \
67*49cdfc7eSAndroid Build Coastguard Worker 		} \
68*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(mmapaddr+2*pagesize, pagesize) == -1) { \
69*49cdfc7eSAndroid Build Coastguard Worker 			ERROR("munmap failed"); \
70*49cdfc7eSAndroid Build Coastguard Worker 		} \
71*49cdfc7eSAndroid Build Coastguard Worker 		if (unlink(tmpname)) { \
72*49cdfc7eSAndroid Build Coastguard Worker 			ERROR("couldn't clean up temp file"); \
73*49cdfc7eSAndroid Build Coastguard Worker 		}
74*49cdfc7eSAndroid Build Coastguard Worker 
75*49cdfc7eSAndroid Build Coastguard Worker #define CERROR(M)	CLEAN; ERROR(M)
76*49cdfc7eSAndroid Build Coastguard Worker 
77*49cdfc7eSAndroid Build Coastguard Worker #define CATCH_SIG(SIG) \
78*49cdfc7eSAndroid Build Coastguard Worker         if (sigaction(SIG, &sa, 0) == -1) { \
79*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't catch signal " #SIG); \
80*49cdfc7eSAndroid Build Coastguard Worker                 exit(1); \
81*49cdfc7eSAndroid Build Coastguard Worker         }
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker extern time_t time(time_t *);
84*49cdfc7eSAndroid Build Coastguard Worker extern char *ctime(const time_t *);
85*49cdfc7eSAndroid Build Coastguard Worker extern void exit(int);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker static int fd;
88*49cdfc7eSAndroid Build Coastguard Worker //static char *tmpname; 12/31/02
89*49cdfc7eSAndroid Build Coastguard Worker static char tmpname[] = "fileXXXXXX";
90*49cdfc7eSAndroid Build Coastguard Worker static char *progname;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker  /*ARGSUSED*/ static
cleanup(int sig)93*49cdfc7eSAndroid Build Coastguard Worker void cleanup(int sig)
94*49cdfc7eSAndroid Build Coastguard Worker {
95*49cdfc7eSAndroid Build Coastguard Worker 	/*
96*49cdfc7eSAndroid Build Coastguard Worker 	 * Don't check error codes - we could be signaled before the file is
97*49cdfc7eSAndroid Build Coastguard Worker 	 * created.
98*49cdfc7eSAndroid Build Coastguard Worker 	 */
99*49cdfc7eSAndroid Build Coastguard Worker 	(void)close(fd);
100*49cdfc7eSAndroid Build Coastguard Worker 	(void)unlink(tmpname);
101*49cdfc7eSAndroid Build Coastguard Worker 	exit(1);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])104*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	size_t pagesize = (size_t) sysconf(_SC_PAGE_SIZE);
107*49cdfc7eSAndroid Build Coastguard Worker 	caddr_t mmapaddr;
108*49cdfc7eSAndroid Build Coastguard Worker 	char *buf;
109*49cdfc7eSAndroid Build Coastguard Worker 	time_t t;
110*49cdfc7eSAndroid Build Coastguard Worker 	int i;
111*49cdfc7eSAndroid Build Coastguard Worker 	struct sigaction sa;
112*49cdfc7eSAndroid Build Coastguard Worker 
113*49cdfc7eSAndroid Build Coastguard Worker 	if (!argc) {
114*49cdfc7eSAndroid Build Coastguard Worker 		(void)fprintf(stderr, "argc == 0\n");
115*49cdfc7eSAndroid Build Coastguard Worker 		return 1;
116*49cdfc7eSAndroid Build Coastguard Worker 	}
117*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
118*49cdfc7eSAndroid Build Coastguard Worker 	progname = argv[0];
119*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
120*49cdfc7eSAndroid Build Coastguard Worker //      (void)printf("%s: Started %s", argv[0], ctime(&t)); LTP Port
121*49cdfc7eSAndroid Build Coastguard Worker 	if (sbrk(pagesize - ((ulong) sbrk(0) & (pagesize - 1))) == (char *)-1) {
122*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't round up brk");
123*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
124*49cdfc7eSAndroid Build Coastguard Worker 	}
125*49cdfc7eSAndroid Build Coastguard Worker 	if ((buf = sbrk(pagesize)) == (char *)-1) {
126*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't allocate output buffer");
127*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
128*49cdfc7eSAndroid Build Coastguard Worker 	}
129*49cdfc7eSAndroid Build Coastguard Worker 	if ((mmapaddr = (caddr_t) sbrk(0)) == (caddr_t) - 1) {
130*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("couldn't find top of brk");
131*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
132*49cdfc7eSAndroid Build Coastguard Worker 	}
133*49cdfc7eSAndroid Build Coastguard Worker 
134*49cdfc7eSAndroid Build Coastguard Worker 	/* i changed the second argument to NULL
135*49cdfc7eSAndroid Build Coastguard Worker 	   from argv[0]. otherwise it causes the
136*49cdfc7eSAndroid Build Coastguard Worker 	   open to fail
137*49cdfc7eSAndroid Build Coastguard Worker 	   -- sreeni
138*49cdfc7eSAndroid Build Coastguard Worker 	 */
139*49cdfc7eSAndroid Build Coastguard Worker 
140*49cdfc7eSAndroid Build Coastguard Worker 	if ((fd = mkstemp(tmpname)) == -1) {
141*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("mkstemp failed");
142*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
143*49cdfc7eSAndroid Build Coastguard Worker 	}
144*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_handler = cleanup;
145*49cdfc7eSAndroid Build Coastguard Worker 	sa.sa_flags = 0;
146*49cdfc7eSAndroid Build Coastguard Worker 	if (sigemptyset(&sa.sa_mask)) {
147*49cdfc7eSAndroid Build Coastguard Worker 		ERROR("sigemptyset failed");
148*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
149*49cdfc7eSAndroid Build Coastguard Worker 	}
150*49cdfc7eSAndroid Build Coastguard Worker 	CATCH_SIG(SIGINT);
151*49cdfc7eSAndroid Build Coastguard Worker 	CATCH_SIG(SIGQUIT);
152*49cdfc7eSAndroid Build Coastguard Worker 	CATCH_SIG(SIGTERM);
153*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < pagesize; i++)
154*49cdfc7eSAndroid Build Coastguard Worker 		buf[i] = 'a';
155*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fd, buf, pagesize) != pagesize) {
156*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("couldn't write page case 1");
157*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
158*49cdfc7eSAndroid Build Coastguard Worker 	}
159*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
160*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("lseek case 1 failed");
161*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
162*49cdfc7eSAndroid Build Coastguard Worker 	}
163*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fd, buf, pagesize) != pagesize) {
164*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("couldn't write page case 2");
165*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
166*49cdfc7eSAndroid Build Coastguard Worker 	}
167*49cdfc7eSAndroid Build Coastguard Worker 	if (lseek(fd, 2 * MMU_NARROWPTEPG * pagesize, SEEK_SET) == -1) {
168*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("lseek case 2 failed");
169*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
170*49cdfc7eSAndroid Build Coastguard Worker 	}
171*49cdfc7eSAndroid Build Coastguard Worker 	if (write(fd, buf, pagesize) != pagesize) {
172*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("couldn't write page case 3");
173*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
174*49cdfc7eSAndroid Build Coastguard Worker 	}
175*49cdfc7eSAndroid Build Coastguard Worker 	/* fd now references a sparce file which has three pages widely spaced.
176*49cdfc7eSAndroid Build Coastguard Worker 	 * Hopefully different mfile objects will be needed to reference each
177*49cdfc7eSAndroid Build Coastguard Worker 	 * page.
178*49cdfc7eSAndroid Build Coastguard Worker 	 */
179*49cdfc7eSAndroid Build Coastguard Worker 	if (mmap(mmapaddr + pagesize, pagesize, PROT_READ,
180*49cdfc7eSAndroid Build Coastguard Worker 		 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
181*49cdfc7eSAndroid Build Coastguard Worker 		 MMU_NARROWPTEPG * pagesize)
182*49cdfc7eSAndroid Build Coastguard Worker 	    == (caddr_t) - 1) {
183*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("first mmap (of third page) failed");
184*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
185*49cdfc7eSAndroid Build Coastguard Worker 	}
186*49cdfc7eSAndroid Build Coastguard Worker 	if (mmap(mmapaddr, pagesize, PROT_READ,
187*49cdfc7eSAndroid Build Coastguard Worker 		 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd,
188*49cdfc7eSAndroid Build Coastguard Worker 		 2 * MMU_NARROWPTEPG * pagesize)
189*49cdfc7eSAndroid Build Coastguard Worker 	    == (caddr_t) - 1) {
190*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("second mmap (of fifth page) failed");
191*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
192*49cdfc7eSAndroid Build Coastguard Worker 	}
193*49cdfc7eSAndroid Build Coastguard Worker 	if (mmap(mmapaddr + 2 * pagesize, pagesize, PROT_READ,
194*49cdfc7eSAndroid Build Coastguard Worker 		 MAP_FILE | MAP_PRIVATE | MAP_FIXED, fd, 0) == (caddr_t) - 1) {
195*49cdfc7eSAndroid Build Coastguard Worker 		CERROR("third mmap (of first page) failed");
196*49cdfc7eSAndroid Build Coastguard Worker 		anyfail();
197*49cdfc7eSAndroid Build Coastguard Worker 	}
198*49cdfc7eSAndroid Build Coastguard Worker 	CLEAN;			/*comment */
199*49cdfc7eSAndroid Build Coastguard Worker 	(void)time(&t);
200*49cdfc7eSAndroid Build Coastguard Worker //      (void)printf("%s: Finished %s", argv[0], ctime(&t)); LTP Port
201*49cdfc7eSAndroid Build Coastguard Worker 	ok_exit();
202*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
203*49cdfc7eSAndroid Build Coastguard Worker }
204*49cdfc7eSAndroid Build Coastguard Worker 
ok_exit(void)205*49cdfc7eSAndroid Build Coastguard Worker void ok_exit(void)
206*49cdfc7eSAndroid Build Coastguard Worker {
207*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TPASS, "Test passed");
208*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
209*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker 
anyfail(void)212*49cdfc7eSAndroid Build Coastguard Worker int anyfail(void)
213*49cdfc7eSAndroid Build Coastguard Worker {
214*49cdfc7eSAndroid Build Coastguard Worker 	tst_brkm(TFAIL, tst_rmdir, "Test failed");
215*49cdfc7eSAndroid Build Coastguard Worker }
216