Sfoglia il codice sorgente

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.
Raphael Hertzog 18 anni fa
parent
commit
1a4277f41b
1 ha cambiato i file con 15 aggiunte e 0 eliminazioni
  1. 15 0
      scripts/Dpkg/IPC.pm

+ 15 - 0
scripts/Dpkg/IPC.pm

@@ -28,6 +28,7 @@ our @EXPORT = qw(fork_and_exec wait_child);
 
 
 sub fork_and_exec {
 sub fork_and_exec {
     my (%opts) = @_;
     my (%opts) = @_;
+    $opts{"close_in_child"} ||= [];
     error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"};
     error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"};
     my @prog;
     my @prog;
     if (ref($opts{"exec"}) =~ /ARRAY/) {
     if (ref($opts{"exec"}) =~ /ARRAY/) {
@@ -37,6 +38,20 @@ sub fork_and_exec {
     } else {
     } else {
 	error(_g("invalid exec parameter in fork_and_exec()"));
 	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
     # Fork and exec
     my $pid = fork();
     my $pid = fork();
     syserr(_g("fork for %s"), "@prog") unless defined $pid;
     syserr(_g("fork for %s"), "@prog") unless defined $pid;