xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/mmap/mmap14.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 FNST, DAN LI <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software;  you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker  * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker  * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker  * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
12*49cdfc7eSAndroid Build Coastguard Worker  * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program;  if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker /*
20*49cdfc7eSAndroid Build Coastguard Worker  * Test Description:
21*49cdfc7eSAndroid Build Coastguard Worker  *  Verify MAP_LOCKED works fine.
22*49cdfc7eSAndroid Build Coastguard Worker  *  "Lock the pages of the mapped region into memory in the manner of mlock(2)."
23*49cdfc7eSAndroid Build Coastguard Worker  *
24*49cdfc7eSAndroid Build Coastguard Worker  * Expected Result:
25*49cdfc7eSAndroid Build Coastguard Worker  *  mmap() should succeed returning the address of the mapped region,
26*49cdfc7eSAndroid Build Coastguard Worker  *  and this region should be locked into memory.
27*49cdfc7eSAndroid Build Coastguard Worker  */
28*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <sys/mman.h>
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define TEMPFILE        "mmapfile"
34*49cdfc7eSAndroid Build Coastguard Worker #define MMAPSIZE        (1UL<<20)
35*49cdfc7eSAndroid Build Coastguard Worker #define LINELEN         256
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "mmap14";
38*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker static char *addr;
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static void getvmlck(unsigned int *lock_sz);
43*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
45*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])46*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
49*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sz_before;
50*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sz_after;
51*49cdfc7eSAndroid Build Coastguard Worker 	unsigned int sz_ch;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(argc, argv, NULL, NULL);
54*49cdfc7eSAndroid Build Coastguard Worker 
55*49cdfc7eSAndroid Build Coastguard Worker 	setup();
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); lc++) {
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 		tst_count = 0;
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker 		getvmlck(&sz_before);
62*49cdfc7eSAndroid Build Coastguard Worker 
63*49cdfc7eSAndroid Build Coastguard Worker 		addr = mmap(NULL, MMAPSIZE, PROT_READ | PROT_WRITE,
64*49cdfc7eSAndroid Build Coastguard Worker 			    MAP_PRIVATE | MAP_LOCKED | MAP_ANONYMOUS,
65*49cdfc7eSAndroid Build Coastguard Worker 			    -1, 0);
66*49cdfc7eSAndroid Build Coastguard Worker 
67*49cdfc7eSAndroid Build Coastguard Worker 		if (addr == MAP_FAILED) {
68*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL | TERRNO, "mmap of %s failed", TEMPFILE);
69*49cdfc7eSAndroid Build Coastguard Worker 			continue;
70*49cdfc7eSAndroid Build Coastguard Worker 		}
71*49cdfc7eSAndroid Build Coastguard Worker 
72*49cdfc7eSAndroid Build Coastguard Worker 		getvmlck(&sz_after);
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 		sz_ch = sz_after - sz_before;
75*49cdfc7eSAndroid Build Coastguard Worker 		if (sz_ch == MMAPSIZE / 1024) {
76*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TPASS, "Functionality of mmap() "
77*49cdfc7eSAndroid Build Coastguard Worker 					"successful");
78*49cdfc7eSAndroid Build Coastguard Worker 		} else {
79*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TFAIL, "Expected %luK locked, "
80*49cdfc7eSAndroid Build Coastguard Worker 					"get %uK locked",
81*49cdfc7eSAndroid Build Coastguard Worker 					MMAPSIZE / 1024, sz_ch);
82*49cdfc7eSAndroid Build Coastguard Worker 		}
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 		if (munmap(addr, MMAPSIZE) != 0)
85*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TFAIL | TERRNO, NULL, "munmap failed");
86*49cdfc7eSAndroid Build Coastguard Worker 	}
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
89*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker 
getvmlck(unsigned int * lock_sz)92*49cdfc7eSAndroid Build Coastguard Worker void getvmlck(unsigned int *lock_sz)
93*49cdfc7eSAndroid Build Coastguard Worker {
94*49cdfc7eSAndroid Build Coastguard Worker 	int ret;
95*49cdfc7eSAndroid Build Coastguard Worker 	char line[LINELEN];
96*49cdfc7eSAndroid Build Coastguard Worker 	FILE *fstatus = NULL;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	fstatus = fopen("/proc/self/status", "r");
99*49cdfc7eSAndroid Build Coastguard Worker 	if (fstatus == NULL)
100*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TERRNO, NULL, "Open dev status failed");
101*49cdfc7eSAndroid Build Coastguard Worker 
102*49cdfc7eSAndroid Build Coastguard Worker 	while (fgets(line, LINELEN, fstatus) != NULL)
103*49cdfc7eSAndroid Build Coastguard Worker 		if (strstr(line, "VmLck") != NULL)
104*49cdfc7eSAndroid Build Coastguard Worker 			break;
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker 	ret = sscanf(line, "%*[^0-9]%d%*[^0-9]", lock_sz);
107*49cdfc7eSAndroid Build Coastguard Worker 	if (ret != 1)
108*49cdfc7eSAndroid Build Coastguard Worker 		tst_brkm(TFAIL | TERRNO, NULL, "Get lock size failed");
109*49cdfc7eSAndroid Build Coastguard Worker 
110*49cdfc7eSAndroid Build Coastguard Worker 	fclose(fstatus);
111*49cdfc7eSAndroid Build Coastguard Worker }
112*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)113*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
114*49cdfc7eSAndroid Build Coastguard Worker {
115*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
116*49cdfc7eSAndroid Build Coastguard Worker 
117*49cdfc7eSAndroid Build Coastguard Worker 	tst_sig(FORK, DEF_HANDLER, cleanup);
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	TEST_PAUSE;
120*49cdfc7eSAndroid Build Coastguard Worker }
121*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)122*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
123*49cdfc7eSAndroid Build Coastguard Worker {
124*49cdfc7eSAndroid Build Coastguard Worker }
125