setrlimit.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #else
  24. # if !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  25. # if defined(__NR_prlimit64)
  26. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  27. {
  28. return INLINE_SYSCALL (prlimit64, 4, 0, resource, rlimits, NULL);
  29. }
  30. # else
  31. /* We don't need to wrap setrlimit() */
  32. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  33. const struct rlimit *, rlim)
  34. # endif
  35. # else
  36. # define __need_NULL
  37. # include <stddef.h>
  38. # include <errno.h>
  39. # include <sys/param.h>
  40. /* we have to handle old style setrlimit() */
  41. # define __NR___syscall_setrlimit __NR_setrlimit
  42. static __always_inline
  43. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
  44. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  45. {
  46. struct rlimit rlimits_small;
  47. if (rlimits == NULL) {
  48. __set_errno(EINVAL);
  49. return -1;
  50. }
  51. /* We might have to correct the limits values. Since the old values
  52. * were signed the new values might be too large. */
  53. rlimits_small.rlim_cur = MIN((unsigned long int) rlimits->rlim_cur,
  54. RLIM_INFINITY >> 1);
  55. rlimits_small.rlim_max = MIN((unsigned long int) rlimits->rlim_max,
  56. RLIM_INFINITY >> 1);
  57. # if defined(__NR_prlimit64)
  58. return INLINE_SYSCALL (prlimit64, 4, 0, resource, &rlimits_small, NULL);
  59. # else
  60. return __syscall_setrlimit(resource, &rlimits_small);
  61. # endif
  62. }
  63. # endif
  64. #endif
  65. libc_hidden_def(setrlimit)
  66. #if __WORDSIZE == 64
  67. strong_alias_untyped(setrlimit, setrlimit64)
  68. #endif