PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/POSIX/Open3.pm

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