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 static int test_limit(void)
10 {
11 	int ret = 1;
12 	struct rlimit lims;
13 	void *map;
14 
15 	if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
16 		perror("getrlimit");
17 		return ret;
18 	}
19 
20 	if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
21 		perror("mlockall");
22 		return ret;
23 	}
24 
25 	map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
26 		   MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
27 	if (map != MAP_FAILED)
28 		printf("mmap should have failed, but didn't\n");
29 	else {
30 		ret = 0;
31 		munmap(map, 2 * lims.rlim_max);
32 	}
33 
34 	munlockall();
35 	return ret;
36 }
37 
38 int main(int argc, char **argv)
39 {
40 	int ret = 0;
41 
42 	ret += test_limit();
43 	return ret;
44 }
45