1 // SPDX-License-Identifier: GPL-2.0
2 #include <sys/mman.h>
3 #include <stdio.h>
4 #include <unistd.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <sys/resource.h>
8 
9 #ifndef MCL_ONFAULT
10 #define MCL_ONFAULT (MCL_FUTURE << 1)
11 #endif
12 
test_limit(void)13 static int test_limit(void)
14 {
15 	int ret = 1;
16 	struct rlimit lims;
17 	void *map;
18 
19 	if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
20 		perror("getrlimit");
21 		return ret;
22 	}
23 
24 	if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
25 		perror("mlockall");
26 		return ret;
27 	}
28 
29 	map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
30 		   MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
31 	if (map != MAP_FAILED)
32 		printf("mmap should have failed, but didn't\n");
33 	else {
34 		ret = 0;
35 		munmap(map, 2 * lims.rlim_max);
36 	}
37 
38 	munlockall();
39 	return ret;
40 }
41 
main(int argc,char ** argv)42 int main(int argc, char **argv)
43 {
44 	int ret = 0;
45 
46 	ret += test_limit();
47 	return ret;
48 }
49