setrlimit.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 <sys/resource.h>
  11. #include <bits/wordsize.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. # define __need_NULL
  29. # include <stddef.h>
  30. # include <errno.h>
  31. # include <sys/param.h>
  32. /* we have to handle old style setrlimit() */
  33. # define __NR___syscall_setrlimit __NR_setrlimit
  34. static __always_inline
  35. _syscall2(int, __syscall_setrlimit, int, resource, const struct rlimit *, rlim)
  36. int setrlimit(__rlimit_resource_t resource, const struct rlimit *rlimits)
  37. {
  38. struct rlimit rlimits_small;
  39. if (rlimits == NULL) {
  40. __set_errno(EINVAL);
  41. return -1;
  42. }
  43. /* We might have to correct the limits values. Since the old values
  44. * were signed the new values might be too large. */
  45. rlimits_small.rlim_cur = MIN((unsigned long int) rlimits->rlim_cur,
  46. RLIM_INFINITY >> 1);
  47. rlimits_small.rlim_max = MIN((unsigned long int) rlimits->rlim_max,
  48. RLIM_INFINITY >> 1);
  49. return __syscall_setrlimit(resource, &rlimits_small);
  50. }
  51. #endif
  52. libc_hidden_def(setrlimit)
  53. #if __WORDSIZE == 64
  54. strong_alias_untyped(setrlimit, setrlimit64)
  55. #endif