setrlimit.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * setrlimit() for uClibc
  4. *
  5. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  6. *
  7. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  8. */
  9. #define setrlimit64 __hide_setrlimit64
  10. #include <sys/syscall.h>
  11. #include <unistd.h>
  12. #include <sys/resource.h>
  13. #undef setrlimit64
  14. /* Only wrap setrlimit if the new usetrlimit is not present and setrlimit sucks */
  15. #if defined(__NR_usetrlimit)
  16. /* just call usetrlimit() */
  17. # define __NR___syscall_usetrlimit __NR_usetrlimit
  18. static __always_inline
  19. _syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
  20. const struct rlimit *, rlim)
  21. int setrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
  22. {
  23. return (__syscall_usetrlimit(resource, rlimits));
  24. }
  25. #elif !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  26. /* We don't need to wrap setrlimit() */
  27. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  28. const struct rlimit *, rlim)
  29. #else
  30. /* we have to handle old style setrlimit() */
  31. # define __NR___syscall_setrlimit __NR_setrlimit
  32. static __always_inline
  33. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
  34. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  35. {
  36. struct rlimit rlimits_small;
  37. if (rlimits == NULL) {
  38. __set_errno(EINVAL);
  39. return -1;
  40. }
  41. /* We might have to correct the limits values. Since the old values
  42. * were signed the new values might be too large. */
  43. # define RMIN(x, y) ((x) < (y) ? (x) : (y))
  44. rlimits_small.rlim_cur = RMIN((unsigned long int) rlimits->rlim_cur,
  45. RLIM_INFINITY >> 1);
  46. rlimits_small.rlim_max = RMIN((unsigned long int) rlimits->rlim_max,
  47. RLIM_INFINITY >> 1);
  48. #undef RMIN
  49. return (__syscall_setrlimit(resource, &rlimits_small));
  50. }
  51. #endif
  52. libc_hidden_def(setrlimit)
  53. #if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  54. strong_alias(setrlimit, setrlimit64)
  55. #endif