Skip to content

Commit

Permalink
Resolving merge conflicts with sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
shashankkumar committed Nov 23, 2011
2 parents d21307e + 87f0eb1 commit 8f50fa5
Show file tree
Hide file tree
Showing 7 changed files with 295 additions and 38 deletions.
2 changes: 1 addition & 1 deletion CurlWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ void CurlWrapper::SendResultsToWebpage(const char* fileid, const char* status, c
if(CURLE_OK!=res){
sprintf(logs, "Could not send results. Curl Error code: %d\n", res);
Logs::WriteLine(logs);
}
}
else{
sprintf(logs, "Results sent succesfully.\n");
Logs::WriteLine(logs);
Expand Down
250 changes: 250 additions & 0 deletions Execution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,250 @@
#include "includes.h"
#include "resources.h"
#include "config.h"

pid_t cpid;
const int MicroSecSleepInterval = 71;
void ToPipe(const char* str){
printf("%s\n", str);
return;
}

void SignalHandler(int signum){
if(cpid!=0){
killpg(cpid,SIGKILL);
}
}

/* high resolution (microsecond) sleep */
void MicroSecSleep (int ms){
struct timeval tv;
int w;
do{
tv.tv_sec = ms / 1000;
tv.tv_usec = (ms % 1000) * 1000;
w = select (0, NULL, NULL, NULL, &tv);
/* The value of the timeout is undefined after the select returns */
} while ((w < 0) && (errno == EINTR));
if (w < 0)
ToPipe("IE ERROR Failed while calling select in microsleep");
}

int MemoryUsage(pid_t cpid){
char a[10000], *p, *q;
int data, stack;
int rss; /* ZP */
int n, v, fd;

p = a;
sprintf (p, "/proc/%d/status", cpid);
fd = open (p, O_RDONLY);
if (fd < 0)
ToPipe("IE ERROR Failed to open /proc/pid/status") ;
do{
n = read (fd, p, 10000);
} while ((n < 0) && (errno == EINTR));
if (n < 0)
ToPipe("IE ERROR Failed to read /proc/pid/status") ;
do{
v = close (fd);
} while ((v < 0) && (errno == EINTR));
if (v < 0)
ToPipe("IE ERROR Failed to close file descriptor for /proc/pid/status") ;

rss = data = stack = 0;
q = strstr (p, "VmData:");
if (q != NULL)
{
sscanf (q, "%*s %d", &data);
q = strstr (q, "VmStk:");
if (q != NULL)
sscanf (q, "%*s %d\n", &stack);

q = strstr (q, "VmRSS:");
if (q != NULL)
sscanf (q, "%*s %d\n", &rss);
else
rss = data + stack;
}

return min(data + stack, rss);
/* return (data + stack); */
}

int main(int args, char *argv[]){

if(args<4){
ToPipe("IE ERROR Not enough arguments.");
return 1;
}

int TimeLimit, MemoryLimit, TestCaseFileId, FileId;
char InputFile[10];
struct timeval start,finish;
struct sigaction SignalAction;
pid_t w;
int status;
long long t_sec, t_usec;

FileId = atoi(argv[1]), strcpy(InputFile, argv[2]);
TestCaseFileId = atoi(argv[3]), TimeLimit = atoi(argv[4]), MemoryLimit = atoi(argv[5]);

SignalAction.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT;
SignalAction.sa_handler = SignalHandler;

if(sigaction(SIGALRM, &SignalAction, NULL)==-1){
ToPipe("IE ERROR Could not register handler for SIGALRM");
return 1;
}
if(sigaction(SIGXCPU, &SignalAction, NULL)==-1){
ToPipe("IE ERROR Could not register handler fo SIGXCPU");
return 1;
}

gettimeofday(&start,NULL);
cpid = fork();
if (cpid == -1) { perror("fork"); exit(EXIT_FAILURE); }

if (cpid == 0) { /* Code executed by child */

pid_t ChildProcessId = getpid();
passwd* UserDetails = getpwnam("nobody");
char dir[10];
sprintf(dir, "%s%d/", FILEPATH, FileId);
//ToPipe(dir);
if( chdir(dir) == -1){
printf("%d", errno);
ToPipe("IE ERROR Cannot change directory to that of file");
return 1;
}

//ToPipe(get_current_dir_name());
/*if(chroot(get_current_dir_name())!=0){
ToPipe("IE ERROR Cannot jail the program");
return 1;
}*/

if(( setpgid( ChildProcessId, ChildProcessId) ) == -1 ){
ToPipe("IE ERROR process group could not be created\n");
return 1;
}

if(UserDetails!=NULL){
gid_t GroupId = UserDetails->pw_gid;
if( GroupId > 0 ){
setgid( GroupId );
}
else{
ToPipe("IE ERROR Could not set groupid as groupid is invalid\n");
return 1;
}
uid_t UserId = UserDetails->pw_uid;
if( UserId > 0 ){
setuid(UserId);
}
else{
ToPipe("IE ERROR Failed to set userid as it is invalid\n");
return 1;
}
}
else{
ToPipe("IE ERROR No user id associated with user nobody\n");
return 1;
}

char TestCaseFile[10], OutputFile[10];
sprintf(TestCaseFile, "%d.txt", TestCaseFileId);
sprintf(OutputFile, "%do.txt", TestCaseFileId);
if(freopen(TestCaseFile, "r", stdin)==NULL){
ToPipe("IE ERROR Could not open test case file\n");
}
if(freopen(OutputFile, "w", stdout)==NULL){
ToPipe("IE ERROR Could not open output file\n");
}
if(freopen("/dev/null", "w", stderr)==NULL){
ToPipe("IE ERROR Could not redirect error stream to null\n");
}
SetResourceLimitValues(TimeLimit);
alarm(TimeLimit);
if(execl(InputFile,InputFile,(char *) NULL) == -1){
fclose(stdout);
ToPipe("IE ERROR File not present or some other error.");
}

}
else { /* Code executed by parent */
struct rusage resourceUsage;

int MemoryUsed = 0;

do{
MicroSecSleep(MicroSecSleepInterval);
MemoryUsed = max(MemoryUsed, MemoryUsage(cpid));
if(MemoryUsed > MemoryLimit){
kill(cpid, SIGKILL);
}
w = wait4 (cpid, &status, WUNTRACED | WCONTINUED, &resourceUsage);
} while(w==0);

gettimeofday(&finish,NULL);
t_sec = finish.tv_sec-start.tv_sec;
t_sec=finish.tv_sec-start.tv_sec;
t_usec=finish.tv_usec-start.tv_usec;
if(t_usec<0){
t_sec--;
t_usec+=1000000;
}

timeval tv = resourceUsage.ru_utime;
float SysTime = (float)( tv.tv_sec * 1000000 + (int) tv.tv_usec ) / 1000000 ;
float TimeUsed = (float)( t_sec * 1000000 + (int) t_usec ) / 1000000 ;

bool sigkill = false, sigalrm = false;

if(MemoryUsed > MemoryLimit){
;
}
else if( WIFEXITED(status) == true ){
if( WEXITSTATUS(status) !=0 ){
ToPipe("RE NZEC");
}
else{
ToPipe("AC");
}
}
else if( WIFSIGNALED(status) == true ){
if (WTERMSIG (status) == SIGKILL ){
ToPipe("TLE");
sigkill = true;
}
else if (WTERMSIG(status) == SIGALRM){
ToPipe("TLE");
sigalrm = true;
}
else if (WTERMSIG (status) == SIGXFSZ)
ToPipe("RE SIGXFSZ");
else if (WTERMSIG (status) == SIGSEGV)
ToPipe("RE SIGSEGV");
else if (WTERMSIG (status) == SIGFPE)
ToPipe("RE SIGFPE");
else if (WTERMSIG (status) == SIGABRT)
ToPipe("RE SIGABRT");
else if (WTERMSIG (status) == SIGHUP)
ToPipe("IE SIGHUP");
else if (WTERMSIG (status) == SIGPIPE)
ToPipe("IE SIGPIPE");
else{
ToPipe("RE OTHER");
printf("%d\n", WTERMSIG(status));
}
}
char tmp[10];
sprintf(tmp, "%0.4f", TimeUsed);
ToPipe(tmp);
sprintf(tmp, "%d", MemoryUsed);
ToPipe(tmp);
//printf("%d %d\n", (int)tv.tv_sec, (int)tv.tv_usec);
if(sigkill) printf("SIGKILL");
else if(sigalrm) printf("SIGALRM");
}
}
34 changes: 19 additions & 15 deletions FileHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ FileHandle::FileHandle(FileInfoStruct* FileInfo){
result=true;
sprintf(detailstatus,"\0");
sprintf(FileAddr, "%s%d", FILEPATH, FileInfo->FileId);
sprintf(FileDirAddr, "%s%d/", FILEPATH, FileInfo->FileId);
if(strcmp(FileInfo->lang, "java")){
strcpy(FileName, "Main");
}
else strcpy(FileName, "test");
sprintf(FullFileAddr, "%s%s", FileAddr, FullFileAddr);
Logs::OpenLogFile();
sprintf(logs, "Beginning operations on File Id ==> %d\n", FileInfo->FileId);
Logs::WriteLine(logs, true);
Expand Down Expand Up @@ -71,7 +77,7 @@ int FileHandle::MakeDir(){
int ErrNo;
char dirString[100];
Logs::WriteLine("Creating directory.");
if( mkdir(FileAddr, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH | S_IWOTH)==-1){
if( mkdir(FileDirAddr, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH | S_IWOTH)==-1){
ErrNo=errno;
if(ErrNo==17) Logs::WriteLine("Directory already created.. Continuing");
else {
Expand All @@ -80,7 +86,7 @@ int FileHandle::MakeDir(){
return -1;
}
}
sprintf(systemString, "cp %s.txt %s/main.%s", FileAddr, FileAddr, FileInfo->lang);
sprintf(systemString, "cp %s.txt %s.%s", FileAddr, FullFileAddr, FileInfo->lang);
if(system(systemString)==-1){
strcpy(status, "IE");
strcpy(detailstatus, "Error in copying dowloaded file.");
Expand All @@ -105,11 +111,11 @@ void FileHandle::Compile(){
void FileHandle::pipeCompile(){
FILE *fpipe;
if(strcmp(FileInfo->lang, "cpp")==0)
sprintf(command, "g++ -w -static %s/main.cpp -o %s/main 2>&1", FileAddr, FileAddr);
sprintf(command, "g++ -w -static %s.cpp -o %s 2>&1", FullFileAddr, FullFileAddr);
else if(strcmp(FileInfo->lang, "c") == 0)
sprintf(command, "g++ -w -static %s/main.c -o %s/main 2>&1", FileAddr, FileAddr);
sprintf(command, "gcc -w -static %s.c -o %s 2>&1", FullFileAddr, FullFileAddr);
else if(strcmp(FileInfo->lang, "java")==0)
sprintf(command, "javac -nowarn -deprecation %s/main.java 2>&1", FileAddr);
sprintf(command, "javac -nowarn -deprecation %s.java 2>&1", FullFileAddr);
Logs::Write("Compiling file ==> ");
char line[256];

Expand Down Expand Up @@ -145,7 +151,7 @@ int FileHandle::pipeNoOfTestCases(){
}

int FileHandle::PrepareToExecute(){
sprintf(systemString, "cp %s%s/Input/* %s/", TESTCASESPATH, FileInfo->ProblemId, FileAddr);
sprintf(systemString, "cp %s%s/Input/* %s", TESTCASESPATH, FileInfo->ProblemId, FileDirAddr);
system(systemString);

NoOfTestCases = pipeNoOfTestCases();
Expand All @@ -161,12 +167,8 @@ int FileHandle::PrepareToExecute(){

void FileHandle::PipeExecute(){
FILE *fpipe;
if(strcmp(FileInfo->lang,"cpp")==0 || strcmp(FileInfo->lang,"c")==0){
sprintf(command, "./cpp_execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, MemoryLimit, FileInfo->lang);
}
else if(strcmp(FileInfo->lang, "java")==0){
sprintf(command, "./java_execution %d %d %d %d %s", FileInfo->FileId, TestCaseId, FileInfo->TimeLimit, FileInfo->MemoryLimit, FileInfo->lang);
}

sprintf(command, "./Execution %d %s %d %d %d %s", FileInfo->FileId, FileName, TestCaseId, FileInfo->TimeLimit, MemoryLimit, FileInfo->lang);

char line[1024];

Expand All @@ -192,7 +194,7 @@ void FileHandle::Execute(){

PipeExecute();
strcpy(str, ExecutionStr.c_str());
printf("\n%s\n", str);
//printf("\n%s\n", str);
token = strtok(str, " \n");
strcpy(status, token);
if(strcmp(token, "AC")!=0) result=false;
Expand All @@ -202,14 +204,16 @@ void FileHandle::Execute(){
}
token = strtok(NULL, " \n"); sprintf(TestCaseExecutionTime, "%s", token);
TimeUsed += (float) atof( TestCaseExecutionTime );
printf("time - %f %d\n", TimeUsed, FileInfo->TimeLimit);
token = strtok(NULL, " \n"); sprintf(TestCaseExecutionMemory, "%s", token);
MemoryUsed = max(MemoryUsed, atoi( TestCaseExecutionMemory));
printf("time - %f \n", TimeUsed);
if( TimeUsed > (float) FileInfo->TimeLimit){
result = false;
sprintf(status, "TLE");
sprintf(detailstatus, "\0");
}

sprintf(logs, "%d ==> %s %s %s\n", TestCaseId, status, detailstatus, TestCaseExecutionTime);
sprintf(logs, "%d ==> %s %s %s %s\n", TestCaseId, status, detailstatus, TestCaseExecutionTime, TestCaseExecutionMemory);
Logs::Write(logs);
if(result==false){
break;
Expand Down
4 changes: 2 additions & 2 deletions FileHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class FileHandle{
char status[10], logs[10000], detailstatus[10000];
char str[100], *token, tmp[10];
char logString[100];
char TestCaseExecutionTime[10];
char FileAddr[10], timeused[10], memoryused[10], fileid[10];
char TestCaseExecutionTime[10], TestCaseExecutionMemory[10];
char FileDirAddr[100], FileAddr[100], FileName[10], FullFileAddr[100], timeused[10], memoryused[10], fileid[10];
char command[1000];
const char* lang, *ProblemId;
string CompileOutput;
Expand Down
7 changes: 3 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VAR=includeh.h includes.h config.h Logs.h
complete: main *_execution
complete: main Execution

main : main.o FileHandle.o CurlWrapper.o Logs.o ContentParser.o -lcurl
g++ -o main main.o FileHandle.o CurlWrapper.o ContentParser.o Logs.o -lcurl
Expand All @@ -23,6 +23,5 @@ Logs.o : Logs.cpp $(VAR)
clean:
rm *.o main *_execution

*_execution : cpp_execution.cpp java_execution.cpp resources.h
g++ cpp_execution.cpp -o cpp_execution
g++ java_execution.cpp -o java_execution
Execution : Execution.cpp
g++ Execution.cpp -o Execution
Loading

0 comments on commit 8f50fa5

Please sign in to comment.