Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 #include <sys/mman.h>
0003 #include <stdio.h>
0004 #include <unistd.h>
0005 #include <string.h>
0006 #include <sys/time.h>
0007 #include <sys/resource.h>
0008 
0009 #ifndef MCL_ONFAULT
0010 #define MCL_ONFAULT (MCL_FUTURE << 1)
0011 #endif
0012 
0013 static int test_limit(void)
0014 {
0015     int ret = 1;
0016     struct rlimit lims;
0017     void *map;
0018 
0019     if (getrlimit(RLIMIT_MEMLOCK, &lims)) {
0020         perror("getrlimit");
0021         return ret;
0022     }
0023 
0024     if (mlockall(MCL_ONFAULT | MCL_FUTURE)) {
0025         perror("mlockall");
0026         return ret;
0027     }
0028 
0029     map = mmap(NULL, 2 * lims.rlim_max, PROT_READ | PROT_WRITE,
0030            MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
0031     if (map != MAP_FAILED)
0032         printf("mmap should have failed, but didn't\n");
0033     else {
0034         ret = 0;
0035         munmap(map, 2 * lims.rlim_max);
0036     }
0037 
0038     munlockall();
0039     return ret;
0040 }
0041 
0042 int main(int argc, char **argv)
0043 {
0044     int ret = 0;
0045 
0046     ret += test_limit();
0047     return ret;
0048 }