8 require Config; import Config;
9 $can_fork = $Config{'d_fork'} || $Config{'d_pseudofork'};
11 if ($^O eq "hpux" or $Config{'extensions'} !~ /\bSocket\b/ &&
12 !(($^O eq 'VMS') && $Config{d_socket})) {
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.
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.
28 die "Fork failed" unless defined $child;
30 $SIG{INT} = sub {exit 0}; # You have 60 seconds. Your time starts now.
31 my $must_finish_by = time + 60;
33 while (($remaining = $must_finish_by - time) > 0) {
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" );
46 unless ($has_perlio = PerlIO::Layer->can("find") && PerlIO::Layer->find('perlio')) {
48 # Since you don't have perlio you might get failures with UTF-8 locales.
61 if( !$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";
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)';
73 if ($@ =~ /^Your vendor has not defined Socket macro AF_UNIX/) {
74 plan skip_all => 'No AF_UNIX';
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"};
84 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC),
85 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_STREAM, PF_UNSPEC)")
86 or print "# \$\! = $!\n";
89 binmode(LEFT, ":bytes");
90 binmode(RIGHT, ":bytes");
93 my @left = ("hello ", "world\n");
94 my @right = ("perl ", "rules!"); # Not like I'm trying to bias any survey here.
97 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
98 is (syswrite (LEFT, $_), length $_, "syswrite to left");
101 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
102 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
105 # stream socket, so our writes will become joined:
106 my ($buffer, $expect);
107 $expect = join '', @right;
109 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
110 is ($buffer, $expect, "content what we expected?");
111 $expect = join '', @left;
113 is (read (RIGHT, $buffer, length $expect), length $expect, "read on right");
114 is ($buffer, $expect, "content what we expected?");
116 ok (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.
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';
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');
134 $SIG{PIPE} = 'IGNORE';
137 sub { warn "syswrite to left didn't fail within 3 seconds" };
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");
143 is ($ans, undef, "syswrite to shutdown left should fail");
147 # This may need skipping on some OSes - restoring value saved above
150 ok (($!{EPIPE} or $!{ESHUTDOWN}), '$! should be EPIPE or ESHUTDOWN')
151 or printf "\$\!=%d(%s)\n", $err, $err;
154 my @gripping = (chr 255, chr 127);
155 foreach (@gripping) {
156 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
159 ok (!eof LEFT, "left is not at EOF");
161 $expect = join '', @gripping;
163 is (read (LEFT, $buffer, length $expect), length $expect, "read on left");
164 is ($buffer, $expect, "content what we expected?");
166 ok (close LEFT, "close left");
167 ok (close RIGHT, "close right");
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.
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';
179 ok (socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC),
180 "socketpair (LEFT, RIGHT, AF_UNIX, SOCK_DGRAM, PF_UNSPEC)")
181 or print "# \$\! = $!\n";
184 binmode(LEFT, ":bytes");
185 binmode(RIGHT, ":bytes");
189 # is (syswrite (LEFT, $_), length $_, "write " . _qq ($_) . " to left");
190 is (syswrite (LEFT, $_), length $_, "syswrite to left");
193 # is (syswrite (RIGHT, $_), length $_, "write " . _qq ($_) . " to right");
194 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
197 # stream socket, so our writes will become joined:
199 $total = join '', @right;
200 foreach $expect (@right) {
202 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
203 is ($buffer, $expect, "content what we expected?");
205 $total = join '', @left;
206 foreach $expect (@left) {
208 is (sysread (RIGHT, $buffer, length $total), length $expect, "read on right");
209 is ($buffer, $expect, "content what we expected?");
212 ok (shutdown(LEFT, 1), "shutdown left for writing");
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
218 skip "$^O does length 0 udp reads", 2 if ($^O eq 'os390');
221 local $SIG{ALRM} = sub { $alarmed = 1; };
222 print "# Approximate forever as 3 seconds. Wait 'forever'...\n";
225 is (sysread (RIGHT, $buffer, 1), undef,
226 "read on right should be interrupted");
227 is ($alarmed, 1, "alarm should have fired");
232 foreach (@gripping) {
233 is (syswrite (RIGHT, $_), length $_, "syswrite to right");
236 $total = join '', @gripping;
237 foreach $expect (@gripping) {
239 is (sysread (LEFT, $buffer, length $total), length $expect, "read on left");
240 is ($buffer, $expect, "content what we expected?");
243 ok (close LEFT, "close left");
244 ok (close RIGHT, "close right");
246 } # end of DGRAM SKIP
248 kill "INT", $child or warn "Failed to kill child process $child: $!";