Fix previous commit
[gruntmaster-daemon.git] / lib / Gruntmaster / Daemon / Format / CPP.pm
CommitLineData
5c5cd38a
MG
1package Gruntmaster::Daemon::Format::CPP;
2
3use 5.014000;
4use strict;
5use warnings;
6
5c5cd38a
MG
7use POSIX qw//;
8use File::Basename qw/fileparse/;
9use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/;
10use Log::Log4perl qw/get_logger/;
11use Time::HiRes qw/alarm/;
12use List::MoreUtils qw/natatime/;
13use IPC::Signal qw/sig_name sig_num/;
14use IPC::Open3 qw/open3/;
15use File::Spec::Functions qw/devnull/;
16use Fcntl qw/F_GETFD F_SETFD FD_CLOEXEC/;
17
18our $VERSION = '0.001';
19
20##################################################
21
22sub prepare{
23 my $name = $_[0];
24 my $basename = fileparse $name, qr/\.[^.]*/;
25 get_logger->trace("Preparing file $name...");
26
27 open my $devnull, devnull;
da905f9e
MG
28 open my $errors, '>>compile-error';
29 my $ret = open3 $devnull, $errors, undef, 'gruntmaster-compile', CPP => $basename, $name;
30 waitpid $ret, 0;
5c5cd38a
MG
31 close $devnull;
32 close $errors;
33 die 'Compile error' if $?
34}
35
36sub run{
37 my ($name, %args) = @_;
38 my $basename = fileparse $name, qr/\.[^.]*/;
39 my $ret = fork // die 'Cannot fork';
40 if ($ret) {
41 my $tle;
42 local $SIG{ALRM} = sub { kill KILL => $ret; $tle = 1};
43 alarm $args{timeout} if exists $args{timeout};
44 wait;
45 alarm 0;
46 my $sig = $? & 127;
47 my $signame = sig_name $sig;
48 die [TLE, "Time Limit Exceeded"] if $tle;
49 die [OLE, 'Output Limit Exceeded'] if $sig && $signame eq 'XFSZ';
50 die [DIED, "Crash (SIG$signame)"] if $sig;
51 die [NZX, "Non-zero exit status: " . ($? >> 8)] if $?;
52 } else {
5c5cd38a 53 my @fds = exists $args{fds} ? @{$args{fds}} : ();
fe185d88 54 $^F = 50;
da905f9e 55 POSIX::close $_ for 0 .. $^F;
5c5cd38a
MG
56 my $it = natatime 2, @fds;
57 while (my ($fd, $file) = $it->()) {
58 open my $fh, $file or die $!;
59 my $oldfd = fileno $fh;
60 if ($oldfd != $fd) {
61 POSIX::dup2 $oldfd, $fd or die $!;
62 POSIX::close $oldfd or die $!;
63 }
64 }
da905f9e 65 exec 'gruntmaster-exec', $args{mlimit} // 0, $args{olimit} // 0, "./$basename", exists $args{args} ? @{$args{args}} : ();
5c5cd38a
MG
66 }
67}
68
691
This page took 0.013785 seconds and 4 git commands to generate.