This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldiag: Reflow for better splain output
[perl5.git] / t / run / cloexec.t
CommitLineData
742218b3
AS
1#!./perl
2#
3# Test inheriting file descriptors across exec (close-on-exec).
4#
5# perlvar describes $^F aka $SYSTEM_FD_MAX as follows:
6#
7# The maximum system file descriptor, ordinarily 2. System file
8# descriptors are passed to exec()ed processes, while higher file
9# descriptors are not. Also, during an open(), system file descriptors
10# are preserved even if the open() fails. (Ordinary file descriptors
11# are closed before the open() is attempted.) The close-on-exec
12# status of a file descriptor will be decided according to the value of
13# C<$^F> when the corresponding file, pipe, or socket was opened, not
14# the time of the exec().
15#
16# This documented close-on-exec behaviour is typically implemented in
17# various places (e.g. pp_sys.c) with code something like:
18#
19# #if defined(HAS_FCNTL) && defined(F_SETFD)
20# fcntl(fd, F_SETFD, fd > PL_maxsysfd); /* ensure close-on-exec */
21# #endif
22#
23# This behaviour, therefore, is only currently implemented for platforms
24# where:
25#
26# a) HAS_FCNTL and F_SETFD are both defined
27# b) Integer fds are native OS handles
28#
29# ... which is typically just the Unix-like platforms.
30#
31# Notice that though integer fds are supported by the C runtime library
32# on Windows, they are not native OS handles, and so are not inherited
33# across an exec (though native Windows file handles are).
34
35BEGIN {
36 chdir 't' if -d 't';
37 @INC = '../lib';
742218b3 38 require './test.pl';
9c8416b2 39 skip_all_without_config('d_fcntl');
742218b3
AS
40}
41
42use strict;
43
44$|=1;
45
46my $Is_VMS = $^O eq 'VMS';
742218b3 47my $Is_Win32 = $^O eq 'MSWin32';
742218b3
AS
48
49# When in doubt, skip.
742218b3 50skip_all("VMS") if $Is_VMS;
742218b3
AS
51skip_all("Win32") if $Is_Win32;
52
53sub make_tmp_file {
54 my ($fname, $fcontents) = @_;
55 local *FHTMP;
56 open FHTMP, ">$fname" or die "open '$fname': $!";
57 print FHTMP $fcontents or die "print '$fname': $!";
58 close FHTMP or die "close '$fname': $!";
59}
60
61my $Perl = which_perl();
62my $quote = $Is_VMS || $Is_Win32 ? '"' : "'";
63
2d90ac95
NC
64my $tmperr = tempfile();
65my $tmpfile1 = tempfile();
66my $tmpfile2 = tempfile();
742218b3
AS
67my $tmpfile1_contents = "tmpfile1 line 1\ntmpfile1 line 2\n";
68my $tmpfile2_contents = "tmpfile2 line 1\ntmpfile2 line 2\n";
69make_tmp_file($tmpfile1, $tmpfile1_contents);
70make_tmp_file($tmpfile2, $tmpfile2_contents);
71
72# $Child_prog is the program run by the child that inherits the fd.
73# Note: avoid using ' or " in $Child_prog since it is run with -e
74my $Child_prog = <<'CHILD_PROG';
75my $fd = shift;
76print qq{childfd=$fd\n};
77open INHERIT, qq{<&=$fd} or die qq{open $fd: $!};
78my $line = <INHERIT>;
79close INHERIT or die qq{close $fd: $!};
80print $line
81CHILD_PROG
82$Child_prog =~ tr/\n//d;
83
dcb5c535 84plan(tests => 22);
742218b3
AS
85
86sub test_not_inherited {
87 my $expected_fd = shift;
88 ok( -f $tmpfile2, "tmpfile '$tmpfile2' exists" );
dcb5c535 89 my $cmd = qq{$Perl -e $quote$Child_prog$quote $expected_fd};
742218b3
AS
90 # Expect 'Bad file descriptor' or similar to be written to STDERR.
91 local *SAVERR; open SAVERR, ">&STDERR"; # save original STDERR
92 open STDERR, ">$tmperr" or die "open '$tmperr': $!";
93 my $out = `$cmd`;
94 my $rc = $? >> 8;
95 open STDERR, ">&SAVERR" or die "error: restore STDERR: $!";
96 close SAVERR or die "error: close SAVERR: $!";
dcb5c535
AS
97 # XXX: it seems one cannot rely on a non-zero return code,
98 # at least not on Tru64.
99 # cmp_ok( $rc, '!=', 0,
100 # "child return code=$rc (non-zero means cannot inherit fd=$expected_fd)" );
101 cmp_ok( $out =~ tr/\n//, '==', 1,
102 "child stdout: has 1 newline (rc=$rc, should be non-zero)" );
742218b3 103 is( $out, "childfd=$expected_fd\n", 'child stdout: fd' );
742218b3
AS
104}
105
106sub test_inherited {
107 my $expected_fd = shift;
108 ok( -f $tmpfile1, "tmpfile '$tmpfile1' exists" );
dcb5c535 109 my $cmd = qq{$Perl -e $quote$Child_prog$quote $expected_fd};
742218b3
AS
110 my $out = `$cmd`;
111 my $rc = $? >> 8;
112 cmp_ok( $rc, '==', 0,
dcb5c535 113 "child return code=$rc (zero means inherited fd=$expected_fd ok)" );
742218b3
AS
114 my @lines = split(/^/, $out);
115 cmp_ok( $out =~ tr/\n//, '==', 2, 'child stdout: has 2 newlines' );
116 cmp_ok( scalar(@lines), '==', 2, 'child stdout: split into 2 lines' );
117 is( $lines[0], "childfd=$expected_fd\n", 'child stdout: fd' );
118 is( $lines[1], "tmpfile1 line 1\n", 'child stdout: line 1' );
742218b3
AS
119}
120
121$^F == 2 or print STDERR "# warning: \$^F is $^F (not 2)\n";
122
dcb5c535
AS
123# Should not be able to inherit > $^F in the default case.
124open FHPARENT2, "<$tmpfile2" or die "open '$tmpfile2': $!";
125my $parentfd2 = fileno FHPARENT2;
126defined $parentfd2 or die "fileno: $!";
127cmp_ok( $parentfd2, '>', $^F, "parent open fd=$parentfd2 (\$^F=$^F)" );
128test_not_inherited($parentfd2);
129close FHPARENT2 or die "close '$tmpfile2': $!";
130
131# Should be able to inherit $^F after setting to $parentfd2
132# Need to set $^F before open because close-on-exec set at time of open.
133$^F = $parentfd2;
134open FHPARENT1, "<$tmpfile1" or die "open '$tmpfile1': $!";
135my $parentfd1 = fileno FHPARENT1;
136defined $parentfd1 or die "fileno: $!";
137cmp_ok( $parentfd1, '<=', $^F, "parent open fd=$parentfd1 (\$^F=$^F)" );
138test_inherited($parentfd1);
139close FHPARENT1 or die "close '$tmpfile1': $!";
742218b3 140
c7fb9cc8 141# ... and test that you cannot inherit fd = $^F+n.
742218b3 142open FHPARENT1, "<$tmpfile1" or die "open '$tmpfile1': $!";
dcb5c535
AS
143open FHPARENT2, "<$tmpfile2" or die "open '$tmpfile2': $!";
144$parentfd2 = fileno FHPARENT2;
145defined $parentfd2 or die "fileno: $!";
146cmp_ok( $parentfd2, '>', $^F, "parent open fd=$parentfd2 (\$^F=$^F)" );
147test_not_inherited($parentfd2);
148close FHPARENT2 or die "close '$tmpfile2': $!";
742218b3 149close FHPARENT1 or die "close '$tmpfile1': $!";
dcb5c535 150
742218b3 151# ... and now you can inherit after incrementing.
c7fb9cc8 152$^F = $parentfd2;
742218b3 153open FHPARENT2, "<$tmpfile2" or die "open '$tmpfile2': $!";
dcb5c535
AS
154open FHPARENT1, "<$tmpfile1" or die "open '$tmpfile1': $!";
155$parentfd1 = fileno FHPARENT1;
156defined $parentfd1 or die "fileno: $!";
157cmp_ok( $parentfd1, '<=', $^F, "parent open fd=$parentfd1 (\$^F=$^F)" );
158test_inherited($parentfd1);
159close FHPARENT1 or die "close '$tmpfile1': $!";
742218b3 160close FHPARENT2 or die "close '$tmpfile2': $!";