setrlimit.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * setrlimit() for uClibc
  3. *
  4. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  5. *
  6. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  7. */
  8. #include <sys/syscall.h>
  9. #include <sys/resource.h>
  10. #include <bits/wordsize.h>
  11. #include <stddef.h> // needed for NULL to be defined
  12. /* Only wrap setrlimit if the new usetrlimit is not present and setrlimit sucks */
  13. #if defined(__NR_usetrlimit)
  14. /* just call usetrlimit() */
  15. # define __NR___syscall_usetrlimit __NR_usetrlimit
  16. static __always_inline
  17. _syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
  18. const struct rlimit *, rlim)
  19. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  20. {
  21. return __syscall_usetrlimit(resource, rlimits);
  22. }
  23. libc_hidden_def(setrlimit)
  24. #elif defined(__NR_prlimit64)
  25. /* Use prlimit64 if present, the prlimit64 syscall is free from a back
  26. compatibility stuff for setrlimit */
  27. # if __WORDSIZE == 32 && !defined(__USE_FILE_OFFSET64)
  28. /* If struct rlimit has 64-bit fields (if __WORDSIZE == 64 or __USE_FILE_OFFSET64
  29. is defined), then use setrlimit as an alias to setrlimit64, see setrlimit64.c */
  30. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  31. {
  32. struct rlimit64 rlimits64;
  33. if (rlimits->rlim_cur == RLIM_INFINITY)
  34. rlimits64.rlim_cur = RLIM64_INFINITY;
  35. else
  36. rlimits64.rlim_cur = rlimits->rlim_cur;
  37. if (rlimits->rlim_max == RLIM_INFINITY)
  38. rlimits64.rlim_max = RLIM64_INFINITY;
  39. else
  40. rlimits64.rlim_max = rlimits->rlim_max;
  41. return INLINE_SYSCALL (prlimit64, 4, 0, resource, &rlimits64, NULL);
  42. }
  43. libc_hidden_def(setrlimit)
  44. # endif
  45. #else
  46. # if !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  47. /* We don't need to wrap setrlimit() */
  48. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  49. const struct rlimit *, rlim)
  50. # else
  51. # define __need_NULL
  52. # include <stddef.h>
  53. # include <errno.h>
  54. # include <sys/param.h>
  55. /* we have to handle old style setrlimit() */
  56. # define __NR___syscall_setrlimit __NR_setrlimit
  57. static __always_inline
  58. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
  59. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  60. {
  61. struct rlimit rlimits_small;
  62. if (rlimits == NULL) {
  63. __set_errno(EINVAL);
  64. return -1;
  65. }
  66. /* We might have to correct the limits values. Since the old values
  67. * were signed the new values might be too large. */
  68. rlimits_small.rlim_cur = MIN((unsigned long int) rlimits->rlim_cur,
  69. RLIM_INFINITY >> 1);
  70. rlimits_small.rlim_max = MIN((unsigned long int) rlimits->rlim_max,
  71. RLIM_INFINITY >> 1);
  72. return __syscall_setrlimit(resource, &rlimits_small);
  73. }
  74. # endif
  75. libc_hidden_def(setrlimit)
  76. #endif