This commit is contained in:
virusdefender 2016-01-12 19:04:16 +08:00
parent 7f3d100827
commit 72e7b6b5e8
2 changed files with 22 additions and 12 deletions

View File

@ -5,39 +5,49 @@
static PyObject *error; static PyObject *error;
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs){ static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
struct config config; struct config config;
struct result result; struct result result;
config.path = config.in_file = config.out_file = NULL; config.path = config.in_file = config.out_file = NULL;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", NULL}; static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", NULL};
if(!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii", kwargs_list, &(config.path), &(config.in_file), &(config.out_file), &(config.max_cpu_time), &(config.max_memory))){ if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii", kwargs_list, &(config.path), &(config.in_file),
&(config.out_file), &(config.max_cpu_time), &(config.max_memory))) {
PyErr_SetString(error, "Invalid args and kwargs"); PyErr_SetString(error, "Invalid args and kwargs");
return NULL; return NULL;
} }
if(config.max_cpu_time <= 1){ if (config.max_cpu_time <= 1) {
PyErr_SetString(error, "Max cpu time can not less than 1 ms"); PyErr_SetString(error, "Max cpu time can not less than 1 ms");
return NULL; return NULL;
} }
if(config.max_memory < 16 * 1024 * 1024){ if (config.max_memory < 16 * 1024 * 1024) {
PyErr_SetString(error, "Max memory can not be les than 16M"); PyErr_SetString(error, "Max memory can not be less than 16M");
return NULL;
}
if (access(config.path, F_OK) == -1) {
PyErr_SetString(error, "Exec file does not exist");
return NULL;
}
if (access(config.in_file, F_OK) == -1) {
PyErr_SetString(error, "Input file does not exist");
return NULL; return NULL;
} }
printf("%s %s %s %d %d", config.path, config.in_file, config.out_file, config.max_cpu_time, config.max_memory); printf("%s %s %s %d %d", config.path, config.in_file, config.out_file, config.max_cpu_time, config.max_memory);
run(&config, &result); run(&config, &result);
printf("%d %ld %d", result.cpu_time, result.memory, result.flag); return Py_BuildValue("{s: i, s:l, s:i, s:i, s:i, s:i}",
Py_INCREF(Py_None); "cpu_time", result.cpu_time, "memory", result.memory, "real_time", result.real_time, "signal",
return Py_None; result.signal, "flag", result.flag, "error", result.error);
} }
static PyMethodDef judger_methods[] = { static PyMethodDef judger_methods[] = {
{"run", (PyCFunction)judger_run, METH_VARARGS | METH_KEYWORDS, NULL}, {"run", (PyCFunction) judger_run, METH_VARARGS | METH_KEYWORDS, NULL},
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
PyMODINIT_FUNC initjudger(){ PyMODINIT_FUNC initjudger() {
PyObject *module = Py_InitModule3("judger", judger_methods, NULL); PyObject *module = Py_InitModule3("judger", judger_methods, NULL);
error = PyErr_NewException("judger.error", NULL, NULL); error = PyErr_NewException("judger.error", NULL, NULL);
Py_INCREF(error); Py_INCREF(error);

View File

@ -112,8 +112,8 @@ int run(struct config *config, struct result *result) {
// real time * 3 // real time * 3
set_timer(config->max_cpu_time / 1000 * 3, (config->max_cpu_time % 1000) * 3 % 1000, 0); set_timer(config->max_cpu_time / 1000 * 3, (config->max_cpu_time % 1000) * 3 % 1000, 0);
//dup2(fileno(fopen(config->in_file, "r")), 0); dup2(fileno(fopen(config->in_file, "r")), 0);
//dup2(fileno(fopen(config->out_file, "w")), 1); dup2(fileno(fopen(config->out_file, "w")), 1);
execve(config->path, argv, NULL); execve(config->path, argv, NULL);
print("execve failed\n"); print("execve failed\n");