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