Build/PPK/Exec.pm untested

filter: Statementstmt 0.0% 0 / 95 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 226 CC 30 SCAR 63.7
CC
30
Cov
0%
run
812.0
call
12.0
lazyrun
2.0
CRAP 584.3
1 package Build::PPK::Exec;
2  
3 # Copyright (c) 2018, cPanel, L.L.C.
4 # All rights reserved.
5 # http://cpanel.net/
6 #
7 # This is free software; you can redistribute it and/or modify it under the same
8 # terms as Perl itself. See the LICENSE file for further details.
9  
10 use strict;
11 use warnings;
12  
13 use POSIX ();
14 use Fcntl ();
15  
16 sub run {
17 my ( $class, $argv, %opts ) = @_;
18  
19 my ( $out, $out_i ); # Standard output pipe pair
20 my ( $err, $err_i ); # Standard error pipe pair
21  
22 #
23 # Create pipes for standard output or standard error, if either are indicated
24 # by the current options.
25 #
26 if ( $opts{'capture_stdout'} ) {
27 pipe( $out, $out_i ) or die("Unable to pipe() for stdout: $!");
28 }
29  
30 if ( $opts{'capture_stderr'} ) {
31 pipe( $err, $err_i ) or die("Unable to pipe() for stderr: $!");
32 }
33  
34 if ( $opts{'die_on_stderr'} ) {
35 die("Option 'die_on_stderr' requires 'capture_stderr'") unless $opts{'capture_stderr'};
36 }
37  
38 my $pid = fork();
39  
40 if ( !defined $pid ) {
41 die("Unable to fork(): $!");
42 }
43 elsif ( $pid == 0 ) {
44 if ( $opts{'close_stdin'} ) {
45 close STDIN;
46 }
47 else {
48 open( STDIN, '<', '/dev/null' ) or die("Unable to redirect stdin to /dev/null: $!");
49 }
50  
51 #
52 # If standard output or error are to be captured by the parent process,
53 # then redirect them to their respective pipe inputs. If they are to be
54 # suppressed, then simply close them. Otherwise, let standard output and
55 # error leak out.
56 #
57  
58 if ( $opts{'capture_stdout'} ) {
59 close $out;
60 POSIX::dup2( fileno($out_i), fileno(STDOUT) ) or die("Unable to redirect standard output to pipe: $!");
61 }
62 elsif ( $opts{'suppress_stdout'} ) {
63 open( STDOUT, '>', '/dev/null' ) or die("Unable to redirect stdout to /dev/null: $!");
64 }
65  
66 if ( $opts{'capture_stderr'} ) {
67 close $err;
68 POSIX::dup2( fileno($err_i), fileno(STDERR) ) or die("Unable to redirect standard error to pipe: $!");
69 }
70 elsif ( $opts{'suppress_stderr'} ) {
71 open( STDERR, '>', '/dev/null' ) or die("Unable to redirect stderr to /dev/null: $!");
72 }
73  
74 exec( @{$argv} ) or die("Unable to exec() $argv->[0]: $!");
75 }
76  
77 my $stdout;
78 my $stderr;
79  
80 my $set = '';
81  
82 #
83 # If we are to capture standard output or error, then close the input ends
84 # of each requisite pipe and prepare to select() for data ready to be read().
85 #
86 if ( $opts{'capture_stdout'} ) {
87 close $out_i;
88 vec( $set, fileno($out), 1 ) = 1;
89 }
90  
91 if ( $opts{'capture_stderr'} ) {
92 close $err_i;
93 vec( $set, fileno($err), 1 ) = 1;
94 }
95  
96 while ( $set && select( my $ready = $set, undef, undef, undef ) ) {
97 my $found = 0;
98  
99 if ( $opts{'capture_stdout'} && vec( $ready, fileno($out), 1 ) ) {
100 if ( sysread( $out, my $buf, 512 ) ) {
101 print $buf if $opts{'print_stdout'};
102  
103 $stdout .= $buf;
104 $found++;
105 }
106 else {
107 vec( $set, fileno($out), 1 ) = 0;
108 }
109 }
110  
111 if ( $opts{'capture_stderr'} && vec( $ready, fileno($err), 1 ) ) {
112 if ( sysread( $err, my $buf, 512 ) ) {
113 print STDERR $buf if $opts{'print_stderr'};
114  
115 $stderr .= $buf;
116 $found++;
117 }
118 else {
119 vec( $set, fileno($err), 1 ) = 0;
120 }
121 }
122  
123 last unless $found;
124 }
125  
126 waitpid( $pid, 0 ) or die("Unable to waitpid(): $!");
127  
128 my $status = $? >> 8;
129  
130 #
131 # If we are to die on a nonzero exit status, and a nonzero exit status was
132 # returned by the child, then either die with messages written to standard
133 # error by the child, or a more generic message if standard error was not
134 # chosen to be captured by the caller.
135 #
136 if ( $opts{'die_on_nonzero'} && $status != 0 ) {
137 if ( $opts{'capture_stderr'} && defined $stderr ) {
138 chomp $stderr;
139 die("Nonzero exit status $status from $argv->[0]: $stderr");
140 }
141 else {
142 die("Program $argv->[0] exited with nonzero status $status");
143 }
144 }
145  
146 #
147 # Even if the program exited with a zero exit status, die if any errors were
148 # captured, if this behavior is indicated by the current options.
149 #
150 if ( $opts{'die_on_stderr'} && defined $stderr ) {
151 die($stderr);
152 }
153  
154 #
155 # If standard output was captured, then return it to the caller. Otherwise,
156 # return the child process exit status.
157 #
158 if ( $opts{'capture_stdout'} ) {
159 chomp $stdout if $stdout;
160 return $stdout;
161 }
162  
163 #
164 # If standard error was indicated for capture by the caller, then set $@ to
165 # the data captured, if any.
166 #
167 if ( $opts{'capture_stderr'} ) {
168 chomp $stderr if $stderr;
169 $@ = $stderr;
170 }
171  
172 return $status;
173 }
174  
175 sub lazyrun {
176 my ( $class, @args ) = @_;
177  
178 return $class->run(
179 \@args,
180 'capture_stdout' => 1,
181 'suppress_stderr' => 1
182 );
183 }
184  
185 sub call {
186 my ( $class, $verbosity, $command, @args ) = @_;
187  
188 my %opts = ( 'capture_stderr' => 1 );
189  
190 if ( $verbosity > 0 ) {
191 $opts{'print_stderr'} = 1;
192 }
193  
194 if ( $verbosity < 2 ) {
195 $opts{'suppress_stdout'} = 1;
196 }
197  
198 return $class->run( [ $command, @args ], %opts );
199 }
200  
201 sub silent {
202 my ( $class, $command, @args ) = @_;
203 return $class->call( 0, $command, @args );
204 }
205  
206 sub quiet {
207 my ( $class, $command, @args ) = @_;
208 return $class->call( 1, $command, @args );
209 }
210  
211 sub verbose {
212 my ( $class, $command, @args ) = @_;
213 return $class->call( 2, $command, @args );
214 }
215  
216 1;