Drop privileges in gruntmaster-exec if possible
[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/;
9577371b 7no if $] > 5.017011, warnings => 'experimental::smartmatch';
ddceb393
MG
8
9use POSIX qw//;
10use File::Basename qw/fileparse/;
c40a2dd0 11use File::Slurp qw/read_file write_file/;
ddceb393 12use Gruntmaster::Daemon::Constants qw/TLE OLE DIED NZX/;
ddceb393
MG
13use Time::HiRes qw/alarm/;
14use List::MoreUtils qw/natatime/;
f5e29130 15use Log::Log4perl qw/get_logger/;
ddceb393 16use IPC::Signal qw/sig_name sig_num/;
c40a2dd0 17use Try::Tiny;
ddceb393 18
3e7fd903 19our $VERSION = "5999.000_004";
ad77b7d3 20our @EXPORT_OK = qw/prepare_files/;
ddceb393
MG
21
22##################################################
23
c40a2dd0
MG
24sub execlist {
25 my (@args) = @_;
26 my $ret = fork // die 'Cannot fork';
27 if ($ret) {
28 waitpid $ret, 0;
29 die "gruntmaster-exec died\n" if -z 'exec-result';
30 my ($excode, $exmsg) = read_file 'exec-result';
31 unlink 'exec-result';
32 chomp ($excode, $exmsg);
33 die [$excode, $exmsg] if $excode > 0;
34 } else {
35 open STDOUT, '>exec-result';
36 exec 'gruntmaster-exec', @args;
37 }
38}
39
ddceb393
MG
40sub command_and_args{
41 my ($format, $basename) = @_;
9577371b
MG
42
43 given($format) {
5a4392d5 44 "./$basename" when [qw/C CPP GCCGO GOLANG HASKELL PASCAL/];
9577371b 45 "./$basename.exe" when 'MONO';
cdd811c0
MG
46 java => $basename when 'JAVA';
47 perl => $basename when 'PERL';
48 python => $basename when 'PYTHON';
c40a2dd0 49 default { die "Don't know how to execute format $format\n" }
9577371b 50 }
ddceb393
MG
51}
52
53sub mkrun{
54 my $format = shift;
55 sub{
d16a999c 56 local *__ANON__ = 'mkrun_runner';
ddceb393 57 my ($name, %args) = @_;
d16a999c 58 get_logger->trace("Running $name...");
ddceb393 59 my $basename = fileparse $name, qr/\.[^.]*/;
c40a2dd0
MG
60 my @args;
61 push @args, '--timeout', $args{timeout} if $args{timeout};
62 push @args, '--mlimit', $args{mlimit} if $args{mlimit};
63 push @args, '--olimit', $args{olimit} if $args{olimit};
64 my @fds = exists $args{fds} ? @{$args{fds}} : ();
65 my $it = natatime 2, @fds;
66 while (my ($fd, $file) = $it->()) {
67 push @args, "--fd=$fd $file";
ddceb393 68 }
c40a2dd0 69 execlist @args, command_and_args($format, $basename);
ddceb393
MG
70 }
71}
72
ad77b7d3
MG
73sub prepare{
74 my ($name, $format) = @_;
ad77b7d3
MG
75 get_logger->trace("Preparing file $name...");
76
c40a2dd0
MG
77 try {
78 execlist '--fd=1 >>errors', '--fd=2 >>errors', 'gruntmaster-compile', $format, $name;
79 } catch {
80 die "Compile error\n"
81 } finally {
82 $Gruntmaster::Daemon::errors .= read_file 'errors';
83 $Gruntmaster::Daemon::errors .= "\n";
84 unlink 'errors';
85 };
ad77b7d3
MG
86}
87
88sub prepare_files{
89 my $meta = shift;
90
759d3ca5 91 for my $file (values %{$meta->{files}}) {
ad77b7d3
MG
92 my ($format, $name, $content) = @{$file}{qw/format name content/};
93
94 $file->{run} = mkrun($format);
95 write_file $name, $content;
96 prepare $name, $format;
97 }
98}
99
bc372959
MG
1001;
101__END__
102
103=encoding utf-8
104
105=head1 NAME
106
107Gruntmaster::Daemon::Format - Utility functions for handling source files
108
109=head1 SYNOPSIS
110
111 use Gruntmaster::Daemon::Format qw/prepare_files/;
112 prepare_files { files => {
113 prog => {
114 name => 'prog.pl',
115 format => 'PERL',
116 content => 'print "Hello, world!"'
117 },
118 ver => {
119 name => 'ver.cpp',
120 format => 'CPP',
121 content => ...
122 },
123 }};
124
125=head1 DESCRIPTION
126
127Gruntmaster::Daemon::Format exports utility functions for handling source files.
128
129=over
130
131=item B<prepare_files> I<$meta>
132
133Compiles all the source files in C<< $meta->{files} >>.
134
135=back
136
137=head1 AUTHOR
138
139Marius Gavrilescu E<lt>marius@ieval.roE<gt>
140
141=head1 COPYRIGHT AND LICENSE
142
143Copyright (C) 2014 by Marius Gavrilescu
144
145This library is free software: you can redistribute it and/or modify
146it under the terms of the GNU Affero General Public License as published by
147the Free Software Foundation, either version 3 of the License, or
148(at your option) any later version.
149
150
151=cut
This page took 0.052303 seconds and 4 git commands to generate.