prlimit.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright (C) 2010-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 <sys/resource.h>
  16. #include <sys/syscall.h>
  17. int
  18. prlimit (__pid_t pid, enum __rlimit_resource resource,
  19. const struct rlimit *new_rlimit, struct rlimit *old_rlimit)
  20. {
  21. struct rlimit64 new_rlimit64_mem;
  22. struct rlimit64 *new_rlimit64 = NULL;
  23. struct rlimit64 old_rlimit64_mem;
  24. struct rlimit64 *old_rlimit64 = (old_rlimit != NULL
  25. ? &old_rlimit64_mem : NULL);
  26. if (new_rlimit != NULL)
  27. {
  28. if (new_rlimit->rlim_cur == RLIM_INFINITY)
  29. new_rlimit64_mem.rlim_cur = RLIM64_INFINITY;
  30. else
  31. new_rlimit64_mem.rlim_cur = new_rlimit->rlim_cur;
  32. if (new_rlimit->rlim_max == RLIM_INFINITY)
  33. new_rlimit64_mem.rlim_max = RLIM64_INFINITY;
  34. else
  35. new_rlimit64_mem.rlim_max = new_rlimit->rlim_max;
  36. new_rlimit64 = &new_rlimit64_mem;
  37. }
  38. int res = INLINE_SYSCALL (prlimit64, 4, pid, resource, new_rlimit64,
  39. old_rlimit64);
  40. if (res == 0 && old_rlimit != NULL)
  41. {
  42. /* The prlimit64 syscall is ill-designed for 32-bit machines.
  43. We have to provide a 32-bit variant since otherwise the LFS
  44. system would not work. The infinity value can be translated,
  45. but otherwise what shall we do if the syscall succeeds but the
  46. old values do not fit into a rlimit structure? We cannot return
  47. an error because the operation itself worked. Best is perhaps
  48. to return RLIM_INFINITY. */
  49. old_rlimit->rlim_cur = old_rlimit64_mem.rlim_cur;
  50. if (old_rlimit->rlim_cur != old_rlimit64_mem.rlim_cur)
  51. {
  52. if ((new_rlimit == NULL)
  53. && (old_rlimit64_mem.rlim_cur != RLIM64_INFINITY))
  54. return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
  55. old_rlimit->rlim_cur = RLIM_INFINITY;
  56. }
  57. old_rlimit->rlim_max = old_rlimit64_mem.rlim_max;
  58. if (old_rlimit->rlim_max != old_rlimit64_mem.rlim_max)
  59. {
  60. if ((new_rlimit == NULL)
  61. && (old_rlimit64_mem.rlim_max != RLIM64_INFINITY))
  62. return INLINE_SYSCALL_ERROR_RETURN_VALUE (EOVERFLOW);
  63. old_rlimit->rlim_max = RLIM_INFINITY;
  64. }
  65. }
  66. return res;
  67. }