setrlimit.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. #include <sys/syscall.h>
  10. #include <unistd.h>
  11. #include <sys/resource.h>
  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, struct rlimit *rlimits)
  20. {
  21. return (__syscall_usetrlimit(resource, rlimits));
  22. }
  23. #elif !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  24. /* We don't need to wrap setrlimit() */
  25. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  26. const struct rlimit *, rlim)
  27. #else
  28. /* we have to handle old style setrlimit() */
  29. # define __NR___syscall_setrlimit __NR_setrlimit
  30. static __always_inline
  31. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
  32. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  33. {
  34. struct rlimit rlimits_small;
  35. if (rlimits == NULL) {
  36. __set_errno(EINVAL);
  37. return -1;
  38. }
  39. /* We might have to correct the limits values. Since the old values
  40. * were signed the new values might be too large. */
  41. # define RMIN(x, y) ((x) < (y) ? (x) : (y))
  42. rlimits_small.rlim_cur = RMIN((unsigned long int) rlimits->rlim_cur,
  43. RLIM_INFINITY >> 1);
  44. rlimits_small.rlim_max = RMIN((unsigned long int) rlimits->rlim_max,
  45. RLIM_INFINITY >> 1);
  46. #undef RMIN
  47. return (__syscall_setrlimit(resource, &rlimits_small));
  48. }
  49. #endif
  50. libc_hidden_def(setrlimit)
  51. #if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  52. strong_alias_untyped(setrlimit, setrlimit64)
  53. #endif