ulimit.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003 by Erik Andersen <andersen@codepoet.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU Library General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
  13. * for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public License
  16. * along with this program; if not, write to the Free Software Foundation,
  17. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. #define sysconf __sysconf
  21. #define getrlimit __getrlimit
  22. #define setrlimit __setrlimit
  23. #define _GNU_SOURCE
  24. #define _LARGEFILE64_SOURCE
  25. #include <features.h>
  26. #undef __OPTIMIZE__
  27. /* We absolutely do _NOT_ want interfaces silently
  28. * * * renamed under us or very bad things will happen... */
  29. #ifdef __USE_FILE_OFFSET64
  30. # undef __USE_FILE_OFFSET64
  31. #endif
  32. #ifdef __NR_ulimit
  33. #include <sys/types.h>
  34. #include <sys/syscall.h>
  35. _syscall2(long, ulimit, int, cmd, int, arg);
  36. #else
  37. #include <stdarg.h>
  38. #include <unistd.h>
  39. #include <ulimit.h>
  40. #include <errno.h>
  41. #include <sys/resource.h>
  42. long int ulimit(int cmd, ...)
  43. {
  44. va_list va;
  45. struct rlimit limit;
  46. long int result = -1;
  47. va_start (va, cmd);
  48. switch (cmd) {
  49. /* Get limit on file size. */
  50. case UL_GETFSIZE:
  51. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  52. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  53. break;
  54. /* Set limit on file size. */
  55. case UL_SETFSIZE:
  56. result = va_arg (va, long int);
  57. if ((rlim_t) result > RLIM_INFINITY / 512) {
  58. limit.rlim_cur = RLIM_INFINITY;
  59. limit.rlim_max = RLIM_INFINITY;
  60. } else {
  61. limit.rlim_cur = result * 512;
  62. limit.rlim_max = result * 512;
  63. }
  64. result = setrlimit(RLIMIT_FSIZE, &limit);
  65. break;
  66. case __UL_GETOPENMAX:
  67. result = sysconf(_SC_OPEN_MAX);
  68. break;
  69. default:
  70. __set_errno(EINVAL);
  71. }
  72. va_end (va);
  73. return result;
  74. }
  75. #endif