REJ => 10,
};
-use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE/;
+use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE RLIMIT_NPROC/;
use IPC::Signal qw/sig_name sig_num/;
use sigtrap qw/XFSZ/;
use Getopt::Long;
use POSIX qw//;
+use Text::ParseWords qw/shellwords/;
use Time::HiRes qw/alarm/;
-my (@fds, $timeout, $mlimit, $olimit);
+my (@fds, $timeout, $mlimit, $olimit, $sudo);
+my $close = 1;
+
GetOptions(
"fd=s" => \@fds,
"timeout=f" => \$timeout,
"mlimit=i" => \$mlimit,
"olimit=i" => \$olimit,
+ "close!" => \$close,
+ "sudo!" => \$sudo,
);
+my $killuser = $ENV{GRUNTMASTER_KILL_USER};
+my @sudo;
+@sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo;
+undef $mlimit if @sudo; # sudo wants a lot of address space
+
my $ret = fork // die 'Cannot fork';
if ($ret) {
my $tle;
- local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1};
- alarm ($timeout || 5);
+ local $SIG{ALRM} = sub {
+ if ($killuser) {
+ system @sudo, 'pkill', '-KILL', '-u', $killuser;
+ } else {
+ kill KILL => $ret
+ }
+ $tle = 1
+ };
+ alarm ($timeout || 10);
waitpid $ret, 0;
alarm 0;
+ if (@sudo) {
+ $? = $? >> 8;
+ $? = $? < 128 || $? > 128+32 ? ($? << 8) : $? - 128;
+ }
my $sig = $? & 127;
my $signame = sig_name $sig;
exit !say TLE, "\nTime Limit Exceeded" if $tle;
exit !say AC, "\nAll OK";
} else {
$^F = 50;
- POSIX::close $_ for 0 .. $^F;
+ if ($close) {
+ POSIX::close $_ for 0 .. $^F;
+ }
for my $fdstring (@fds) {
my ($fd, $file) = split ' ', $fdstring, 2;
open my $fh, $file or die $!;
POSIX::close $oldfd or die $!;
}
}
+ my $nproc = $killuser ? 15 : 1;
+ my $debug = $ENV{TEST_VERBOSE};
%ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME});
setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
- POSIX::setgid 65534; # Set group id to nogroup
- POSIX::setuid 65534; # Set user id to nobody
+ setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo;
+ unshift @ARGV, @sudo;
+ say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug;
exec @ARGV;
}