ulimit.c 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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 "syscalls.h"
  8. #ifdef __NR_ulimit
  9. _syscall2(long, ulimit, int, cmd, int, arg);
  10. #else
  11. #include <stdarg.h>
  12. #include <unistd.h>
  13. #include <ulimit.h>
  14. #include <sys/resource.h>
  15. libc_hidden_proto(sysconf)
  16. libc_hidden_proto(getrlimit)
  17. libc_hidden_proto(setrlimit)
  18. long int ulimit(int cmd, ...)
  19. {
  20. va_list va;
  21. struct rlimit limit;
  22. long int result = -1;
  23. va_start (va, cmd);
  24. switch (cmd) {
  25. /* Get limit on file size. */
  26. case UL_GETFSIZE:
  27. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  28. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  29. break;
  30. /* Set limit on file size. */
  31. case UL_SETFSIZE:
  32. result = va_arg (va, long int);
  33. if ((rlim_t) result > RLIM_INFINITY / 512) {
  34. limit.rlim_cur = RLIM_INFINITY;
  35. limit.rlim_max = RLIM_INFINITY;
  36. } else {
  37. limit.rlim_cur = result * 512;
  38. limit.rlim_max = result * 512;
  39. }
  40. result = setrlimit(RLIMIT_FSIZE, &limit);
  41. break;
  42. case __UL_GETOPENMAX:
  43. result = sysconf(_SC_OPEN_MAX);
  44. break;
  45. default:
  46. __set_errno(EINVAL);
  47. }
  48. va_end (va);
  49. return result;
  50. }
  51. #endif