Split database-related work from process to another sub
[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
42ce8ba9 31my (@fds, $timeout, $mlimit, $olimit, $sudo);
1fe52cde
MG
32my $close = 1;
33
c40a2dd0
MG
34GetOptions(
35 "fd=s" => \@fds,
36 "timeout=f" => \$timeout,
37 "mlimit=i" => \$mlimit,
38 "olimit=i" => \$olimit,
1fe52cde 39 "close!" => \$close,
42ce8ba9 40 "sudo!" => \$sudo,
c40a2dd0
MG
41);
42
65ab2558
MG
43my $killuser = $ENV{GRUNTMASTER_KILL_USER};
44my @sudo;
42ce8ba9 45@sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo;
91c5f490 46undef $mlimit if @sudo; # sudo wants a lot of address space
65ab2558 47
c40a2dd0
MG
48my $ret = fork // die 'Cannot fork';
49if ($ret) {
50 my $tle;
65ab2558
MG
51 local $SIG{ALRM} = sub {
52 if ($killuser) {
53 system @sudo, 'pkill', '-KILL', '-u', $killuser;
54 } else {
55 kill KILL => $ret
56 }
57 $tle = 1
58 };
97358afc 59 alarm ($timeout || 10);
c40a2dd0
MG
60 waitpid $ret, 0;
61 alarm 0;
65ab2558
MG
62 if (@sudo) {
63 $? = $? >> 8;
74ab8869 64 $? = $? < 128 || $? > 128+32 ? ($? << 8) : $? - 128;
65ab2558 65 }
c40a2dd0
MG
66 my $sig = $? & 127;
67 my $signame = sig_name $sig;
68 exit !say TLE, "\nTime Limit Exceeded" if $tle;
69 exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ';
70 exit !say DIED, "\nCrash (SIG$signame)" if $sig && $signame ne 'PIPE';
71 exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8;
72 exit !say AC, "\nAll OK";
73} else {
74 $^F = 50;
1fe52cde
MG
75 if ($close) {
76 POSIX::close $_ for 0 .. $^F;
77 }
c40a2dd0
MG
78 for my $fdstring (@fds) {
79 my ($fd, $file) = split ' ', $fdstring, 2;
80 open my $fh, $file or die $!;
81 my $oldfd = fileno $fh;
82 if ($oldfd != $fd) {
83 POSIX::dup2 $oldfd, $fd or die $!;
84 POSIX::close $oldfd or die $!;
85 }
86 }
d9ea4614 87 my $nproc = $killuser ? 15 : 1;
16de3659 88 my $debug = $ENV{TEST_VERBOSE};
c40a2dd0
MG
89 %ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME});
90 setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
91 setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
42ce8ba9 92 setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo;
1e5f2b8b 93 unshift @ARGV, @sudo;
16de3659 94 say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug;
c40a2dd0
MG
95 exec @ARGV;
96}
97
981;
69c25408
MG
99__END__
100
101=encoding utf-8
102
103=head1 NAME
104
105gruntmaster-exec - Gruntmaster 6000 executor
106
107=head1 SYNOPSIS
108
109 gruntmaster-exec 20000000 111 echo 'Hello, world!'
110
111=head1 DESCRIPTION
112
113gruntmaster-exec is the script used by gruntmasterd to run programs.
114
115The 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.
116
117gruntmaster-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.
118
119=head1 AUTHOR
120
121Marius Gavrilescu E<lt>marius@ieval.roE<gt>
122
123=head1 COPYRIGHT AND LICENSE
124
125Copyright (C) 2014 by Marius Gavrilescu
126
127This program is free software: you can redistribute it and/or modify
128it under the terms of the GNU Affero General Public License as published by
129the Free Software Foundation, either version 3 of the License, or
130(at your option) any later version.
This page took 0.021335 seconds and 4 git commands to generate.