增加可以更改log文件的功能,默认log文件是judger.log

This commit is contained in:
spxcds 2016-04-26 09:50:28 +08:00
parent 9958ef7c14
commit a29925b48d
6 changed files with 21 additions and 8 deletions

1
.gitignore vendored
View File

@ -8,3 +8,4 @@ main
demo demo
.DS_Store .DS_Store
judger.log judger.log
*.swp

View File

@ -13,7 +13,8 @@ def _compile():
max_memory=judger.MEMORY_UNLIMITED, max_memory=judger.MEMORY_UNLIMITED,
args=[os.path.join(base_path, "demo.c"), "-o", os.path.join(base_path, "demo")], args=[os.path.join(base_path, "demo.c"), "-o", os.path.join(base_path, "demo")],
env=["PATH=" + os.environ["PATH"]], env=["PATH=" + os.environ["PATH"]],
use_sandbox=False, use_sandbox=False,
log_path="test.log",
use_nobody=False) use_nobody=False)

View File

@ -10,15 +10,15 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
struct config config; struct config config;
struct result result = {0, 0, 0, 0, 0, 0}; struct result result = {0, 0, 0, 0, 0, 0};
PyObject *args_list = NULL, *env_list = NULL, *use_sandbox = NULL, *use_nobody = NULL, PyObject *args_list = NULL, *env_list = NULL, *use_sandbox = NULL, *use_nobody = NULL,
*next = NULL, *args_iter = NULL, *env_iter = NULL; *next = NULL, *args_iter = NULL, *env_iter = NULL, *log_path = NULL;
int count = 0; int count = 0;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time",
"max_memory", "args", "env", "use_sandbox", "use_nobody", NULL}; "max_memory", "args", "env", "use_sandbox", "use_nobody", "log_path", NULL};
config.path = config.in_file = config.out_file = NULL; config.path = config.in_file = config.out_file = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssil|OOOO", kwargs_list, &(config.path), &(config.in_file), if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssil|OOOOO", kwargs_list, &(config.path), &(config.in_file),
&(config.out_file), &(config.max_cpu_time), &(config.max_memory), &(config.out_file), &(config.max_cpu_time), &(config.max_memory),
&args_list, &env_list, &use_sandbox, &use_nobody)) { &args_list, &env_list, &use_sandbox, &use_nobody, &log_path)) {
RaiseValueError("Invalid args and kwargs"); RaiseValueError("Invalid args and kwargs");
} }
if (config.max_cpu_time < 1 && config.max_cpu_time != CPU_TIME_UNLIMITED) { if (config.max_cpu_time < 1 && config.max_cpu_time != CPU_TIME_UNLIMITED) {
@ -100,6 +100,16 @@ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
config.use_nobody = 1; config.use_nobody = 1;
} }
if (log_path != NULL) {
if (!PyString_Check(log_path)) {
RaiseValueError("log path must be a string");
}
config.log_path = PyString_AsString(log_path);
}
else {
config.log_path = "judger.log";
}
if(config.use_nobody && getuid() != 0) { if(config.use_nobody && getuid() != 0) {
RaiseValueError("Root user is required when use_nobody=True"); RaiseValueError("Root user is required when use_nobody=True");
} }

View File

@ -149,4 +149,4 @@ void log_add_info(const char *info)
snprintf(log_extra_info + len, log_buffer_size - len, "\n [%s]", info); snprintf(log_extra_info + len, log_buffer_size - len, "\n [%s]", info);
} }
#endif #endif

View File

@ -46,8 +46,8 @@ void run(struct config *config, struct result *result) {
int syscalls_whitelist_length = sizeof(syscalls_whitelist) / sizeof(int); int syscalls_whitelist_length = sizeof(syscalls_whitelist) / sizeof(int);
scmp_filter_ctx ctx = NULL; scmp_filter_ctx ctx = NULL;
log_open("judger.log"); log_open(config->log_path);
#ifdef __APPLE__ #ifdef __APPLE__
#warning "setrlimit with RLIMIT_AS to limit memory usage will not work on OSX" #warning "setrlimit with RLIMIT_AS to limit memory usage will not work on OSX"

View File

@ -49,6 +49,7 @@ struct config {
char *env[100]; char *env[100];
int use_sandbox; int use_sandbox;
int use_nobody; int use_nobody;
char *log_path;
}; };