IPC.pm 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. error("exec parameter is mandatory in fork_and_exec()") unless $opts{"exec"};
  24. my @prog;
  25. if (ref($opts{"exec"}) =~ /ARRAY/) {
  26. push @prog, @{$opts{"exec"}};
  27. } elsif (not ref($opts{"exec"})) {
  28. push @prog, $opts{"exec"};
  29. } else {
  30. error(_g("invalid exec parameter in fork_and_exec()"));
  31. }
  32. # Fork and exec
  33. my $pid = fork();
  34. syserr(_g("fork for %s"), "@prog") unless defined $pid;
  35. if (not $pid) {
  36. # Redirect STDIN if needed
  37. if ($opts{"from_file"}) {
  38. open(STDIN, "<", $opts{"from_file"}) ||
  39. syserr(_g("cannot open %s"), $opts{"from_file"});
  40. } elsif ($opts{"from_handle"}) {
  41. open(STDIN, "<&", $opts{"from_handle"}) || syserr(_g("reopen stdin"));
  42. close($opts{"from_handle"}); # has been duped, can be closed
  43. }
  44. # Redirect STDOUT if needed
  45. if ($opts{"to_file"}) {
  46. open(STDOUT, ">", $opts{"to_file"}) ||
  47. syserr(_g("cannot write %s"), $opts{"to_file"});
  48. } elsif ($opts{"to_handle"}) {
  49. open(STDOUT, ">&", $opts{"to_handle"}) || syserr(_g("reopen stdout"));
  50. close($opts{"to_handle"}); # has been duped, can be closed
  51. }
  52. # Close some inherited filehandles
  53. close($_) foreach (@{$opts{"close_in_child"}});
  54. # Execute the program
  55. exec(@prog) or syserr(_g("exec %s"), "@prog");
  56. }
  57. # Close handle that we can't use any more
  58. close($opts{"from_handle"}) if exists $opts{"from_handle"};
  59. close($opts{"to_handle"}) if exists $opts{"to_handle"};
  60. return $pid;
  61. }
  62. sub wait_child {
  63. my ($pid, %opts) = @_;
  64. $opts{"cmdline"} ||= _g("child process");
  65. error(_g("no PID set, cannot wait end of process")) unless $pid;
  66. $pid == waitpid($pid, 0) or syserr(_g("wait for %s"), $opts{"cmdline"});
  67. subprocerr($opts{"cmdline"}) if $?;
  68. }
  69. 1;