ulimit.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2000-2006 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include <sys/syscall.h>
  8. #ifdef __NR_ulimit
  9. extern long int ulimit(int cmd, long arg);
  10. _syscall2(long, ulimit, int, cmd, long, arg)
  11. #else
  12. #include <stdarg.h>
  13. #include <unistd.h>
  14. #include <ulimit.h>
  15. #include <sys/resource.h>
  16. long int ulimit(int cmd, ...)
  17. {
  18. va_list va;
  19. struct rlimit limit;
  20. long int result = -1;
  21. va_start (va, cmd);
  22. switch (cmd) {
  23. /* Get limit on file size. */
  24. case UL_GETFSIZE:
  25. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  26. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  27. break;
  28. /* Set limit on file size. */
  29. case UL_SETFSIZE:
  30. result = va_arg (va, long int);
  31. if ((rlim_t) result > RLIM_INFINITY / 512) {
  32. limit.rlim_cur = RLIM_INFINITY;
  33. limit.rlim_max = RLIM_INFINITY;
  34. } else {
  35. limit.rlim_cur = result * 512;
  36. limit.rlim_max = result * 512;
  37. }
  38. result = setrlimit(RLIMIT_FSIZE, &limit);
  39. break;
  40. case __UL_GETOPENMAX:
  41. result = sysconf(_SC_OPEN_MAX);
  42. break;
  43. default:
  44. __set_errno(EINVAL);
  45. }
  46. va_end (va);
  47. return result;
  48. }
  49. #endif