]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Syscall.pm
syscall: fix i386/i686 detection
[public-inbox.git] / lib / PublicInbox / Syscall.pm
index 22b779ad6933464e327340593669439e12b3aef6..530ee93bbb39a88e4be4415810bb5a87845a83e1 100644 (file)
 # You may distribute under the terms of either the GNU General Public
 # License or the Artistic License, as specified in the Perl README file.
 package PublicInbox::Syscall;
-use strict;
-use v5.10.1;
+use v5.12;
 use parent qw(Exporter);
 use POSIX qw(ENOENT ENOSYS EINVAL O_NONBLOCK);
 use Socket qw(SOL_SOCKET SCM_RIGHTS);
 use Config;
+our %SIGNUM = (WINCH => 28); # most Linux, {Free,Net,Open}BSD, *Darwin
 
 # $VERSION = '0.25'; # Sys::Syscall version
 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
                   EPOLLIN EPOLLOUT EPOLLET
                   EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
                   EPOLLONESHOT EPOLLEXCLUSIVE
-                  signalfd rename_noreplace);
+                  signalfd rename_noreplace %SIGNUM);
 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
                              EPOLLIN EPOLLOUT
                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
@@ -60,21 +60,6 @@ use constant {
 };
 
 my @BYTES_4_hole = BYTES_4_hole ? (0) : ();
-our $loaded_syscall = 0;
-
-sub _load_syscall {
-    # props to Gaal for this!
-    return if $loaded_syscall++;
-    my $clean = sub {
-        delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
-                        _h2ph_pre.ph sys/syscall.ph>};
-    };
-    $clean->(); # don't trust modules before us
-    my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
-    $clean->(); # don't require modules after us trust us
-    $rv;
-}
-
 
 our (
      $SYS_epoll_create,
@@ -98,15 +83,16 @@ if ($^O eq "linux") {
     # boundaries.
     my $u64_mod_8 = 0;
 
-    # if we're running on an x86_64 kernel, but a 32-bit process,
-    # we need to use the x32 or i386 syscall numbers.
-    if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
-        $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
-    }
-
-    # Similarly for mips64 vs mips
-    if ($machine eq "mips64" && $Config{ptrsize} == 4) {
-        $machine = "mips";
+    if ($Config{ptrsize} == 4) {
+       # if we're running on an x86_64 kernel, but a 32-bit process,
+       # we need to use the x32 or i386 syscall numbers.
+       if ($machine eq 'x86_64') {
+           my $s = $Config{cppsymbols};
+           $machine = ($s =~ /\b__ILP32__=1\b/ && $s =~ /\b__x86_64__=1\b/) ?
+                               'x32' : 'i386'
+       } elsif ($machine eq 'mips64') { # similarly for mips64 vs mips
+           $machine = 'mips';
+       }
     }
 
     if ($machine =~ m/^i[3456]86$/) {
@@ -161,6 +147,7 @@ if ($^O eq "linux") {
         $SYS_epoll_wait   = 226;
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 309;
+        $SIGNUM{WINCH} = 23;
     } elsif ($machine =~ m/^ppc64/) {
         $SYS_epoll_create = 236;
         $SYS_epoll_ctl    = 237;
@@ -207,7 +194,7 @@ if ($^O eq "linux") {
         $u64_mod_8        = 1;
         $SYS_signalfd4 = 484;
        $SFD_CLOEXEC = 010000000;
-    } elsif ($machine eq 'aarch64' || $machine eq 'loongarch64') {
+    } elsif ($machine =~ /\A(?:loong)?aarch64\z/ || $machine eq 'riscv64') {
         $SYS_epoll_create = 20;  # (sys_epoll_create1)
         $SYS_epoll_ctl    = 21;
         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
@@ -254,20 +241,14 @@ if ($^O eq "linux") {
        $SYS_recvmsg = 4177;
        $FS_IOC_GETFLAGS = 0x40046601;
        $FS_IOC_SETFLAGS = 0x80046602;
+       $SIGNUM{WINCH} = 20;
     } else {
-        # as a last resort, try using the *.ph files which may not
-        # exist or may be wrong
-        _load_syscall();
-        $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
-        $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
-        $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
-
-       # Note: do NOT add new syscalls to depend on *.ph, here.
-       # Better to miss syscalls (so we can fallback to IO::Poll)
-       # than to use wrong ones, since the names are not stable
-       # (at least not on FreeBSD), if the actual numbers are.
+        warn <<EOM;
+machine=$machine ptrsize=$Config{ptrsize} has no syscall definitions
+git clone https://80x24.org/public-inbox.git and
+Send the output of ./devel/syscall-list to meta\@public-inbox.org
+EOM
     }
-
     if ($u64_mod_8) {
         *epoll_wait = \&epoll_wait_mod8;
         *epoll_ctl = \&epoll_ctl_mod8;
@@ -278,14 +259,12 @@ if ($^O eq "linux") {
 }
 # use Inline::C for *BSD-only or general POSIX stuff.
 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
-# use scripts/syscall-list on Linux to detect new syscall numbers
+# use devel/syscall-list on Linux to detect new syscall numbers
 
 ############################################################################
 # epoll functions
 ############################################################################
 
-sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
-
 sub epoll_create {
        syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
 }
@@ -455,7 +434,7 @@ no warnings 'once';
 
 *recv_cmd4 = sub ($$$) {
        my ($sock, undef, $len) = @_;
-       vec($_[1], ($len + 1) * 8, 1) = 0;
+       vec($_[1] //= '', ($len + 1) * 8, 1) = 0;
        my $cmsghdr = "\0" x msg_controllen; # 10 * sizeof(int)
        my $iov = pack('P'.TMPL_size_t, $_[1], $len);
        my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))