setrlimit.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. libc_hidden_proto(setrlimit)
  15. /* Only wrap setrlimit if the new usetrlimit is not present and setrlimit sucks */
  16. #if defined(__NR_usetrlimit)
  17. /* just call usetrlimit() */
  18. # define __NR___syscall_usetrlimit __NR_usetrlimit
  19. static inline
  20. _syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
  21. const struct rlimit *, rlim);
  22. int setrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
  23. {
  24. return (__syscall_usetrlimit(resource, rlimits));
  25. }
  26. #elif !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  27. /* We don't need to wrap setrlimit() */
  28. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  29. const struct rlimit *, rlim);
  30. #else
  31. /* we have to handle old style setrlimit() */
  32. # define __NR___syscall_setrlimit __NR_setrlimit
  33. static inline
  34. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim);
  35. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  36. {
  37. struct rlimit rlimits_small;
  38. if (rlimits == NULL) {
  39. __set_errno(EINVAL);
  40. return -1;
  41. }
  42. /* We might have to correct the limits values. Since the old values
  43. * were signed the new values might be too large. */
  44. # define RMIN(x, y) ((x) < (y) ? (x) : (y))
  45. rlimits_small.rlim_cur = RMIN((unsigned long int) rlimits->rlim_cur,
  46. RLIM_INFINITY >> 1);
  47. rlimits_small.rlim_max = RMIN((unsigned long int) rlimits->rlim_max,
  48. RLIM_INFINITY >> 1);
  49. #undef RMIN
  50. return (__syscall_setrlimit(resource, &rlimits_small));
  51. }
  52. #endif
  53. libc_hidden_def(setrlimit)
  54. #if defined __UCLIBC_HAS_LFS__ && __WORDSIZE == 64
  55. strong_alias(setrlimit, setrlimit64)
  56. #endif