IPC.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. # Copyright © 2008-2009 Raphaël Hertzog <hertzog@debian.org>
  2. # Copyright © 2008 Frank Lichtenheld <djpig@debian.org>
  3. # Copyright © 2008-2010, 2012-2015 Guillem Jover <guillem@debian.org>
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. package Dpkg::IPC;
  18. use strict;
  19. use warnings;
  20. our $VERSION = '1.02';
  21. our @EXPORT = qw(
  22. spawn
  23. wait_child
  24. );
  25. use Carp;
  26. use Exporter qw(import);
  27. use Dpkg::ErrorHandling;
  28. use Dpkg::Gettext;
  29. =encoding utf8
  30. =head1 NAME
  31. Dpkg::IPC - helper functions for IPC
  32. =head1 DESCRIPTION
  33. Dpkg::IPC offers helper functions to allow you to execute
  34. other programs in an easy, yet flexible way, while hiding
  35. all the gory details of IPC (Inter-Process Communication)
  36. from you.
  37. =head1 FUNCTIONS
  38. =over 4
  39. =item $pid = spawn(%opts)
  40. Creates a child process and executes another program in it.
  41. The arguments are interpreted as a hash of options, specifying
  42. how to handle the in and output of the program to execute.
  43. Returns the pid of the child process (unless the wait_child
  44. option was given).
  45. Any error will cause the function to exit with one of the
  46. Dpkg::ErrorHandling functions.
  47. Options:
  48. =over 4
  49. =item exec
  50. Can be either a scalar, i.e. the name of the program to be
  51. executed, or an array reference, i.e. the name of the program
  52. plus additional arguments. Note that the program will never be
  53. executed via the shell, so you can't specify additional arguments
  54. in the scalar string and you can't use any shell facilities like
  55. globbing.
  56. Mandatory Option.
  57. =item from_file, to_file, error_to_file
  58. Filename as scalar. Standard input/output/error of the
  59. child process will be redirected to the file specified.
  60. =item from_handle, to_handle, error_to_handle
  61. Filehandle. Standard input/output/error of the child process will be
  62. dup'ed from the handle.
  63. =item from_pipe, to_pipe, error_to_pipe
  64. Scalar reference or object based on IO::Handle. A pipe will be opened for
  65. each of the two options and either the reading (C<to_pipe> and
  66. C<error_to_pipe>) or the writing end (C<from_pipe>) will be returned in
  67. the referenced scalar. Standard input/output/error of the child process
  68. will be dup'ed to the other ends of the pipes.
  69. =item from_string, to_string, error_to_string
  70. Scalar reference. Standard input/output/error of the child
  71. process will be redirected to the string given as reference. Note
  72. that it wouldn't be strictly necessary to use a scalar reference
  73. for C<from_string>, as the string is not modified in any way. This was
  74. chosen only for reasons of symmetry with C<to_string> and
  75. C<error_to_string>. C<to_string> and C<error_to_string> imply the
  76. C<wait_child> option.
  77. =item wait_child
  78. Scalar. If containing a true value, wait_child() will be called before
  79. returning. The return value of spawn() will be a true value, not the pid.
  80. =item nocheck
  81. Scalar. Option of the wait_child() call.
  82. =item timeout
  83. Scalar. Option of the wait_child() call.
  84. =item chdir
  85. Scalar. The child process will chdir in the indicated directory before
  86. calling exec.
  87. =item env
  88. Hash reference. The child process will populate %ENV with the items of the
  89. hash before calling exec. This allows exporting environment variables.
  90. =item delete_env
  91. Array reference. The child process will remove all environment variables
  92. listed in the array before calling exec.
  93. =item sig
  94. Hash reference. The child process will populate %SIG with the items of the
  95. hash before calling exec. This allows setting signal dispositions.
  96. =item delete_sig
  97. Array reference. The child process will reset all signals listed in the
  98. array to their default dispositions before calling exec.
  99. =back
  100. =cut
  101. sub _sanity_check_opts {
  102. my (%opts) = @_;
  103. croak 'exec parameter is mandatory in spawn()'
  104. unless $opts{exec};
  105. my $to = my $error_to = my $from = 0;
  106. foreach my $thing (qw(file handle string pipe)) {
  107. $to++ if $opts{"to_$thing"};
  108. $error_to++ if $opts{"error_to_$thing"};
  109. $from++ if $opts{"from_$thing"};
  110. }
  111. croak 'not more than one of to_* parameters is allowed'
  112. if $to > 1;
  113. croak 'not more than one of error_to_* parameters is allowed'
  114. if $error_to > 1;
  115. croak 'not more than one of from_* parameters is allowed'
  116. if $from > 1;
  117. foreach my $param (qw(to_string error_to_string from_string)) {
  118. if (exists $opts{$param} and
  119. (not ref $opts{$param} or ref $opts{$param} ne 'SCALAR')) {
  120. croak "parameter $param must be a scalar reference";
  121. }
  122. }
  123. foreach my $param (qw(to_pipe error_to_pipe from_pipe)) {
  124. if (exists $opts{$param} and
  125. (not ref $opts{$param} or (ref $opts{$param} ne 'SCALAR' and
  126. not $opts{$param}->isa('IO::Handle')))) {
  127. croak "parameter $param must be a scalar reference or " .
  128. 'an IO::Handle object';
  129. }
  130. }
  131. if (exists $opts{timeout} and defined($opts{timeout}) and
  132. $opts{timeout} !~ /^\d+$/) {
  133. croak 'parameter timeout must be an integer';
  134. }
  135. if (exists $opts{env} and ref($opts{env}) ne 'HASH') {
  136. croak 'parameter env must be a hash reference';
  137. }
  138. if (exists $opts{delete_env} and ref($opts{delete_env}) ne 'ARRAY') {
  139. croak 'parameter delete_env must be an array reference';
  140. }
  141. if (exists $opts{sig} and ref($opts{sig}) ne 'HASH') {
  142. croak 'parameter sig must be a hash reference';
  143. }
  144. if (exists $opts{delete_sig} and ref($opts{delete_sig}) ne 'ARRAY') {
  145. croak 'parameter delete_sig must be an array reference';
  146. }
  147. return %opts;
  148. }
  149. sub spawn {
  150. my (%opts) = @_;
  151. my @prog;
  152. _sanity_check_opts(%opts);
  153. $opts{close_in_child} //= [];
  154. if (ref($opts{exec}) =~ /ARRAY/) {
  155. push @prog, @{$opts{exec}};
  156. } elsif (not ref($opts{exec})) {
  157. push @prog, $opts{exec};
  158. } else {
  159. croak 'invalid exec parameter in spawn()';
  160. }
  161. my ($from_string_pipe, $to_string_pipe, $error_to_string_pipe);
  162. if ($opts{to_string}) {
  163. $opts{to_pipe} = \$to_string_pipe;
  164. $opts{wait_child} = 1;
  165. }
  166. if ($opts{error_to_string}) {
  167. $opts{error_to_pipe} = \$error_to_string_pipe;
  168. $opts{wait_child} = 1;
  169. }
  170. if ($opts{from_string}) {
  171. $opts{from_pipe} = \$from_string_pipe;
  172. }
  173. # Create pipes if needed
  174. my ($input_pipe, $output_pipe, $error_pipe);
  175. if ($opts{from_pipe}) {
  176. pipe($opts{from_handle}, $input_pipe)
  177. or syserr(g_('pipe for %s'), "@prog");
  178. ${$opts{from_pipe}} = $input_pipe;
  179. push @{$opts{close_in_child}}, $input_pipe;
  180. }
  181. if ($opts{to_pipe}) {
  182. pipe($output_pipe, $opts{to_handle})
  183. or syserr(g_('pipe for %s'), "@prog");
  184. ${$opts{to_pipe}} = $output_pipe;
  185. push @{$opts{close_in_child}}, $output_pipe;
  186. }
  187. if ($opts{error_to_pipe}) {
  188. pipe($error_pipe, $opts{error_to_handle})
  189. or syserr(g_('pipe for %s'), "@prog");
  190. ${$opts{error_to_pipe}} = $error_pipe;
  191. push @{$opts{close_in_child}}, $error_pipe;
  192. }
  193. # Fork and exec
  194. my $pid = fork();
  195. syserr(g_('cannot fork for %s'), "@prog") unless defined $pid;
  196. if (not $pid) {
  197. # Define environment variables
  198. if ($opts{env}) {
  199. foreach (keys %{$opts{env}}) {
  200. $ENV{$_} = $opts{env}{$_};
  201. }
  202. }
  203. if ($opts{delete_env}) {
  204. delete $ENV{$_} foreach (@{$opts{delete_env}});
  205. }
  206. # Define signal dispositions.
  207. if ($opts{sig}) {
  208. foreach (keys %{$opts{sig}}) {
  209. $SIG{$_} = $opts{sig}{$_};
  210. }
  211. }
  212. if ($opts{delete_sig}) {
  213. delete $SIG{$_} foreach (@{$opts{delete_sig}});
  214. }
  215. # Change the current directory
  216. if ($opts{chdir}) {
  217. chdir($opts{chdir}) or syserr(g_('chdir to %s'), $opts{chdir});
  218. }
  219. # Redirect STDIN if needed
  220. if ($opts{from_file}) {
  221. open(STDIN, '<', $opts{from_file})
  222. or syserr(g_('cannot open %s'), $opts{from_file});
  223. } elsif ($opts{from_handle}) {
  224. open(STDIN, '<&', $opts{from_handle})
  225. or syserr(g_('reopen stdin'));
  226. # has been duped, can be closed
  227. push @{$opts{close_in_child}}, $opts{from_handle};
  228. }
  229. # Redirect STDOUT if needed
  230. if ($opts{to_file}) {
  231. open(STDOUT, '>', $opts{to_file})
  232. or syserr(g_('cannot write %s'), $opts{to_file});
  233. } elsif ($opts{to_handle}) {
  234. open(STDOUT, '>&', $opts{to_handle})
  235. or syserr(g_('reopen stdout'));
  236. # has been duped, can be closed
  237. push @{$opts{close_in_child}}, $opts{to_handle};
  238. }
  239. # Redirect STDERR if needed
  240. if ($opts{error_to_file}) {
  241. open(STDERR, '>', $opts{error_to_file})
  242. or syserr(g_('cannot write %s'), $opts{error_to_file});
  243. } elsif ($opts{error_to_handle}) {
  244. open(STDERR, '>&', $opts{error_to_handle})
  245. or syserr(g_('reopen stdout'));
  246. # has been duped, can be closed
  247. push @{$opts{close_in_child}}, $opts{error_to_handle};
  248. }
  249. # Close some inherited filehandles
  250. close($_) foreach (@{$opts{close_in_child}});
  251. # Execute the program
  252. exec({ $prog[0] } @prog) or syserr(g_('unable to execute %s'), "@prog");
  253. }
  254. # Close handle that we can't use any more
  255. close($opts{from_handle}) if exists $opts{from_handle};
  256. close($opts{to_handle}) if exists $opts{to_handle};
  257. close($opts{error_to_handle}) if exists $opts{error_to_handle};
  258. if ($opts{from_string}) {
  259. print { $from_string_pipe } ${$opts{from_string}};
  260. close($from_string_pipe);
  261. }
  262. if ($opts{to_string}) {
  263. local $/ = undef;
  264. ${$opts{to_string}} = readline($to_string_pipe);
  265. }
  266. if ($opts{error_to_string}) {
  267. local $/ = undef;
  268. ${$opts{error_to_string}} = readline($error_to_string_pipe);
  269. }
  270. if ($opts{wait_child}) {
  271. my $cmdline = "@prog";
  272. if ($opts{env}) {
  273. foreach (keys %{$opts{env}}) {
  274. $cmdline = "$_=\"" . $opts{env}{$_} . "\" $cmdline";
  275. }
  276. }
  277. wait_child($pid, nocheck => $opts{nocheck},
  278. timeout => $opts{timeout}, cmdline => $cmdline);
  279. return 1;
  280. }
  281. return $pid;
  282. }
  283. =item wait_child($pid, %opts)
  284. Takes as first argument the pid of the process to wait for.
  285. Remaining arguments are taken as a hash of options. Returns
  286. nothing. Fails if the child has been ended by a signal or
  287. if it exited non-zero.
  288. Options:
  289. =over 4
  290. =item cmdline
  291. String to identify the child process in error messages.
  292. Defaults to "child process".
  293. =item nocheck
  294. If true do not check the return status of the child (and thus
  295. do not fail it has been killed or if it exited with a
  296. non-zero return code).
  297. =item timeout
  298. Set a maximum time to wait for the process, after that kill the process and
  299. fail with an error message.
  300. =back
  301. =cut
  302. sub wait_child {
  303. my ($pid, %opts) = @_;
  304. $opts{cmdline} //= g_('child process');
  305. croak 'no PID set, cannot wait end of process' unless $pid;
  306. eval {
  307. local $SIG{ALRM} = sub { die "alarm\n" };
  308. alarm($opts{timeout}) if defined($opts{timeout});
  309. $pid == waitpid($pid, 0) or syserr(g_('wait for %s'), $opts{cmdline});
  310. alarm(0) if defined($opts{timeout});
  311. };
  312. if ($@) {
  313. die $@ unless $@ eq "alarm\n";
  314. kill 'TERM', $pid;
  315. error(P_("%s didn't complete in %d second",
  316. "%s didn't complete in %d seconds",
  317. $opts{timeout}),
  318. $opts{cmdline}, $opts{timeout});
  319. }
  320. unless ($opts{nocheck}) {
  321. subprocerr($opts{cmdline}) if $?;
  322. }
  323. }
  324. 1;
  325. __END__
  326. =back
  327. =head1 CHANGES
  328. =head2 Version 1.02 (dpkg 1.18.0)
  329. Change options: wait_child() now kills the process when reaching the 'timeout'.
  330. =head2 Version 1.01 (dpkg 1.17.11)
  331. New options: spawn() now accepts 'sig' and 'delete_sig'.
  332. =head2 Version 1.00 (dpkg 1.15.6)
  333. Mark the module as public.
  334. =head1 SEE ALSO
  335. Dpkg, Dpkg::ErrorHandling