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