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