add args and env list

This commit is contained in:
virusdefender 2016-01-12 23:51:11 +08:00
parent 71b64d2aff
commit fb96e31733
4 changed files with 54 additions and 6 deletions

View File

@ -8,13 +8,54 @@ static PyObject *error;
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs) {
struct config config;
struct result result = {0, 0, 0, 0, 0};
PyObject *args_list, *env_list;
int count = 0;
config.path = config.in_file = config.out_file = 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))) {
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", "args", "env", NULL};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "sssiiOO", kwargs_list, &(config.path), &(config.in_file),
&(config.out_file), &(config.max_cpu_time), &(config.max_memory),
&args_list, &env_list)) {
PyErr_SetString(error, "Invalid args and kwargs");
return NULL;
}
PyObject *args_iter = PyObject_GetIter(args_list);
while (1) {
PyObject *next = PyIter_Next((args_iter));
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "arg must be string");
return NULL;
}
config.args[count] = PyString_AsString(next);
count++;
}
config.args[count] = NULL;
count = 0;
PyObject *env_iter = PyObject_GetIter(env_list);
while (1) {
PyObject *next = PyIter_Next(env_iter);
if (!next) {
break;
}
if (!PyString_Check(next)) {
PyErr_SetString(error, "env must be string");
return NULL;
}
config.env[count] = PyString_AsString(next);
count++;
}
config.env[count] = NULL;
if (!PyList_Check(args_list) || !PyList_Check(env_list)) {
PyErr_SetString(error, "args and env must be a list");
return NULL;
}
if (config.max_cpu_time <= 1) {
PyErr_SetString(error, "Max cpu time can not less than 1 ms");
return NULL;

View File

@ -23,7 +23,7 @@ int run(struct config *config, struct result *result) {
struct timeval start, end;
struct rlimit memory_limit;
int signal;
char *argv[] = {config->path, NULL};
// char *argv[] = {config->path, NULL};
#ifdef __APPLE__
print("Warning: setrlimit will not work on OSX");
@ -113,7 +113,7 @@ int run(struct config *config, struct result *result) {
dup2(fileno(fopen(config->in_file, "r")), 0);
dup2(fileno(fopen(config->out_file, "w")), 1);
execve(config->path, argv, NULL);
execve(config->path, config->args, config->env);
print("execve failed\n");
return RUN_FAILED;
}

View File

@ -43,6 +43,8 @@ struct config {
char *path;
char *in_file;
char *out_file;
char *args[100];
char *env[100];
};

7
test.c
View File

@ -1,7 +1,12 @@
int main() {
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int *a = NULL;
// 150M
int v = 150000000;
printf("%s\n", getenv("LD_PRELOAD"));
for (int j = 0; j < argc; j++)
printf("argv[%d]: %s\n", j, argv[j]);
a = (int *) malloc(v);
if (a == NULL) {
printf("error\n");