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 parent qw(Exporter);
17 use POSIX qw(ENOSYS SEEK_CUR);
20 # $VERSION = '0.25'; # Sys::Syscall version
21 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
22 EPOLLIN EPOLLOUT EPOLLET
23 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
24 EPOLLONESHOT EPOLLEXCLUSIVE
25 signalfd SFD_NONBLOCK);
26 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
28 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
29 EPOLLONESHOT EPOLLEXCLUSIVE)],
38 EPOLLEXCLUSIVE => (1 << 28),
39 EPOLLONESHOT => (1 << 30),
45 SFD_CLOEXEC => 02000000,
46 SFD_NONBLOCK => 00004000,
49 our $loaded_syscall = 0;
52 # props to Gaal for this!
53 return if $loaded_syscall++;
55 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
56 _h2ph_pre.ph sys/syscall.ph>};
58 $clean->(); # don't trust modules before us
59 my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
60 $clean->(); # don't require modules after us trust us
72 our $no_deprecated = 0;
75 my $machine = (POSIX::uname())[-1];
76 # whether the machine requires 64-bit numbers to be on 8-byte
80 # if we're running on an x86_64 kernel, but a 32-bit process,
81 # we need to use the i386 syscall numbers.
82 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
86 # Similarly for mips64 vs mips
87 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
91 if ($machine =~ m/^i[3456]86$/) {
92 $SYS_epoll_create = 254;
94 $SYS_epoll_wait = 256;
96 } elsif ($machine eq "x86_64") {
97 $SYS_epoll_create = 213;
99 $SYS_epoll_wait = 232;
100 $SYS_signalfd4 = 289;
101 } elsif ($machine =~ m/^parisc/) {
102 $SYS_epoll_create = 224;
103 $SYS_epoll_ctl = 225;
104 $SYS_epoll_wait = 226;
106 $SYS_signalfd4 = 309;
107 } elsif ($machine =~ m/^ppc64/) {
108 $SYS_epoll_create = 236;
109 $SYS_epoll_ctl = 237;
110 $SYS_epoll_wait = 238;
112 $SYS_signalfd4 = 313;
113 } elsif ($machine eq "ppc") {
114 $SYS_epoll_create = 236;
115 $SYS_epoll_ctl = 237;
116 $SYS_epoll_wait = 238;
118 $SYS_signalfd4 = 313;
119 } elsif ($machine =~ m/^s390/) {
120 $SYS_epoll_create = 249;
121 $SYS_epoll_ctl = 250;
122 $SYS_epoll_wait = 251;
124 $SYS_signalfd4 = 322;
125 } elsif ($machine eq "ia64") {
126 $SYS_epoll_create = 1243;
127 $SYS_epoll_ctl = 1244;
128 $SYS_epoll_wait = 1245;
130 $SYS_signalfd4 = 289;
131 } elsif ($machine eq "alpha") {
132 # natural alignment, ints are 32-bits
133 $SYS_epoll_create = 407;
134 $SYS_epoll_ctl = 408;
135 $SYS_epoll_wait = 409;
137 $SYS_signalfd4 = 484;
138 } elsif ($machine eq "aarch64") {
139 $SYS_epoll_create = 20; # (sys_epoll_create1)
141 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
145 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
147 $SYS_epoll_create = 250;
148 $SYS_epoll_ctl = 251;
149 $SYS_epoll_wait = 252;
151 $SYS_signalfd4 = 355;
152 } elsif ($machine =~ m/^mips64/) {
153 $SYS_epoll_create = 5207;
154 $SYS_epoll_ctl = 5208;
155 $SYS_epoll_wait = 5209;
157 $SYS_signalfd4 = 5283;
158 } elsif ($machine =~ m/^mips/) {
159 $SYS_epoll_create = 4248;
160 $SYS_epoll_ctl = 4249;
161 $SYS_epoll_wait = 4250;
163 $SYS_signalfd4 = 4324;
165 # as a last resort, try using the *.ph files which may not
166 # exist or may be wrong
168 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
169 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
170 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
172 # Note: do NOT add new syscalls to depend on *.ph, here.
173 # Better to miss syscalls (so we can fallback to IO::Poll)
174 # than to use wrong ones, since the names are not stable
175 # (at least not on FreeBSD), if the actual numbers are.
179 *epoll_wait = \&epoll_wait_mod8;
180 *epoll_ctl = \&epoll_ctl_mod8;
182 *epoll_wait = \&epoll_wait_mod4;
183 *epoll_ctl = \&epoll_ctl_mod4;
187 elsif ($^O eq "freebsd") {
188 if ($ENV{FREEBSD_SENDFILE}) {
189 # this is still buggy and in development
193 ############################################################################
195 ############################################################################
197 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
200 syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
204 # ARGS: (epfd, op, fd, events_mask)
206 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
209 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
213 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
214 # arrayref: values modified to be [$fd, $event]
215 our $epoll_wait_events;
216 our $epoll_wait_size = 0;
217 sub epoll_wait_mod4 {
218 # resize our static buffer if requested size is bigger than we've ever done
219 if ($_[1] > $epoll_wait_size) {
220 $epoll_wait_size = $_[1];
221 $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
223 my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
225 @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
230 sub epoll_wait_mod8 {
231 # resize our static buffer if requested size is bigger than we've ever done
232 if ($_[1] > $epoll_wait_size) {
233 $epoll_wait_size = $_[1];
234 $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
237 if ($no_deprecated) {
238 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
240 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
243 # 16 byte epoll_event structs, with format:
244 # 4 byte mask [idx 1]
245 # 4 byte padding (we put it into idx 2, useless)
246 # 8 byte data (first 4 bytes are fd, into idx 0)
247 @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
253 my ($fd, $signos, $flags) = @_;
254 if ($SYS_signalfd4) {
255 # Not sure if there's a way to get pack/unpack to get the
256 # contents of POSIX::SigSet to a buffer, but prepping the
257 # bitmap like one would for select() works:
259 vec($buf, $_ - 1, 1) = 1 for @$signos;
261 syscall($SYS_signalfd4, $fd, $buf, 8, $flags|SFD_CLOEXEC);
272 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
276 Brad Fitzpatrick <brad@danga.com>