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 _GNU_SOURCE
  21. #define _LARGEFILE64_SOURCE
  22. #include <features.h>
  23. #undef __OPTIMIZE__
  24. /* We absolutely do _NOT_ want interfaces silently
  25. * * * renamed under us or very bad things will happen... */
  26. #ifdef __USE_FILE_OFFSET64
  27. # undef __USE_FILE_OFFSET64
  28. #endif
  29. #ifdef __NR_ulimit
  30. #include <sys/types.h>
  31. #include <sys/syscall.h>
  32. _syscall2(long, ulimit, int, cmd, int, arg);
  33. #else
  34. #include <stdarg.h>
  35. #include <unistd.h>
  36. #include <ulimit.h>
  37. #include <errno.h>
  38. #include <sys/resource.h>
  39. libc_hidden_proto(sysconf)
  40. libc_hidden_proto(getrlimit)
  41. libc_hidden_proto(setrlimit)
  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