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;
17 use parent qw(Exporter);
18 use POSIX qw(ENOENT EEXIST ENOSYS O_NONBLOCK);
21 # $VERSION = '0.25'; # Sys::Syscall version
22 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
23 EPOLLIN EPOLLOUT EPOLLET
24 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
25 EPOLLONESHOT EPOLLEXCLUSIVE
26 signalfd rename_noreplace);
27 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
29 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
30 EPOLLONESHOT EPOLLEXCLUSIVE)],
39 EPOLLEXCLUSIVE => (1 << 28),
40 EPOLLONESHOT => (1 << 30),
47 our $loaded_syscall = 0;
50 # props to Gaal for this!
51 return if $loaded_syscall++;
53 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
54 _h2ph_pre.ph sys/syscall.ph>};
56 $clean->(); # don't trust modules before us
57 my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
58 $clean->(); # don't require modules after us trust us
71 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
72 our $no_deprecated = 0;
75 my (undef, undef, $release, undef, $machine) = POSIX::uname();
76 my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
77 $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
78 # whether the machine requires 64-bit numbers to be on 8-byte
82 # if we're running on an x86_64 kernel, but a 32-bit process,
83 # we need to use the x32 or i386 syscall numbers.
84 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
85 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
88 # Similarly for mips64 vs mips
89 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
93 if ($machine =~ m/^i[3456]86$/) {
94 $SYS_epoll_create = 254;
96 $SYS_epoll_wait = 256;
98 $SYS_renameat2 //= 353;
99 } elsif ($machine eq "x86_64") {
100 $SYS_epoll_create = 213;
101 $SYS_epoll_ctl = 233;
102 $SYS_epoll_wait = 232;
103 $SYS_signalfd4 = 289;
104 $SYS_renameat2 //= 316;
105 } elsif ($machine eq 'x32') {
106 $SYS_epoll_create = 1073742037;
107 $SYS_epoll_ctl = 1073742057;
108 $SYS_epoll_wait = 1073742056;
109 $SYS_signalfd4 = 1073742113;
110 $SYS_renameat2 //= 0x40000000 + 316;
111 } elsif ($machine eq 'sparc64') {
112 $SYS_epoll_create = 193;
113 $SYS_epoll_ctl = 194;
114 $SYS_epoll_wait = 195;
116 $SYS_signalfd4 = 317;
117 $SYS_renameat2 //= 345;
118 $SFD_CLOEXEC = 020000000;
119 } elsif ($machine =~ m/^parisc/) {
120 $SYS_epoll_create = 224;
121 $SYS_epoll_ctl = 225;
122 $SYS_epoll_wait = 226;
124 $SYS_signalfd4 = 309;
125 } elsif ($machine =~ m/^ppc64/) {
126 $SYS_epoll_create = 236;
127 $SYS_epoll_ctl = 237;
128 $SYS_epoll_wait = 238;
130 $SYS_signalfd4 = 313;
131 $SYS_renameat2 //= 357;
132 } elsif ($machine eq "ppc") {
133 $SYS_epoll_create = 236;
134 $SYS_epoll_ctl = 237;
135 $SYS_epoll_wait = 238;
137 $SYS_signalfd4 = 313;
138 $SYS_renameat2 //= 357;
139 } elsif ($machine =~ m/^s390/) {
140 $SYS_epoll_create = 249;
141 $SYS_epoll_ctl = 250;
142 $SYS_epoll_wait = 251;
144 $SYS_signalfd4 = 322;
145 $SYS_renameat2 //= 347;
146 } elsif ($machine eq "ia64") {
147 $SYS_epoll_create = 1243;
148 $SYS_epoll_ctl = 1244;
149 $SYS_epoll_wait = 1245;
151 $SYS_signalfd4 = 289;
152 } elsif ($machine eq "alpha") {
153 # natural alignment, ints are 32-bits
154 $SYS_epoll_create = 407;
155 $SYS_epoll_ctl = 408;
156 $SYS_epoll_wait = 409;
158 $SYS_signalfd4 = 484;
159 $SFD_CLOEXEC = 010000000;
160 } elsif ($machine eq "aarch64") {
161 $SYS_epoll_create = 20; # (sys_epoll_create1)
163 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
167 $SYS_renameat2 //= 276;
168 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
170 $SYS_epoll_create = 250;
171 $SYS_epoll_ctl = 251;
172 $SYS_epoll_wait = 252;
174 $SYS_signalfd4 = 355;
175 $SYS_renameat2 //= 382;
176 } elsif ($machine =~ m/^mips64/) {
177 $SYS_epoll_create = 5207;
178 $SYS_epoll_ctl = 5208;
179 $SYS_epoll_wait = 5209;
181 $SYS_signalfd4 = 5283;
182 $SYS_renameat2 //= 5311;
183 } elsif ($machine =~ m/^mips/) {
184 $SYS_epoll_create = 4248;
185 $SYS_epoll_ctl = 4249;
186 $SYS_epoll_wait = 4250;
188 $SYS_signalfd4 = 4324;
189 $SYS_renameat2 //= 4351;
191 # as a last resort, try using the *.ph files which may not
192 # exist or may be wrong
194 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
195 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
196 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
198 # Note: do NOT add new syscalls to depend on *.ph, here.
199 # Better to miss syscalls (so we can fallback to IO::Poll)
200 # than to use wrong ones, since the names are not stable
201 # (at least not on FreeBSD), if the actual numbers are.
205 *epoll_wait = \&epoll_wait_mod8;
206 *epoll_ctl = \&epoll_ctl_mod8;
208 *epoll_wait = \&epoll_wait_mod4;
209 *epoll_ctl = \&epoll_ctl_mod4;
212 # use Inline::C for *BSD-only or general POSIX stuff.
213 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
214 # use scripts/syscall-list on Linux to detect new syscall numbers
216 ############################################################################
218 ############################################################################
220 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
223 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
227 # ARGS: (epfd, op, fd, events_mask)
229 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
232 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
236 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
237 # arrayref: values modified to be [$fd, $event]
238 our $epoll_wait_events = '';
239 our $epoll_wait_size = 0;
240 sub epoll_wait_mod4 {
241 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
242 # resize our static buffer if maxevents bigger than we've ever done
243 if ($maxevents > $epoll_wait_size) {
244 $epoll_wait_size = $maxevents;
245 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
248 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
249 $maxevents, $timeout_msec);
251 # 12-byte struct epoll_event
252 # 4 bytes uint32_t events mask (skipped, useless to us)
253 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
254 # So we skip the first 4 bytes and take the middle 4:
255 $events->[$_] = unpack('L', substr($epoll_wait_events,
260 sub epoll_wait_mod8 {
261 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
263 # resize our static buffer if maxevents bigger than we've ever done
264 if ($maxevents > $epoll_wait_size) {
265 $epoll_wait_size = $maxevents;
266 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
269 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
270 $maxevents, $timeout_msec,
271 $no_deprecated ? undef : ());
273 # 16-byte struct epoll_event
274 # 4 bytes uint32_t events mask (skipped, useless to us)
275 # 4 bytes padding (skipped, useless)
276 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
277 # So skip the first 8 bytes, take 4, and ignore the last 4:
278 $events->[$_] = unpack('L', substr($epoll_wait_events,
284 my ($signos, $nonblock) = @_;
285 if ($SYS_signalfd4) {
286 my $set = POSIX::SigSet->new(@$signos);
287 syscall($SYS_signalfd4, -1, "$$set",
288 # $Config{sig_count} is NSIG, so this is NSIG/8:
289 int($Config{sig_count}/8),
290 # SFD_NONBLOCK == O_NONBLOCK for every architecture
291 ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
298 sub _rename_noreplace_racy ($$) {
299 my ($old, $new) = @_;
300 if (link($old, $new)) {
301 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
308 # TODO: support FD args?
309 sub rename_noreplace ($$) {
310 my ($old, $new) = @_;
311 if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
312 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
314 1; # like rename() perlop
315 } elsif ($! == ENOSYS) {
316 undef $SYS_renameat2;
317 _rename_noreplace_racy($old, $new);
322 _rename_noreplace_racy($old, $new);
330 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
334 Brad Fitzpatrick <brad@danga.com>