X-Git-Url: https://git.ucc.asn.au/?p=progcomp2012.git;a=blobdiff_plain;f=judge%2Fmanager%2Fprogram.cpp;h=5ea1591611a6dbab3f631a3018c978a4e4c4ef1c;hp=2d09a510aa6e9c70ba0c0f0bf4922812de7cae63;hb=f37d392713a0b6fec7a2c543edcd8402156f3744;hpb=476617a8222c8c56404c12a65dd75c55b8019542 diff --git a/judge/manager/program.cpp b/judge/manager/program.cpp index 2d09a51..5ea1591 100644 --- a/judge/manager/program.cpp +++ b/judge/manager/program.cpp @@ -8,6 +8,13 @@ #include "program.h" #include #include +#include + +#include +#include +#include +#include + using namespace std; @@ -56,10 +63,13 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0 } while (token != NULL); - char ** arguments = new char*[args.size()+2]; - for (unsigned int i=0; i < args.size(); ++i) - arguments[i] = args[i]; - + char ** arguments = NULL; + if (args.size() > 0) + { + arguments = new char*[args.size()]; + for (unsigned int i=0; i < args.size(); ++i) + arguments[i] = args[i]; + } //See if file exists and is executable... if (access(executablePath, X_OK) != 0) { @@ -89,8 +99,10 @@ Program::Program(const char * executablePath) : input(NULL), output(NULL), pid(0 if (access(executablePath, X_OK) == 0) //Check we STILL have permissions to start the file + { execv(executablePath,arguments); ///Replace process with desired executable - + } + perror("execv error:\n"); fprintf(stderr, "Program::Program - Could not run program \"%s\"!\n", executablePath); exit(EXIT_FAILURE); //We will probably have to terminate the whole program if this happens } @@ -215,6 +227,31 @@ bool Program::GetMessage(string & buffer, double timeout) if (!Running() || timeout == 0) return false; + struct timeval tv; + fd_set readfds; + + tv.tv_sec = (int)(timeout); + tv.tv_usec = (timeout - (double)((int)timeout)) * 1000000; + + int fd = fileno(input); + + FD_ZERO(&readfds); + FD_SET(fd, &readfds); + + select(fd+1, &readfds, NULL, NULL, &tv); + + if (!FD_ISSET(fd, &readfds)) + return false; //Timed out + //fprintf(stderr, "Got message!\n"); + for (char c = fgetc(input); c != '\n' && (int)(c) != EOF; c = fgetc(input)) + { + //fprintf(stderr, "%c", c); + buffer += c; + } + //fprintf(stderr, "%s\n", buffer.c_str()); + return true; + + /* Old way, using threads, which apparently is terrible assert(&buffer != NULL); GetterThread getterThread(input, buffer); assert(&(getterThread.buffer) != NULL); @@ -245,7 +282,7 @@ bool Program::GetMessage(string & buffer, double timeout) if (buffer.size() == 1 && buffer[0] == EOF) return false; return true; - + */ }