Add ruby test
[gruntmaster-daemon.git] / gruntmaster-exec
CommitLineData
c40a2dd0 1#!/usr/bin/perl
da905f9e
MG
2use v5.14;
3use strict;
c40a2dd0 4use warnings;
da905f9e 5
c40a2dd0
MG
6use constant +{
7 # Accepted
8 AC => 0,
da905f9e 9
c40a2dd0
MG
10 # Internal server error
11 ERR => -1,
da905f9e 12
c40a2dd0
MG
13 # All other errors
14 WA => 1,
15 NZX => 2,
16 TLE => 3,
17 OLE => 4,
18 DIED => 5,
19 REJ => 10,
20};
50cdf3cb 21
a6b04042 22use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE RLIMIT_NPROC/;
c40a2dd0
MG
23use IPC::Signal qw/sig_name sig_num/;
24use sigtrap qw/XFSZ/;
69c25408 25
c40a2dd0
MG
26use Getopt::Long;
27use POSIX qw//;
65ab2558 28use Text::ParseWords qw/shellwords/;
c40a2dd0
MG
29use Time::HiRes qw/alarm/;
30
13b1661a 31my (@fds, $timeout, $mlimit, $olimit, $sudo, $keep_stderr);
1fe52cde 32
c40a2dd0 33GetOptions(
13b1661a
MG
34 "fd=s" => \@fds,
35 "timeout=f" => \$timeout,
36 "mlimit=i" => \$mlimit,
37 "olimit=i" => \$olimit,
38 "keep-stderr!" => \$keep_stderr,
39 "sudo!" => \$sudo,
c40a2dd0
MG
40);
41
65ab2558
MG
42my $killuser = $ENV{GRUNTMASTER_KILL_USER};
43my @sudo;
42ce8ba9 44@sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo;
91c5f490 45undef $mlimit if @sudo; # sudo wants a lot of address space
65ab2558 46
c40a2dd0
MG
47my $ret = fork // die 'Cannot fork';
48if ($ret) {
49 my $tle;
65ab2558
MG
50 local $SIG{ALRM} = sub {
51 if ($killuser) {
52 system @sudo, 'pkill', '-KILL', '-u', $killuser;
53 } else {
54 kill KILL => $ret
55 }
56 $tle = 1
57 };
97358afc 58 alarm ($timeout || 10);
c40a2dd0
MG
59 waitpid $ret, 0;
60 alarm 0;
65ab2558
MG
61 if (@sudo) {
62 $? = $? >> 8;
74ab8869 63 $? = $? < 128 || $? > 128+32 ? ($? << 8) : $? - 128;
65ab2558 64 }
c40a2dd0
MG
65 my $sig = $? & 127;
66 my $signame = sig_name $sig;
67 exit !say TLE, "\nTime Limit Exceeded" if $tle;
68 exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ';
69 exit !say DIED, "\nCrash (SIG$signame)" if $sig && $signame ne 'PIPE';
70 exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8;
71 exit !say AC, "\nAll OK";
72} else {
73 $^F = 50;
13b1661a
MG
74 POSIX::close 2 unless $keep_stderr;
75 POSIX::close $_ for 0, 1, 3 .. $^F;
c40a2dd0
MG
76 for my $fdstring (@fds) {
77 my ($fd, $file) = split ' ', $fdstring, 2;
78 open my $fh, $file or die $!;
79 my $oldfd = fileno $fh;
80 if ($oldfd != $fd) {
81 POSIX::dup2 $oldfd, $fd or die $!;
82 POSIX::close $oldfd or die $!;
83 }
84 }
d9ea4614 85 my $nproc = $killuser ? 15 : 1;
16de3659 86 my $debug = $ENV{TEST_VERBOSE};
c40a2dd0
MG
87 %ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME});
88 setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
89 setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
42ce8ba9 90 setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo;
2ddfc547
MG
91 open my $adj, '>', '/proc/self/oom_score_adj';
92 print $adj 900;
93 close $adj;
1e5f2b8b 94 unshift @ARGV, @sudo;
16de3659 95 say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug;
c40a2dd0
MG
96 exec @ARGV;
97}
98
991;
69c25408
MG
100__END__
101
102=encoding utf-8
103
104=head1 NAME
105
106gruntmaster-exec - Gruntmaster 6000 executor
107
108=head1 SYNOPSIS
109
110 gruntmaster-exec 20000000 111 echo 'Hello, world!'
111
112=head1 DESCRIPTION
113
114gruntmaster-exec is the script used by gruntmasterd to run programs.
115
116The 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.
117
118gruntmaster-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.
119
120=head1 AUTHOR
121
122Marius Gavrilescu E<lt>marius@ieval.roE<gt>
123
124=head1 COPYRIGHT AND LICENSE
125
126Copyright (C) 2014 by Marius Gavrilescu
127
128This program is free software: you can redistribute it and/or modify
129it under the terms of the GNU Affero General Public License as published by
130the Free Software Foundation, either version 3 of the License, or
131(at your option) any later version.
This page took 0.0553630000000001 seconds and 4 git commands to generate.