tst-rlimit-infinity.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (C) 2018-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  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 <sys/resource.h>
  17. #include <support/check.h>
  18. static int resources[] = {
  19. /* The following 7 limits are part of POSIX and must exist. */
  20. RLIMIT_CORE,
  21. RLIMIT_CPU,
  22. RLIMIT_DATA,
  23. RLIMIT_FSIZE,
  24. RLIMIT_NOFILE,
  25. RLIMIT_STACK,
  26. RLIMIT_AS
  27. };
  28. #define nresources (sizeof (resources) / sizeof (resources[0]))
  29. /* Assume that the prlimit64 function calls the prlimit64 syscall without
  30. mangling the arguments. */
  31. #define PRLIMIT64_INFINITY ((rlim64_t) -1)
  32. /* As we don't know which limit will be modified, use a sufficiently high
  33. value to not shoot ourself in the foot. Use a 32-bit value to test
  34. both the 32- and 64-bit versions, and keep the highest bit clear to
  35. avoid sign extension. */
  36. #define PRLIMIT64_TESTVAL ((rlim64_t) 0x42420000)
  37. static void
  38. test_getrlimit (int resource, rlim_t exp_cur, rlim_t exp_max)
  39. {
  40. struct rlimit r;
  41. TEST_VERIFY_EXIT (getrlimit (resource, &r) == 0);
  42. TEST_COMPARE (r.rlim_cur, exp_cur);
  43. TEST_COMPARE (r.rlim_max, exp_max);
  44. }
  45. static void
  46. test_getrlimit64 (int resource, rlim64_t exp_cur, rlim64_t exp_max)
  47. {
  48. struct rlimit64 r;
  49. TEST_VERIFY_EXIT (getrlimit64 (resource, &r) == 0);
  50. TEST_COMPARE (r.rlim_cur, exp_cur);
  51. TEST_COMPARE (r.rlim_max, exp_max);
  52. }
  53. static void
  54. test_prlimit_get (int resource, rlim_t exp_cur, rlim_t exp_max)
  55. {
  56. struct rlimit r;
  57. TEST_VERIFY_EXIT (prlimit (0, resource, NULL, &r) == 0);
  58. TEST_COMPARE (r.rlim_cur, exp_cur);
  59. TEST_COMPARE (r.rlim_max, exp_max);
  60. }
  61. static void
  62. test_prlimit64_get (int resource, rlim64_t exp_cur, rlim64_t exp_max)
  63. {
  64. struct rlimit64 r;
  65. TEST_COMPARE (prlimit64 (0, resource, NULL, &r), 0);
  66. TEST_COMPARE (r.rlim_cur, exp_cur);
  67. TEST_COMPARE (r.rlim_max, exp_max);
  68. }
  69. static void
  70. test_setrlimit (int resource, rlim_t new_cur, rlim_t new_max)
  71. {
  72. struct rlimit r = { new_cur, new_max };
  73. TEST_COMPARE (setrlimit (resource, &r), 0);
  74. }
  75. static void
  76. test_setrlimit64 (int resource, rlim64_t new_cur, rlim64_t new_max)
  77. {
  78. struct rlimit64 r = { new_cur, new_max };
  79. TEST_COMPARE (setrlimit64 (resource, &r), 0);
  80. }
  81. static void
  82. test_prlimit_set (int resource, rlim_t new_cur, rlim_t new_max)
  83. {
  84. struct rlimit r = { new_cur, new_max };
  85. TEST_COMPARE (prlimit (0, resource, &r, NULL), 0);
  86. }
  87. static void
  88. test_prlimit64_set (int resource, rlim64_t new_cur, rlim64_t new_max)
  89. {
  90. struct rlimit64 r = { new_cur, new_max };
  91. TEST_COMPARE (prlimit64 (0, resource, &r, NULL), 0);
  92. }
  93. static int
  94. do_test (void)
  95. {
  96. int resource = -1;
  97. /* Find a resource with hard limit set to infinity, so that the soft limit
  98. can be manipulated to any value. */
  99. for (int i = 0; i < nresources; ++i)
  100. {
  101. struct rlimit64 r64;
  102. int res = prlimit64 (0, resources[i], NULL, &r64);
  103. if ((res == 0) && (r64.rlim_max == PRLIMIT64_INFINITY))
  104. {
  105. resource = resources[i];
  106. break;
  107. }
  108. }
  109. if (resource == -1)
  110. FAIL_UNSUPPORTED
  111. ("Could not find and limit with hard limit set to infinity.");
  112. /* First check that the get functions work correctly with the test value. */
  113. test_prlimit64_set (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
  114. test_getrlimit (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
  115. test_getrlimit64 (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
  116. test_prlimit_get (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
  117. test_prlimit64_get (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
  118. /* Then check that the get functions work correctly with infinity. */
  119. test_prlimit64_set (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
  120. test_getrlimit (resource, RLIM_INFINITY, RLIM_INFINITY);
  121. test_getrlimit64 (resource, RLIM64_INFINITY, RLIM64_INFINITY);
  122. test_prlimit_get (resource, RLIM_INFINITY, RLIM_INFINITY);
  123. test_prlimit64_get (resource, RLIM64_INFINITY, RLIM64_INFINITY);
  124. /* Then check that setrlimit works correctly with the test value. */
  125. test_setrlimit (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
  126. test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
  127. /* Then check that setrlimit works correctly with infinity. */
  128. test_setrlimit (resource, RLIM_INFINITY, RLIM_INFINITY);
  129. test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
  130. /* Then check that setrlimit64 works correctly with the test value. */
  131. test_setrlimit64 (resource, PRLIMIT64_TESTVAL, RLIM64_INFINITY);
  132. test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
  133. /* Then check that setrlimit64 works correctly with infinity. */
  134. test_setrlimit64 (resource, RLIM64_INFINITY, RLIM64_INFINITY);
  135. test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
  136. /* Then check that prlimit works correctly with the test value. */
  137. test_prlimit_set (resource, RLIM_INFINITY, RLIM_INFINITY);
  138. test_prlimit64_get (resource, PRLIMIT64_INFINITY, PRLIMIT64_INFINITY);
  139. /* Finally check that prlimit works correctly with infinity. */
  140. test_prlimit_set (resource, PRLIMIT64_TESTVAL, RLIM_INFINITY);
  141. test_prlimit64_get (resource, PRLIMIT64_TESTVAL, PRLIMIT64_INFINITY);
  142. return 0;
  143. }
  144. #include <support/test-driver.c>