Dpkg/IPC.pm

filter: Statementstmt 90.1% 147 / 163 Branchbran 73.4% 94 / 128 Conditioncond 52.0% 26 / 50 MC/DCmcdc 6.9% 2 / 29 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 73.4% 280 / 381 CC 91 SCAR 40.3
CC
91
Cov
78%
_check_opts
77.5
spawn
60.9
wait_child
14.9
CRAP 56.1
« Dpkg/Index.pm ↑ Summary Dpkg/Vendor/PureOS.pm »
1 # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
2 # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
3 # Copyright © 2008-2010, 2012-2015 Guillem Jover <guillem@debian.org>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <https://www.gnu.org/licenses/>.
17  
18 =encoding utf8
19  
20 =head1 NAME
21  
22 Dpkg::IPC - helper functions for IPC
23  
24 =head1 DESCRIPTION
25  
26 Dpkg::IPC offers helper functions to allow you to execute
27 other programs in an easy, yet flexible way, while hiding
28 all the gory details of IPC (Inter-Process Communication)
29 from you.
30  
31 =cut
32  
33 package Dpkg::IPC 1.03;
34  
35 556 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 554 our @EXPORT = qw(
38 spawn
39 wait_child
40 );
41  
42 556 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 556 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 556 ▶use Dpkg::ErrorHandling;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 556 ▶use Dpkg::Gettext;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 =head1 FUNCTIONS
49  
50 =over 4
51  
52 =item $pid = spawn(%opts)
53  
54 Creates a child process and executes another program in it.
55 The arguments are interpreted as a hash of options, specifying
56 how to handle the in and output of the program to execute.
57 Returns the pid of the child process (unless the wait_child
58 option was given).
59  
60 Any error will cause the function to exit with one of the
61 L<Dpkg::ErrorHandling> functions.
62  
63 Options:
64  
65 =over 4
66  
67 =item B<exec> (required)
68  
69 Can be either a scalar, i.e. the name of the program to be
70 executed, or an array reference, i.e. the name of the program
71 plus additional arguments. Note that the program will never be
72 executed via the shell, so you can't specify additional arguments
73 in the scalar string and you can't use any shell facilities like
74 globbing.
75  
76 =item B<from_file>, B<to_file>, B<error_to_file>
77  
78 Filename as scalar. Standard input/output/error of the
79 child process will be redirected to the file specified.
80  
81 =item B<from_handle>, B<to_handle>, B<error_to_handle>
82  
83 Filehandle. Standard input/output/error of the child process will be
84 dup'ed from the handle.
85  
86 =item B<from_pipe>, B<to_pipe>, B<error_to_pipe>
87  
88 Scalar reference or object based on L<IO::Handle>. A pipe will be opened for
89 each of the two options and either the reading (C<to_pipe> and
90 C<error_to_pipe>) or the writing end (C<from_pipe>) will be returned in
91 the referenced scalar. Standard input/output/error of the child process
92 will be dup'ed to the other ends of the pipes.
93  
94 =item B<from_string>, B<to_string>, B<error_to_string>
95  
96 Scalar reference. Standard input/output/error of the child
97 process will be redirected to the string given as reference. Note
98 that it wouldn't be strictly necessary to use a scalar reference
99 for C<from_string>, as the string is not modified in any way. This was
100 chosen only for reasons of symmetry with C<to_string> and
101 C<error_to_string>. C<to_string> and C<error_to_string> imply the
102 C<wait_child> option.
103  
104 =item B<wait_child>
105  
106 Scalar. If containing a true value, wait_child() will be called before
107 returning. The return value of spawn() will be a true value, not the pid.
108  
109 =item B<no_check>
110  
111 Scalar. Option of the wait_child() call.
112  
113 =item B<nocheck>
114  
115 Deprecated alias for no_check.
116 It emits a deprecation warning, but if it is required for backwards
117 compatibility it can be passed alongside B<no_check> and the warning
118 will not be emitted.
119  
120 =item B<timeout>
121  
122 Scalar. Option of the wait_child() call.
123  
124 =item B<chdir>
125  
126 Scalar. The child process will chdir in the indicated directory before
127 calling exec.
128  
129 =item B<env>
130  
131 Hash reference. The child process will populate %ENV with the items of the
132 hash before calling exec. This allows exporting environment variables.
133  
134 =item B<delete_env>
135  
136 Array reference. The child process will remove all environment variables
137 listed in the array before calling exec.
138  
139 =item B<sig>
140  
141 Hash reference. The child process will populate %SIG with the items of the
142 hash before calling exec. This allows setting signal dispositions.
143  
144 =item B<delete_sig>
145  
146 Array reference. The child process will reset all signals listed in the
147 array to their default dispositions before calling exec.
148  
149 =back
150  
151 =cut
152  
153 133958 ▶sub _check_opts {
Subroutine _check_opts: called CC=29 SCAR=43.5
154 133958 my (%opts) = @_;
155  
156 croak 'exec parameter is mandatory in spawn()'
157 133958 ▶ unless $opts{exec};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts{"exec"} 0 133958 133958
158  
159 133958 my $to = my $error_to = my $from = 0;
160 133958 foreach my $thing (qw(file handle string pipe)) {
161 535832 ▶ $to++ if $opts{"to_$thing"};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"to_$thing"} 52 535780 535832
162 535832 ▶ $error_to++ if $opts{"error_to_$thing"};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"error_to_$thing"} 133924 401908 535832
163 535832 ▶ $from++ if $opts{"from_$thing"};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"from_$thing"} 40 535792 535832
164 }
165 133958 ▶ croak 'not more than one of to_* parameters is allowed'
Branch: 1 branch
BranchTrueFalseTotal
if $to > 1 0 133958 133958
166 if $to > 1;
167 133958 ▶ croak 'not more than one of error_to_* parameters is allowed'
Branch: 1 branch
BranchTrueFalseTotal
if $error_to > 1 0 133958 133958
168 if $error_to > 1;
169 133958 ▶ croak 'not more than one of from_* parameters is allowed'
Branch: 1 branch
BranchTrueFalseTotal
if $from > 1 0 133958 133958
170 if $from > 1;
171  
172 133958 foreach my $param (qw(to_string error_to_string from_string)) {
173 401874 ▶ if (exists $opts{$param} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR") 0 401874 401874
Condition50%3/6
exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR"
!ll&&!rl&&r
2679181339560
not ref $opts{$param} or ref $opts{$param} ne "SCALAR"
l!l&&r!l&&!r
00133956
MC/DC0%0/3
exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR"
exists $opts{$param} not ref $opts{$param} ref $opts{$param} ne "SCALAR"
Truth table50%2/4
A and not(B) or C
A= exists $opts{$param}B= not ref $opts{$param}C= ref $opts{$param} ne "SCALAR"
ABCresult
0XX0
1000
11X1
1011
174 (not ref $opts{$param} or ref $opts{$param} ne 'SCALAR')) {
175 0 croak "parameter $param must be a scalar reference";
176 }
177 }
178  
179 133958 foreach my $param (qw(to_pipe error_to_pipe from_pipe)) {
180 401874 ▶ if (exists $opts{$param} and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR" && !$opts{$param}->isa("IO::Handle")) 0 401874 401874
Condition44%4/9
exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR" && !$opts{$param}->isa("IO::Handle")
!ll&&!rl&&r
401852220
not ref $opts{$param} or ref $opts{$param} ne "SCALAR" and not $opts{$param}->isa("IO::Handle")
l!l&&r!l&&!r
0022
ref $opts{$param} ne "SCALAR" and not $opts{$param}->isa("IO::Handle")
!ll&&!rl&&r
2200
MC/DC0%0/4
exists $opts{$param} and !ref($opts{$param}) || ref $opts{$param} ne "SCALAR" && !$opts{$param}->isa("IO::Handle")
exists $opts{$param} not ref $opts{$param} ref $opts{$param} ne "SCALAR" not $opts{$param}->isa("IO::Handle")
Truth table40%2/5
A and not(B) or C and not(D)
A= exists $opts{$param}B= not ref $opts{$param}C= ref $opts{$param} ne "SCALAR"D= not $opts{$param}->isa("IO::Handle")
ABCDresult
0XXX0
100X0
10100
11XX1
10111
181 (not ref $opts{$param} or (ref $opts{$param} ne 'SCALAR' and
182 not $opts{$param}->isa('IO::Handle')))) {
183 0 croak "parameter $param must be a scalar reference or " .
184 'an IO::Handle object';
185 }
186 }
187  
188 133958 ▶ if (exists $opts{timeout} and defined($opts{timeout}) and
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"timeout"} and defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/u) 0 133958 133958
Condition66%4/6
exists $opts{"timeout"} and defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/u
!ll&&!rl&&r
133942160
exists $opts{"timeout"} and defined $opts{"timeout"}
!ll&&!rl&&r
133942016
MC/DC0%0/3
exists $opts{"timeout"} and defined $opts{"timeout"} and not $opts{"timeout"} =~ /^\d+$/u
exists $opts{"timeout"} defined $opts{"timeout"} not $opts{"timeout"} =~ /^\d+$/u
Truth table50%2/4
A and B and not(C)
A= exists $opts{"timeout"}B= defined $opts{"timeout"}C= not $opts{"timeout"} =~ /^\d+$/u
ABCresult
0XX0
10X0
1100
1111
189 $opts{timeout} !~ /^\d+$/) {
190 0 croak 'parameter timeout must be an integer';
191 }
192  
193 133958 ▶ if (exists $opts{env} and ref($opts{env}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"env"} and ref $opts{"env"} ne "HASH") 0 133958 133958
Condition66%2/3
exists $opts{"env"} and ref $opts{"env"} ne "HASH"
!ll&&!rl&&r
13395080
MC/DC0%0/2
exists $opts{"env"} and ref $opts{"env"} ne "HASH"
exists $opts{"env"} ref $opts{"env"} ne "HASH"
Truth table66%2/3
A and B
A= exists $opts{"env"}B= ref $opts{"env"} ne "HASH"
ABresult
0X0
100
111
194 0 croak 'parameter env must be a hash reference';
195 }
196  
197 133958 ▶ if (exists $opts{delete_env} and ref($opts{delete_env}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"delete_env"} and ref $opts{"delete_env"} ne "ARRAY") 0 133958 133958
Condition66%2/3
exists $opts{"delete_env"} and ref $opts{"delete_env"} ne "ARRAY"
!ll&&!rl&&r
133948100
MC/DC0%0/2
exists $opts{"delete_env"} and ref $opts{"delete_env"} ne "ARRAY"
exists $opts{"delete_env"} ref $opts{"delete_env"} ne "ARRAY"
Truth table66%2/3
A and B
A= exists $opts{"delete_env"}B= ref $opts{"delete_env"} ne "ARRAY"
ABresult
0X0
100
111
198 0 croak 'parameter delete_env must be an array reference';
199 }
200  
201 133958 ▶ if (exists $opts{sig} and ref($opts{sig}) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"sig"} and ref $opts{"sig"} ne "HASH") 0 133958 133958
Condition33%1/3
exists $opts{"sig"} and ref $opts{"sig"} ne "HASH"
!ll&&!rl&&r
13395800
MC/DC0%0/2
exists $opts{"sig"} and ref $opts{"sig"} ne "HASH"
exists $opts{"sig"} ref $opts{"sig"} ne "HASH"
Truth table33%1/3
A and B
A= exists $opts{"sig"}B= ref $opts{"sig"} ne "HASH"
ABresult
0X0
100
111
202 0 croak 'parameter sig must be a hash reference';
203 }
204  
205 133958 ▶ if (exists $opts{delete_sig} and ref($opts{delete_sig}) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"delete_sig"} and ref $opts{"delete_sig"} ne "ARRAY") 0 133958 133958
Condition33%1/3
exists $opts{"delete_sig"} and ref $opts{"delete_sig"} ne "ARRAY"
!ll&&!rl&&r
13395800
MC/DC0%0/2
exists $opts{"delete_sig"} and ref $opts{"delete_sig"} ne "ARRAY"
exists $opts{"delete_sig"} ref $opts{"delete_sig"} ne "ARRAY"
Truth table33%1/3
A and B
A= exists $opts{"delete_sig"}B= ref $opts{"delete_sig"} ne "ARRAY"
ABresult
0X0
100
111
206 0 croak 'parameter delete_sig must be an array reference';
207 }
208  
209 133958 return %opts;
210 }
211  
212 133958 ▶sub spawn {
Subroutine spawn: called CC=51 SCAR=41.1
Pod documented
213 133958 my (%opts) = @_;
214 133958 my @prog;
215  
216 133958 _check_opts(%opts);
217 133958 ▶ $opts{close_in_child} //= [];
Condition50%1/2
$opts{"close_in_child"} //= []
l!l
0133958
MC/DC0%0/1
$opts{"close_in_child"} //= []
$opts{"close_in_child"}
Truth table50%1/2
A
A= $opts{"close_in_child"}
Aresult
11
00
218 133958 ▶ if (ref($opts{exec}) =~ /ARRAY/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($opts{'exec'}) =~ /ARRAY/u) { } 133946 12 133958
elsif (not ref $opts{'exec'}) { } 12 0 12
219 133946 push @prog, @{$opts{exec}};
220 } elsif (not ref($opts{exec})) {
221 12 push @prog, $opts{exec};
222 } else {
223 0 croak 'invalid exec parameter in spawn()';
224 }
225 133958 my ($from_string_pipe, $to_string_pipe, $error_to_string_pipe);
226 133958 ▶ if ($opts{to_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"to_string"}) 27 133931 133958
227 27 $opts{to_pipe} = \$to_string_pipe;
228 27 $opts{wait_child} = 1;
229 }
230 133958 ▶ if ($opts{error_to_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"error_to_string"}) 133924 34 133958
231 133924 $opts{error_to_pipe} = \$error_to_string_pipe;
232 133924 $opts{wait_child} = 1;
233 }
234 133958 ▶ if ($opts{from_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"from_string"}) 5 133953 133958
235 5 $opts{from_pipe} = \$from_string_pipe;
236 }
237 # Create pipes if needed.
238 133958 my ($input_pipe, $output_pipe, $error_pipe);
239 133958 ▶ if ($opts{from_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"from_pipe"}) 18 133940 133958
240 18 ▶ pipe($opts{from_handle}, $input_pipe)
Branch: 1 branch
BranchTrueFalseTotal
pipe $opts{"from_handle"}, $input_pipe or syserr(g_("cannot create pipe for %s"), join($", @prog)) 18 0 18
241 or syserr(g_('cannot create pipe for %s'), "@prog");
242 18 ${$opts{from_pipe}} = $input_pipe;
243 18 push @{$opts{close_in_child}}, $input_pipe;
244 }
245 133958 ▶ if ($opts{to_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"to_pipe"}) 36 133922 133958
246 pipe($output_pipe, $opts{to_handle})
247 36 ▶ or syserr(g_('cannot create pipe for %s'), "@prog");
Branch: 1 branch
BranchTrueFalseTotal
pipe $output_pipe, $opts{"to_handle"} or syserr(g_("cannot create pipe for %s"), join($", @prog)) 36 0 36
248 36 ${$opts{to_pipe}} = $output_pipe;
249 36 push @{$opts{close_in_child}}, $output_pipe;
250 }
251 133958 ▶ if ($opts{error_to_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"error_to_pipe"}) 133924 34 133958
252 pipe($error_pipe, $opts{error_to_handle})
253 133924 ▶ or syserr(g_('cannot create pipe for %s'), "@prog");
Branch: 1 branch
BranchTrueFalseTotal
pipe $error_pipe, $opts{"error_to_handle"} or syserr(g_("cannot create pipe for %s"), join($", @prog)) 133924 0 133924
254 133924 ${$opts{error_to_pipe}} = $error_pipe;
255 133924 push @{$opts{close_in_child}}, $error_pipe;
256 }
257 # Fork and exec.
258 133958 my $pid = fork();
259 133958 ▶ syserr(g_('cannot create child process for %s'), "@prog")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 133958 133958
260 unless defined $pid;
261 133958 ▶ if (not $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 535 133423 133958
262 # Define environment variables.
263 535 ▶ if ($opts{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"env"}) 4 531 535
264 4 foreach (keys %{$opts{env}}) {
265 8 $ENV{$_} = $opts{env}{$_};
266 }
267 }
268 535 ▶ if ($opts{delete_env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"delete_env"}) 5 530 535
269 5 delete $ENV{$_} foreach (@{$opts{delete_env}});
270 }
271 # Define signal dispositions.
272 535 ▶ if ($opts{sig}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"sig"}) 0 535 535
273 0 foreach (keys %{$opts{sig}}) {
274 0 $SIG{$_} = $opts{sig}{$_};
275 }
276 }
277 535 ▶ if ($opts{delete_sig}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"delete_sig"}) 0 535 535
278 0 delete $SIG{$_} foreach (@{$opts{delete_sig}});
279 }
280 # Change the current directory.
281 535 ▶ if ($opts{chdir}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"chdir"}) 5 530 535
282 chdir($opts{chdir})
283 5 ▶ or syserr(g_('cannot change directory to %s'), $opts{chdir});
Branch: 1 branch
BranchTrueFalseTotal
chdir $opts{"chdir"} or syserr(g_("cannot change directory to %s"), $opts{"chdir"}) 5 0 5
284 }
285 # Redirect STDIN if needed.
286 535 ▶ if ($opts{from_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'from_file'}) { } 3 532 535
elsif ($opts{'from_handle'}) { } 11 521 532
287 open(STDIN, '<', $opts{from_file})
288 3 ▶ or syserr(g_('cannot open %s'), $opts{from_file});
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", $opts{"from_file"} or syserr(g_("cannot open %s"), $opts{"from_file"}) 3 0 3
289 } elsif ($opts{from_handle}) {
290 open(STDIN, '<&', $opts{from_handle})
291 11 ▶ or syserr(g_('cannot reopen %s'), g_('<standard input>'));
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<&", $opts{"from_handle"} or syserr(g_("cannot reopen %s"), g_("<standard input>")) 11 0 11
292 # Has been duped, can be closed.
293 11 push @{$opts{close_in_child}}, $opts{from_handle};
294 }
295 # Redirect STDOUT if needed.
296 535 ▶ if ($opts{to_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'to_file'}) { } 3 532 535
elsif ($opts{'to_handle'}) { } 14 518 532
297 open(STDOUT, '>', $opts{to_file})
298 3 ▶ or syserr(g_('cannot write %s'), $opts{to_file});
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", $opts{"to_file"} or syserr(g_("cannot write %s"), $opts{"to_file"}) 3 0 3
299 } elsif ($opts{to_handle}) {
300 open(STDOUT, '>&', $opts{to_handle})
301 14 ▶ or syserr(g_('cannot reopen %s'), g_('<standard output>'));
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $opts{"to_handle"} or syserr(g_("cannot reopen %s"), g_("<standard output>")) 14 0 14
302 # Has been duped, can be closed.
303 14 push @{$opts{close_in_child}}, $opts{to_handle};
304 }
305 # Redirect STDERR if needed.
306 535 ▶ if ($opts{error_to_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($opts{'error_to_file'}) { } 0 535 535
elsif ($opts{'error_to_handle'}) { } 524 11 535
307 open(STDERR, '>', $opts{error_to_file})
308 0 ▶ or syserr(g_('cannot write %s'), $opts{error_to_file});
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">", $opts{"error_to_file"} or syserr(g_("cannot write %s"), $opts{"error_to_file"}) 0 0 0
309 } elsif ($opts{error_to_handle}) {
310 open(STDERR, '>&', $opts{error_to_handle})
311 524 ▶ or syserr(g_('cannot reopen %s'), g_('<standard error>'));
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&", $opts{"error_to_handle"} or syserr(g_("cannot reopen %s"), g_("<standard error>")) 524 0 524
312 # Has been duped, can be closed.
313 524 push @{$opts{close_in_child}}, $opts{error_to_handle};
314 }
315 # Close some inherited filehandles.
316 535 close($_) foreach (@{$opts{close_in_child}});
317 # Execute the program.
318 535 ▶ exec({ $prog[0] } @prog) or syserr(g_('cannot execute %s'), "@prog");
Branch: 1 branch
BranchTrueFalseTotal
exec {$prog[0];} @prog or syserr(g_("cannot execute %s"), join($", @prog)) 0 2 2
319 }
320 # Close handle that we cannot use any more.
321 133423 ▶ close($opts{from_handle}) if exists $opts{from_handle};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"from_handle"} 21 133402 133423
322 133423 ▶ close($opts{to_handle}) if exists $opts{to_handle};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"to_handle"} 26 133397 133423
323 133423 ▶ close($opts{error_to_handle}) if exists $opts{error_to_handle};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"error_to_handle"} 133400 23 133423
324  
325 133423 ▶ if ($opts{from_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"from_string"}) 4 133419 133423
326 4 print { $from_string_pipe } ${$opts{from_string}};
327 4 close($from_string_pipe);
328 }
329 133423 ▶ if ($opts{to_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"to_string"}) 18 133405 133423
330 18 local $/ = undef;
331 18 ${$opts{to_string}} = readline($to_string_pipe);
332 }
333 133423 ▶ if ($opts{error_to_string}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"error_to_string"}) 133400 23 133423
334 133400 local $/ = undef;
335 133400 ${$opts{error_to_string}} = readline($error_to_string_pipe);
336 }
337 133423 ▶ if ($opts{wait_child}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"wait_child"}) 133408 15 133423
338 133408 my $cmdline = "@prog";
339 133408 ▶ if ($opts{env}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"env"}) 4 133404 133408
340 4 foreach (keys %{$opts{env}}) {
341 8 $cmdline = "$_=\"" . $opts{env}{$_} . "\" $cmdline";
342 }
343 }
344 # TODO: Backwards compatibility, to be removed in VERSION 2.00.
345 133408 ▶ if (exists $opts{nocheck} && ! exists $opts{no_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"nocheck"} and not exists $opts{"no_check"}) 0 133408 133408
Condition33%1/3
exists $opts{"nocheck"} and not exists $opts{"no_check"}
!ll&&!rl&&r
13340800
MC/DC0%0/2
exists $opts{"nocheck"} and not exists $opts{"no_check"}
exists $opts{"nocheck"} not exists $opts{"no_check"}
Truth table33%1/3
A and not(B)
A= exists $opts{"nocheck"}B= not exists $opts{"no_check"}
ABresult
0X0
100
111
346 0 warnings::warnif('deprecated',
347 'Dpkg::IPC::spawn() option nocheck is deprecated, ' .
348 'either switch to no_check or pass nocheck alongside it');
349 }
350 wait_child($pid,
351 no_check => $opts{no_check} // $opts{nocheck},
352 timeout => $opts{timeout},
353 133408 ▶ cmdline => $cmdline,
Condition66%2/3
$opts{"no_check"} // $opts{"nocheck"}
l!l&&r!l&&!r
13340008
MC/DC50%1/2
$opts{"no_check"} // $opts{"nocheck"}
$opts{"no_check"} $opts{"nocheck"}
Truth table66%2/3
A // B
A= $opts{"no_check"}B= $opts{"nocheck"}
ABresult
1X1
011
000
354 );
355 133407 return 1;
356 }
357  
358 15 return $pid;
359 }
360  
361  
362 =item wait_child($pid, %opts)
363  
364 Takes as first argument the pid of the process to wait for.
365 Remaining arguments are taken as a hash of options. Returns
366 nothing. Fails if the child has been ended by a signal or
367 if it exited non-zero.
368  
369 Options:
370  
371 =over 4
372  
373 =item cmdline
374  
375 String to identify the child process in error messages.
376 Defaults to "child process".
377  
378 =item no_check
379  
380 If true do not check the return status of the child (and thus
381 do not fail it has been killed or if it exited with a
382 non-zero return code).
383  
384 =item nocheck
385  
386 Deprecated alias for no_check.
387 It emits a deprecation warning, but if it is required for backwards
388 compatibility it can be passed alongside B<no_check> and the warning
389 will not be emitted.
390  
391 =item timeout
392  
393 Set a maximum time to wait for the process, after that kill the process and
394 fail with an error message.
395  
396 =back
397  
398 =cut
399  
400 133423 ▶sub wait_child {
Subroutine wait_child: called CC=13 SCAR=27.0
Pod documented
401 133423 my ($pid, %opts) = @_;
402 133423 ▶ $opts{cmdline} //= g_('child process');
Condition66%2/3
$opts{"cmdline"} //= g_("child process")
l!l&&r!l&&!r
13342030
MC/DC0%0/2
$opts{"cmdline"} //= g_("child process")
$opts{"cmdline"} g_("child process")
Truth table66%2/3
A //= B
A= $opts{"cmdline"}B= g_("child process")
ABresult
1X1
011
000
403 # TODO: Backwards compatibility, to be removed in VERSION 2.00.
404 133423 ▶ if (exists $opts{nocheck} && ! exists $opts{no_check}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"nocheck"} and not exists $opts{"no_check"}) 0 133423 133423
Condition33%1/3
exists $opts{"nocheck"} and not exists $opts{"no_check"}
!ll&&!rl&&r
13342300
MC/DC0%0/2
exists $opts{"nocheck"} and not exists $opts{"no_check"}
exists $opts{"nocheck"} not exists $opts{"no_check"}
Truth table33%1/3
A and not(B)
A= exists $opts{"nocheck"}B= not exists $opts{"no_check"}
ABresult
0X0
100
111
405 0 warnings::warnif('deprecated',
406 'Dpkg::IPC::wait_child() option nocheck is deprecated, ' .
407 'either switch to no_check or pass nocheck alongside it');
408 }
409 133423 ▶ $opts{no_check} //= $opts{nocheck};
Condition66%2/3
$opts{"no_check"} //= $opts{"nocheck"}
l!l&&r!l&&!r
133412011
MC/DC50%1/2
$opts{"no_check"} //= $opts{"nocheck"}
$opts{"no_check"} $opts{"nocheck"}
Truth table66%2/3
A //= B
A= $opts{"no_check"}B= $opts{"nocheck"}
ABresult
1X1
011
000
410 133423 ▶ croak 'no PID set, cannot wait end of process' unless $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid 0 133423 133423
411 133423 eval {
412 133423 ▶ local $SIG{ALRM} = sub { die "alarm\n" };
Subroutine __ANON__: called CC=1 SCAR=0.0
413 133423 ▶ alarm($opts{timeout}) if defined($opts{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"timeout"} 13 133410 133423
414 $pid == waitpid($pid, 0)
415 133423 ▶ or syserr(g_('cannot reap %s subprocess'), $opts{cmdline});
Branch: 1 branch
BranchTrueFalseTotal
$pid == waitpid($pid, 0) or syserr(g_("cannot reap %s subprocess"), $opts{"cmdline"}) 133422 0 133422
416 133422 ▶ alarm(0) if defined($opts{timeout});
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"timeout"} 12 133410 133422
417 };
418 133423 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 1 133422 133423
419 1 ▶ die $@ unless $@ eq "alarm\n";
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "alarm\n" 0 1 1
420 1 kill 'TERM', $pid;
421 error(P_('%s did not complete in %d second',
422 '%s did not complete in %d seconds',
423 $opts{timeout}),
424 1 $opts{cmdline}, $opts{timeout});
425 }
426 133422 ▶ unless ($opts{no_check}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"no_check"}) 17 133405 133422
427 17 ▶ subprocerr($opts{cmdline}) if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 17 17
428 }
429 }
430  
431 554 1;
432  
433 =back
434  
435 =head1 CHANGES
436  
437 =head2 Version 1.03 (dpkg 1.23.0)
438  
439 New options: spawn() and wait_child() now accept 'no_check'.
440  
441 Deprecated options: 'nocheck' in spawn() and wait_child().
442  
443 =head2 Version 1.02 (dpkg 1.18.0)
444  
445 Change options: wait_child() now kills the process when reaching the 'timeout'.
446  
447 =head2 Version 1.01 (dpkg 1.17.11)
448  
449 New options: spawn() now accepts 'sig' and 'delete_sig'.
450  
451 =head2 Version 1.00 (dpkg 1.15.6)
452  
453 Mark the module as public.
454  
455 =head1 SEE ALSO
456  
457 L<Dpkg>, L<Dpkg::ErrorHandling>.
« Dpkg/Index.pm ↑ Summary Dpkg/Vendor/PureOS.pm »