Commit | Line | Data |
cf7fe8a2 |
1 | #!./perl |
2 | |
3 | BEGIN { |
4 | unless(grep /blib/, @INC) { |
5 | chdir 't' if -d 't'; |
20822f61 |
6 | @INC = '../lib'; |
cf7fe8a2 |
7 | } |
8 | } |
9 | |
10 | use Config; |
11 | |
12 | BEGIN { |
90b9a713 |
13 | my $reason; |
14 | if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) { |
15 | $reason = 'Socket extension unavailable'; |
16 | } |
17 | elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) { |
18 | $reason = 'IO extension unavailable'; |
19 | } |
20 | elsif ($^O eq 'os2') { |
21 | require IO::Socket; |
22 | |
23 | eval {IO::Socket::pack_sockaddr_un('/foo/bar') || 1} |
24 | or $@ !~ /not implemented/ or |
25 | $reason = 'compiled without TCP/IP stack v4'; |
26 | } |
27 | elsif ($^O =~ m/^(?:qnx|nto|vos)$/ ) { |
28 | $reason = 'Not implemented'; |
29 | } |
30 | elsif (! $Config{'d_fork'}) { |
31 | $reason = 'no fork'; |
32 | } |
33 | if ($reason) { |
34 | print "1..0 # Skip: $reason\n"; |
35 | exit 0; |
cf7fe8a2 |
36 | } |
37 | } |
38 | |
2359510d |
39 | $PATH = "sock-$$"; |
cf7fe8a2 |
40 | |
41 | # Test if we can create the file within the tmp directory |
202975e6 |
42 | if (-e $PATH or not open(TEST, ">$PATH") and $^O ne 'os2') { |
43 | print "1..0 # Skip: cannot open '$PATH' for write\n"; |
cf7fe8a2 |
44 | exit 0; |
45 | } |
46 | close(TEST); |
202975e6 |
47 | unlink($PATH) or $^O eq 'os2' or die "Can't unlink $PATH: $!"; |
cf7fe8a2 |
48 | |
49 | # Start testing |
50 | $| = 1; |
51 | print "1..5\n"; |
52 | |
53 | use IO::Socket; |
54 | |
cb7854e0 |
55 | $listen = IO::Socket::UNIX->new(Local => $PATH, Listen => 0); |
56 | |
57 | # Sometimes UNIX filesystems are mounted for security reasons |
58 | # with "nodev" option which spells out "no" for creating UNIX |
59 | # local sockets. Therefore we will retry with a File::Temp |
60 | # generated filename from a temp directory. |
61 | unless (defined $listen) { |
62 | eval { require File::Temp }; |
63 | unless ($@) { |
64 | import File::Temp 'mktemp'; |
65 | for my $TMPDIR ($ENV{TMPDIR}, "/tmp") { |
66 | if (defined $TMPDIR && -d $TMPDIR && -w $TMPDIR) { |
67 | $PATH = mktemp("$TMPDIR/sXXXXXXXX"); |
68 | last if $listen = IO::Socket::UNIX->new(Local => $PATH, |
69 | Listen => 0); |
70 | } |
71 | } |
72 | } |
73 | defined $listen or die "$PATH: $!"; |
74 | } |
cf7fe8a2 |
75 | print "ok 1\n"; |
76 | |
77 | if($pid = fork()) { |
78 | |
79 | $sock = $listen->accept(); |
cf7fe8a2 |
80 | |
c46b27b8 |
81 | if (defined $sock) { |
82 | print "ok 2\n"; |
cf7fe8a2 |
83 | |
c46b27b8 |
84 | print $sock->getline(); |
cf7fe8a2 |
85 | |
c46b27b8 |
86 | print $sock "ok 4\n"; |
cf7fe8a2 |
87 | |
c46b27b8 |
88 | $sock->close; |
cf7fe8a2 |
89 | |
c46b27b8 |
90 | waitpid($pid,0); |
91 | unlink($PATH) || $^O eq 'os2' || warn "Can't unlink $PATH: $!"; |
cf7fe8a2 |
92 | |
c46b27b8 |
93 | print "ok 5\n"; |
94 | } else { |
95 | print "# accept() failed: $!\n"; |
96 | for (2..5) { |
97 | print "not ok $_ # accept failed\n"; |
98 | } |
99 | } |
cf7fe8a2 |
100 | } elsif(defined $pid) { |
101 | |
102 | $sock = IO::Socket::UNIX->new(Peer => $PATH) or die "$!"; |
103 | |
104 | print $sock "ok 3\n"; |
105 | |
106 | print $sock->getline(); |
107 | |
108 | $sock->close; |
109 | |
110 | exit; |
111 | } else { |
112 | die; |
113 | } |