-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsandbox.py
36 lines (29 loc) · 1.39 KB
/
sandbox.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import subprocess
from OJ.settings import SUDO_PASSWORD, SUDO_USER
class Sandbox:
def __init__(self):
self.create_sandbox()
def create_sandbox(self):
count = id(self)
cmd = 'echo ' + SUDO_PASSWORD + \
' | sudo -S cgcreate -g cpu,memory,blkio,devices,freezer:/sandbox_' + \
str(count) + ';\n' + \
'sudo cgset -r memory.limit_in_bytes=256M sandbox_' + str(count) + ';\n' \
'sudo cgset -r memory.soft_limit_in_bytes=256M sandbox_' + str(count) + ';\n' \
'sudo cgset -r memory.kmem.limit_in_bytes=256M sandbox_' + str(count) + ';\n' \
'sudo cgset -r memory.memsw.limit_in_bytes=256M sandbox_' + str(count) + ';\n'
subprocess.Popen(cmd, shell=True)
print("CREATE : "+cmd)
return
def run_sandbox(self):
cmd = 'echo ' + SUDO_PASSWORD + ' | sudo -S unshare --mount;sudo unshare --mount;' \
'sudo cgexec -g cpu,memory,blkio,devices,freezer:sandbox_' + \
str(id(self)) + ' numactl --physcpubind=+1 '
return cmd
def delete_sandbox(self):
cmd = 'echo ' + SUDO_PASSWORD + ' | ' \
'sudo -S cgdelete -g cpu,memory,blkio,devices,freezer:/sandbox_' + \
str(id(self)) + ';rm -Rf /etc/netns/sandbox_' + str(id(self))
print("DELETE : "+cmd)
subprocess.Popen(cmd, shell=True)
return