3 # If a read or write is interrupted by a signal, Perl will call the
4 # signal handler and then attempt to restart the call. If the handler does
5 # something nasty like close the handle or pop layers, make sure that the
6 # read/write handles this gracefully (for some definition of 'graceful':
7 # principally, don't segfault).
13 skip_all_without_dynamic_extension('Fcntl');
26 skip_all('pipe not implemented');
29 unless (exists $Config{'d_alarm'}) {
30 skip_all('alarm not implemented');
34 # XXX for some reason the stdio layer doesn't seem to interrupt
35 # write system call when the alarm triggers. This makes the tests
38 if (exists $ENV{PERLIO} && $ENV{PERLIO} =~ /stdio/ ) {
39 skip_all('stdio not supported for this script');
43 # on Win32, alarm() won't interrupt the read/write call.
44 # Similar issues with VMS.
45 # On FreeBSD, writes to pipes of 8192 bytes or more use a mechanism
46 # that is not interruptible (see perl #85842 and #84688).
47 # "close during print" also hangs on Solaris 8 (but not 10 or 11).
49 # Also skip on release builds, to avoid other possibly problematic
52 my ($osmajmin) = $Config{osvers} =~ /^(\d+\.\d+)/;
53 if ($^O eq 'VMS' || $^O eq 'MSWin32' || $^O eq 'cygwin' || $^O =~ /freebsd/ || $^O eq 'midnightbsd' ||
54 ($^O eq 'solaris' && $Config{osvers} eq '2.8') || $^O eq 'nto' ||
55 ($^O eq 'darwin' && $osmajmin < 9) ||
56 ((int($]*1000) & 1) == 0)
58 skip_all('various portability issues');
62 my ($in, $out, $st, $sigst, $buf);
67 # make two handles that will always block
70 close $in if $in; close $out if $out;
71 undef $in; undef $out; # use fresh handles each time
76 $SIG{PIPE} = 'IGNORE';
81 $SIG{ALRM} = sub { $sigst = close($in) ? "ok" : "nok" };
83 $st = read($in, $buf, 1);
85 is($sigst, 'ok', 'read/close: sig handler close status');
86 ok(!$st, 'read/close: read status');
87 ok(!close($in), 'read/close: close status');
92 $SIG{ALRM} = sub { die };
94 $st = eval { read($in, $buf, 1) };
96 ok(!$st, 'read/die: read status');
97 ok(close($in), 'read/die: close status');
99 # This used to be 1_000_000, but on Linux/ppc64 (POWER7) this kept
100 # consistently failing. At exactly 0x100000 it started passing
101 # again. Now we're asking the kernel what the pipe buffer is, and if
102 # that fails, hoping this number is bigger than any pipe buffer.
103 my $surely_this_arbitrary_number_is_fine = (eval {
104 use Fcntl qw(F_GETPIPE_SZ);
105 fcntl($out, F_GETPIPE_SZ, 0);
111 $SIG{ALRM} = sub { $sigst = close($out) ? "ok" : "nok" };
112 $buf = "a" x $surely_this_arbitrary_number_is_fine . "\n";
113 select $out; $| = 1; select STDOUT;
115 $st = print $out $buf;
117 is($sigst, 'nok', 'print/close: sig handler close status');
118 ok(!$st, 'print/close: print status');
119 ok(!close($out), 'print/close: close status');
124 $SIG{ALRM} = sub { die };
125 $buf = "a" x $surely_this_arbitrary_number_is_fine . "\n";
126 select $out; $| = 1; select STDOUT;
128 $st = eval { print $out $buf };
130 ok(!$st, 'print/die: print status');
131 # the close will hang since there's data to flush, so use alarm
133 ok(!eval {close($out)}, 'print/die: close status');
138 # Apparently there's nothing in standard Linux that can cause an
139 # EINTR in close(2); but run the code below just in case it does on some
140 # platform, just to see if it segfaults.
142 $SIG{ALRM} = sub { $sigst = close($in) ? "ok" : "nok" };
150 $SIG{ALRM} = sub { die };
155 # vim: ts=4 sts=4 sw=4: