on-fault-limit.c 805 B

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