setrlimit.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /* Only wrap setrlimit if the new usetrlimit is not present and setrlimit sucks */
  12. #if defined(__NR_usetrlimit)
  13. /* just call usetrlimit() */
  14. # define __NR___syscall_usetrlimit __NR_usetrlimit
  15. static __always_inline
  16. _syscall2(int, __syscall_usetrlimit, enum __rlimit_resource, resource,
  17. const struct rlimit *, rlim)
  18. int setrlimit(__rlimit_resource_t resource, struct rlimit *rlimits)
  19. {
  20. return __syscall_usetrlimit(resource, rlimits);
  21. }
  22. #elif !defined(__UCLIBC_HANDLE_OLDER_RLIMIT__)
  23. /* We don't need to wrap setrlimit() */
  24. _syscall2(int, setrlimit, __rlimit_resource_t, resource,
  25. const struct rlimit *, rlim)
  26. #else
  27. # define __need_NULL
  28. # include <stddef.h>
  29. # include <errno.h>
  30. # include <sys/param.h>
  31. /* we have to handle old style setrlimit() */
  32. # define __NR___syscall_setrlimit __NR_setrlimit
  33. static __always_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. rlimits_small.rlim_cur = MIN((unsigned long int) rlimits->rlim_cur,
  45. RLIM_INFINITY >> 1);
  46. rlimits_small.rlim_max = MIN((unsigned long int) rlimits->rlim_max,
  47. RLIM_INFINITY >> 1);
  48. return __syscall_setrlimit(resource, &rlimits_small);
  49. }
  50. #endif
  51. libc_hidden_def(setrlimit)
  52. #if __WORDSIZE == 64
  53. strong_alias_untyped(setrlimit, setrlimit64)
  54. #endif