|
| 1 | +import json |
| 2 | +import subprocess |
| 3 | + |
| 4 | +UNLIMITED = -1 |
| 5 | +VERSION = 0x020100 |
| 6 | + |
| 7 | +RESULT_SUCCESS = 0 |
| 8 | +RESULT_WRONG_ANSWER = -1 |
| 9 | +RESULT_CPU_TIME_LIMIT_EXCEEDED = 1 |
| 10 | +RESULT_REAL_TIME_LIMIT_EXCEEDED = 2 |
| 11 | +RESULT_MEMORY_LIMIT_EXCEEDED = 3 |
| 12 | +RESULT_RUNTIME_ERROR = 4 |
| 13 | +RESULT_SYSTEM_ERROR = 5 |
| 14 | + |
| 15 | +ERROR_INVALID_CONFIG = -1 |
| 16 | +ERROR_FORK_FAILED = -2 |
| 17 | +ERROR_PTHREAD_FAILED = -3 |
| 18 | +ERROR_WAIT_FAILED = -4 |
| 19 | +ERROR_ROOT_REQUIRED = -5 |
| 20 | +ERROR_LOAD_SECCOMP_FAILED = -6 |
| 21 | +ERROR_SETRLIMIT_FAILED = -7 |
| 22 | +ERROR_DUP2_FAILED = -8 |
| 23 | +ERROR_SETUID_FAILED = -9 |
| 24 | +ERROR_EXECVE_FAILED = -10 |
| 25 | +ERROR_SPJ_ERROR = -11 |
| 26 | + |
| 27 | + |
| 28 | +def run(max_cpu_time, |
| 29 | + max_real_time, |
| 30 | + max_memory, |
| 31 | + max_stack, |
| 32 | + max_output_size, |
| 33 | + max_process_number, |
| 34 | + exe_path, |
| 35 | + input_path, |
| 36 | + output_path, |
| 37 | + error_path, |
| 38 | + args, |
| 39 | + env, |
| 40 | + log_path, |
| 41 | + seccomp_rule_name, |
| 42 | + uid, |
| 43 | + gid): |
| 44 | + str_list_vars = ["args", "env"] |
| 45 | + int_vars = ["max_cpu_time", "max_real_time", |
| 46 | + "max_memory", "max_stack", "max_output_size", |
| 47 | + "max_process_number", "uid", "gid"] |
| 48 | + str_vars = ["exe_path", "input_path", "output_path", "error_path", "log_path"] |
| 49 | + |
| 50 | + proc_args = ["/usr/lib/judger/libjudger.so"] |
| 51 | + |
| 52 | + for var in str_list_vars: |
| 53 | + value = vars()[var] |
| 54 | + if not isinstance(value, list): |
| 55 | + raise ValueError("{} must be a list".format(var)) |
| 56 | + for item in value: |
| 57 | + if not isinstance(item, str): |
| 58 | + raise ValueError("{} item must be a string".format(var)) |
| 59 | + proc_args.append("--{}={}".format(var, item)) |
| 60 | + |
| 61 | + for var in int_vars: |
| 62 | + value = vars()[var] |
| 63 | + if not isinstance(value, int): |
| 64 | + raise ValueError("{} must be a int".format(var)) |
| 65 | + if value != UNLIMITED: |
| 66 | + proc_args.append("--{}={}".format(var, value)) |
| 67 | + |
| 68 | + for var in str_vars: |
| 69 | + value = vars()[var] |
| 70 | + if not isinstance(value, str): |
| 71 | + raise ValueError("{} must be a string".format(var)) |
| 72 | + proc_args.append("--{}={}".format(var, value)) |
| 73 | + |
| 74 | + if not isinstance(seccomp_rule_name, str) and seccomp_rule_name is not None: |
| 75 | + raise ValueError("seccomp_rule_name must be a string or None") |
| 76 | + if seccomp_rule_name: |
| 77 | + proc_args.append("--seccomp_rule={}".format(seccomp_rule_name)) |
| 78 | + |
| 79 | + proc = subprocess.Popen(proc_args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 80 | + out, err = proc.communicate() |
| 81 | + if err: |
| 82 | + raise ValueError("Error occurred while calling judger: {}".format(err)) |
| 83 | + return json.loads(out.decode("utf-8")) |
0 commit comments