Ignore SIGPIPE *only* for user programs
[gruntmaster-daemon.git] / gruntmaster-exec
index 8ab857b2c469504182859214d6d2ab402489b5fc..b6577f4649206fee2e1cd108b6ec62cce581d8f1 100755 (executable)
@@ -28,21 +28,20 @@ use POSIX qw//;
 use Text::ParseWords qw/shellwords/;
 use Time::HiRes qw/alarm/;
 
-my (@fds, $timeout, $mlimit, $olimit, $nobody);
-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,
-       "nobody!"   => \$nobody,
+       "fd=s"         => \@fds,
+       "timeout=f"    => \$timeout,
+       "mlimit=i"     => \$mlimit,
+       "olimit=i"     => \$olimit,
+       "keep-stderr!" => \$keep_stderr,
+       "sudo!"        => \$sudo,
 );
 
 my $killuser = $ENV{GRUNTMASTER_KILL_USER};
 my @sudo;
-@sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $nobody;
+@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';
@@ -67,14 +66,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 $!;
@@ -89,7 +87,10 @@ if ($ret) {
        %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 $nobody;
+       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;
        say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug;
        exec @ARGV;
This page took 0.011497 seconds and 4 git commands to generate.