From: Raphael Hertzog Date: Fri, 15 Feb 2008 18:30:21 +0000 (+0100) Subject: Dpkg::IPC: extend fork_and_exec() to create pipes on request X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1a4277f41bd2fd62dac94cdddea2d4db3eccbb5a;p=dpkg Dpkg::IPC: extend fork_and_exec() to create pipes on request * scripts/Dpkg/IPC.pm (fork_and_exec): Now accept from_pipe and to_pipe parameters that should point to a scalar reference. The scalar will be set with the filehandle of the other side of the created pipe. --- diff --git a/scripts/Dpkg/IPC.pm b/scripts/Dpkg/IPC.pm index 090ecc97..b3937cd6 100644 --- a/scripts/Dpkg/IPC.pm +++ b/scripts/Dpkg/IPC.pm @@ -28,6 +28,7 @@ our @EXPORT = qw(fork_and_exec wait_child); sub fork_and_exec { my (%opts) = @_; + $opts{"close_in_child"} ||= []; error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"}; my @prog; if (ref($opts{"exec"}) =~ /ARRAY/) { @@ -37,6 +38,20 @@ sub fork_and_exec { } else { error(_g("invalid exec parameter in fork_and_exec()")); } + # Create pipes if needed + my ($input_pipe, $output_pipe); + if ($opts{"from_pipe"}) { + pipe($opts{"from_handle"}, $input_pipe) || + syserr(_g("pipe for %s"), "@prog"); + ${$opts{"from_pipe"}} = $input_pipe; + push @{$opts{"close_in_child"}}, $input_pipe; + } + if ($opts{"to_pipe"}) { + pipe($output_pipe, $opts{"to_handle"}) || + syserr(_g("pipe for %s"), "@prog"); + ${$opts{"to_pipe"}} = $output_pipe; + push @{$opts{"close_in_child"}}, $output_pipe; + } # Fork and exec my $pid = fork(); syserr(_g("fork for %s"), "@prog") unless defined $pid;