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