Copy input to Scaleway in Interactive.pm
[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
42sub test_pipe_read {
43 my $data = '';
44 sysread STDIN, $data, 4096 and syswrite STDOUT, "recvd\n" until $data =~ /done/;
45 syswrite STDOUT, 'done';
46}
47
48sub test_pipe_write {
49 my ($rin, $rout);
50 vec($rin, fileno STDIN, 1) = 1;
51 syswrite STDOUT, "data\n" until select $rout = $rin, undef, undef, 0.05;
52 syswrite STDOUT, 'done';
53 my $data = '';
54 sysread STDIN, $data, 4096 until $data =~ /done/;
55}
56
57sub test_pipes {
58 $ARGV[0] =~ /prog/ ? test_pipe_read : test_pipe_write
59}
60
61my @sudo;
62@sudo = (shellwords ($ENV{GRUNTMASTER_SUDO}), '--') if $ENV{GRUNTMASTER_SUDO} && $sudo;
63undef $mlimit if @sudo; # sudo wants a lot of address space
64
65my $ret = fork // die 'Cannot fork';
66if ($ret) {
67 my ($tle, $child_ready);
68 local $SIG{USR1} = sub { $child_ready = 1 };
69 sleep 3; # Wait for ready signal (SIGUSR1)
70 unless ($child_ready) {
71 kill KILL => $ret;
72 exit !say ERR, "\nNo response from gruntmaster-exec child";
73 }
74 local $SIG{ALRM} = sub {
75 kill KILL => $ret;
76 $tle = 1
77 };
78 alarm ($timeout || 10);
79 waitpid $ret, 0;
80 alarm 0;
81 if (@sudo) {
82 $? = $? >> 8;
83 $? = $? < 128 || $? > 128+32 ? ($? << 8) : $? - 128;
84 }
85 my $sig = $? & 127;
86 my $signame = sig_name $sig;
87 exit !say TLE, "\nTime Limit Exceeded" if $tle;
88 exit !say OLE, "\nOutput Limit Exceeded" if $sig && $signame eq 'XFSZ';
89 exit !say DIED, "\nCrash (SIG$signame)" if $sig && ($signame ne 'PIPE' || $ARGV[0] !~ /prog/);
90 exit !say NZX, "\nNon-zero exit status: " . ($? >> 8) if $? >> 8;
91 exit !say AC, "\nAll OK";
92} else {
93 $^F = 50;
94 POSIX::close 2 unless $keep_stderr;
95 POSIX::close $_ for 0, 1, 3 .. $^F;
96 for my $fdstring (@fds) {
97 my ($fd, $file) = split ' ', $fdstring, 2;
98 open my $fh, $file or die $!;
99 my $oldfd = fileno $fh;
100 if ($oldfd != $fd) {
101 POSIX::dup2 $oldfd, $fd or die $!;
102 POSIX::close $oldfd or die $!;
103 }
104 }
105 test_pipes if grep /tty|fifo/, @fds;
106 my $nproc = 15;
107 my $debug = $ENV{TEST_VERBOSE};
108 %ENV = (ONLINE_JUDGE => 1, PATH => $ENV{PATH}, HOME => $ENV{HOME});
109 setrlimit RLIMIT_AS, $mlimit, $mlimit or die $! if $mlimit;
110 setrlimit RLIMIT_FSIZE, $olimit, $olimit or die $! if $olimit;
111 setrlimit RLIMIT_NPROC, $nproc, $nproc or die $! if $sudo;
112 open my $adj, '>', '/proc/self/oom_score_adj';
113 print $adj 900;
114 close $adj;
115 unshift @ARGV, @sudo;
116 kill USR1 => getppid; # Tell parent process that we're ready
117 say STDERR "Executing: ", join ' ', map { "'$_'" } @ARGV if $debug;
118 exec @ARGV;
119}
120
1211;
122__END__
123
124=encoding utf-8
125
126=head1 NAME
127
128gruntmaster-exec - Gruntmaster 6000 executor
129
130=head1 SYNOPSIS
131
132 gruntmaster-exec 20000000 111 echo 'Hello, world!'
133
134=head1 DESCRIPTION
135
136gruntmaster-exec is the script used by gruntmasterd to run programs.
137
138The 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.
139
140gruntmaster-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.
141
142=head1 AUTHOR
143
144Marius Gavrilescu E<lt>marius@ieval.roE<gt>
145
146=head1 COPYRIGHT AND LICENSE
147
148Copyright (C) 2014 by Marius Gavrilescu
149
150This program is free software: you can redistribute it and/or modify
151it under the terms of the GNU Affero General Public License as published by
152the Free Software Foundation, either version 3 of the License, or
153(at your option) any later version.
This page took 0.008691 seconds and 4 git commands to generate.