#include <sys/sysinfo.h>
#include <sys/syscall.h>
#include <sys/resource.h>
+#include <linux/ptrace.h>
#define NONRET __attribute__((noreturn))
#define UNUSED __attribute__((unused))
static int filter_syscalls; /* 0=off, 1=liberal, 2=totalitarian */
static int timeout; /* milliseconds */
static int wall_timeout;
+static int extra_timeout;
static int pass_environ;
static int file_access;
static int verbose;
static int memory_limit;
+static int stack_limit;
static char *redir_stdin, *redir_stdout, *redir_stderr;
static char *set_cwd;
ptrace(PTRACE_KILL, box_pid);
kill(-box_pid, SIGKILL);
kill(box_pid, SIGKILL);
+ meta_printf("killed:1\n");
struct rusage rus;
- int stat;
- int p = wait4(box_pid, &stat, 0, &rus);
+ int p, stat;
+ do
+ p = wait4(box_pid, &stat, 0, &rus);
+ while (p < 0 && errno == EINTR);
if (p < 0)
- fprintf(stderr, "UGH: Lost track of the process\n");
+ fprintf(stderr, "UGH: Lost track of the process (%m)\n");
else
final_stats(&rus);
}
ms = (utime + stime) * 1000 / ticks_per_sec;
if (verbose > 1)
fprintf(stderr, "[time check: %d msec]\n", ms);
- if (ms > timeout)
+ if (ms > timeout && ms > extra_timeout)
err("TO: Time limit exceeded");
}
}
static void
boxkeeper(void)
{
- int syscall_count = 0;
+ int syscall_count = (filter_syscalls ? 0 : 1);
struct sigaction sa;
is_ptraced = 1;
+
bzero(&sa, sizeof(sa));
sa.sa_handler = signal_int;
sigaction(SIGINT, &sa, NULL);
+
gettimeofday(&start_time, NULL);
ticks_per_sec = sysconf(_SC_CLK_TCK);
if (ticks_per_sec <= 0)
die("Invalid ticks_per_sec!");
+
if (timeout || wall_timeout)
{
sa.sa_handler = signal_alarm;
sigaction(SIGALRM, &sa, NULL);
alarm(1);
}
+
for(;;)
{
struct rusage rus;
else
dup2(1, 2);
setpgrp();
+
if (memory_limit)
{
rl.rlim_cur = rl.rlim_max = memory_limit * 1024;
if (setrlimit(RLIMIT_AS, &rl) < 0)
- die("setrlimit: %m");
+ die("setrlimit(RLIMIT_AS): %m");
}
+
+ rl.rlim_cur = rl.rlim_max = (stack_limit ? (rlim_t)stack_limit * 1024 : RLIM_INFINITY);
+ if (setrlimit(RLIMIT_STACK, &rl) < 0)
+ die("setrlimit(RLIMIT_STACK): %m");
+
rl.rlim_cur = rl.rlim_max = 64;
if (setrlimit(RLIMIT_NOFILE, &rl) < 0)
- die("setrlimit: %m");
+ die("setrlimit(RLIMIT_NOFILE): %m");
+
char **env = setup_environment();
if (filter_syscalls)
{
-E <var>=<val>\tSet the environment variable <var> to <val>; unset it if <var> is empty\n\
-f\t\tFilter system calls (-ff=very restricted)\n\
-i <file>\tRedirect stdin from <file>\n\
+-k <size>\tLimit stack size to <size> KB (default: 0=unlimited)\n\
-m <size>\tLimit address space to <size> KB\n\
-M <file>\tOutput process information to <file> (name:value)\n\
-o <file>\tRedirect stdout to <file>\n\
-T\t\tAllow syscalls for measuring run time\n\
-v\t\tBe verbose (use multiple times for even more verbosity)\n\
-w <time>\tSet wall clock time limit (seconds, fractions allowed)\n\
+-x <time>\tSet extra timeout, before which a timing-out program is not yet killed,\n\
+\t\tso that its real execution time is reported (seconds, fractions allowed)\n\
");
exit(2);
}
int c;
uid_t uid;
- while ((c = getopt(argc, argv, "a:c:eE:fi:m:M:o:p:r:s:t:Tvw:")) >= 0)
+ while ((c = getopt(argc, argv, "a:c:eE:fi:k:m:M:o:p:r:s:t:Tvw:x:")) >= 0)
switch (c)
{
case 'a':
case 'f':
filter_syscalls++;
break;
+ case 'k':
+ stack_limit = atol(optarg);
+ break;
case 'i':
redir_stdin = optarg;
break;
case 'w':
wall_timeout = 1000*atof(optarg);
break;
+ case 'x':
+ extra_timeout = 1000*atof(optarg);
+ break;
default:
usage();
}