PageRenderTime 59ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/IPC-Open3/lib/IPC/Open3.pm

https://github.com/toddr/perl
Perl | 444 lines | 324 code | 67 blank | 53 comment | 57 complexity | 263eaa9862729875dec3e7b126f30722 MD5 | raw file
Possible License(s): AGPL-1.0
  1. package IPC::Open3;
  2. use strict;
  3. no strict 'refs'; # because users pass me bareword filehandles
  4. our ($VERSION, @ISA, @EXPORT);
  5. require Exporter;
  6. use Carp;
  7. use Symbol qw(gensym qualify);
  8. $VERSION = '1.21';
  9. @ISA = qw(Exporter);
  10. @EXPORT = qw(open3);
  11. =head1 NAME
  12. IPC::Open3 - open a process for reading, writing, and error handling using open3()
  13. =head1 SYNOPSIS
  14. use Symbol 'gensym'; # vivify a separate handle for STDERR
  15. my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym,
  16. 'some', 'cmd', 'and', 'args');
  17. # or pass the command through the shell
  18. my $pid = open3(my $chld_in, my $chld_out, my $chld_err = gensym,
  19. 'some cmd and args');
  20. # read from parent STDIN
  21. # send STDOUT and STDERR to already open handle
  22. open my $outfile, '>>', 'output.txt' or die "open failed: $!";
  23. my $pid = open3('<&STDIN', $outfile, undef,
  24. 'some', 'cmd', 'and', 'args');
  25. # write to parent STDOUT and STDERR
  26. my $pid = open3(my $chld_in, '>&STDOUT', '>&STDERR',
  27. 'some', 'cmd', 'and', 'args');
  28. # reap zombie and retrieve exit status
  29. waitpid( $pid, 0 );
  30. my $child_exit_status = $? >> 8;
  31. =head1 DESCRIPTION
  32. Extremely similar to open2(), open3() spawns the given command and
  33. connects $chld_out for reading from the child, $chld_in for writing to
  34. the child, and $chld_err for errors. If $chld_err is false, or the
  35. same file descriptor as $chld_out, then STDOUT and STDERR of the child
  36. are on the same filehandle. This means that an autovivified lexical
  37. cannot be used for the STDERR filehandle, but gensym from L<Symbol> can
  38. be used to vivify a new glob reference, see L</SYNOPSIS>. The $chld_in
  39. will have autoflush turned on.
  40. If $chld_in begins with C<< <& >>, then $chld_in will be closed in the
  41. parent, and the child will read from it directly. If $chld_out or
  42. $chld_err begins with C<< >& >>, then the child will send output
  43. directly to that filehandle. In both cases, there will be a L<dup(2)>
  44. instead of a L<pipe(2)> made.
  45. If either reader or writer is the empty string or undefined, this will
  46. be replaced by an autogenerated filehandle. If so, you must pass a
  47. valid lvalue in the parameter slot so it can be overwritten in the
  48. caller, or an exception will be raised.
  49. The filehandles may also be integers, in which case they are understood
  50. as file descriptors.
  51. open3() returns the process ID of the child process. It doesn't return on
  52. failure: it just raises an exception matching C</^open3:/>. However,
  53. C<exec> failures in the child (such as no such file or permission denied),
  54. are just reported to $chld_err under Windows and OS/2, as it is not possible
  55. to trap them.
  56. If the child process dies for any reason, the next write to $chld_in is
  57. likely to generate a SIGPIPE in the parent, which is fatal by default.
  58. So you may wish to handle this signal.
  59. Note if you specify C<-> as the command, in an analogous fashion to
  60. C<open(my $fh, "-|")> the child process will just be the forked Perl
  61. process rather than an external command. This feature isn't yet
  62. supported on Win32 platforms.
  63. open3() does not wait for and reap the child process after it exits.
  64. Except for short programs where it's acceptable to let the operating system
  65. take care of this, you need to do this yourself. This is normally as
  66. simple as calling C<waitpid $pid, 0> when you're done with the process.
  67. Failing to do this can result in an accumulation of defunct or "zombie"
  68. processes. See L<perlfunc/waitpid> for more information.
  69. If you try to read from the child's stdout writer and their stderr
  70. writer, you'll have problems with blocking, which means you'll want
  71. to use select() or L<IO::Select>, which means you'd best use
  72. sysread() instead of readline() for normal stuff.
  73. This is very dangerous, as you may block forever. It assumes it's
  74. going to talk to something like L<bc(1)>, both writing to it and reading
  75. from it. This is presumably safe because you "know" that commands
  76. like L<bc(1)> will read a line at a time and output a line at a time.
  77. Programs like L<sort(1)> that read their entire input stream first,
  78. however, are quite apt to cause deadlock.
  79. The big problem with this approach is that if you don't have control
  80. over source code being run in the child process, you can't control
  81. what it does with pipe buffering. Thus you can't just open a pipe to
  82. C<cat -v> and continually read and write a line from it.
  83. =head1 See Also
  84. =over 4
  85. =item L<IPC::Open2>
  86. Like Open3 but without STDERR capture.
  87. =item L<IPC::Run>
  88. This is a CPAN module that has better error handling and more facilities
  89. than Open3.
  90. =back
  91. =head1 WARNING
  92. The order of arguments differs from that of open2().
  93. =cut
  94. # &open3: Marc Horowitz <marc@mit.edu>
  95. # derived mostly from &open2 by tom christiansen, <tchrist@convex.com>
  96. # fixed for 5.001 by Ulrich Kunitz <kunitz@mai-koeln.com>
  97. # ported to Win32 by Ron Schmidt, Merrill Lynch almost ended my career
  98. # fixed for autovivving FHs, tchrist again
  99. # allow fd numbers to be used, by Frank Tobin
  100. # allow '-' as command (c.f. open "-|"), by Adam Spiers <perl@adamspiers.org>
  101. #
  102. # usage: $pid = open3('wtr', 'rdr', 'err' 'some cmd and args', 'optarg', ...);
  103. #
  104. # spawn the given $cmd and connect rdr for
  105. # reading, wtr for writing, and err for errors.
  106. # if err is '', or the same as rdr, then stdout and
  107. # stderr of the child are on the same fh. returns pid
  108. # of child (or dies on failure).
  109. # if wtr begins with '<&', then wtr will be closed in the parent, and
  110. # the child will read from it directly. if rdr or err begins with
  111. # '>&', then the child will send output directly to that fd. In both
  112. # cases, there will be a dup() instead of a pipe() made.
  113. # WARNING: this is dangerous, as you may block forever
  114. # unless you are very careful.
  115. #
  116. # $wtr is left unbuffered.
  117. #
  118. # abort program if
  119. # rdr or wtr are null
  120. # a system call fails
  121. our $Me = 'open3 (bug)'; # you should never see this, it's always localized
  122. # Fatal.pm needs to be fixed WRT prototypes.
  123. sub xpipe {
  124. pipe $_[0], $_[1] or croak "$Me: pipe($_[0], $_[1]) failed: $!";
  125. }
  126. # I tried using a * prototype character for the filehandle but it still
  127. # disallows a bareword while compiling under strict subs.
  128. sub xopen {
  129. open $_[0], $_[1], @_[2..$#_] and return;
  130. local $" = ', ';
  131. carp "$Me: open(@_) failed: $!";
  132. }
  133. sub xclose {
  134. $_[0] =~ /\A=?(\d+)\z/
  135. ? do { my $fh; open($fh, $_[1] . '&=' . $1) and close($fh); }
  136. : close $_[0]
  137. or croak "$Me: close($_[0]) failed: $!";
  138. }
  139. sub xfileno {
  140. return $1 if $_[0] =~ /\A=?(\d+)\z/; # deal with fh just being an fd
  141. return fileno $_[0];
  142. }
  143. use constant FORCE_DEBUG_SPAWN => 0;
  144. use constant DO_SPAWN => $^O eq 'os2' || $^O eq 'MSWin32' || FORCE_DEBUG_SPAWN;
  145. sub _open3 {
  146. local $Me = shift;
  147. # simulate autovivification of filehandles because
  148. # it's too ugly to use @_ throughout to make perl do it for us
  149. # tchrist 5-Mar-00
  150. # Historically, open3(undef...) has silently worked, so keep
  151. # it working.
  152. splice @_, 0, 1, undef if \$_[0] == \undef;
  153. splice @_, 1, 1, undef if \$_[1] == \undef;
  154. unless (eval {
  155. $_[0] = gensym unless defined $_[0] && length $_[0];
  156. $_[1] = gensym unless defined $_[1] && length $_[1];
  157. 1; })
  158. {
  159. # must strip crud for croak to add back, or looks ugly
  160. $@ =~ s/(?<=value attempted) at .*//s;
  161. croak "$Me: $@";
  162. }
  163. my @handles = ({ mode => '<', handle => \*STDIN },
  164. { mode => '>', handle => \*STDOUT },
  165. { mode => '>', handle => \*STDERR },
  166. );
  167. foreach (@handles) {
  168. $_->{parent} = shift;
  169. $_->{open_as} = gensym;
  170. }
  171. if (@_ > 1 and $_[0] eq '-') {
  172. croak "Arguments don't make sense when the command is '-'"
  173. }
  174. $handles[2]{parent} ||= $handles[1]{parent};
  175. $handles[2]{dup_of_out} = $handles[1]{parent} eq $handles[2]{parent};
  176. my $package;
  177. foreach (@handles) {
  178. $_->{dup} = ($_->{parent} =~ s/^[<>]&//);
  179. if ($_->{parent} !~ /\A=?(\d+)\z/) {
  180. # force unqualified filehandles into caller's package
  181. $package //= caller 1;
  182. $_->{parent} = qualify $_->{parent}, $package;
  183. }
  184. next if $_->{dup} or $_->{dup_of_out};
  185. if ($_->{mode} eq '<') {
  186. xpipe $_->{open_as}, $_->{parent};
  187. } else {
  188. xpipe $_->{parent}, $_->{open_as};
  189. }
  190. }
  191. my $kidpid;
  192. if (!DO_SPAWN) {
  193. # Used to communicate exec failures.
  194. xpipe my $stat_r, my $stat_w;
  195. $kidpid = fork;
  196. croak "$Me: fork failed: $!" unless defined $kidpid;
  197. if ($kidpid == 0) { # Kid
  198. eval {
  199. # A tie in the parent should not be allowed to cause problems.
  200. untie *STDIN;
  201. untie *STDOUT;
  202. untie *STDERR;
  203. close $stat_r;
  204. require Fcntl;
  205. my $flags = fcntl $stat_w, &Fcntl::F_GETFD, 0;
  206. croak "$Me: fcntl failed: $!" unless $flags;
  207. fcntl $stat_w, &Fcntl::F_SETFD, $flags|&Fcntl::FD_CLOEXEC
  208. or croak "$Me: fcntl failed: $!";
  209. # If she wants to dup the kid's stderr onto her stdout I need to
  210. # save a copy of her stdout before I put something else there.
  211. if (!$handles[2]{dup_of_out} && $handles[2]{dup}
  212. && xfileno($handles[2]{parent}) == fileno \*STDOUT) {
  213. my $tmp = gensym;
  214. xopen($tmp, '>&', $handles[2]{parent});
  215. $handles[2]{parent} = $tmp;
  216. }
  217. foreach (@handles) {
  218. if ($_->{dup_of_out}) {
  219. xopen \*STDERR, ">&STDOUT"
  220. if defined fileno STDERR && fileno STDERR != fileno STDOUT;
  221. } elsif ($_->{dup}) {
  222. xopen $_->{handle}, $_->{mode} . '&', $_->{parent}
  223. if fileno $_->{handle} != xfileno($_->{parent});
  224. } else {
  225. xclose $_->{parent}, $_->{mode};
  226. xopen $_->{handle}, $_->{mode} . '&=',
  227. fileno $_->{open_as};
  228. }
  229. }
  230. return 1 if ($_[0] eq '-');
  231. exec @_ or do {
  232. local($")=(" ");
  233. croak "$Me: exec of @_ failed: $!";
  234. };
  235. } and do {
  236. close $stat_w;
  237. return 0;
  238. };
  239. my $bang = 0+$!;
  240. my $err = $@;
  241. utf8::encode $err if $] >= 5.008;
  242. print $stat_w pack('IIa*', $bang, length($err), $err);
  243. close $stat_w;
  244. eval { require POSIX; POSIX::_exit(255); };
  245. exit 255;
  246. }
  247. else { # Parent
  248. close $stat_w;
  249. my $to_read = length(pack('I', 0)) * 2;
  250. my $bytes_read = read($stat_r, my $buf = '', $to_read);
  251. if ($bytes_read) {
  252. (my $bang, $to_read) = unpack('II', $buf);
  253. read($stat_r, my $err = '', $to_read);
  254. waitpid $kidpid, 0; # Reap child which should have exited
  255. if ($err) {
  256. utf8::decode $err if $] >= 5.008;
  257. } else {
  258. $err = "$Me: " . ($! = $bang);
  259. }
  260. $! = $bang;
  261. die($err);
  262. }
  263. }
  264. }
  265. else { # DO_SPAWN
  266. # All the bookkeeping of coincidence between handles is
  267. # handled in spawn_with_handles.
  268. my @close;
  269. foreach (@handles) {
  270. if ($_->{dup_of_out}) {
  271. $_->{open_as} = $handles[1]{open_as};
  272. } elsif ($_->{dup}) {
  273. $_->{open_as} = $_->{parent} =~ /\A[0-9]+\z/
  274. ? $_->{parent} : \*{$_->{parent}};
  275. push @close, $_->{open_as};
  276. } else {
  277. push @close, \*{$_->{parent}}, $_->{open_as};
  278. }
  279. }
  280. require IO::Pipe;
  281. $kidpid = eval {
  282. spawn_with_handles(\@handles, \@close, @_);
  283. };
  284. die "$Me: $@" if $@;
  285. }
  286. foreach (@handles) {
  287. next if $_->{dup} or $_->{dup_of_out};
  288. xclose $_->{open_as}, $_->{mode};
  289. }
  290. # If the write handle is a dup give it away entirely, close my copy
  291. # of it.
  292. xclose $handles[0]{parent}, $handles[0]{mode} if $handles[0]{dup};
  293. select((select($handles[0]{parent}), $| = 1)[0]); # unbuffer pipe
  294. $kidpid;
  295. }
  296. sub open3 {
  297. if (@_ < 4) {
  298. local $" = ', ';
  299. croak "open3(@_): not enough arguments";
  300. }
  301. return _open3 'open3', @_
  302. }
  303. sub spawn_with_handles {
  304. my $fds = shift; # Fields: handle, mode, open_as
  305. my $close_in_child = shift;
  306. my ($fd, %saved, @errs);
  307. foreach $fd (@$fds) {
  308. $fd->{tmp_copy} = IO::Handle->new_from_fd($fd->{handle}, $fd->{mode});
  309. $saved{fileno $fd->{handle}} = $fd->{tmp_copy} if $fd->{tmp_copy};
  310. }
  311. foreach $fd (@$fds) {
  312. bless $fd->{handle}, 'IO::Handle'
  313. unless eval { $fd->{handle}->isa('IO::Handle') } ;
  314. # If some of handles to redirect-to coincide with handles to
  315. # redirect, we need to use saved variants:
  316. my $open_as = $fd->{open_as};
  317. my $fileno = fileno($open_as);
  318. $fd->{handle}->fdopen(defined($fileno)
  319. ? $saved{$fileno} || $open_as
  320. : $open_as,
  321. $fd->{mode});
  322. }
  323. unless ($^O eq 'MSWin32') {
  324. require Fcntl;
  325. # Stderr may be redirected below, so we save the err text:
  326. foreach $fd (@$close_in_child) {
  327. next unless fileno $fd;
  328. fcntl($fd, Fcntl::F_SETFD(), 1) or push @errs, "fcntl $fd: $!"
  329. unless $saved{fileno $fd}; # Do not close what we redirect!
  330. }
  331. }
  332. my $pid;
  333. unless (@errs) {
  334. if (FORCE_DEBUG_SPAWN) {
  335. pipe my $r, my $w or die "Pipe failed: $!";
  336. $pid = fork;
  337. die "Fork failed: $!" unless defined $pid;
  338. if (!$pid) {
  339. { no warnings; exec @_ }
  340. print $w 0 + $!;
  341. close $w;
  342. require POSIX;
  343. POSIX::_exit(255);
  344. }
  345. close $w;
  346. my $bad = <$r>;
  347. if (defined $bad) {
  348. $! = $bad;
  349. undef $pid;
  350. }
  351. } else {
  352. $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
  353. }
  354. if($@) {
  355. push @errs, "IO::Pipe: Can't spawn-NOWAIT: $@";
  356. } elsif(!$pid || $pid < 0) {
  357. push @errs, "IO::Pipe: Can't spawn-NOWAIT: $!";
  358. }
  359. }
  360. # Do this in reverse, so that STDERR is restored first:
  361. foreach $fd (reverse @$fds) {
  362. $fd->{handle}->fdopen($fd->{tmp_copy}, $fd->{mode});
  363. }
  364. foreach (values %saved) {
  365. $_->close or croak "Can't close: $!";
  366. }
  367. croak join "\n", @errs if @errs;
  368. return $pid;
  369. }
  370. 1; # so require is happy