limit.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Loco program runner core
  3. * Copyright (C) 2011 Lodevil(Du Jiong)
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "limit.h"
  19. #include <sys/resource.h>
  20. #include <sys/time.h>
  21. const char *last_limit_err;
  22. int setResLimit(struct Runobj *runobj) {
  23. #define RAISE_EXIT(err) {last_limit_err = err;return -1;}
  24. struct rlimit rl;
  25. struct itimerval p_realt;
  26. rl.rlim_cur = runobj->time_limit / 1000 + 1;
  27. if (runobj->time_limit % 1000 > 800) {
  28. rl.rlim_cur += 1;
  29. }
  30. rl.rlim_max = rl.rlim_cur + 1;
  31. if (setrlimit(RLIMIT_CPU, &rl))
  32. RAISE_EXIT("set RLIMIT_CPU failure");
  33. /*rl.rlim_cur = runobj->memory_limit * 1024;
  34. rl.rlim_max = rl.rlim_cur + 1024;
  35. if (setrlimit(RLIMIT_DATA, &rl))
  36. RAISE_EXIT("set RLIMIT_DATA failure");
  37. rl.rlim_cur = runobj->memory_limit * 1024 * 2;
  38. rl.rlim_max = rl.rlim_cur + 1024;
  39. if (setrlimit(RLIMIT_AS, &rl))
  40. RAISE_EXIT("set RLIMIT_AS failure");*/
  41. rl.rlim_cur = 256 * 1024 * 1024;
  42. rl.rlim_max = rl.rlim_cur + 1024;
  43. if (setrlimit(RLIMIT_STACK, &rl))
  44. RAISE_EXIT("set RLIMIT_STACK failure");
  45. p_realt.it_interval.tv_sec = runobj->time_limit / 1000 + 3;
  46. p_realt.it_interval.tv_usec = 0;
  47. p_realt.it_value = p_realt.it_interval;
  48. if (setitimer(ITIMER_REAL, &p_realt, (struct itimerval *) 0) == -1)
  49. RAISE_EXIT("set ITIMER_REAL failure");
  50. return 0;
  51. }