prlimit.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Get/set resource limits. Linux specific syscall.
  2. Copyright (C) 2021-2022 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. <https://www.gnu.org/licenses/>. */
  14. #include <sys/resource.h>
  15. #include <sysdep.h>
  16. #include <stddef.h> // needed for NULL to be defined
  17. #if defined(__NR_prlimit64) && __WORDSIZE == 32 && !defined(__USE_FILE_OFFSET64)
  18. int
  19. prlimit (__pid_t pid, enum __rlimit_resource resource,
  20. const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
  21. {
  22. struct rlimit64 new_rlimit64;
  23. struct rlimit64 *new_rlimit64_ptr = NULL;
  24. struct rlimit64 old_rlimit64;
  25. struct rlimit64 *old_rlimit64_ptr = (old_rlimit != NULL ? &old_rlimit64 : NULL);
  26. int res;
  27. if (new_rlimit != NULL) {
  28. if (new_rlimit->rlim_cur == RLIM_INFINITY)
  29. new_rlimit64.rlim_cur = RLIM64_INFINITY;
  30. else
  31. new_rlimit64.rlim_cur = new_rlimit->rlim_cur;
  32. if (new_rlimit->rlim_max == RLIM_INFINITY)
  33. new_rlimit64.rlim_max = RLIM64_INFINITY;
  34. else
  35. new_rlimit64.rlim_max = new_rlimit->rlim_max;
  36. new_rlimit64_ptr = &new_rlimit64;
  37. }
  38. res = INLINE_SYSCALL (prlimit64, 4, pid, resource, new_rlimit64_ptr,
  39. old_rlimit64_ptr);
  40. if (res == 0 && old_rlimit != NULL) {
  41. /* If the syscall succeeds but the values do not fit into a
  42. rlimit structure set EOVERFLOW errno and retrun -1.
  43. With current Linux implementation of the prlimit64 syscall,
  44. overflow can't happen. An extra condition has been added to get
  45. the same behavior as in glibc for future potential overflows. */
  46. old_rlimit->rlim_cur = old_rlimit64.rlim_cur;
  47. if (old_rlimit64.rlim_cur != old_rlimit->rlim_cur) {
  48. if (new_rlimit == NULL &&
  49. old_rlimit64.rlim_cur != RLIM64_INFINITY) {
  50. __set_errno(EOVERFLOW);
  51. return -1;
  52. }
  53. old_rlimit->rlim_cur = RLIM_INFINITY;
  54. }
  55. old_rlimit->rlim_max = old_rlimit64.rlim_max;
  56. if (old_rlimit64.rlim_max != old_rlimit->rlim_max) {
  57. if (new_rlimit == NULL &&
  58. old_rlimit64.rlim_max != RLIM64_INFINITY) {
  59. __set_errno(EOVERFLOW);
  60. return -1;
  61. }
  62. old_rlimit->rlim_max = RLIM_INFINITY;
  63. }
  64. }
  65. return res;
  66. }
  67. #endif