update python binding

This commit is contained in:
virusdefender 2016-01-12 18:23:56 +08:00
parent a24c071181
commit 7f3d100827
6 changed files with 34 additions and 209 deletions

View File

@ -1,17 +1,31 @@
#include <python2.7/Python.h>
#include "runner.h"
static PyObject *error;
static PyObject *judger_run(PyObject *self, PyObject *args, PyObject *kwargs){
struct config config;
struct result result;
config.path = config.in_file = config.out_file = NULL;
static char *kwargs_list[] = {"path", "in_file", "out_file", "max_cpu_time", "max_memory", NULL};
char *path = NULL, *in_file = NULL, *out_file = NULL;
int max_cpu_time, max_memory;
if(!PyArg_ParseTupleAndKeywords(args, kwargs, "sssii", kwargs_list, &path, &in_file, &out_file, &max_cpu_time, &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");
return NULL;
}
printf("%s %s %s %d %d", path, in_file, out_file, max_cpu_time, max_memory);
if(config.max_cpu_time <= 1){
PyErr_SetString(error, "Max cpu time can not less than 1 ms");
return NULL;
}
if(config.max_memory < 16 * 1024 * 1024){
PyErr_SetString(error, "Max memory can not be les than 16M");
return NULL;
}
printf("%s %s %s %d %d", config.path, config.in_file, config.out_file, config.max_cpu_time, config.max_memory);
run(&config, &result);
printf("%d %ld %d", result.cpu_time, result.memory, result.flag);
Py_INCREF(Py_None);
return Py_None;
}
@ -22,6 +36,7 @@ static PyMethodDef judger_methods[] = {
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC initjudger(){
PyObject *module = Py_InitModule3("judger", judger_methods, NULL);
error = PyErr_NewException("judger.error", NULL, NULL);

24
limit.c
View File

@ -1,24 +0,0 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/resource.h>
int main()
{
int *a = NULL;
int v = 150;
struct rlimit memory_limit;
memory_limit.rlim_cur = memory_limit.rlim_max = 90000000;
setrlimit(RLIMIT_AS, &memory_limit);
printf("111111111111111\n");
a = (int *)malloc(v);
printf("222222222222222\n");
if(a == NULL){
printf("error\n");
}
else {
memset(a, 0, v);
printf("success\n");
}
return 0;
}

171
main.c
View File

@ -1,171 +0,0 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/time.h>
#define __DEBUG__
#ifdef __DEBUG__
#define DEBUG(format, ...) printf("File: "__FILE__", Line: %05d: "format"\n", __LINE__, ##__VA_ARGS__)
#else
#define DEBUG(format,...)
#endif
#define RUN_SUCCEEDED 0
#define FORK_FAILED -1
#define WAIT4_FAILED -2
#define RUN_FAILED -3
#define SUCCESS 0
#define CPU_TIME_LIMIT_EXCEEDED 1
#define REAL_TIME_LIMIT_EXCEEDED 2
#define MEMORY_LIMIT_EXCEEDED 3
#define RUNTIME_ERROR 4
#define SYSTEM_ERROR 5
struct result {
int cpu_time;
long memory;
int real_time;
int signal;
int flag;
int err;
};
struct config {
int max_cpu_time;
int max_memory;
char path[200];
char in_file[200];
char out_file[200];
};
void set_timer(int sec, int ms, int is_cpu_time) {
struct itimerval time_val;
time_val.it_interval.tv_sec = time_val.it_interval.tv_usec = 0;
time_val.it_value.tv_sec = sec;
time_val.it_value.tv_usec = ms * 1000;
if (setitimer(is_cpu_time ? ITIMER_VIRTUAL : ITIMER_REAL, &time_val, NULL) == -1) {
DEBUG("settimer failed\n");
}
}
int run(struct config *config, struct result *result) {
int status;
struct rusage resource_usage;
struct timeval start, end;
struct rlimit memory_limit;
int signal;
char *argv[] = {config->path, NULL};
gettimeofday(&start, NULL);
memory_limit.rlim_cur = memory_limit.rlim_max = (rlim_t) (config->max_memory);
pid_t pid = fork();
if (pid < 0) {
DEBUG("fork failed\n");
result->flag = SYSTEM_ERROR;
result->err = FORK_FAILED;
return RUN_FAILED;
}
if (pid > 0) {
//parent process
DEBUG("I'm parent process\n");
if (wait4(pid, &status, 0, &resource_usage) == -1) {
DEBUG("wait4 failed\n");
result->flag = SYSTEM_ERROR;
result->err = WAIT4_FAILED;
return RUN_FAILED;
}
result->cpu_time = (int) (resource_usage.ru_utime.tv_sec * 1000 +
resource_usage.ru_utime.tv_usec / 1000 +
resource_usage.ru_stime.tv_sec * 1000 +
resource_usage.ru_stime.tv_usec / 1000);
result->memory = resource_usage.ru_maxrss * 1024;
result->signal = 0;
result->flag = result->err = SUCCESS;
if (WIFSIGNALED(status)) {
signal = WTERMSIG(status);
DEBUG("signal %d\n", signal);
result->signal = signal;
if (signal == SIGALRM) {
result->flag = REAL_TIME_LIMIT_EXCEEDED;
}
else if (signal == SIGVTALRM) {
result->flag = CPU_TIME_LIMIT_EXCEEDED;
}
else if (signal == SIGSEGV) {
if (result->memory > config->max_memory) {
result->flag = MEMORY_LIMIT_EXCEEDED;
}
else {
result->flag = RUNTIME_ERROR;
}
}
else {
result->flag = RUNTIME_ERROR;
}
}
gettimeofday(&end, NULL);
result->real_time = (int) (end.tv_sec * 1000 + end.tv_usec / 1000 - start.tv_sec * 1000 - start.tv_usec / 1000);
return RUN_SUCCEEDED;
}
else {
//child process
DEBUG("I'm child process\n");
if (setrlimit(RLIMIT_AS, &memory_limit) != 0)
DEBUG("setrlimit failed\n");
// cpu time
set_timer(config->max_cpu_time / 1000, config->max_cpu_time % 1000, 1);
// real time * 3
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->out_file, "w")), 1);
execve(config->path, argv, NULL);
DEBUG("execve failed\n");
return RUN_FAILED;
}
}
int main() {
struct config config;
struct result result;
int run_ret;
config.max_cpu_time = 4300;
config.max_memory = 150000000;
strcpy(config.path, "/Users/virusdefender/Desktop/judger/limit");
strcpy(config.in_file, "/Users/virusdefender/Desktop/judger/in");
strcpy(config.out_file, "/Users/virusdefender/Desktop/judger/out");
run_ret = run(&config, &result);
if (run_ret) {
DEBUG("run failed\n");
return RUN_FAILED;
}
DEBUG("cpu time %d\nreal time %d\nmemory %ld\nflag %d\nsignal %d\nerr %d",
result.cpu_time, result.real_time, result.memory, result.flag, result.signal, result.err);
return 0;
}

View File

@ -1,6 +1,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/time.h>
@ -39,7 +40,7 @@ int run(struct config *config, struct result *result) {
if (pid < 0) {
print("fork failed\n");
result->flag = SYSTEM_ERROR;
result->err = FORK_FAILED;
result->error = FORK_FAILED;
return RUN_FAILED;
}
@ -49,7 +50,7 @@ int run(struct config *config, struct result *result) {
if (wait4(pid, &status, 0, &resource_usage) == -1) {
print("wait4 failed\n");
result->flag = SYSTEM_ERROR;
result->err = WAIT4_FAILED;
result->error = WAIT4_FAILED;
return RUN_FAILED;
}
result->cpu_time = (int) (resource_usage.ru_utime.tv_sec * 1000 +
@ -68,7 +69,7 @@ int run(struct config *config, struct result *result) {
result->memory = result->memory * 1024;
#endif
result->signal = 0;
result->flag = result->err = SUCCESS;
result->flag = result->error = SUCCESS;
if (WIFSIGNALED(status)) {
signal = WTERMSIG(status);
@ -129,6 +130,10 @@ int main() {
config.max_cpu_time = 4300;
config.max_memory = 180000000;
config.path = (char *)malloc(200);
config.in_file = (char *)malloc(200);
config.out_file = (char *)malloc(200);
strcpy(config.path, "/Users/virusdefender/Desktop/judger/limit");
strcpy(config.in_file, "/Users/virusdefender/Desktop/judger/in");
strcpy(config.out_file, "/Users/virusdefender/Desktop/judger/out");
@ -140,8 +145,8 @@ int main() {
return RUN_FAILED;
}
print("cpu time %d\nreal time %d\nmemory %ld\nflag %d\nsignal %d\nerr %d",
result.cpu_time, result.real_time, result.memory, result.flag, result.signal, result.err);
print("cpu time %d\nreal time %d\nmemory %ld\nflag %d\nsignal %d\nerr %d\n",
result.cpu_time, result.real_time, result.memory, result.flag, result.signal, result.error);
return 0;
}

View File

@ -33,16 +33,16 @@ struct result {
int real_time;
int signal;
int flag;
int err;
int error;
};
struct config {
int max_cpu_time;
int max_memory;
char path[200];
char in_file[200];
char out_file[200];
char *path;
char *in_file;
char *out_file;
};

View File

@ -1,3 +1,3 @@
# coding=utf-8
from distutils.core import setup, Extension
setup(name = 'judger', version = '1.0', ext_modules = [Extension('judger', ['judger.c'])])
setup(name = 'judger', version = '1.0', ext_modules = [Extension('judger', ['judger.c', 'runner.c'])])