vlimit.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* Copyright (C) 1991-2019 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <http://www.gnu.org/licenses/>. */
  14. /* This is generic in the sense that it will work with the BSD, SYSV,
  15. or stub versions of getrlimit. Separate versions could be written
  16. for efficiency, but it's probably not worth it. */
  17. #include <sys/vlimit.h>
  18. #include <sys/resource.h>
  19. #include <errno.h>
  20. /* Set the soft limit for RESOURCE to be VALUE.
  21. Returns 0 for success, -1 for failure. */
  22. int
  23. vlimit (enum __vlimit_resource resource, int value)
  24. {
  25. if (resource >= LIM_CPU && resource <= LIM_MAXRSS)
  26. {
  27. /* The rlimit codes happen to each be one less
  28. than the corresponding vlimit codes. */
  29. enum __rlimit_resource rlimit_res =
  30. (enum __rlimit_resource) ((int) resource - 1);
  31. struct rlimit lims;
  32. if (__getrlimit (rlimit_res, &lims) < 0)
  33. return -1;
  34. lims.rlim_cur = value;
  35. return __setrlimit (rlimit_res, &lims);
  36. }
  37. __set_errno (EINVAL);
  38. return -1;
  39. }