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) 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 EINVAL 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 $SYS_fstatfs; # don't need fstatfs64, just statfs.f_type
72 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
73 our $no_deprecated = 0;
76 my (undef, undef, $release, undef, $machine) = POSIX::uname();
77 my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
78 $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
79 # whether the machine requires 64-bit numbers to be on 8-byte
83 # if we're running on an x86_64 kernel, but a 32-bit process,
84 # we need to use the x32 or i386 syscall numbers.
85 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
86 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
89 # Similarly for mips64 vs mips
90 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
94 if ($machine =~ m/^i[3456]86$/) {
95 $SYS_epoll_create = 254;
97 $SYS_epoll_wait = 256;
99 $SYS_renameat2 //= 353;
101 } elsif ($machine eq "x86_64") {
102 $SYS_epoll_create = 213;
103 $SYS_epoll_ctl = 233;
104 $SYS_epoll_wait = 232;
105 $SYS_signalfd4 = 289;
106 $SYS_renameat2 //= 316;
108 } elsif ($machine eq 'x32') {
109 $SYS_epoll_create = 1073742037;
110 $SYS_epoll_ctl = 1073742057;
111 $SYS_epoll_wait = 1073742056;
112 $SYS_signalfd4 = 1073742113;
113 $SYS_renameat2 //= 0x40000000 + 316;
115 } elsif ($machine eq 'sparc64') {
116 $SYS_epoll_create = 193;
117 $SYS_epoll_ctl = 194;
118 $SYS_epoll_wait = 195;
120 $SYS_signalfd4 = 317;
121 $SYS_renameat2 //= 345;
122 $SFD_CLOEXEC = 020000000;
124 } elsif ($machine =~ m/^parisc/) {
125 $SYS_epoll_create = 224;
126 $SYS_epoll_ctl = 225;
127 $SYS_epoll_wait = 226;
129 $SYS_signalfd4 = 309;
130 } elsif ($machine =~ m/^ppc64/) {
131 $SYS_epoll_create = 236;
132 $SYS_epoll_ctl = 237;
133 $SYS_epoll_wait = 238;
135 $SYS_signalfd4 = 313;
136 $SYS_renameat2 //= 357;
138 } elsif ($machine eq "ppc") {
139 $SYS_epoll_create = 236;
140 $SYS_epoll_ctl = 237;
141 $SYS_epoll_wait = 238;
143 $SYS_signalfd4 = 313;
144 $SYS_renameat2 //= 357;
146 } elsif ($machine =~ m/^s390/) {
147 $SYS_epoll_create = 249;
148 $SYS_epoll_ctl = 250;
149 $SYS_epoll_wait = 251;
151 $SYS_signalfd4 = 322;
152 $SYS_renameat2 //= 347;
154 } elsif ($machine eq "ia64") {
155 $SYS_epoll_create = 1243;
156 $SYS_epoll_ctl = 1244;
157 $SYS_epoll_wait = 1245;
159 $SYS_signalfd4 = 289;
160 } elsif ($machine eq "alpha") {
161 # natural alignment, ints are 32-bits
162 $SYS_epoll_create = 407;
163 $SYS_epoll_ctl = 408;
164 $SYS_epoll_wait = 409;
166 $SYS_signalfd4 = 484;
167 $SFD_CLOEXEC = 010000000;
168 } elsif ($machine eq "aarch64") {
169 $SYS_epoll_create = 20; # (sys_epoll_create1)
171 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
175 $SYS_renameat2 //= 276;
177 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
179 $SYS_epoll_create = 250;
180 $SYS_epoll_ctl = 251;
181 $SYS_epoll_wait = 252;
183 $SYS_signalfd4 = 355;
184 $SYS_renameat2 //= 382;
186 } elsif ($machine =~ m/^mips64/) {
187 $SYS_epoll_create = 5207;
188 $SYS_epoll_ctl = 5208;
189 $SYS_epoll_wait = 5209;
191 $SYS_signalfd4 = 5283;
192 $SYS_renameat2 //= 5311;
194 } elsif ($machine =~ m/^mips/) {
195 $SYS_epoll_create = 4248;
196 $SYS_epoll_ctl = 4249;
197 $SYS_epoll_wait = 4250;
199 $SYS_signalfd4 = 4324;
200 $SYS_renameat2 //= 4351;
203 # as a last resort, try using the *.ph files which may not
204 # exist or may be wrong
206 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
207 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
208 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
210 # Note: do NOT add new syscalls to depend on *.ph, here.
211 # Better to miss syscalls (so we can fallback to IO::Poll)
212 # than to use wrong ones, since the names are not stable
213 # (at least not on FreeBSD), if the actual numbers are.
217 *epoll_wait = \&epoll_wait_mod8;
218 *epoll_ctl = \&epoll_ctl_mod8;
220 *epoll_wait = \&epoll_wait_mod4;
221 *epoll_ctl = \&epoll_ctl_mod4;
224 # use Inline::C for *BSD-only or general POSIX stuff.
225 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
226 # use scripts/syscall-list on Linux to detect new syscall numbers
228 ############################################################################
230 ############################################################################
232 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
235 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
239 # ARGS: (epfd, op, fd, events_mask)
241 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
244 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
248 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
249 # arrayref: values modified to be [$fd, $event]
250 our $epoll_wait_events = '';
251 our $epoll_wait_size = 0;
252 sub epoll_wait_mod4 {
253 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
254 # resize our static buffer if maxevents bigger than we've ever done
255 if ($maxevents > $epoll_wait_size) {
256 $epoll_wait_size = $maxevents;
257 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
260 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
261 $maxevents, $timeout_msec);
263 # 12-byte struct epoll_event
264 # 4 bytes uint32_t events mask (skipped, useless to us)
265 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
266 # So we skip the first 4 bytes and take the middle 4:
267 $events->[$_] = unpack('L', substr($epoll_wait_events,
272 sub epoll_wait_mod8 {
273 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
275 # resize our static buffer if maxevents bigger than we've ever done
276 if ($maxevents > $epoll_wait_size) {
277 $epoll_wait_size = $maxevents;
278 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
281 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
282 $maxevents, $timeout_msec,
283 $no_deprecated ? undef : ());
285 # 16-byte struct epoll_event
286 # 4 bytes uint32_t events mask (skipped, useless to us)
287 # 4 bytes padding (skipped, useless)
288 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
289 # So skip the first 8 bytes, take 4, and ignore the last 4:
290 $events->[$_] = unpack('L', substr($epoll_wait_events,
296 my ($signos, $nonblock) = @_;
297 if ($SYS_signalfd4) {
298 my $set = POSIX::SigSet->new(@$signos);
299 syscall($SYS_signalfd4, -1, "$$set",
300 # $Config{sig_count} is NSIG, so this is NSIG/8:
301 int($Config{sig_count}/8),
302 # SFD_NONBLOCK == O_NONBLOCK for every architecture
303 ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
310 sub _rename_noreplace_racy ($$) {
311 my ($old, $new) = @_;
312 if (link($old, $new)) {
313 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
320 # TODO: support FD args?
321 sub rename_noreplace ($$) {
322 my ($old, $new) = @_;
323 if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
324 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
326 1; # like rename() perlop
327 } elsif ($! == ENOSYS || $! == EINVAL) {
328 undef $SYS_renameat2;
329 _rename_noreplace_racy($old, $new);
334 _rename_noreplace_racy($old, $new);
339 return if !defined($SYS_fstatfs);
341 vec($buf, 120 * 8 - 1, 1) = 0;
343 syscall($SYS_fstatfs, fileno($fh), $buf) == 0 or
344 return warn("fstatfs: $!\n");
345 my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
346 return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
348 state ($FS_IOC_GETFLAGS, $FS_IOC_SETFLAGS);
349 unless (defined $FS_IOC_GETFLAGS) {
350 if (substr($Config{byteorder}, 0, 4) eq '1234') {
351 $FS_IOC_GETFLAGS = 0x80086601;
352 $FS_IOC_SETFLAGS = 0x40086602;
353 } else { # Big endian
354 $FS_IOC_GETFLAGS = 0x40086601;
355 $FS_IOC_SETFLAGS = 0x80086602;
358 ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
359 return warn("FS_IOC_GET_FLAGS: $!\n");
360 my $attr = unpack('l!', $buf);
361 return if ($attr & 0x00800000); # FS_NOCOW_FL;
362 ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
363 warn("FS_IOC_SET_FLAGS: $!\n");
367 if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
374 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
378 Brad Fitzpatrick <brad@danga.com>