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