ulimit.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 _GNU_SOURCE
  22. #define _LARGEFILE64_SOURCE
  23. #include <features.h>
  24. #undef __OPTIMIZE__
  25. /* We absolutely do _NOT_ want interfaces silently
  26. * * * renamed under us or very bad things will happen... */
  27. #ifdef __USE_FILE_OFFSET64
  28. # undef __USE_FILE_OFFSET64
  29. #endif
  30. #ifdef __NR_ulimit
  31. #include <sys/types.h>
  32. #include <sys/syscall.h>
  33. _syscall2(long, ulimit, int, cmd, int, arg);
  34. #else
  35. #include <stdarg.h>
  36. #include <unistd.h>
  37. #include <ulimit.h>
  38. #include <errno.h>
  39. #include <sys/resource.h>
  40. long int ulimit(int cmd, ...)
  41. {
  42. va_list va;
  43. struct rlimit limit;
  44. long int result = -1;
  45. va_start (va, cmd);
  46. switch (cmd) {
  47. /* Get limit on file size. */
  48. case UL_GETFSIZE:
  49. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  50. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  51. break;
  52. /* Set limit on file size. */
  53. case UL_SETFSIZE:
  54. result = va_arg (va, long int);
  55. if ((rlim_t) result > RLIM_INFINITY / 512) {
  56. limit.rlim_cur = RLIM_INFINITY;
  57. limit.rlim_max = RLIM_INFINITY;
  58. } else {
  59. limit.rlim_cur = result * 512;
  60. limit.rlim_max = result * 512;
  61. }
  62. result = setrlimit(RLIMIT_FSIZE, &limit);
  63. break;
  64. case __UL_GETOPENMAX:
  65. result = sysconf(_SC_OPEN_MAX);
  66. break;
  67. default:
  68. __set_errno(EINVAL);
  69. }
  70. va_end (va);
  71. return result;
  72. }
  73. #endif