IPC.pm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2008 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::IPC;
  14. use strict;
  15. use warnings;
  16. use Dpkg::ErrorHandling qw(error syserr subprocerr);
  17. use Dpkg::Gettext;
  18. use Exporter;
  19. our @ISA = qw(Exporter);
  20. our @EXPORT = qw(fork_and_exec wait_child);
  21. sub fork_and_exec {
  22. my (%opts) = @_;
  23. $opts{"close_in_child"} ||= [];
  24. error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"};
  25. my @prog;
  26. if (ref($opts{"exec"}) =~ /ARRAY/) {
  27. push @prog, @{$opts{"exec"}};
  28. } elsif (not ref($opts{"exec"})) {
  29. push @prog, $opts{"exec"};
  30. } else {
  31. error(_g("invalid exec parameter in fork_and_exec()"));
  32. }
  33. # Create pipes if needed
  34. my ($input_pipe, $output_pipe);
  35. if ($opts{"from_pipe"}) {
  36. pipe($opts{"from_handle"}, $input_pipe) ||
  37. syserr(_g("pipe for %s"), "@prog");
  38. ${$opts{"from_pipe"}} = $input_pipe;
  39. push @{$opts{"close_in_child"}}, $input_pipe;
  40. }
  41. if ($opts{"to_pipe"}) {
  42. pipe($output_pipe, $opts{"to_handle"}) ||
  43. syserr(_g("pipe for %s"), "@prog");
  44. ${$opts{"to_pipe"}} = $output_pipe;
  45. push @{$opts{"close_in_child"}}, $output_pipe;
  46. }
  47. # Fork and exec
  48. my $pid = fork();
  49. syserr(_g("fork for %s"), "@prog") unless defined $pid;
  50. if (not $pid) {
  51. # Redirect STDIN if needed
  52. if ($opts{"from_file"}) {
  53. open(STDIN, "<", $opts{"from_file"}) ||
  54. syserr(_g("cannot open %s"), $opts{"from_file"});
  55. } elsif ($opts{"from_handle"}) {
  56. open(STDIN, "<&", $opts{"from_handle"}) || syserr(_g("reopen stdin"));
  57. close($opts{"from_handle"}); # has been duped, can be closed
  58. }
  59. # Redirect STDOUT if needed
  60. if ($opts{"to_file"}) {
  61. open(STDOUT, ">", $opts{"to_file"}) ||
  62. syserr(_g("cannot write %s"), $opts{"to_file"});
  63. } elsif ($opts{"to_handle"}) {
  64. open(STDOUT, ">&", $opts{"to_handle"}) || syserr(_g("reopen stdout"));
  65. close($opts{"to_handle"}); # has been duped, can be closed
  66. }
  67. # Close some inherited filehandles
  68. close($_) foreach (@{$opts{"close_in_child"}});
  69. # Execute the program
  70. exec(@prog) or syserr(_g("exec %s"), "@prog");
  71. }
  72. # Close handle that we can't use any more
  73. close($opts{"from_handle"}) if exists $opts{"from_handle"};
  74. close($opts{"to_handle"}) if exists $opts{"to_handle"};
  75. return $pid;
  76. }
  77. sub wait_child {
  78. my ($pid, %opts) = @_;
  79. $opts{"cmdline"} ||= _g("child process");
  80. error(_g("no PID set, cannot wait end of process")) unless $pid;
  81. $pid == waitpid($pid, 0) or syserr(_g("wait for %s"), $opts{"cmdline"});
  82. subprocerr($opts{"cmdline"}) if $?;
  83. }
  84. 1;