Commit 94dc7cea by Ned Batchelder

Add a configurable limit for real time.

parent 1ff63843
......@@ -70,6 +70,8 @@ if hasattr(sys, 'real_prefix'):
LIMITS = {
# CPU seconds, defaulting to 1.
"CPU": 1,
# Real time, defaulting to 1 second.
"REALTIME": 1,
# Total process virutal memory, in bytes, defaulting to 30 Mb.
"VMEM": 30000000,
}
......@@ -88,6 +90,9 @@ def set_limit(limit_name, value):
* `"CPU"`: the maximum number of CPU seconds the jailed code can use.
The value is an integer, defaulting to 1.
* `"REALTIME"`: the maximum number of seconds the jailed code can run,
in real time. The default is 1 second.
* `"VMEM"`: the total virtual memory available to the jailed code, in
bytes. The default is 30 Mb.
......@@ -168,8 +173,10 @@ def jail_code(command, code=None, files=None, argv=None, stdin=None):
)
# Start the time killer thread.
killer = ProcessKillerThread(subproc, limit=1.0)
killer.start()
realtime = LIMITS["REALTIME"]
if realtime:
killer = ProcessKillerThread(subproc, limit=realtime)
killer.start()
result = JailResult()
result.stdout, result.stderr = subproc.communicate(stdin)
......
......@@ -143,14 +143,25 @@ class TestLimits(JailCodeHelpers, unittest.TestCase):
self.assertNotEqual(res.status, 0)
def test_cant_use_too_much_time(self):
res = jailpy(code="""\
import time
time.sleep(5)
print 'Done!'
""")
# Default time limit is 1 second. Sleep for 1.5 seconds.
res = jailpy(code="import time; time.sleep(1.5); print 'Done!'")
self.assertNotEqual(res.status, 0)
self.assertEqual(res.stdout, "")
def test_changing_realtime_limit(self):
# Change time limit to 2 seconds, sleeping for 1.5 will be fine.
set_limit('REALTIME', 2)
res = jailpy(code="import time; time.sleep(1.5); print 'Done!'")
self.assertResultOk(res)
self.assertEqual(res.stdout, "Done!\n")
def test_disabling_realtime_limit(self):
# Disable the time limit, sleeping for 1.5 will be fine.
set_limit('REALTIME', 0)
res = jailpy(code="import time; time.sleep(1.5); print 'Done!'")
self.assertResultOk(res)
self.assertEqual(res.stdout, "Done!\n")
def test_cant_write_files(self):
res = jailpy(code="""\
print "Trying"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment