+use Getopt::Long;
+use POSIX qw//;
+use Time::HiRes qw/alarm/;
+
+my (@fds, $timeout, $mlimit, $olimit);
+my $close = 1;
+
+GetOptions(
+ "fd=s" => \@fds,
+ "timeout=f" => \$timeout,
+ "mlimit=i" => \$mlimit,
+ "olimit=i" => \$olimit,
+ "close!" => \$close,
+);
+
+my $ret = fork // die 'Cannot fork';
+if ($ret) {
+ my $tle;
+ local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1};
+ alarm ($timeout || 5);
+ waitpid $ret, 0;
+ alarm 0;
+ my $sig = $? & 127;
+ my $signame = sig_name $sig;
+ exit !say TLE, "\nTime Limit Exceeded" if $tle;
+ exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ';
+ exit !say DIED, "\nCrash (SIG$signame)" if $sig && $signame ne 'PIPE';
+ exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8;
+ exit !say AC, "\nAll OK";
+} else {
+ $^F = 50;
+ if ($close) {
+ POSIX::close $_ for 0 .. $^F;
+ }
+ for my $fdstring (@fds) {
+ my ($fd, $file) = split ' ', $fdstring, 2;
+ open my $fh, $file or die $!;
+ my $oldfd = fileno $fh;
+ if ($oldfd != $fd) {
+ POSIX::dup2 $oldfd, $fd or die $!;
+ POSIX::close $oldfd or die $!;
+ }
+ }
+ %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
+ exec @ARGV;
+}
+
+1;
+__END__
+
+=encoding utf-8
+
+=head1 NAME
+
+gruntmaster-exec - Gruntmaster 6000 executor
+
+=head1 SYNOPSIS
+
+ gruntmaster-exec 20000000 111 echo 'Hello, world!'
+
+=head1 DESCRIPTION
+
+gruntmaster-exec is the script used by gruntmasterd to run programs.
+
+The first argument is the address space limit (in bytes), the second argument is the output limit (also in bytes). The rest of the arguments are the command that should be run and its arguments.
+
+gruntmaster-exec sets the resource limits, cleans the environment (except for PATH and HOME), adds the ONLINE_JUDGE environment variable with value 1, and finally C<exec>s the given command.
+
+=head1 AUTHOR
+
+Marius Gavrilescu E<lt>marius@ieval.roE<gt>