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