]>
Commit | Line | Data |
---|---|---|
c40a2dd0 | 1 | #!/usr/bin/perl |
da905f9e MG |
2 | use v5.14; |
3 | use strict; | |
c40a2dd0 | 4 | use warnings; |
da905f9e | 5 | |
c40a2dd0 MG |
6 | use 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 | 22 | use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE RLIMIT_NPROC/; |
c40a2dd0 MG |
23 | use IPC::Signal qw/sig_name sig_num/; |
24 | use sigtrap qw/XFSZ/; | |
69c25408 | 25 | |
c40a2dd0 MG |
26 | use Getopt::Long; |
27 | use POSIX qw//; | |
65ab2558 | 28 | use Text::ParseWords qw/shellwords/; |
c40a2dd0 MG |
29 | use Time::HiRes qw/alarm/; |
30 | ||
13b1661a | 31 | my (@fds, $timeout, $mlimit, $olimit, $sudo, $keep_stderr); |
1fe52cde | 32 | |
c40a2dd0 | 33 | GetOptions( |
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 | ||
e9f46104 MG |
42 | sub test_pipe_read { |
43 | my $data = ''; | |
44 | sysread STDIN, $data, 4096 and syswrite STDOUT, "recvd\n" until $data =~ /done/; | |
45 | syswrite STDOUT, 'done'; | |
46 | } | |
47 | ||
48 | sub test_pipe_write { | |
49 | my ($rin, $rout); | |
50 | vec($rin, fileno STDIN, 1) = 1; | |
51 | syswrite STDOUT, "data\n" until select $rout = $rin, undef, undef, 0.05; | |
52 | syswrite STDOUT, 'done'; | |
53 | my $data = ''; | |
54 | sysread STDIN, $data, 4096 until $data =~ /done/; | |
55 | } | |
56 | ||
57 | sub test_pipes { | |
58 | $ARGV[0] =~ /prog/ ? test_pipe_read : test_pipe_write | |
59 | } | |
60 | ||
65ab2558 MG |
61 | my $killuser = $ENV{GRUNTMASTER_KILL_USER}; |
62 | my @sudo; | |
42ce8ba9 | 63 | @sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo; |
91c5f490 | 64 | undef $mlimit if @sudo; # sudo wants a lot of address space |
65ab2558 | 65 | |
c40a2dd0 MG |
66 | my $ret = fork // die 'Cannot fork'; |
67 | if ($ret) { | |
0418cda2 MG |
68 | my ($tle, $child_ready); |
69 | local $SIG{USR1} = sub { $child_ready = 1 }; | |
70 | sleep 3; # Wait for ready signal (SIGUSR1) | |
71 | unless ($child_ready) { | |
72 | kill KILL => $ret; | |
73 | exit !say ERR, "\nNo response from gruntmaster-exec child"; | |
74 | } | |
65ab2558 MG |
75 | local $SIG{ALRM} = sub { |
76 | if ($killuser) { | |
77 | system @sudo, 'pkill', '-KILL', '-u', $killuser; | |
78 | } else { | |
79 | kill KILL => $ret | |
80 | } | |
81 | $tle = 1 | |
82 | }; | |
97358afc | 83 | alarm ($timeout || 10); |
c40a2dd0 MG |
84 | waitpid $ret, 0; |
85 | alarm 0; | |
65ab2558 MG |
86 | if (@sudo) { |
87 | $? = $? >> 8; | |
74ab8869 | 88 | $? = $? < 128 || $? > 128+32 ? ($? << 8) : $? - 128; |
65ab2558 | 89 | } |
c40a2dd0 MG |
90 | my $sig = $? & 127; |
91 | my $signame = sig_name $sig; | |
92 | exit !say TLE, "\nTime Limit Exceeded" if $tle; | |
93 | exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ'; | |
5738d1dc | 94 | exit !say DIED, "\nCrash (SIG$signame)" if $sig && ($signame ne 'PIPE' || $ARGV[0] !~ /prog/); |
c40a2dd0 MG |
95 | exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8; |
96 | exit !say AC, "\nAll OK"; | |
97 | } else { | |
98 | $^F = 50; | |
13b1661a MG |
99 | POSIX::close 2 unless $keep_stderr; |
100 | POSIX::close $_ for 0, 1, 3 .. $^F; | |
c40a2dd0 MG |
101 | for my $fdstring (@fds) { |
102 | my ($fd, $file) = split ' ', $fdstring, 2; | |
103 | open my $fh, $file or die $!; | |
104 | my $oldfd = fileno $fh; | |
105 | if ($oldfd != $fd) { | |
106 | POSIX::dup2 $oldfd, $fd or die $!; | |
107 | POSIX::close $oldfd or die $!; | |
108 | } | |
109 | } | |
e9f46104 | 110 | test_pipes if grep /tty|fifo/, @fds; |
d9ea4614 | 111 | my $nproc = $killuser ? 15 : 1; |
16de3659 | 112 | my $debug = $ENV{TEST_VERBOSE}; |
c40a2dd0 MG |
113 | %ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME}); |
114 | setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit; | |
115 | setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit; | |
42ce8ba9 | 116 | setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo; |
2ddfc547 MG |
117 | open my $adj, '>', '/proc/self/oom_score_adj'; |
118 | print $adj 900; | |
119 | close $adj; | |
1e5f2b8b | 120 | unshift @ARGV, @sudo; |
0418cda2 | 121 | kill USR1 => getppid; # Tell parent process that we're ready |
16de3659 | 122 | say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug; |
c40a2dd0 MG |
123 | exec @ARGV; |
124 | } | |
125 | ||
126 | 1; | |
69c25408 MG |
127 | __END__ |
128 | ||
129 | =encoding utf-8 | |
130 | ||
131 | =head1 NAME | |
132 | ||
133 | gruntmaster-exec - Gruntmaster 6000 executor | |
134 | ||
135 | =head1 SYNOPSIS | |
136 | ||
137 | gruntmaster-exec 20000000 111 echo 'Hello, world!' | |
138 | ||
139 | =head1 DESCRIPTION | |
140 | ||
141 | gruntmaster-exec is the script used by gruntmasterd to run programs. | |
142 | ||
143 | 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. | |
144 | ||
145 | 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. | |
146 | ||
147 | =head1 AUTHOR | |
148 | ||
149 | Marius Gavrilescu E<lt>marius@ieval.roE<gt> | |
150 | ||
151 | =head1 COPYRIGHT AND LICENSE | |
152 | ||
153 | Copyright (C) 2014 by Marius Gavrilescu | |
154 | ||
155 | This program is free software: you can redistribute it and/or modify | |
156 | it under the terms of the GNU Affero General Public License as published by | |
157 | the Free Software Foundation, either version 3 of the License, or | |
158 | (at your option) any later version. |