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;
21 use parent qw(Exporter);
22 use POSIX qw(ENOENT ENOSYS EINVAL O_NONBLOCK);
23 use Socket qw(SOL_SOCKET SCM_RIGHTS);
26 # $VERSION = '0.25'; # Sys::Syscall version
27 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
28 EPOLLIN EPOLLOUT EPOLLET
29 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
30 EPOLLONESHOT EPOLLEXCLUSIVE
31 signalfd rename_noreplace);
32 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
34 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
35 EPOLLONESHOT EPOLLEXCLUSIVE)],
44 EPOLLEXCLUSIVE => (1 << 28),
45 EPOLLONESHOT => (1 << 30),
50 SIZEOF_int => $Config{intsize},
51 SIZEOF_size_t => $Config{sizesize},
56 TMPL_size_t => SIZEOF_size_t == 8 ? 'Q' : 'L',
57 BYTES_4_hole => SIZEOF_size_t == 8 ? 'L' : '',
58 # cmsg_len, cmsg_level, cmsg_type
59 SIZEOF_cmsghdr => SIZEOF_int * 2 + SIZEOF_size_t,
62 my @BYTES_4_hole = BYTES_4_hole ? (0) : ();
63 our $loaded_syscall = 0;
66 # props to Gaal for this!
67 return if $loaded_syscall++;
69 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
70 _h2ph_pre.ph sys/syscall.ph>};
72 $clean->(); # don't trust modules before us
73 my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
74 $clean->(); # don't require modules after us trust us
87 my ($SYS_sendmsg, $SYS_recvmsg);
88 my $SYS_fstatfs; # don't need fstatfs64, just statfs.f_type
89 my ($FS_IOC_GETFLAGS, $FS_IOC_SETFLAGS);
90 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
91 our $no_deprecated = 0;
94 my (undef, undef, $release, undef, $machine) = POSIX::uname();
95 my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
96 $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
97 # whether the machine requires 64-bit numbers to be on 8-byte
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" && $Config{ptrsize} == 4) {
104 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
107 # Similarly for mips64 vs mips
108 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
112 if ($machine =~ m/^i[3456]86$/) {
113 $SYS_epoll_create = 254;
114 $SYS_epoll_ctl = 255;
115 $SYS_epoll_wait = 256;
116 $SYS_signalfd4 = 327;
117 $SYS_renameat2 //= 353;
121 $FS_IOC_GETFLAGS = 0x80046601;
122 $FS_IOC_SETFLAGS = 0x40046602;
123 } elsif ($machine eq "x86_64") {
124 $SYS_epoll_create = 213;
125 $SYS_epoll_ctl = 233;
126 $SYS_epoll_wait = 232;
127 $SYS_signalfd4 = 289;
128 $SYS_renameat2 //= 316;
132 $FS_IOC_GETFLAGS = 0x80086601;
133 $FS_IOC_SETFLAGS = 0x40086602;
134 } elsif ($machine eq 'x32') {
135 $SYS_epoll_create = 1073742037;
136 $SYS_epoll_ctl = 1073742057;
137 $SYS_epoll_wait = 1073742056;
138 $SYS_signalfd4 = 1073742113;
139 $SYS_renameat2 //= 0x40000000 + 316;
141 $SYS_sendmsg = 0x40000206;
142 $SYS_recvmsg = 0x40000207;
143 $FS_IOC_GETFLAGS = 0x80046601;
144 $FS_IOC_SETFLAGS = 0x40046602;
145 } elsif ($machine eq 'sparc64') {
146 $SYS_epoll_create = 193;
147 $SYS_epoll_ctl = 194;
148 $SYS_epoll_wait = 195;
150 $SYS_signalfd4 = 317;
151 $SYS_renameat2 //= 345;
152 $SFD_CLOEXEC = 020000000;
156 $FS_IOC_GETFLAGS = 0x40086601;
157 $FS_IOC_SETFLAGS = 0x80086602;
158 } elsif ($machine =~ m/^parisc/) {
159 $SYS_epoll_create = 224;
160 $SYS_epoll_ctl = 225;
161 $SYS_epoll_wait = 226;
163 $SYS_signalfd4 = 309;
164 } elsif ($machine =~ m/^ppc64/) {
165 $SYS_epoll_create = 236;
166 $SYS_epoll_ctl = 237;
167 $SYS_epoll_wait = 238;
169 $SYS_signalfd4 = 313;
170 $SYS_renameat2 //= 357;
174 $FS_IOC_GETFLAGS = 0x40086601;
175 $FS_IOC_SETFLAGS = 0x80086602;
176 } elsif ($machine eq "ppc") {
177 $SYS_epoll_create = 236;
178 $SYS_epoll_ctl = 237;
179 $SYS_epoll_wait = 238;
181 $SYS_signalfd4 = 313;
182 $SYS_renameat2 //= 357;
184 $FS_IOC_GETFLAGS = 0x40086601;
185 $FS_IOC_SETFLAGS = 0x80086602;
186 } elsif ($machine =~ m/^s390/) { # untested, no machine on cfarm
187 $SYS_epoll_create = 249;
188 $SYS_epoll_ctl = 250;
189 $SYS_epoll_wait = 251;
191 $SYS_signalfd4 = 322;
192 $SYS_renameat2 //= 347;
196 } elsif ($machine eq 'ia64') { # untested, no machine on cfarm
197 $SYS_epoll_create = 1243;
198 $SYS_epoll_ctl = 1244;
199 $SYS_epoll_wait = 1245;
201 $SYS_signalfd4 = 289;
202 } elsif ($machine eq "alpha") { # untested, no machine on cfarm
203 # natural alignment, ints are 32-bits
204 $SYS_epoll_create = 407;
205 $SYS_epoll_ctl = 408;
206 $SYS_epoll_wait = 409;
208 $SYS_signalfd4 = 484;
209 $SFD_CLOEXEC = 010000000;
210 } elsif ($machine eq 'aarch64' || $machine eq 'loongarch64') {
211 $SYS_epoll_create = 20; # (sys_epoll_create1)
213 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
217 $SYS_renameat2 //= 276;
221 $FS_IOC_GETFLAGS = 0x80086601;
222 $FS_IOC_SETFLAGS = 0x40086602;
223 } elsif ($machine =~ m/arm(v\d+)?.*l/) { # ARM OABI (untested on cfarm)
224 $SYS_epoll_create = 250;
225 $SYS_epoll_ctl = 251;
226 $SYS_epoll_wait = 252;
228 $SYS_signalfd4 = 355;
229 $SYS_renameat2 //= 382;
233 } elsif ($machine =~ m/^mips64/) { # cfarm only has 32-bit userspace
234 $SYS_epoll_create = 5207;
235 $SYS_epoll_ctl = 5208;
236 $SYS_epoll_wait = 5209;
238 $SYS_signalfd4 = 5283;
239 $SYS_renameat2 //= 5311;
243 $FS_IOC_GETFLAGS = 0x40046601;
244 $FS_IOC_SETFLAGS = 0x80046602;
245 } elsif ($machine =~ m/^mips/) { # 32-bit, tested on mips64 cfarm machine
246 $SYS_epoll_create = 4248;
247 $SYS_epoll_ctl = 4249;
248 $SYS_epoll_wait = 4250;
250 $SYS_signalfd4 = 4324;
251 $SYS_renameat2 //= 4351;
255 $FS_IOC_GETFLAGS = 0x40046601;
256 $FS_IOC_SETFLAGS = 0x80046602;
258 # as a last resort, try using the *.ph files which may not
259 # exist or may be wrong
261 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
262 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
263 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
265 # Note: do NOT add new syscalls to depend on *.ph, here.
266 # Better to miss syscalls (so we can fallback to IO::Poll)
267 # than to use wrong ones, since the names are not stable
268 # (at least not on FreeBSD), if the actual numbers are.
272 *epoll_wait = \&epoll_wait_mod8;
273 *epoll_ctl = \&epoll_ctl_mod8;
275 *epoll_wait = \&epoll_wait_mod4;
276 *epoll_ctl = \&epoll_ctl_mod4;
279 # use Inline::C for *BSD-only or general POSIX stuff.
280 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
281 # use scripts/syscall-list on Linux to detect new syscall numbers
283 ############################################################################
285 ############################################################################
287 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
290 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
294 # ARGS: (epfd, op, fd, events_mask)
296 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
299 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
303 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
304 # arrayref: values modified to be [$fd, $event]
305 our $epoll_wait_events = '';
306 our $epoll_wait_size = 0;
307 sub epoll_wait_mod4 {
308 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
309 # resize our static buffer if maxevents bigger than we've ever done
310 if ($maxevents > $epoll_wait_size) {
311 $epoll_wait_size = $maxevents;
312 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
315 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
316 $maxevents, $timeout_msec);
318 # 12-byte struct epoll_event
319 # 4 bytes uint32_t events mask (skipped, useless to us)
320 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
321 # So we skip the first 4 bytes and take the middle 4:
322 $events->[$_] = unpack('L', substr($epoll_wait_events,
327 sub epoll_wait_mod8 {
328 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
330 # resize our static buffer if maxevents bigger than we've ever done
331 if ($maxevents > $epoll_wait_size) {
332 $epoll_wait_size = $maxevents;
333 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
336 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
337 $maxevents, $timeout_msec,
338 $no_deprecated ? undef : ());
340 # 16-byte struct epoll_event
341 # 4 bytes uint32_t events mask (skipped, useless to us)
342 # 4 bytes padding (skipped, useless)
343 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
344 # So skip the first 8 bytes, take 4, and ignore the last 4:
345 $events->[$_] = unpack('L', substr($epoll_wait_events,
351 my ($signos, $nonblock) = @_;
352 if ($SYS_signalfd4) {
353 my $set = POSIX::SigSet->new(@$signos);
354 syscall($SYS_signalfd4, -1, "$$set",
355 # $Config{sig_count} is NSIG, so this is NSIG/8:
356 int($Config{sig_count}/8),
357 # SFD_NONBLOCK == O_NONBLOCK for every architecture
358 ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
365 sub _rename_noreplace_racy ($$) {
366 my ($old, $new) = @_;
367 if (link($old, $new)) {
368 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
375 # TODO: support FD args?
376 sub rename_noreplace ($$) {
377 my ($old, $new) = @_;
378 if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
379 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
381 1; # like rename() perlop
382 } elsif ($! == ENOSYS || $! == EINVAL) {
383 undef $SYS_renameat2;
384 _rename_noreplace_racy($old, $new);
389 _rename_noreplace_racy($old, $new);
394 return if !defined($SYS_fstatfs);
396 vec($buf, 120 * 8 - 1, 1) = 0;
398 syscall($SYS_fstatfs, fileno($fh), $buf) == 0 or
399 return warn("fstatfs: $!\n");
400 my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
401 return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
404 return warn('FS_IOC_GETFLAGS undefined for platform');
405 ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
406 return warn("FS_IOC_GETFLAGS: $!\n");
407 my $attr = unpack('l!', $buf);
408 return if ($attr & 0x00800000); # FS_NOCOW_FL;
409 ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
410 warn("FS_IOC_SETFLAGS: $!\n");
414 if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
417 sub CMSG_ALIGN ($) { ($_[0] + SIZEOF_size_t - 1) & ~(SIZEOF_size_t - 1) }
418 use constant CMSG_ALIGN_SIZEOF_cmsghdr => CMSG_ALIGN(SIZEOF_cmsghdr);
419 sub CMSG_SPACE ($) { CMSG_ALIGN($_[0]) + CMSG_ALIGN_SIZEOF_cmsghdr }
420 sub CMSG_LEN ($) { CMSG_ALIGN_SIZEOF_cmsghdr + $_[0] }
422 if (defined($SYS_sendmsg) && defined($SYS_recvmsg)) {
424 *send_cmd4 = sub ($$$$) {
425 my ($sock, $fds, undef, $flags) = @_;
426 my $iov = pack('P'.TMPL_size_t,
427 $_[2] // NUL, length($_[2] // NUL) || 1);
428 my $cmsghdr = pack(TMPL_size_t . # cmsg_len
429 'LL' . # cmsg_level, cmsg_type,
430 ('i' x scalar(@$fds)),
431 CMSG_LEN(scalar(@$fds) * SIZEOF_int), # cmsg_len
432 SOL_SOCKET, SCM_RIGHTS, # cmsg_{level,type}
434 my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
435 BYTES_4_hole . # 4-byte padding on 64-bit
436 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
437 'P'.TMPL_size_t . # msg_control, msg_controllen,
439 NUL, 0, # msg_name, msg_namelen (unused)
441 $iov, 1, # msg_iov, msg_iovlen
442 $cmsghdr, # msg_control
443 CMSG_SPACE(scalar(@$fds) * SIZEOF_int), # msg_controllen
448 $sent = syscall($SYS_sendmsg, fileno($sock), $mh, $flags);
449 } while ($sent < 0 &&
450 ($!{ENOBUFS} || $!{ENOMEM} || $!{ETOOMANYREFS}) &&
452 warn "sleeping on sendmsg: $! (#$try)\n" &&
453 select(undef, undef, undef, 0.1) == 0);
454 $sent >= 0 ? $sent : undef;
457 *recv_cmd4 = sub ($$$) {
458 my ($sock, undef, $len) = @_;
459 vec($_[1], ($len + 1) * 8, 1) = 0;
460 vec(my $cmsghdr = '', 256 * 8 - 1, 1) = 1;
461 my $iov = pack('P'.TMPL_size_t, $_[1], $len);
462 my $mh = pack('PL' . # msg_name, msg_namelen (socklen_t (U32))
463 BYTES_4_hole . # 4-byte padding on 64-bit
464 'P'.TMPL_size_t . # msg_iov, msg_iovlen,
465 'P'.TMPL_size_t . # msg_control, msg_controllen,
467 NUL, 0, # msg_name, msg_namelen (unused)
469 $iov, 1, # msg_iov, msg_iovlen
470 $cmsghdr, # msg_control
471 256, # msg_controllen
473 my $r = syscall($SYS_recvmsg, fileno($sock), $mh, 0);
474 return (undef) if $r < 0; # $! set
475 substr($_[1], $r, length($_[1]), '');
478 my ($len, $lvl, $type, @fds) = unpack(TMPL_size_t . # cmsg_len
479 'LLi*', # cmsg_level, cmsg_type, @fds
481 if ($lvl == SOL_SOCKET && $type == SCM_RIGHTS) {
482 $len -= CMSG_ALIGN_SIZEOF_cmsghdr;
483 @ret = @fds[0..(($len / SIZEOF_int) - 1)];
494 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
498 Brad Fitzpatrick <brad@danga.com>