ulimit.c 1.3 KB

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