mlock-intersect-test.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * It tests the duplicate mlock result:
  3. * - the ulimit of lock page is 64k
  4. * - allocate address area 64k starting from p
  5. * - mlock [p -- p + 30k]
  6. * - Then mlock address [ p -- p + 40k ]
  7. *
  8. * It should succeed since totally we locked
  9. * 40k < 64k limitation.
  10. *
  11. * It should not be run with CAP_IPC_LOCK.
  12. */
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <unistd.h>
  16. #include <sys/resource.h>
  17. #include <sys/capability.h>
  18. #include <sys/mman.h>
  19. #include "mlock2.h"
  20. int main(int argc, char **argv)
  21. {
  22. struct rlimit new;
  23. char *p = NULL;
  24. cap_t cap = cap_init();
  25. int i;
  26. /* drop capabilities including CAP_IPC_LOCK */
  27. if (cap_set_proc(cap))
  28. return -1;
  29. /* set mlock limits to 64k */
  30. new.rlim_cur = 65536;
  31. new.rlim_max = 65536;
  32. setrlimit(RLIMIT_MEMLOCK, &new);
  33. /* test VM_LOCK */
  34. p = malloc(1024 * 64);
  35. if (mlock(p, 1024 * 30)) {
  36. printf("mlock() 30k return failure.\n");
  37. return -1;
  38. }
  39. for (i = 0; i < 10; i++) {
  40. if (mlock(p, 1024 * 40)) {
  41. printf("mlock() #%d 40k returns failure.\n", i);
  42. return -1;
  43. }
  44. }
  45. for (i = 0; i < 10; i++) {
  46. if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
  47. printf("mlock2_() #%d 40k returns failure.\n", i);
  48. return -1;
  49. }
  50. }
  51. free(p);
  52. /* Test VM_LOCKONFAULT */
  53. p = malloc(1024 * 64);
  54. if (mlock2_(p, 1024 * 30, MLOCK_ONFAULT)) {
  55. printf("mlock2_() 30k return failure.\n");
  56. return -1;
  57. }
  58. for (i = 0; i < 10; i++) {
  59. if (mlock2_(p, 1024 * 40, MLOCK_ONFAULT)) {
  60. printf("mlock2_() #%d 40k returns failure.\n", i);
  61. return -1;
  62. }
  63. }
  64. for (i = 0; i < 10; i++) {
  65. if (mlock(p, 1024 * 40)) {
  66. printf("mlock() #%d 40k returns failure.\n", i);
  67. return -1;
  68. }
  69. }
  70. return 0;
  71. }