first commit
This commit is contained in:
7
scripts/bash-guard
Executable file
7
scripts/bash-guard
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
7
scripts/binary-guard
Executable file
7
scripts/binary-guard
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
34
scripts/compare-file.py
Executable file
34
scripts/compare-file.py
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import sys, string, hashlib, os
|
||||
from stat import *
|
||||
|
||||
def check_file(name, size, md5sum):
|
||||
global result
|
||||
|
||||
m = hashlib.md5()
|
||||
if os.path.isfile(name):
|
||||
s = os.stat(name)[ST_SIZE]
|
||||
if s == string.atoi(size):
|
||||
f = open(name, 'rb')
|
||||
m.update(f.read(string.atoi(size)))
|
||||
f.close()
|
||||
d = m.hexdigest()
|
||||
result.write("%s %d %s\n" % (name, s, d))
|
||||
return d == md5sum
|
||||
else:
|
||||
result.write("Size of %s is %d\n" % (name, s))
|
||||
else:
|
||||
result.write("%s not found\n" % name)
|
||||
return False
|
||||
|
||||
if __name__ == '__main__':
|
||||
result = open(sys.argv[3], 'w+')
|
||||
fstdout = open(sys.argv[2], 'r')
|
||||
for line in fstdout:
|
||||
name, size, md5sum = string.split(string.strip(line), ' ')
|
||||
if not check_file(name, size, md5sum):
|
||||
print 'WA'; break
|
||||
fstdout.close()
|
||||
result.close()
|
||||
print 'AC'
|
||||
10
scripts/compare-guard
Executable file
10
scripts/compare-guard
Executable file
@@ -0,0 +1,10 @@
|
||||
#!/bin/dash
|
||||
|
||||
scriptdir=`dirname $0`
|
||||
prog=$scriptdir/compare-wrapper-$1
|
||||
if [ -e "$prog" ] ; then
|
||||
shift
|
||||
exec "$prog" "$@"
|
||||
else
|
||||
echo 'JSE'
|
||||
fi
|
||||
4
scripts/compare-wrapper-bash-3
Executable file
4
scripts/compare-wrapper-bash-3
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
exec /bin/bash "$@"
|
||||
11
scripts/compare-wrapper-fpc-2.2
Executable file
11
scripts/compare-wrapper-fpc-2.2
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
cdir=`pwd`
|
||||
cd $vdir
|
||||
/usr/bin/fpc -O3 -vw0 -dONLINE_JUDGE -Sd -omain -MDelphi `basename $1` >&2
|
||||
cd $cdir
|
||||
shift
|
||||
exec "$vdir/main" "$@"
|
||||
9
scripts/compare-wrapper-g++-3.3
Executable file
9
scripts/compare-wrapper-g++-3.3
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
target=$vdir/main
|
||||
/usr/bin/g++ -Wall -O2 -o $target $1 -lm
|
||||
shift
|
||||
exec "$vdir/main" "$@"
|
||||
9
scripts/compare-wrapper-gcc-3.3
Executable file
9
scripts/compare-wrapper-gcc-3.3
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
target=$vdir/main
|
||||
/usr/bin/gcc -Wall -O2 -std=c99 -o $target $1 -lm >&2
|
||||
shift
|
||||
exec "$vdir/main" "$@"
|
||||
11
scripts/compare-wrapper-gmcs-2.0
Executable file
11
scripts/compare-wrapper-gmcs-2.0
Executable file
@@ -0,0 +1,11 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
cdir=`pwd`
|
||||
cd $vdir
|
||||
/usr/bin/gmcs "$1"
|
||||
cd $cdir
|
||||
shift
|
||||
exec "$vdir/main.exe" "$@"
|
||||
16
scripts/compare-wrapper-java-1.5
Executable file
16
scripts/compare-wrapper-java-1.5
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
cdir=`pwd`
|
||||
cd $vdir
|
||||
JAVAC=javac
|
||||
if [ -e '/usr/lib/jvm/java-6-sun/bin/javac ] ; then
|
||||
JAVAC=/usr/lib/jvm/java-6-sun/bin/javac
|
||||
else if [ -e '/usr/lib/jvm/java-6-openjdk/bin/javac ] ; then
|
||||
JAVAC=/usr/lib/jvm/java-6-openjdk/bin/javac
|
||||
fi
|
||||
$JAVAC -source 1.5 -cp . "$@"
|
||||
shift
|
||||
exec java -cp "$vdir" Main "$@"
|
||||
16
scripts/compare-wrapper-java-1.6
Executable file
16
scripts/compare-wrapper-java-1.6
Executable file
@@ -0,0 +1,16 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
|
||||
vdir=`dirname $1`
|
||||
cdir=`pwd`
|
||||
cd $vdir
|
||||
JAVAC=javac
|
||||
if [ -e '/usr/lib/jvm/java-6-sun/bin/javac ] ; then
|
||||
JAVAC=/usr/lib/jvm/java-6-sun/bin/javac
|
||||
else if [ -e '/usr/lib/jvm/java-6-openjdk/bin/javac ] ; then
|
||||
JAVAC=/usr/lib/jvm/java-6-openjdk/bin/javac
|
||||
fi
|
||||
$JAVAC -source 1.5 -cp . "$@"
|
||||
shift
|
||||
exec java -cp "$vdir" Main "$@"
|
||||
8
scripts/compare-wrapper-python-2.5
Executable file
8
scripts/compare-wrapper-python-2.5
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
PYTHON=python
|
||||
if [ -e /usr/bin/python2.6 ] ; then
|
||||
PYTHON=python
|
||||
fi
|
||||
exec $PYTHON "$@"
|
||||
8
scripts/compare-wrapper-python-2.5old
Executable file
8
scripts/compare-wrapper-python-2.5old
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
PYTHON=python3
|
||||
if [ -e /usr/bin/python2.6 ] ; then
|
||||
PYTHON=python3
|
||||
fi
|
||||
exec $PYTHON "$@"
|
||||
6
scripts/compile-guard
Executable file
6
scripts/compile-guard
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/dash
|
||||
|
||||
ulimit -t 30
|
||||
cd $1
|
||||
shift 1
|
||||
exec $@
|
||||
7
scripts/f
Executable file
7
scripts/f
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
echo `dirname $0`/run-guard.py $@>fff.txt
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
4
scripts/fpc
Executable file
4
scripts/fpc
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
exec /usr/bin/fpc -O3 -vw0 -dONLINE_JUDGE -Sd -omain -MDelphi "$@" 2>&1 | sed '1d' | sed '1d'
|
||||
4
scripts/g++-3.3
Executable file
4
scripts/g++-3.3
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
exec /usr/bin/g++ -Wall -O2 -std=c++98 -DOJ -DONLINE_JUDGE -o main "$@" -lm
|
||||
5
scripts/gcc-3.3-bc
Executable file
5
scripts/gcc-3.3-bc
Executable file
@@ -0,0 +1,5 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
|
||||
exec /usr/bin/gcc -Wall -O0 -g -std=c99 -DOJ -DONLINE_JUDGE -o main "$@" -lm
|
||||
4
scripts/gcc-3.3-nobc
Executable file
4
scripts/gcc-3.3-nobc
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/bin:/usr/bin
|
||||
exec /usr/bin/gcc -Wall -O2 -std=c99 -DOJ -DONLINE_JUDGE -o main "$@" -lm
|
||||
4
scripts/gmcs-2.0
Executable file
4
scripts/gmcs-2.0
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
exec /usr/bin/gmcs "$@"
|
||||
7
scripts/java-guard
Executable file
7
scripts/java-guard
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
4
scripts/javac-1.5
Executable file
4
scripts/javac-1.5
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
exec /usr/lib/jvm/java-6-sun/bin/javac -source 1.5 -cp . "$@"
|
||||
4
scripts/javac-1.6
Executable file
4
scripts/javac-1.6
Executable file
@@ -0,0 +1,4 @@
|
||||
#!/bin/dash
|
||||
|
||||
export PATH=$PATH:/usr/bin:/bin
|
||||
exec /usr/lib/jvm/java-6-sun/bin/javac -source 1.6 -cp . "$@"
|
||||
7
scripts/mono-guard
Executable file
7
scripts/mono-guard
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
7
scripts/python-guard
Executable file
7
scripts/python-guard
Executable file
@@ -0,0 +1,7 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
echo `dirname $0`/run-guard.py $@>fff.txt
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
6
scripts/python-guardd
Executable file
6
scripts/python-guardd
Executable file
@@ -0,0 +1,6 @@
|
||||
#!/bin/dash
|
||||
|
||||
# This is just a wrapper of run-guard program.
|
||||
# AppArmor use different profile for different programs, so we create
|
||||
# different scripts for different language.
|
||||
exec `dirname $0`/run-guard.py $@
|
||||
159
scripts/run-guard.py
Executable file
159
scripts/run-guard.py
Executable file
@@ -0,0 +1,159 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import os, sys, string, signal, resource, time, getopt, pickle
|
||||
|
||||
class RunGuard:
|
||||
|
||||
def __init__(self):
|
||||
self.timelimit = 1
|
||||
self.memlimit = 65536 * 1024
|
||||
self.timetime = 5
|
||||
self.args = None
|
||||
self.writeto = None
|
||||
self.nproc = 1
|
||||
self.ofile = 32
|
||||
self.ldpreload = None
|
||||
self.rundir = None
|
||||
|
||||
self.usepickle = False
|
||||
self.memrss = 0
|
||||
self.memdata = 0
|
||||
self.memstack = 0
|
||||
self.timeused = 0
|
||||
self.exitcode = 0
|
||||
self.sig = 0
|
||||
|
||||
def run(self):
|
||||
self.parse_opts()
|
||||
|
||||
self.childpid = os.fork()
|
||||
if self.childpid == 0:
|
||||
self.execute()
|
||||
else:
|
||||
self.monitor()
|
||||
|
||||
self.write_result()
|
||||
|
||||
def parse_opts(self):
|
||||
try:
|
||||
pos = sys.argv.index('-x')
|
||||
optlist, self.args = getopt.gnu_getopt(sys.argv[:pos], 'e:t:m:d:o:T:p')
|
||||
self.args = sys.argv[pos:]
|
||||
except ValueError:
|
||||
optlist, self.args = getopt.gnu_getopt(sys.argv, 'e:t:m:d:o:T:p')
|
||||
for o, v in optlist:
|
||||
if o == '-e':
|
||||
self.nproc += string.atoi(v)
|
||||
if o == '-t':
|
||||
self.timelimit = string.atoi(v)
|
||||
if o == '-m':
|
||||
self.memlimit = string.atoi(v) * 1024
|
||||
if o == '-d':
|
||||
self.rundir = v
|
||||
if o == '-o':
|
||||
self.writeto = v
|
||||
if o == '-T':
|
||||
self.timetime = string.atoi(v)
|
||||
if o == '-p':
|
||||
self.usepickle = True
|
||||
|
||||
v = os.getenv('GUARD_RLIMIT_OFILE')
|
||||
if v: self.ofile = string.atoi(v)
|
||||
self.ldpreload = os.getenv('GUARD_LD_PRELOAD')
|
||||
|
||||
def execute(self):
|
||||
if self.rundir != None and os.path.isdir(self.rundir):
|
||||
os.chdir(self.rundir)
|
||||
|
||||
if self.nproc:
|
||||
resource.setrlimit(resource.RLIMIT_NPROC, (self.nproc, self.nproc))
|
||||
if self.ofile:
|
||||
resource.setrlimit(resource.RLIMIT_OFILE, (self.ofile, self.ofile))
|
||||
if self.ldpreload:
|
||||
os.putenv('LD_PRELOAD', self.ldpreload)
|
||||
|
||||
resource.setrlimit(resource.RLIMIT_CPU, (self.timelimit, self.timelimit))
|
||||
resource.setrlimit(resource.RLIMIT_AS, (self.memlimit, self.memlimit))
|
||||
os.execv(self.args[1], self.args[1:])
|
||||
sys.exit(127) # exit with JGE
|
||||
|
||||
def monitor(self):
|
||||
pid = 0
|
||||
remaintime = self.timelimit * self.timetime
|
||||
while remaintime > 0:
|
||||
pid, status, ru = os.wait4(self.childpid, os.WNOHANG)
|
||||
self._get_memused()
|
||||
if pid > 0: break
|
||||
time.sleep(0.05)
|
||||
remaintime -= 0.05
|
||||
|
||||
while pid == 0:
|
||||
try:
|
||||
os.kill(self.childpid, signal.SIGKILL)
|
||||
except OSError, e:
|
||||
pass
|
||||
pid, status, ru = os.wait4(self.childpid, os.WNOHANG)
|
||||
time.sleep(0.1)
|
||||
|
||||
if os.WIFEXITED(status):
|
||||
self.exitcode = os.WEXITSTATUS(status)
|
||||
|
||||
if os.WIFSIGNALED(status):
|
||||
self.sig = os.WTERMSIG(status)
|
||||
|
||||
self.timeused = ru[0] + ru[1]
|
||||
|
||||
def _get_memused(self):
|
||||
procdir = '/proc/%d' % self.childpid
|
||||
if os.path.isdir(procdir):
|
||||
cmdline = file(procdir + '/cmdline', 'r').readlines()
|
||||
|
||||
# do not get memory usage of this script after just fork
|
||||
if len(cmdline) > 0 and \
|
||||
string.strip(cmdline[0], '\0') != \
|
||||
string.join(self.args[1:], '\0'):
|
||||
return
|
||||
|
||||
procstatus = file(procdir + '/status', 'r')
|
||||
rss = 0; data = 0; stack = 0
|
||||
for line in procstatus:
|
||||
n = line[0:6]
|
||||
if n == 'VmRSS:':
|
||||
rss = string.atoi(line[7:-3])
|
||||
if n == 'VmData':
|
||||
data = string.atoi(line[8:-3])
|
||||
if n == 'VmStk:':
|
||||
stack = string.atoi(line[7:-3])
|
||||
self.memrss = max(self.memrss, rss)
|
||||
if self.memdata + self.memstack < data + stack:
|
||||
self.memdata = data
|
||||
self.memstack = stack
|
||||
return
|
||||
|
||||
def write_result(self):
|
||||
if self.writeto == None:
|
||||
f = sys.stdout
|
||||
else:
|
||||
f = file(self.writeto, 'w')
|
||||
|
||||
if self.usepickle:
|
||||
obj = { 'exitcode' : self.exitcode,
|
||||
'sig' : self.sig,
|
||||
'timeused' : self.timeused,
|
||||
'memrss' : self.memrss,
|
||||
'memdata' : self.memdata,
|
||||
'memstack' : self.memstack }
|
||||
pickle.dump(obj, f)
|
||||
else:
|
||||
print >>f, "exitcode: %d" % self.exitcode
|
||||
print >>f, "sig: %d" % self.sig
|
||||
print >>f, "time: %.3f" % self.timeused
|
||||
print >>f, "rss: %d" % self.memrss
|
||||
print >>f, "data: %d" % self.memdata
|
||||
print >>f, "stack: %d" % self.memstack
|
||||
|
||||
if self.writeto != None: f.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
os.umask(0002)
|
||||
RunGuard().run()
|
||||
Reference in New Issue
Block a user