tst-getrlimit.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* Copyright (C) 2005-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 <stdbool.h>
  16. #include <stdio.h>
  17. #include <sys/resource.h>
  18. static struct
  19. {
  20. const char *name;
  21. int resource;
  22. bool required;
  23. } tests[] =
  24. {
  25. /* The following 7 limits are part of POSIX and must exist. */
  26. { "RLIMIT_CORE", RLIMIT_CORE, true },
  27. { "RLIMIT_CPU", RLIMIT_CPU, true },
  28. { "RLIMIT_DATA", RLIMIT_DATA, true },
  29. { "RLIMIT_FSIZE", RLIMIT_FSIZE, true },
  30. { "RLIMIT_NOFILE", RLIMIT_NOFILE, true },
  31. { "RLIMIT_STACK", RLIMIT_STACK, true },
  32. { "RLIMIT_AS", RLIMIT_AS, true },
  33. /* The following are traditional Unix limits which are also
  34. expected (by us). */
  35. { "RLIMIT_RSS", RLIMIT_RSS, true },
  36. { "RLIMIT_NPROC", RLIMIT_NPROC, true },
  37. /* The following are extensions. */
  38. #ifdef RLIMIT_MEMLOCK
  39. { "RLIMIT_MEMLOCK", RLIMIT_MEMLOCK, false },
  40. #endif
  41. #ifdef RLIMIT_LOCKS
  42. { "RLIMIT_LOCKS", RLIMIT_LOCKS, false },
  43. #endif
  44. #ifdef RLIMIT_SIGPENDING
  45. { "RLIMIT_SIGPENDING", RLIMIT_SIGPENDING, false },
  46. #endif
  47. #ifdef RLIMIT_MSGQUEUE
  48. { "RLIMIT_MSGQUEUE", RLIMIT_MSGQUEUE, false },
  49. #endif
  50. #ifdef RLIMIT_NICE
  51. { "RLIMIT_NICE", RLIMIT_NICE, false },
  52. #endif
  53. #ifdef RLIMIT_RTPRIO
  54. { "RLIMIT_RTPRIO", RLIMIT_RTPRIO, false },
  55. #endif
  56. };
  57. #define ntests (sizeof (tests) / sizeof (tests[0]))
  58. static int
  59. do_test (void)
  60. {
  61. int status = 0;
  62. for (int i = 0; i < ntests; ++i)
  63. {
  64. bool this_ok = true;
  65. struct rlimit r;
  66. int res = getrlimit (tests[i].resource, &r);
  67. if (res == -1)
  68. {
  69. if (errno == EINVAL)
  70. {
  71. if (tests[i].required)
  72. {
  73. printf ("limit %s expectedly not available for getrlimit\n",
  74. tests[i].name);
  75. status = 1;
  76. this_ok = false;
  77. }
  78. }
  79. else
  80. {
  81. printf ("getrlimit for %s returned unexpected error: %m\n",
  82. tests[i].name);
  83. status = 1;
  84. this_ok = false;
  85. }
  86. }
  87. struct rlimit64 r64;
  88. res = getrlimit64 (tests[i].resource, &r64);
  89. if (res == -1)
  90. {
  91. if (errno == EINVAL)
  92. {
  93. if (tests[i].required)
  94. {
  95. printf ("limit %s expectedly not available for getrlimit64"
  96. "\n", tests[i].name);
  97. status = 1;
  98. this_ok = false;
  99. }
  100. }
  101. else
  102. {
  103. printf ("getrlimit64 for %s returned unexpected error: %m\n",
  104. tests[i].name);
  105. status = 1;
  106. this_ok = false;
  107. }
  108. }
  109. if (this_ok)
  110. printf ("limit %s OK\n", tests[i].name);
  111. }
  112. return status;
  113. }
  114. #define TEST_FUNCTION do_test ()
  115. #include "../test-skeleton.c"