Add perlcritic test and make code comply
[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
22 use BSD::Resource qw/setrlimit RLIMIT_AS RLIMIT_FSIZE/;
23 use IPC::Signal qw/sig_name sig_num/;
24 use sigtrap qw/XFSZ/;
25
26 use Getopt::Long;
27 use POSIX qw//;
28 use Time::HiRes qw/alarm/;
29
30 my (@fds, $timeout, $mlimit, $olimit);
31 GetOptions(
32 "fd=s" => \@fds,
33 "timeout=f" => \$timeout,
34 "mlimit=i" => \$mlimit,
35 "olimit=i" => \$olimit,
36 );
37
38 my $ret = fork // die 'Cannot fork';
39 if ($ret) {
40 my $tle;
41 local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1};
42 alarm ($timeout || 5);
43 waitpid $ret, 0;
44 alarm 0;
45 my $sig = $? & 127;
46 my $signame = sig_name $sig;
47 exit !say TLE, "\nTime Limit Exceeded" if $tle;
48 exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ';
49 exit !say DIED, "\nCrash (SIG$signame)" if $sig && $signame ne 'PIPE';
50 exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8;
51 exit !say AC, "\nAll OK";
52 } else {
53 $^F = 50;
54 POSIX::close $_ for 0 .. $^F;
55 for my $fdstring (@fds) {
56 my ($fd, $file) = split ' ', $fdstring, 2;
57 open my $fh, $file or die $!;
58 my $oldfd = fileno $fh;
59 if ($oldfd != $fd) {
60 POSIX::dup2 $oldfd, $fd or die $!;
61 POSIX::close $oldfd or die $!;
62 }
63 }
64 %ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME});
65 setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
66 setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
67 POSIX::setgid 65534; # Set group id to nogroup
68 POSIX::setuid 65534; # Set user id to nobody
69 exec @ARGV;
70 }
71
72 1;
73 __END__
74
75 =encoding utf-8
76
77 =head1 NAME
78
79 gruntmaster-exec - Gruntmaster 6000 executor
80
81 =head1 SYNOPSIS
82
83 gruntmaster-exec 20000000 111 echo 'Hello, world!'
84
85 =head1 DESCRIPTION
86
87 gruntmaster-exec is the script used by gruntmasterd to run programs.
88
89 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.
90
91 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.
92
93 =head1 AUTHOR
94
95 Marius Gavrilescu E<lt>marius@ieval.roE<gt>
96
97 =head1 COPYRIGHT AND LICENSE
98
99 Copyright (C) 2014 by Marius Gavrilescu
100
101 This program is free software: you can redistribute it and/or modify
102 it under the terms of the GNU Affero General Public License as published by
103 the Free Software Foundation, either version 3 of the License, or
104 (at your option) any later version.
This page took 0.025449 seconds and 4 git commands to generate.