代码拉取完成,页面将自动刷新
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <python3.5/Python.h>
#include "runner.h"
#define RaiseValueError(msg) {PyErr_SetString(PyExc_ValueError, msg); return NULL;}
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs)
{
struct config config;
struct result result = {0, 0, 0, 0, 0, 0};
PyObject *args_list = NULL, *env_list = NULL, *use_sandbox = NULL, *use_nobody = NULL,
*next = NULL, *args_iter = NULL, *env_iter = NULL, *log_path = NULL;
int count = 0;
struct passwd *passwd;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time",
"max_memory", "args", "env", "use_sandbox", "use_nobody", "log_path", NULL};
config.path = config.in_file = config.out_file = NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssil|OOOOO", kwargs_list, &(config.path), &(config.in_file),
&(config.out_file), &(config.max_cpu_time), &(config.max_memory),
&args_list, &env_list, &use_sandbox, &use_nobody, &log_path)) {
RaiseValueError("Invalid args and kwargs");
}
if (config.max_cpu_time < 1 && config.max_cpu_time != CPU_TIME_UNLIMITED) {
RaiseValueError("max_cpu_time must > 1 ms or unlimited");
}
config.max_real_time = config.max_cpu_time * 3;
if (config.max_memory < 16 * 1024 * 1024 && config.max_memory != MEMORY_UNLIMITED) {
RaiseValueError("max_memory must > 16M or unlimited");
}
if (access(config.path, F_OK) == -1) {
RaiseValueError("Exec file does not exist");
}
if (access(config.in_file, F_OK) == -1) {
RaiseValueError("in_file does not exist");
}
config.err_file = config.out_file;
config.args[count++] = config.path;
if (args_list != NULL) {
if (!PyList_Check(args_list)) {
RaiseValueError("args must be a list");
}
args_iter = PyObject_GetIter(args_list);
while (1) {
next = PyIter_Next((args_iter));
if (!next) {
break;
}
if (!PyBytes_Check(next) && !PyUnicode_Check(next)) {
RaiseValueError("arg in args must be a string");
}
config.args[count] = PyUnicode_AsUTF8(next);
count++;
if(count > 95) {
RaiseValueError("Number of args must < 95");
}
}
}
config.args[count] = NULL;
count = 0;
if (env_list != NULL) {
if (!PyList_Check(env_list)) {
RaiseValueError("env must be a list");
}
env_iter = PyObject_GetIter(env_list);
while (1) {
next = PyIter_Next(env_iter);
if (!next) {
break;
}
if (!PyBytes_Check(next) && !PyUnicode_Check(next)) {
RaiseValueError("env item must be a string");
}
config.env[count] = PyUnicode_AsUTF8(next);
count++;
if(count > 95) {
RaiseValueError("Number of env must < 95");
}
}
}
config.env[count] = NULL;
if (use_sandbox != NULL) {
if (!PyBool_Check(use_sandbox)) {
RaiseValueError("use_sandbox must ba a bool");
}
config.use_sandbox = PyObject_IsTrue(use_sandbox);
}
else {
config.use_sandbox = 1;
}
if (use_nobody != NULL) {
if (!PyBool_Check(use_nobody)) {
RaiseValueError("use_nobody must be a bool");
}
if (PyObject_IsTrue(use_nobody)) {
passwd = getpwnam("nobody");
if(passwd == NULL) {
RaiseValueError("get nobody user info failed");
}
config.gid = passwd->pw_gid;
config.uid = passwd->pw_uid;
}
else {
config.uid = config.gid = -1;
}
}
else {
config.uid = config.gid = -1;
}
if (log_path != NULL) {
if (!PyUnicode_Check(log_path)) {
RaiseValueError("log path must be a string");
}
config.log_path = PyUnicode_AsUTF8(log_path);
}
else {
config.log_path = "judger.log";
}
printf("log_path %s\n",config.log_path);
if((config.uid != -1 || config.gid != -1) && getuid() != 0) {
RaiseValueError("Root user is required when use_nobody=True");
}
run(&config, &result);
return Py_BuildValue("{s:l, s:i, s:i, s:i, s:i, s:i}",
"cpu_time", result.cpu_time, "memory", result.memory, "real_time", result.real_time, "signal",
result.signal, "flag", result.flag, "exit_status", result.exit_status);
}
static PyMethodDef judger_methods[]={
{"run", (PyCFunction)judger_run, METH_KEYWORDS | METH_VARARGS, "I guess here is description."},
{NULL,NULL,0,NULL}
};
static struct PyModuleDef judger_module={
PyModuleDef_HEAD_INIT,
"judger",
NULL,
-1,
judger_methods
};
PyMODINIT_FUNC PyInit_judger(void)
{
PyObject *obj = PyModule_Create(&judger_module);
PyModule_AddIntConstant(obj, "CPU_TIME_UNLIMITED", CPU_TIME_UNLIMITED);
PyModule_AddIntConstant(obj, "MEMORY_UNLIMITED", MEMORY_UNLIMITED);
return obj;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。