tst-rlimit.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * setrlimit/getrlimit/prlimit and setrlimit64/getrlimit64/prlimit64 functions
  3. * test for uClibc.
  4. *
  5. * The prlimit64 function is not called directly in this test, because it is a
  6. * new function for uclibc and can cause build problems with uclibc-ng <= 1.0.44.
  7. * With _FILE_OFFSET_BITS == 64 the prlimit call is redirected to the prlimit64
  8. * call.
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <errno.h>
  14. #include <sys/resource.h>
  15. #define __ASSERT(x, f, l) \
  16. if (!(x)) { \
  17. fprintf(stderr, "%s: LINE %d: ASSERT: " #x "\n", f, l); \
  18. exit(1); \
  19. }
  20. #define ASSERT(x) __ASSERT(x, __FILE__, __LINE__)
  21. static int resources[] = {
  22. RLIMIT_CORE,
  23. RLIMIT_CPU,
  24. RLIMIT_DATA,
  25. RLIMIT_FSIZE,
  26. RLIMIT_NOFILE,
  27. RLIMIT_STACK,
  28. RLIMIT_AS
  29. };
  30. #define nresources (sizeof (resources) / sizeof (resources[0]))
  31. #define TEST_VAL 0x11223344
  32. static void test_getrlimit(int rnum, rlim_t exp_cur, rlim_t exp_max)
  33. {
  34. struct rlimit r;
  35. ASSERT(getrlimit(rnum, &r) == 0);
  36. ASSERT(r.rlim_cur == exp_cur);
  37. ASSERT(r.rlim_max == exp_max);
  38. }
  39. static void test_setrlimit(int rnum, rlim_t new_cur, rlim_t new_max)
  40. {
  41. struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max};
  42. ASSERT(setrlimit(rnum, &r) == 0);
  43. }
  44. static void test_prlimit_get(int rnum, rlim_t exp_cur, rlim_t exp_max)
  45. {
  46. struct rlimit r;
  47. ASSERT(prlimit(0, rnum, NULL, &r) == 0);
  48. ASSERT(r.rlim_cur == exp_cur);
  49. ASSERT(r.rlim_max == exp_max);
  50. }
  51. static void test_prlimit_set(int rnum, rlim_t new_cur, rlim_t new_max)
  52. {
  53. struct rlimit r = {.rlim_cur = new_cur, .rlim_max = new_max};
  54. ASSERT(prlimit(0, rnum, &r, NULL) == 0);
  55. }
  56. #if defined(__USE_LARGEFILE64)
  57. static void test_getrlimit64(int rnum, rlim64_t exp_cur, rlim64_t exp_max)
  58. {
  59. struct rlimit64 r;
  60. ASSERT(getrlimit64(rnum, &r) == 0);
  61. ASSERT(r.rlim_cur == exp_cur);
  62. ASSERT(r.rlim_max == exp_max);
  63. }
  64. static void test_setrlimit64(int rnum, rlim64_t new_cur, rlim64_t new_max)
  65. {
  66. struct rlimit64 r = {.rlim_cur = new_cur, .rlim_max = new_max};
  67. ASSERT(setrlimit64(rnum, &r) == 0);
  68. }
  69. #endif
  70. int main(void)
  71. {
  72. int rnum = -1;
  73. struct rlimit rlim;
  74. int i, ret;
  75. /* Find a resource with hard limit set to infinity */
  76. for (i = 0; i < nresources; ++i) {
  77. ret = getrlimit(resources[i], &rlim);
  78. if ((!ret) && (rlim.rlim_max == RLIM_INFINITY)) {
  79. rnum = resources[i];
  80. break;
  81. }
  82. }
  83. /* Can't continue, return unsupported */
  84. if (rnum == -1)
  85. return 23;
  86. /* Test cases */
  87. test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  88. test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  89. test_setrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
  90. test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
  91. test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);
  92. test_prlimit_set(rnum, TEST_VAL, RLIM_INFINITY);
  93. test_prlimit_get(rnum, TEST_VAL, RLIM_INFINITY);
  94. test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  95. test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
  96. test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
  97. test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);
  98. #if defined(__USE_LARGEFILE64)
  99. /* Check setrlim64 and getrlim64 in 32-bit offset LFS environment */
  100. test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  101. test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  102. test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  103. test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  104. test_prlimit_get(rnum, RLIM_INFINITY, RLIM_INFINITY);
  105. test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  106. test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  107. test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  108. test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
  109. test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  110. #endif
  111. #if defined(__USE_LARGEFILE64) && _FILE_OFFSET_BITS == 64
  112. /* Check setrlim64/getrlim64/prlimit64 in 64-bit offset environment */
  113. test_setrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  114. test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  115. test_getrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  116. test_setrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  117. test_prlimit_get(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  118. test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
  119. test_prlimit_set(rnum, TEST_VAL, RLIM64_INFINITY);
  120. test_prlimit_get(rnum, TEST_VAL, RLIM64_INFINITY);
  121. test_prlimit_set(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  122. test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  123. test_getrlimit(rnum, RLIM_INFINITY, RLIM_INFINITY);
  124. test_setrlimit(rnum, TEST_VAL, RLIM_INFINITY);
  125. test_getrlimit64(rnum, TEST_VAL, RLIM64_INFINITY);
  126. test_prlimit_set(rnum, RLIM_INFINITY, RLIM_INFINITY);
  127. test_getrlimit64(rnum, RLIM64_INFINITY, RLIM64_INFINITY);
  128. #endif
  129. return 0;
  130. }