1 # This is a fork of the (for now) unmaintained Sys::Syscall 0.25,
2 # specifically the Debian libsys-syscall-perl 0.25-6 version to
3 # fix upstream regressions in 0.25.
5 # This license differs from the rest of public-inbox
7 # This module is Copyright (c) 2005 Six Apart, Ltd.
8 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
10 # All rights reserved.
12 # You may distribute under the terms of either the GNU General Public
13 # License or the Artistic License, as specified in the Perl README file.
14 package PublicInbox::Syscall;
16 use POSIX qw(ENOSYS SEEK_CUR);
20 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
24 @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
25 EPOLLIN EPOLLOUT EPOLLET
26 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
27 EPOLLONESHOT EPOLLEXCLUSIVE);
28 %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
30 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
31 EPOLLONESHOT EPOLLEXCLUSIVE)],
34 use constant EPOLLIN => 1;
35 use constant EPOLLOUT => 4;
36 # use constant EPOLLERR => 8;
37 # use constant EPOLLHUP => 16;
38 # use constant EPOLLRDBAND => 128;
39 use constant EPOLLEXCLUSIVE => (1 << 28);
40 use constant EPOLLONESHOT => (1 << 30);
41 use constant EPOLLET => (1 << 31);
42 use constant EPOLL_CTL_ADD => 1;
43 use constant EPOLL_CTL_DEL => 2;
44 use constant EPOLL_CTL_MOD => 3;
46 our $loaded_syscall = 0;
49 # props to Gaal for this!
50 return if $loaded_syscall++;
52 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
53 _h2ph_pre.ph sys/syscall.ph>};
55 $clean->(); # don't trust modules before us
56 my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
57 $clean->(); # don't require modules after us trust us
68 our $no_deprecated = 0;
71 my $machine = (POSIX::uname())[-1];
72 # whether the machine requires 64-bit numbers to be on 8-byte
76 # if we're running on an x86_64 kernel, but a 32-bit process,
77 # we need to use the i386 syscall numbers.
78 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
82 # Similarly for mips64 vs mips
83 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
87 if ($machine =~ m/^i[3456]86$/) {
88 $SYS_epoll_create = 254;
90 $SYS_epoll_wait = 256;
91 } elsif ($machine eq "x86_64") {
92 $SYS_epoll_create = 213;
94 $SYS_epoll_wait = 232;
95 } elsif ($machine =~ m/^parisc/) {
96 $SYS_epoll_create = 224;
98 $SYS_epoll_wait = 226;
100 } elsif ($machine =~ m/^ppc64/) {
101 $SYS_epoll_create = 236;
102 $SYS_epoll_ctl = 237;
103 $SYS_epoll_wait = 238;
105 } elsif ($machine eq "ppc") {
106 $SYS_epoll_create = 236;
107 $SYS_epoll_ctl = 237;
108 $SYS_epoll_wait = 238;
110 } elsif ($machine =~ m/^s390/) {
111 $SYS_epoll_create = 249;
112 $SYS_epoll_ctl = 250;
113 $SYS_epoll_wait = 251;
115 } elsif ($machine eq "ia64") {
116 $SYS_epoll_create = 1243;
117 $SYS_epoll_ctl = 1244;
118 $SYS_epoll_wait = 1245;
120 } elsif ($machine eq "alpha") {
121 # natural alignment, ints are 32-bits
122 $SYS_epoll_create = 407;
123 $SYS_epoll_ctl = 408;
124 $SYS_epoll_wait = 409;
126 } elsif ($machine eq "aarch64") {
127 $SYS_epoll_create = 20; # (sys_epoll_create1)
129 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
132 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
134 $SYS_epoll_create = 250;
135 $SYS_epoll_ctl = 251;
136 $SYS_epoll_wait = 252;
138 } elsif ($machine =~ m/^mips64/) {
139 $SYS_epoll_create = 5207;
140 $SYS_epoll_ctl = 5208;
141 $SYS_epoll_wait = 5209;
143 } elsif ($machine =~ m/^mips/) {
144 $SYS_epoll_create = 4248;
145 $SYS_epoll_ctl = 4249;
146 $SYS_epoll_wait = 4250;
149 # as a last resort, try using the *.ph files which may not
150 # exist or may be wrong
152 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
153 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
154 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
158 *epoll_wait = \&epoll_wait_mod8;
159 *epoll_ctl = \&epoll_ctl_mod8;
161 *epoll_wait = \&epoll_wait_mod4;
162 *epoll_ctl = \&epoll_ctl_mod4;
166 elsif ($^O eq "freebsd") {
167 if ($ENV{FREEBSD_SENDFILE}) {
168 # this is still buggy and in development
172 ############################################################################
174 ############################################################################
176 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
179 syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
183 # ARGS: (epfd, op, fd, events_mask)
185 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
188 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
192 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
193 # arrayref: values modified to be [$fd, $event]
194 our $epoll_wait_events;
195 our $epoll_wait_size = 0;
196 sub epoll_wait_mod4 {
197 # resize our static buffer if requested size is bigger than we've ever done
198 if ($_[1] > $epoll_wait_size) {
199 $epoll_wait_size = $_[1];
200 $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
202 my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
204 @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
209 sub epoll_wait_mod8 {
210 # resize our static buffer if requested size is bigger than we've ever done
211 if ($_[1] > $epoll_wait_size) {
212 $epoll_wait_size = $_[1];
213 $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
216 if ($no_deprecated) {
217 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
219 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
222 # 16 byte epoll_event structs, with format:
223 # 4 byte mask [idx 1]
224 # 4 byte padding (we put it into idx 2, useless)
225 # 8 byte data (first 4 bytes are fd, into idx 0)
226 @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
235 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
239 Brad Fitzpatrick <brad@danga.com>