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-2021 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 O_NONBLOCK);
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),
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
69 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
70 sub SFD_NONBLOCK () { O_NONBLOCK }
71 our $no_deprecated = 0;
74 my $machine = (POSIX::uname())[-1];
75 # whether the machine requires 64-bit numbers to be on 8-byte
79 # if we're running on an x86_64 kernel, but a 32-bit process,
80 # we need to use the x32 or i386 syscall numbers.
81 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
82 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
85 # Similarly for mips64 vs mips
86 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
90 if ($machine =~ m/^i[3456]86$/) {
91 $SYS_epoll_create = 254;
93 $SYS_epoll_wait = 256;
95 } elsif ($machine eq "x86_64") {
96 $SYS_epoll_create = 213;
98 $SYS_epoll_wait = 232;
100 } elsif ($machine eq 'x32') {
101 $SYS_epoll_create = 1073742037;
102 $SYS_epoll_ctl = 1073742057;
103 $SYS_epoll_wait = 1073742056;
104 $SYS_signalfd4 = 1073742113;
105 } elsif ($machine eq 'sparc64') {
106 $SYS_epoll_create = 193;
107 $SYS_epoll_ctl = 194;
108 $SYS_epoll_wait = 195;
110 $SYS_signalfd4 = 317;
111 $SFD_CLOEXEC = 020000000;
112 } elsif ($machine =~ m/^parisc/) {
113 $SYS_epoll_create = 224;
114 $SYS_epoll_ctl = 225;
115 $SYS_epoll_wait = 226;
117 $SYS_signalfd4 = 309;
118 } elsif ($machine =~ m/^ppc64/) {
119 $SYS_epoll_create = 236;
120 $SYS_epoll_ctl = 237;
121 $SYS_epoll_wait = 238;
123 $SYS_signalfd4 = 313;
124 } elsif ($machine eq "ppc") {
125 $SYS_epoll_create = 236;
126 $SYS_epoll_ctl = 237;
127 $SYS_epoll_wait = 238;
129 $SYS_signalfd4 = 313;
130 } elsif ($machine =~ m/^s390/) {
131 $SYS_epoll_create = 249;
132 $SYS_epoll_ctl = 250;
133 $SYS_epoll_wait = 251;
135 $SYS_signalfd4 = 322;
136 } elsif ($machine eq "ia64") {
137 $SYS_epoll_create = 1243;
138 $SYS_epoll_ctl = 1244;
139 $SYS_epoll_wait = 1245;
141 $SYS_signalfd4 = 289;
142 } elsif ($machine eq "alpha") {
143 # natural alignment, ints are 32-bits
144 $SYS_epoll_create = 407;
145 $SYS_epoll_ctl = 408;
146 $SYS_epoll_wait = 409;
148 $SYS_signalfd4 = 484;
149 $SFD_CLOEXEC = 010000000;
150 } elsif ($machine eq "aarch64") {
151 $SYS_epoll_create = 20; # (sys_epoll_create1)
153 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
157 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
159 $SYS_epoll_create = 250;
160 $SYS_epoll_ctl = 251;
161 $SYS_epoll_wait = 252;
163 $SYS_signalfd4 = 355;
164 } elsif ($machine =~ m/^mips64/) {
165 $SYS_epoll_create = 5207;
166 $SYS_epoll_ctl = 5208;
167 $SYS_epoll_wait = 5209;
169 $SYS_signalfd4 = 5283;
170 } elsif ($machine =~ m/^mips/) {
171 $SYS_epoll_create = 4248;
172 $SYS_epoll_ctl = 4249;
173 $SYS_epoll_wait = 4250;
175 $SYS_signalfd4 = 4324;
177 # as a last resort, try using the *.ph files which may not
178 # exist or may be wrong
180 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
181 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
182 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
184 # Note: do NOT add new syscalls to depend on *.ph, here.
185 # Better to miss syscalls (so we can fallback to IO::Poll)
186 # than to use wrong ones, since the names are not stable
187 # (at least not on FreeBSD), if the actual numbers are.
191 *epoll_wait = \&epoll_wait_mod8;
192 *epoll_ctl = \&epoll_ctl_mod8;
194 *epoll_wait = \&epoll_wait_mod4;
195 *epoll_ctl = \&epoll_ctl_mod4;
198 # use Inline::C for *BSD-only or general POSIX stuff.
199 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
200 # use scripts/syscall-list on Linux to detect new syscall numbers
202 ############################################################################
204 ############################################################################
206 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
209 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
213 # ARGS: (epfd, op, fd, events_mask)
215 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
218 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
222 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
223 # arrayref: values modified to be [$fd, $event]
224 our $epoll_wait_events = '';
225 our $epoll_wait_size = 0;
226 sub epoll_wait_mod4 {
227 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
228 # resize our static buffer if maxevents bigger than we've ever done
229 if ($maxevents > $epoll_wait_size) {
230 $epoll_wait_size = $maxevents;
231 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
234 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
235 $maxevents, $timeout_msec);
237 # 12-byte struct epoll_event
238 # 4 bytes uint32_t events mask (skipped, useless to us)
239 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
240 # So we skip the first 4 bytes and take the middle 4:
241 $events->[$_] = unpack('L', substr($epoll_wait_events,
246 sub epoll_wait_mod8 {
247 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
249 # resize our static buffer if maxevents bigger than we've ever done
250 if ($maxevents > $epoll_wait_size) {
251 $epoll_wait_size = $maxevents;
252 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
255 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
256 $maxevents, $timeout_msec,
257 $no_deprecated ? undef : ());
259 # 16-byte struct epoll_event
260 # 4 bytes uint32_t events mask (skipped, useless to us)
261 # 4 bytes padding (skipped, useless)
262 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
263 # So skip the first 8 bytes, take 4, and ignore the last 4:
264 $events->[$_] = unpack('L', substr($epoll_wait_events,
270 my ($fd, $signos, $flags) = @_;
271 if ($SYS_signalfd4) {
272 my $set = POSIX::SigSet->new(@$signos);
273 syscall($SYS_signalfd4, $fd, "$$set",
274 # $Config{sig_count} is NSIG, so this is NSIG/8:
275 int($Config{sig_count}/8),
276 $flags|$SFD_CLOEXEC);
287 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
291 Brad Fitzpatrick <brad@danga.com>