IPC.pm 3.8 KB

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