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 # See devel/syscall-list in the public-inbox source tree for maintenance
6 # <https://80x24.org/public-inbox.git>, and machines from the GCC Farm:
7 # <https://cfarm.tetaneutral.net/>
9 # This license differs from the rest of public-inbox
11 # This module is Copyright (c) 2005 Six Apart, Ltd.
12 # Copyright (C) all contributors <meta@public-inbox.org>
14 # All rights reserved.
16 # You may distribute under the terms of either the GNU General Public
17 # License or the Artistic License, as specified in the Perl README file.
18 package PublicInbox::Syscall;
20 use parent qw(Exporter);
21 use POSIX qw(ENOENT ENOSYS EINVAL O_NONBLOCK);
22 use Socket qw(SOL_SOCKET SCM_RIGHTS);
25 # $VERSION = '0.25'; # Sys::Syscall version
26 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
27 EPOLLIN EPOLLOUT EPOLLET
28 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
29 EPOLLONESHOT EPOLLEXCLUSIVE
30 signalfd rename_noreplace);
31 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
33 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
34 EPOLLONESHOT EPOLLEXCLUSIVE)],
43 EPOLLEXCLUSIVE => (1 << 28),
44 EPOLLONESHOT => (1 << 30),
49 SIZEOF_int => $Config{intsize},
50 SIZEOF_size_t => $Config{sizesize},
55 TMPL_size_t => SIZEOF_size_t == 8 ? 'Q' : 'L',
56 BYTES_4_hole => SIZEOF_size_t == 8 ? 'L' : '',
57 # cmsg_len, cmsg_level, cmsg_type
58 SIZEOF_cmsghdr => SIZEOF_int * 2 + SIZEOF_size_t,
61 my @BYTES_4_hole = BYTES_4_hole ? (0) : ();
62 our $loaded_syscall = 0;
65 # props to Gaal for this!
66 return if $loaded_syscall++;
68 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
69 _h2ph_pre.ph sys/syscall.ph>};
71 $clean->(); # don't trust modules before us
72 my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
73 $clean->(); # don't require modules after us trust us
86 my ($SYS_sendmsg, $SYS_recvmsg);
87 my $SYS_fstatfs; # don't need fstatfs64, just statfs.f_type
88 my ($FS_IOC_GETFLAGS, $FS_IOC_SETFLAGS);
89 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
90 our $no_deprecated = 0;
93 my (undef, undef, $release, undef, $machine) = POSIX::uname();
94 my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
95 $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
96 # whether the machine requires 64-bit numbers to be on 8-byte
100 if ($Config{ptrsize} == 4) {
101 # if we're running on an x86_64 kernel, but a 32-bit process,
102 # we need to use the x32 or i386 syscall numbers.
103 if ($machine eq 'x86_64') {
104 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386'
105 } elsif ($machine eq 'mips64') { # similarly for mips64 vs mips
110 if ($machine =~ m/^i[3456]86$/) {
111 $SYS_epoll_create = 254;
112 $SYS_epoll_ctl = 255;
113 $SYS_epoll_wait = 256;
114 $SYS_signalfd4 = 327;
115 $SYS_renameat2 //= 353;
119 $FS_IOC_GETFLAGS = 0x80046601;
120 $FS_IOC_SETFLAGS = 0x40046602;
121 } elsif ($machine eq "x86_64") {
122 $SYS_epoll_create = 213;
123 $SYS_epoll_ctl = 233;
124 $SYS_epoll_wait = 232;
125 $SYS_signalfd4 = 289;
126 $SYS_renameat2 //= 316;
130 $FS_IOC_GETFLAGS = 0x80086601;
131 $FS_IOC_SETFLAGS = 0x40086602;
132 } elsif ($machine eq 'x32') {
133 $SYS_epoll_create = 1073742037;
134 $SYS_epoll_ctl = 1073742057;
135 $SYS_epoll_wait = 1073742056;
136 $SYS_signalfd4 = 1073742113;
137 $SYS_renameat2 //= 0x40000000 + 316;
139 $SYS_sendmsg = 0x40000206;
140 $SYS_recvmsg = 0x40000207;
141 $FS_IOC_GETFLAGS = 0x80046601;
142 $FS_IOC_SETFLAGS = 0x40046602;
143 } elsif ($machine eq 'sparc64') {
144 $SYS_epoll_create = 193;
145 $SYS_epoll_ctl = 194;
146 $SYS_epoll_wait = 195;
148 $SYS_signalfd4 = 317;
149 $SYS_renameat2 //= 345;
150 $SFD_CLOEXEC = 020000000;
154 $FS_IOC_GETFLAGS = 0x40086601;
155 $FS_IOC_SETFLAGS = 0x80086602;
156 } elsif ($machine =~ m/^parisc/) {
157 $SYS_epoll_create = 224;
158 $SYS_epoll_ctl = 225;
159 $SYS_epoll_wait = 226;
161 $SYS_signalfd4 = 309;
162 } elsif ($machine =~ m/^ppc64/) {
163 $SYS_epoll_create = 236;
164 $SYS_epoll_ctl = 237;
165 $SYS_epoll_wait = 238;
167 $SYS_signalfd4 = 313;
168 $SYS_renameat2 //= 357;
172 $FS_IOC_GETFLAGS = 0x40086601;
173 $FS_IOC_SETFLAGS = 0x80086602;
174 } elsif ($machine eq "ppc") {
175 $SYS_epoll_create = 236;
176 $SYS_epoll_ctl = 237;
177 $SYS_epoll_wait = 238;
179 $SYS_signalfd4 = 313;
180 $SYS_renameat2 //= 357;
182 $FS_IOC_GETFLAGS = 0x40086601;
183 $FS_IOC_SETFLAGS = 0x80086602;
184 } elsif ($machine =~ m/^s390/) { # untested, no machine on cfarm
185 $SYS_epoll_create = 249;
186 $SYS_epoll_ctl = 250;
187 $SYS_epoll_wait = 251;
189 $SYS_signalfd4 = 322;
190 $SYS_renameat2 //= 347;
194 } elsif ($machine eq 'ia64') { # untested, no machine on cfarm
195 $SYS_epoll_create = 1243;
196 $SYS_epoll_ctl = 1244;
197 $SYS_epoll_wait = 1245;
199 $SYS_signalfd4 = 289;
200 } elsif ($machine eq "alpha") { # untested, no machine on cfarm
201 # natural alignment, ints are 32-bits
202 $SYS_epoll_create = 407;
203 $SYS_epoll_ctl = 408;
204 $SYS_epoll_wait = 409;
206 $SYS_signalfd4 = 484;
207 $SFD_CLOEXEC = 010000000;
208 } elsif ($machine =~ /\A(?:loong)?aarch64\z/ || $machine eq 'riscv64') {
209 $SYS_epoll_create = 20; # (sys_epoll_create1)
211 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
215 $SYS_renameat2 //= 276;
219 $FS_IOC_GETFLAGS = 0x80086601;
220 $FS_IOC_SETFLAGS = 0x40086602;
221 } elsif ($machine =~ m/arm(v\d+)?.*l/) { # ARM OABI (untested on cfarm)
222 $SYS_epoll_create = 250;
223 $SYS_epoll_ctl = 251;
224 $SYS_epoll_wait = 252;
226 $SYS_signalfd4 = 355;
227 $SYS_renameat2 //= 382;
231 } elsif ($machine =~ m/^mips64/) { # cfarm only has 32-bit userspace
232 $SYS_epoll_create = 5207;
233 $SYS_epoll_ctl = 5208;
234 $SYS_epoll_wait = 5209;
236 $SYS_signalfd4 = 5283;
237 $SYS_renameat2 //= 5311;
241 $FS_IOC_GETFLAGS = 0x40046601;
242 $FS_IOC_SETFLAGS = 0x80046602;
243 } elsif ($machine =~ m/^mips/) { # 32-bit, tested on mips64 cfarm machine
244 $SYS_epoll_create = 4248;
245 $SYS_epoll_ctl = 4249;
246 $SYS_epoll_wait = 4250;
248 $SYS_signalfd4 = 4324;
249 $SYS_renameat2 //= 4351;
253 $FS_IOC_GETFLAGS = 0x40046601;
254 $FS_IOC_SETFLAGS = 0x80046602;
256 # as a last resort, try using the *.ph files which may not
257 # exist or may be wrong
259 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
260 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
261 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
263 # Note: do NOT add new syscalls to depend on *.ph, here.
264 # Better to miss syscalls (so we can fallback to IO::Poll)
265 # than to use wrong ones, since the names are not stable
266 # (at least not on FreeBSD), if the actual numbers are.
270 *epoll_wait = \&epoll_wait_mod8;
271 *epoll_ctl = \&epoll_ctl_mod8;
273 *epoll_wait = \&epoll_wait_mod4;
274 *epoll_ctl = \&epoll_ctl_mod4;
277 # use Inline::C for *BSD-only or general POSIX stuff.
278 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
279 # use scripts/syscall-list on Linux to detect new syscall numbers
281 ############################################################################
283 ############################################################################
285 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
288 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
292 # ARGS: (epfd, op, fd, events_mask)
294 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
297 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
301 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
302 # arrayref: values modified to be [$fd, $event]
303 our $epoll_wait_events = '';
304 our $epoll_wait_size = 0;
305 sub epoll_wait_mod4 {
306 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
307 # resize our static buffer if maxevents bigger than we've ever done
308 if ($maxevents > $epoll_wait_size) {
309 $epoll_wait_size = $maxevents;
310 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
313 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
314 $maxevents, $timeout_msec);
316 # 12-byte struct epoll_event
317 # 4 bytes uint32_t events mask (skipped, useless to us)
318 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
319 # So we skip the first 4 bytes and take the middle 4:
320 $events->[$_] = unpack('L', substr($epoll_wait_events,
325 sub epoll_wait_mod8 {
326 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
328 # resize our static buffer if maxevents bigger than we've ever done
329 if ($maxevents > $epoll_wait_size) {
330 $epoll_wait_size = $maxevents;
331 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
334 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
335 $maxevents, $timeout_msec,
336 $no_deprecated ? undef : ());
338 # 16-byte struct epoll_event
339 # 4 bytes uint32_t events mask (skipped, useless to us)
340 # 4 bytes padding (skipped, useless)
341 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
342 # So skip the first 8 bytes, take 4, and ignore the last 4:
343 $events->[$_] = unpack('L', substr($epoll_wait_events,
349 my ($signos, $nonblock) = @_;
350 if ($SYS_signalfd4) {
351 my $set = POSIX::SigSet->new(@$signos);
352 syscall($SYS_signalfd4, -1, "$$set",
353 # $Config{sig_count} is NSIG, so this is NSIG/8:
354 int($Config{sig_count}/8),
355 # SFD_NONBLOCK == O_NONBLOCK for every architecture
356 ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
363 sub _rename_noreplace_racy ($$) {
364 my ($old, $new) = @_;
365 if (link($old, $new)) {
366 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
373 # TODO: support FD args?
374 sub rename_noreplace ($$) {
375 my ($old, $new) = @_;
376 if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
377 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
379 1; # like rename() perlop
380 } elsif ($! == ENOSYS || $! == EINVAL) {
381 undef $SYS_renameat2;
382 _rename_noreplace_racy($old, $new);
387 _rename_noreplace_racy($old, $new);
391 sub nodatacow_fh ($) {
393 my $buf = "\0" x 120;
394 syscall($SYS_fstatfs // return, fileno($fh), $buf) == 0 or
395 return warn("fstatfs: $!\n");
396 my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
397 return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
400 return warn('FS_IOC_GETFLAGS undefined for platform');
401 ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
402 return warn("FS_IOC_GETFLAGS: $!\n");
403 my $attr = unpack('l!', $buf);
404 return if ($attr & 0x00800000); # FS_NOCOW_FL;
405 ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
406 warn("FS_IOC_SETFLAGS: $!\n");
410 if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
413 sub CMSG_ALIGN ($) { ($_[0] + SIZEOF_size_t - 1) & ~(SIZEOF_size_t - 1) }
414 use constant CMSG_ALIGN_SIZEOF_cmsghdr => CMSG_ALIGN(SIZEOF_cmsghdr);
415 sub CMSG_SPACE ($) { CMSG_ALIGN($_[0]) + CMSG_ALIGN_SIZEOF_cmsghdr }
416 sub CMSG_LEN ($) { CMSG_ALIGN_SIZEOF_cmsghdr + $_[0] }
417 use constant msg_controllen => CMSG_SPACE(10 * SIZEOF_int) + 16; # 10 FDs
419 if (defined($SYS_sendmsg) && defined($SYS_recvmsg)) {
421 *send_cmd4 = sub ($$$$) {
422 my ($sock, $fds, undef, $flags) = @_;
423 my $iov = pack('P'.TMPL_size_t,
424 $_[2] // NUL, length($_[2] // NUL) || 1);
425 my $cmsghdr = pack(TMPL_size_t . # cmsg_len
426 'LL' . # cmsg_level, cmsg_type,
427 ('i' x scalar(@$fds)),
428 CMSG_LEN(scalar(@$fds) * SIZEOF_int), # cmsg_len
429 SOL_SOCKET, SCM_RIGHTS, # cmsg_{level,type}
431 my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
432 BYTES_4_hole . # 4-byte padding on 64-bit
433 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
434 'P'.TMPL_size_t . # msg_control, msg_controllen,
436 NUL, 0, # msg_name, msg_namelen (unused)
438 $iov, 1, # msg_iov, msg_iovlen
439 $cmsghdr, # msg_control
440 CMSG_SPACE(scalar(@$fds) * SIZEOF_int), # msg_controllen
445 $sent = syscall($SYS_sendmsg, fileno($sock), $mh, $flags);
446 } while ($sent < 0 &&
447 ($!{ENOBUFS} || $!{ENOMEM} || $!{ETOOMANYREFS}) &&
449 warn "sleeping on sendmsg: $! (#$try)\n" &&
450 select(undef, undef, undef, 0.1) == 0);
451 $sent >= 0 ? $sent : undef;
454 *recv_cmd4 = sub ($$$) {
455 my ($sock, undef, $len) = @_;
456 vec($_[1] //= '', ($len + 1) * 8, 1) = 0;
457 my $cmsghdr = "\0" x msg_controllen; # 10 * sizeof(int)
458 my $iov = pack('P'.TMPL_size_t, $_[1], $len);
459 my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
460 BYTES_4_hole . # 4-byte padding on 64-bit
461 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
462 'P'.TMPL_size_t . # msg_control, msg_controllen,
464 NUL, 0, # msg_name, msg_namelen (unused)
466 $iov, 1, # msg_iov, msg_iovlen
467 $cmsghdr, # msg_control
470 my $r = syscall($SYS_recvmsg, fileno($sock), $mh, 0);
471 return (undef) if $r < 0; # $! set
472 substr($_[1], $r, length($_[1]), '');
475 my ($len, $lvl, $type, @fds) = unpack(TMPL_size_t . # cmsg_len
476 'LLi*', # cmsg_level, cmsg_type, @fds
478 if ($lvl == SOL_SOCKET && $type == SCM_RIGHTS) {
479 $len -= CMSG_ALIGN_SIZEOF_cmsghdr;
480 @ret = @fds[0..(($len / SIZEOF_int) - 1)];
491 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
495 Brad Fitzpatrick <brad@danga.com>