tst-mlock2.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Test the mlock2 function.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <support/check.h>
  17. #include <support/xunistd.h>
  18. #include <sys/mman.h>
  19. /* Allocate a page using mmap. */
  20. static void *
  21. get_page (void)
  22. {
  23. return xmmap (NULL, 1, PROT_READ | PROT_WRITE,
  24. MAP_ANONYMOUS | MAP_PRIVATE, -1);
  25. }
  26. static int
  27. do_test (void)
  28. {
  29. /* Current kernels have a small reserve of locked memory, so this
  30. test does not need any privileges to run. */
  31. void *page = get_page ();
  32. if (mlock (page, 1) != 0)
  33. FAIL_EXIT1 ("mlock: %m\n");
  34. xmunmap (page, 1);
  35. page = get_page ();
  36. if (mlock2 (page, 1, 0) != 0)
  37. /* Should be implemented using mlock if necessary. */
  38. FAIL_EXIT1 ("mlock2 (0): %m\n");
  39. xmunmap (page, 1);
  40. page = get_page ();
  41. int ret = mlock2 (page, 1, MLOCK_ONFAULT);
  42. if (ret != 0)
  43. {
  44. TEST_VERIFY (ret == -1);
  45. if (errno != EINVAL)
  46. /* EINVAL means the system does not support the mlock2 system
  47. call. */
  48. FAIL_EXIT1 ("mlock2 (0): %m\n");
  49. else
  50. puts ("warning: mlock2 system call not supported");
  51. }
  52. xmunmap (page, 1);
  53. return 0;
  54. }
  55. #include <support/test-driver.c>