From: Martin Mares Date: Thu, 15 May 2008 09:39:33 +0000 (+0200) Subject: The iwrapper moved to eval/. X-Git-Tag: python-dummy-working~203 X-Git-Url: http://mj.ucw.cz/gitweb/?a=commitdiff_plain;h=83d5d45462f131b0425f43e12699a2b2f7817fc6;p=eval.git The iwrapper moved to eval/. --- diff --git a/Makefile b/Makefile index 3a68c08..17ea270 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,7 @@ endif include $(s)/box/Makefile include $(s)/utils/Makefile +include $(s)/eval/Makefile # And finally the default rules of the build system include $(s)/build/Makebottom diff --git a/eval/iwrapper.c b/eval/iwrapper.c new file mode 100644 index 0000000..3b31cbe --- /dev/null +++ b/eval/iwrapper.c @@ -0,0 +1,170 @@ +/* + * A Wrapper for Interactive Tests + * + * (c) 2001 Martin Mares + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define NONRET __attribute__((noreturn)) +#define UNUSED __attribute__((unused)) + +static void NONRET __attribute__((format(printf,1,2))) +die(char *msg, ...) +{ + va_list args; + va_start(args, msg); + vfprintf(stderr, msg, args); + fputc('\n', stderr); + exit(1); +} + +static void +copy(int fd) +{ + char buf[4096]; + int c; + + while (c = read(fd, buf, sizeof(buf))) + write(2, buf, c); +} + +int +main(int argc, char **argv) +{ + int sep, nbox, nchk; + char **abox, **achk; + pid_t pbox, pchk; + int inpipe[2], outpipe[2], boxepipe[2], chkepipe[2]; + int exited = 0, sbox, schk; + + for (sep=1; sep < argc; sep++) + if (!strcmp(argv[sep], "@@")) + break; + if (sep >= argc - 1) + die("Usage: iwrapper @@ "); + nbox = sep - 1; + abox = alloca((nbox+1) * sizeof(char *)); + memcpy(abox, argv+1, nbox*sizeof(char *)); + abox[nbox] = NULL; + nchk = argc - sep - 1; + achk = alloca((nchk+1) * sizeof(char *)); + memcpy(achk, argv+sep+1, nchk*sizeof(char *)); + achk[nchk] = NULL; + + if (pipe(inpipe) < 0 || + pipe(outpipe) < 0 || + pipe(boxepipe) < 0 || + pipe(chkepipe) < 0) + die("pipe: %m"); + + pbox = fork(); + if (pbox < 0) + die("fork: %m"); + if (!pbox) + { + close(inpipe[1]); + close(0); + dup(inpipe[0]); + close(inpipe[0]); + close(outpipe[0]); + close(1); + dup(outpipe[1]); + close(outpipe[1]); + close(boxepipe[0]); + close(2); + dup(boxepipe[1]); + close(boxepipe[1]); + close(chkepipe[0]); + close(chkepipe[1]); + execv(abox[0], abox); + die("exec: %m"); + } + + pchk = fork(); + if (pchk < 0) + die("fork: %m"); + if (!pchk) + { + close(inpipe[0]); + close(1); + dup(inpipe[1]); + close(inpipe[1]); + close(outpipe[1]); + close(0); + dup(outpipe[0]); + close(outpipe[0]); + close(chkepipe[0]); + close(2); + dup(chkepipe[1]); + close(chkepipe[1]); + close(boxepipe[0]); + close(boxepipe[1]); + execv(achk[0], achk); + die("exec: %m"); + } + + close(inpipe[0]); + close(inpipe[1]); + close(outpipe[0]); + close(outpipe[1]); + close(chkepipe[1]); + close(boxepipe[1]); + + sbox = schk = 0; + while (exited != 3) + { + int st; + pid_t p = wait(&st); + if (p < 0) + die("wait: %m"); + if (p == pbox) + { + exited |= 1; + sbox = st; + } + else if (p == pchk) + { + exited |= 2; + schk = st; + } + else + die("Unknown process %d died", p); + } + + if (!WIFEXITED(sbox)) + die("Sandbox fault, status=%x", sbox); + if (!WIFEXITED(schk) || WEXITSTATUS(schk) >= 100) + die("Checker fault, status=%x", schk); + if (WEXITSTATUS(sbox)) + { + copy(boxepipe[0]); + copy(chkepipe[0]); + return 1; + } + else if (WEXITSTATUS(schk)) + { + copy(chkepipe[0]); + copy(boxepipe[0]); + return 1; + } + copy(boxepipe[0]); + copy(chkepipe[0]); + return 0; +} diff --git a/src/iwrapper.c b/src/iwrapper.c deleted file mode 100644 index 3b31cbe..0000000 --- a/src/iwrapper.c +++ /dev/null @@ -1,170 +0,0 @@ -/* - * A Wrapper for Interactive Tests - * - * (c) 2001 Martin Mares - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#define NONRET __attribute__((noreturn)) -#define UNUSED __attribute__((unused)) - -static void NONRET __attribute__((format(printf,1,2))) -die(char *msg, ...) -{ - va_list args; - va_start(args, msg); - vfprintf(stderr, msg, args); - fputc('\n', stderr); - exit(1); -} - -static void -copy(int fd) -{ - char buf[4096]; - int c; - - while (c = read(fd, buf, sizeof(buf))) - write(2, buf, c); -} - -int -main(int argc, char **argv) -{ - int sep, nbox, nchk; - char **abox, **achk; - pid_t pbox, pchk; - int inpipe[2], outpipe[2], boxepipe[2], chkepipe[2]; - int exited = 0, sbox, schk; - - for (sep=1; sep < argc; sep++) - if (!strcmp(argv[sep], "@@")) - break; - if (sep >= argc - 1) - die("Usage: iwrapper @@ "); - nbox = sep - 1; - abox = alloca((nbox+1) * sizeof(char *)); - memcpy(abox, argv+1, nbox*sizeof(char *)); - abox[nbox] = NULL; - nchk = argc - sep - 1; - achk = alloca((nchk+1) * sizeof(char *)); - memcpy(achk, argv+sep+1, nchk*sizeof(char *)); - achk[nchk] = NULL; - - if (pipe(inpipe) < 0 || - pipe(outpipe) < 0 || - pipe(boxepipe) < 0 || - pipe(chkepipe) < 0) - die("pipe: %m"); - - pbox = fork(); - if (pbox < 0) - die("fork: %m"); - if (!pbox) - { - close(inpipe[1]); - close(0); - dup(inpipe[0]); - close(inpipe[0]); - close(outpipe[0]); - close(1); - dup(outpipe[1]); - close(outpipe[1]); - close(boxepipe[0]); - close(2); - dup(boxepipe[1]); - close(boxepipe[1]); - close(chkepipe[0]); - close(chkepipe[1]); - execv(abox[0], abox); - die("exec: %m"); - } - - pchk = fork(); - if (pchk < 0) - die("fork: %m"); - if (!pchk) - { - close(inpipe[0]); - close(1); - dup(inpipe[1]); - close(inpipe[1]); - close(outpipe[1]); - close(0); - dup(outpipe[0]); - close(outpipe[0]); - close(chkepipe[0]); - close(2); - dup(chkepipe[1]); - close(chkepipe[1]); - close(boxepipe[0]); - close(boxepipe[1]); - execv(achk[0], achk); - die("exec: %m"); - } - - close(inpipe[0]); - close(inpipe[1]); - close(outpipe[0]); - close(outpipe[1]); - close(chkepipe[1]); - close(boxepipe[1]); - - sbox = schk = 0; - while (exited != 3) - { - int st; - pid_t p = wait(&st); - if (p < 0) - die("wait: %m"); - if (p == pbox) - { - exited |= 1; - sbox = st; - } - else if (p == pchk) - { - exited |= 2; - schk = st; - } - else - die("Unknown process %d died", p); - } - - if (!WIFEXITED(sbox)) - die("Sandbox fault, status=%x", sbox); - if (!WIFEXITED(schk) || WEXITSTATUS(schk) >= 100) - die("Checker fault, status=%x", schk); - if (WEXITSTATUS(sbox)) - { - copy(boxepipe[0]); - copy(chkepipe[0]); - return 1; - } - else if (WEXITSTATUS(schk)) - { - copy(chkepipe[0]); - copy(boxepipe[0]); - return 1; - } - copy(boxepipe[0]); - copy(chkepipe[0]); - return 0; -}