X-Git-Url: http://git.ieval.ro/?p=gruntmaster-daemon.git;a=blobdiff_plain;f=gruntmaster-exec;h=d2807656d506909f27e14d68897b2317f20c4f53;hp=0a2c50c204c12477cd93d574353fe3a7bdeb80bd;hb=614700357f0af54cdba3ac21b7420738bda2f3fe;hpb=42ce8ba99a9d50c8bb440451f45098a98769216a diff --git a/gruntmaster-exec b/gruntmaster-exec index 0a2c50c..d280765 100755 --- a/gruntmaster-exec +++ b/gruntmaster-exec @@ -28,18 +28,36 @@ use POSIX qw//; use Text::ParseWords qw/shellwords/; use Time::HiRes qw/alarm/; -my (@fds, $timeout, $mlimit, $olimit, $sudo); -my $close = 1; +my (@fds, $timeout, $mlimit, $olimit, $sudo, $keep_stderr); GetOptions( - "fd=s" => \@fds, - "timeout=f" => \$timeout, - "mlimit=i" => \$mlimit, - "olimit=i" => \$olimit, - "close!" => \$close, - "sudo!" => \$sudo, + "fd=s" => \@fds, + "timeout=f" => \$timeout, + "mlimit=i" => \$mlimit, + "olimit=i" => \$olimit, + "keep-stderr!" => \$keep_stderr, + "sudo!" => \$sudo, ); +sub test_pipe_read { + my $data = ''; + sysread STDIN, $data, 4096 and syswrite STDOUT, "recvd\n" until $data =~ /done/; + syswrite STDOUT, 'done'; +} + +sub test_pipe_write { + my ($rin, $rout); + vec($rin, fileno STDIN, 1) = 1; + syswrite STDOUT, "data\n" until select $rout = $rin, undef, undef, 0.05; + syswrite STDOUT, 'done'; + my $data = ''; + sysread STDIN, $data, 4096 until $data =~ /done/; +} + +sub test_pipes { + $ARGV[0] =~ /prog/ ? test_pipe_read : test_pipe_write +} + my $killuser = $ENV{GRUNTMASTER_KILL_USER}; my @sudo; @sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo; @@ -47,7 +65,13 @@ undef $mlimit if @sudo; # sudo wants a lot of address space my $ret = fork // die 'Cannot fork'; if ($ret) { - my $tle; + my ($tle, $child_ready); + local $SIG{USR1} = sub { $child_ready = 1 }; + sleep 3; # Wait for ready signal (SIGUSR1) + unless ($child_ready) { + kill KILL => $ret; + exit !say ERR, "\nNo response from gruntmaster-exec child"; + } local $SIG{ALRM} = sub { if ($killuser) { system @sudo, 'pkill', '-KILL', '-u', $killuser; @@ -67,14 +91,13 @@ if ($ret) { 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 DIED, "\nCrash (SIG$signame)" if $sig && ($signame ne 'PIPE' || $ARGV[0] !~ /prog/); 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; - } + POSIX::close 2 unless $keep_stderr; + POSIX::close $_ for 0, 1, 3 .. $^F; for my $fdstring (@fds) { my ($fd, $file) = split ' ', $fdstring, 2; open my $fh, $file or die $!; @@ -84,13 +107,18 @@ if ($ret) { POSIX::close $oldfd or die $!; } } + test_pipes if grep /tty|fifo/, @fds; 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; setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo; + open my $adj, '>', '/proc/self/oom_score_adj'; + print $adj 900; + close $adj; unshift @ARGV, @sudo; + kill USR1 => getppid; # Tell parent process that we're ready say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug; exec @ARGV; }