1#!./perl -w 2 3my $child; 4my $can_fork; 5my $has_perlio; 6 7BEGIN { 8 require Config; import Config; 9 $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'}; 10 11 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ && 12 !(($^O eq 'VMS') && $Config{d_socket})) { 13 print "1..0\n"; 14 exit 0; 15 } 16} 17 18{ 19 # This was in the BEGIN block, but since Test::More 0.47 added support to 20 # detect forking, we don't need to fork before Test::More initialises. 21 22 # Too many things in this test will hang forever if something is wrong, 23 # so we need a self destruct timer. And IO can hang despite an alarm. 24 25 if( $can_fork) { 26 my $parent = $$; 27 $child = fork; 28 die "Fork failed" unless defined $child; 29 if (!$child) { 30 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now. 31 my $must_finish_by = time + 60; 32 my $remaining; 33 while (($remaining = $must_finish_by - time) > 0) { 34 sleep $remaining; 35 } 36 warn "Something unexpectedly hung during testing"; 37 kill "INT", $parent or die "Kill failed: $!"; 38 if( $^O eq "cygwin" ) { 39 # sometimes the above isn't enough on cygwin 40 sleep 1; # wait a little, it might have worked after all 41 system( "/bin/kill -f $parent; echo die $parent" ); 42 } 43 exit 1; 44 } 45 } 46 unless ($has_perlio = PerlIO::Layer->can("find") && PerlIO::Layer->find('perlio')) { 47 print <<EOF; 48# Since you don't have perlio you might get failures with UTF-8 locales. 49EOF 50 } 51} 52 53use Socket; 54use Test::More; 55use strict; 56use warnings; 57use Errno; 58 59my $skip_reason; 60 61if( !$Config{d_alarm} ) { 62 plan skip_all => "alarm() not implemented on this platform"; 63} elsif( !$can_fork ) { 64 plan skip_all => "fork() not implemented on this platform"; 65} else { 66 # This should fail but not die if there is real socketpair 67 eval {socketpair LEFT, RIGHT, -1, -1, -1}; 68 if ($@ =~ /^Unsupported socket function "socketpair" called/ || 69 $! =~ /^The operation requested is not supported./) { # Stratus VOS 70 plan skip_all => 'No socketpair (real or emulated)'; 71 } else { 72 eval {AF_UNIX}; 73 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) { 74 plan skip_all => 'No AF_UNIX'; 75 } else { 76 plan tests => 45; 77 } 78 } 79} 80 81# But we'll install an alarm handler in case any of the races below fail. 82$SIG{ALRM} = sub {die "Unexpected alarm during testing"}; 83 84ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC), 85 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)") 86 or print STDERR "# \$\! = $!\n"; 87 88if ($has_perlio) { 89 binmode(LEFT, ":bytes"); 90 binmode(RIGHT, ":bytes"); 91} 92 93my @left = ("hello ", "world\n"); 94my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here. 95 96foreach (@left) { 97 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); 98 is (syswrite (LEFT, $_), length $_, "syswrite to left"); 99} 100foreach (@right) { 101 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); 102 is (syswrite (RIGHT, $_), length $_, "syswrite to right"); 103} 104 105# stream socket, so our writes will become joined: 106my ($buffer, $expect); 107$expect = join '', @right; 108undef $buffer; 109is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); 110is ($buffer, $expect, "content what we expected?"); 111$expect = join '', @left; 112undef $buffer; 113is (read (RIGHT, $buffer, length $expect), length $expect, "read on right"); 114is ($buffer, $expect, "content what we expected?"); 115 116ok (shutdown(LEFT, SHUT_WR), "shutdown left for writing"); 117# This will hang forever if eof is buggy, and alarm doesn't interrupt system 118# Calls. Hence the child process minder. 119SKIP: { 120 skip "SCO Unixware / OSR have a bug with shutdown",2 if $^O =~ /^(?:svr|sco)/; 121 local $SIG{ALRM} = sub { warn "EOF on right took over 3 seconds" }; 122 local $TODO = "Known problems with unix sockets on $^O" 123 if $^O eq 'hpux' || $^O eq 'super-ux'; 124 alarm 3; 125 $! = 0; 126 ok (eof RIGHT, "right is at EOF"); 127 local $TODO = "Known problems with unix sockets on $^O" 128 if $^O eq 'unicos' || $^O eq 'unicosmk'; 129 is ($!, '', 'and $! should report no error'); 130 alarm 60; 131} 132 133my $err = $!; 134$SIG{PIPE} = 'IGNORE'; 135{ 136 local $SIG{ALRM} = 137 sub { warn "syswrite to left didn't fail within 3 seconds" }; 138 alarm 3; 139 # Split the system call from the is() - is() does IO so 140 # (say) a flush may do a seek which on a pipe may disturb errno 141 my $ans = syswrite (LEFT, "void"); 142 $err = $!; 143 is ($ans, undef, "syswrite to shutdown left should fail"); 144 alarm 60; 145} 146{ 147 # This may need skipping on some OSes - restoring value saved above 148 # should help 149 $! = $err; 150 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN') 151 or printf STDERR "# \$\! = %d (%s)\n", $err, $err; 152} 153 154my @gripping = (chr 255, chr 127); 155foreach (@gripping) { 156 is (syswrite (RIGHT, $_), length $_, "syswrite to right"); 157} 158 159ok (!eof LEFT, "left is not at EOF"); 160 161$expect = join '', @gripping; 162undef $buffer; 163is (read (LEFT, $buffer, length $expect), length $expect, "read on left"); 164is ($buffer, $expect, "content what we expected?"); 165 166ok (close LEFT, "close left"); 167ok (close RIGHT, "close right"); 168 169 170# And now datagrams 171# I suspect we also need a self destruct time-bomb for these, as I don't see any 172# guarantee that the stack won't drop a UDP packet, even if it is for localhost. 173 174SKIP: { 175 skip "No usable SOCK_DGRAM for socketpair", 24 if ($^O =~ /^(MSWin32|os2)\z/); 176 skip "alarm doesn't interrupt I/O on this Perl", 24 if "$]" < 5.008; 177 local $TODO = "socketpair not supported on $^O" if $^O eq 'nto'; 178 179 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC), 180 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)") 181 or print STDERR "# \$\! = $!\n"; 182 183 if ($has_perlio) { 184 binmode(LEFT, ":bytes"); 185 binmode(RIGHT, ":bytes"); 186 } 187 188 foreach (@left) { 189 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left"); 190 is (syswrite (LEFT, $_), length $_, "syswrite to left"); 191 } 192 foreach (@right) { 193 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right"); 194 is (syswrite (RIGHT, $_), length $_, "syswrite to right"); 195 } 196 197 # stream socket, so our writes will become joined: 198 my ($total); 199 $total = join '', @right; 200 foreach $expect (@right) { 201 undef $buffer; 202 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); 203 is ($buffer, $expect, "content what we expected?"); 204 } 205 $total = join '', @left; 206 foreach $expect (@left) { 207 undef $buffer; 208 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right"); 209 is ($buffer, $expect, "content what we expected?"); 210 } 211 212 ok (shutdown(LEFT, 1), "shutdown left for writing"); 213 214 # eof uses buffering. eof is indicated by a sysread of zero. 215 # but for a datagram socket there's no way it can know nothing will ever be 216 # sent 217 SKIP: { 218 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390'); 219 220 my $alarmed = 0; 221 local $SIG{ALRM} = sub { $alarmed = 1; }; 222 print "# Approximate forever as 3 seconds. Wait 'forever'...\n"; 223 alarm 3; 224 undef $buffer; 225 is (sysread (RIGHT, $buffer, 1), undef, 226 "read on right should be interrupted"); 227 is ($alarmed, 1, "alarm should have fired"); 228 } 229 230 alarm 30; 231 232 foreach (@gripping) { 233 is (syswrite (RIGHT, $_), length $_, "syswrite to right"); 234 } 235 236 $total = join '', @gripping; 237 foreach $expect (@gripping) { 238 undef $buffer; 239 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left"); 240 is ($buffer, $expect, "content what we expected?"); 241 } 242 243 ok (close LEFT, "close left"); 244 ok (close RIGHT, "close right"); 245 246} # end of DGRAM SKIP 247 248kill "INT", $child or warn "Failed to kill child process $child: $!"; 249exit 0; 250