ulimit.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. long int ulimit(int cmd, ...)
  40. {
  41. va_list va;
  42. struct rlimit limit;
  43. long int result = -1;
  44. va_start (va, cmd);
  45. switch (cmd) {
  46. /* Get limit on file size. */
  47. case UL_GETFSIZE:
  48. if (getrlimit(RLIMIT_FSIZE, &limit) == 0)
  49. result = limit.rlim_cur / 512; /* bytes to 512 byte blocksize */
  50. break;
  51. /* Set limit on file size. */
  52. case UL_SETFSIZE:
  53. result = va_arg (va, long int);
  54. if ((rlim_t) result > RLIM_INFINITY / 512) {
  55. limit.rlim_cur = RLIM_INFINITY;
  56. limit.rlim_max = RLIM_INFINITY;
  57. } else {
  58. limit.rlim_cur = result * 512;
  59. limit.rlim_max = result * 512;
  60. }
  61. result = setrlimit(RLIMIT_FSIZE, &limit);
  62. break;
  63. case __UL_GETOPENMAX:
  64. result = sysconf(_SC_OPEN_MAX);
  65. break;
  66. default:
  67. __set_errno(EINVAL);
  68. }
  69. va_end (va);
  70. return result;
  71. }
  72. #endif