CAT-SOOP is a flexible, programmable learning management system based on the Python programming language. https://catsoop.mit.edu
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

97 lines
3.1 KiB

  1. # This file is part of CAT-SOOP
  2. # Copyright (c) 2011-2017 Adam Hartz <hartz@mit.edu>
  3. #
  4. # This program is free software: you can redistribute it and/or modify it under
  5. # the terms of the GNU Affero General Public License as published by the Free
  6. # Software Foundation, either version 3 of the License, or (at your option) any
  7. # later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but WITHOUT
  10. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  12. # details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. import sys
  17. import time
  18. import shutil
  19. import signal
  20. import hashlib
  21. import resource
  22. import threading
  23. import subprocess
  24. _resource_mapper = {
  25. 'CPUTIME': (resource.RLIMIT_CPU, lambda x: (x, x + 1)),
  26. 'MEMORY': (resource.RLIMIT_AS, lambda x: (x, x)),
  27. 'FILESIZE': (resource.RLIMIT_FSIZE, lambda x: (x, x)),
  28. }
  29. class PKiller(threading.Thread):
  30. def __init__(self, proc, timeout):
  31. threading.Thread.__init__(self)
  32. self.proc = proc
  33. self.timeout = timeout
  34. def run(self):
  35. end = time.time() + self.timeout
  36. while (time.time() < end):
  37. time.sleep(0.1)
  38. if self.proc.poll() is not None:
  39. return
  40. try:
  41. os.killpg(os.getpgid(self.proc.pid), signal.SIGKILL)
  42. except:
  43. pass
  44. def run_code(context, code, options):
  45. rlimits = [(resource.RLIMIT_NPROC, (0, 0))]
  46. for key, val in _resource_mapper.items():
  47. if key == 'MEMORY' and options[key] <= 0:
  48. continue
  49. rlimits.append((val[0], val[1](options[key])))
  50. def limiter():
  51. os.setsid()
  52. for i in rlimits:
  53. resource.setrlimit(*i)
  54. tmpdir = context.get('csq_sandbox_dir', '/tmp/sandbox')
  55. this_one = hashlib.sha512(('%s-%s' % (context.get('cs_username', 'None'),
  56. time.time())).encode()).hexdigest()
  57. tmpdir = os.path.join(tmpdir, this_one)
  58. os.makedirs(tmpdir, 0o777)
  59. for f in options['FILES']:
  60. typ = f[0].strip().lower()
  61. if typ == 'copy':
  62. shutil.copyfile(f[1], os.path.join(tmpdir, f[2]))
  63. elif typ == 'string':
  64. with open(os.path.join(tmpdir, f[1]), 'w') as fileobj:
  65. fileobj.write(f[2])
  66. fname = '%s.py' % this_one
  67. with open(os.path.join(tmpdir, fname), 'w') as fileobj:
  68. fileobj.write(code.replace('\r\n', '\n'))
  69. interp = context.get('csq_python_interpreter', '/usr/local/bin/python3')
  70. p = subprocess.Popen([interp, '-E', '-B', fname],
  71. cwd=tmpdir,
  72. preexec_fn=limiter,
  73. stdin=subprocess.PIPE,
  74. stdout=subprocess.PIPE,
  75. stderr=subprocess.PIPE)
  76. killer = PKiller(p, options['CLOCKTIME'])
  77. killer.start()
  78. out, err = p.communicate(options['STDIN'])
  79. out = out.decode()
  80. err = err.decode()
  81. shutil.rmtree(tmpdir, True)
  82. return fname, out, err