Add ruby test
[gruntmaster-daemon.git] / gruntmaster-exec
index d11803fa6156d6d1880a4815923f5378ad73bb3a..5235e8e5ed3430fa06b37a73a8bc5304c78d54ae 100755 (executable)
-#!/usr/bin/perl -w
+#!/usr/bin/perl
 use v5.14;
 use strict;
+use warnings;
 
-use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE/;
+use constant +{
+       # Accepted
+       AC => 0,
+
+       # Internal server error
+       ERR => -1,
+
+       # All other errors
+       WA => 1,
+       NZX => 2,
+       TLE => 3,
+       OLE => 4,
+       DIED => 5,
+       REJ => 10,
+};
+
+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, $sudo, $keep_stderr);
+
+GetOptions(
+       "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} && $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 {
+               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 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;
+       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 $!;
+               my $oldfd = fileno $fh;
+               if ($oldfd != $fd) {
+                       POSIX::dup2 $oldfd, $fd 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;
+       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;
+}
+
+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>
 
-my ($mlimit, $olimit, @args) = @ARGV;
+=head1 COPYRIGHT AND LICENSE
 
-setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
-setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
+Copyright (C) 2014 by Marius Gavrilescu
 
-%ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{PATH});
-exec @args;
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Affero General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
This page took 0.012459 seconds and 4 git commands to generate.