Check unix domain socket functionality
Michal Josef Špaček [Mon, 26 Jul 2021 11:25:41 +0000 (13:25 +0200)]
This code check is from
https://metacpan.org/release/TODDR/IO-1.45/source/t/io_unix.t

perl/Makefile.PL
perl/t/02-unix_domain_socket.t

index 0239aa5..a389f0c 100644 (file)
@@ -138,9 +138,10 @@ WriteMakefile(
     PM            => {'FCGI.pm' => '$(INST_ARCHLIBDIR)/FCGI.pm'},
     PREREQ_PM     => {'XSLoader' => '0'},
     TEST_REQUIRES => {
+        'Config' => 0,
         'FCGI::Client' => 0.09,
         'File::Temp' => 0,
-        'IO::Socket::UNIX' => 0,
+        'IO::Socket' => 0,
         'Test::More' => 0,
     },
     @extras,
index c30af69..fa00a9c 100644 (file)
@@ -3,12 +3,52 @@
 use strict;
 use warnings;
 
+use Config;
 use FCGI;
 use FCGI::Client;
 use File::Temp qw(tempfile);
-use IO::Socket::UNIX;
+use IO::Socket;
 use Test::More 'tests' => 4;
 
+BEGIN {
+       my $reason;
+       my $can_fork = $Config{d_fork}
+               || (
+                       ($^O eq 'MSWin32' || $^O eq 'NetWare')
+                       and $Config{useithreads}
+                       and $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/
+               );
+       if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bSocket\b/) {
+               $reason = 'Socket extension unavailable';
+       } elsif ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bIO\b/) {
+               $reason = 'IO extension unavailable';
+       } elsif ($^O eq 'os2') {
+               eval { IO::Socket::pack_sockaddr_un('/foo/bar') || 1 };
+               if ($@ !~ /not implemented/) {
+                       $reason = 'compiled without TCP/IP stack v4';
+               }
+       } elsif ($^O =~ m/^(?:qnx|nto|vos)$/ ) {
+               $reason = "UNIX domain sockets not implemented on $^O";
+       } elsif (! $can_fork) {
+               $reason = 'no fork';
+       } elsif ($^O eq 'MSWin32') {
+               if ($ENV{CONTINUOUS_INTEGRATION}) {
+                       # https://github.com/Perl/perl5/issues/17429
+                       $reason = 'Skipping on Windows CI';
+               } else {
+                       # https://github.com/Perl/perl5/issues/17575
+                       if (! eval { socket(my $sock, PF_UNIX, SOCK_STREAM, 0) }) {
+                               $reason = "AF_UNIX unavailable or disabled on this platform"
+                       }
+               }
+       }
+
+       if ($reason) {
+               print "1..0 # Skip: $reason\n";
+               exit 0;
+       }
+}
+
 my (undef, $unix_socket_file) = tempfile();
 my $fcgi_socket = FCGI::OpenSocket($unix_socket_file, 5);