ulimit.c 1.1 KB

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