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 ($FS_IOC_GETFLAGS, $FS_IOC_SETFLAGS);
73 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
74 our $no_deprecated = 0;
77 my (undef, undef, $release, undef, $machine) = POSIX::uname();
78 my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
79 $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
80 # whether the machine requires 64-bit numbers to be on 8-byte
84 # if we're running on an x86_64 kernel, but a 32-bit process,
85 # we need to use the x32 or i386 syscall numbers.
86 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
87 $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
90 # Similarly for mips64 vs mips
91 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
95 if ($machine =~ m/^i[3456]86$/) {
96 $SYS_epoll_create = 254;
98 $SYS_epoll_wait = 256;
100 $SYS_renameat2 //= 353;
102 $FS_IOC_GETFLAGS = 0x80046601;
103 $FS_IOC_SETFLAGS = 0x40046602;
104 } elsif ($machine eq "x86_64") {
105 $SYS_epoll_create = 213;
106 $SYS_epoll_ctl = 233;
107 $SYS_epoll_wait = 232;
108 $SYS_signalfd4 = 289;
109 $SYS_renameat2 //= 316;
111 $FS_IOC_GETFLAGS = 0x80086601;
112 $FS_IOC_SETFLAGS = 0x40086602;
113 } elsif ($machine eq 'x32') {
114 $SYS_epoll_create = 1073742037;
115 $SYS_epoll_ctl = 1073742057;
116 $SYS_epoll_wait = 1073742056;
117 $SYS_signalfd4 = 1073742113;
118 $SYS_renameat2 //= 0x40000000 + 316;
120 $FS_IOC_GETFLAGS = 0x80046601;
121 $FS_IOC_SETFLAGS = 0x40046602;
122 } elsif ($machine eq 'sparc64') {
123 $SYS_epoll_create = 193;
124 $SYS_epoll_ctl = 194;
125 $SYS_epoll_wait = 195;
127 $SYS_signalfd4 = 317;
128 $SYS_renameat2 //= 345;
129 $SFD_CLOEXEC = 020000000;
131 $FS_IOC_GETFLAGS = 0x40086601;
132 $FS_IOC_SETFLAGS = 0x80086602;
133 } elsif ($machine =~ m/^parisc/) {
134 $SYS_epoll_create = 224;
135 $SYS_epoll_ctl = 225;
136 $SYS_epoll_wait = 226;
138 $SYS_signalfd4 = 309;
139 } elsif ($machine =~ m/^ppc64/) {
140 $SYS_epoll_create = 236;
141 $SYS_epoll_ctl = 237;
142 $SYS_epoll_wait = 238;
144 $SYS_signalfd4 = 313;
145 $SYS_renameat2 //= 357;
147 $FS_IOC_GETFLAGS = 0x40086601;
148 $FS_IOC_SETFLAGS = 0x80086602;
149 } elsif ($machine eq "ppc") {
150 $SYS_epoll_create = 236;
151 $SYS_epoll_ctl = 237;
152 $SYS_epoll_wait = 238;
154 $SYS_signalfd4 = 313;
155 $SYS_renameat2 //= 357;
157 $FS_IOC_GETFLAGS = 0x40086601;
158 $FS_IOC_SETFLAGS = 0x80086602;
159 } elsif ($machine =~ m/^s390/) {
160 $SYS_epoll_create = 249;
161 $SYS_epoll_ctl = 250;
162 $SYS_epoll_wait = 251;
164 $SYS_signalfd4 = 322;
165 $SYS_renameat2 //= 347;
167 } elsif ($machine eq "ia64") {
168 $SYS_epoll_create = 1243;
169 $SYS_epoll_ctl = 1244;
170 $SYS_epoll_wait = 1245;
172 $SYS_signalfd4 = 289;
173 } elsif ($machine eq "alpha") {
174 # natural alignment, ints are 32-bits
175 $SYS_epoll_create = 407;
176 $SYS_epoll_ctl = 408;
177 $SYS_epoll_wait = 409;
179 $SYS_signalfd4 = 484;
180 $SFD_CLOEXEC = 010000000;
181 } elsif ($machine eq "aarch64") {
182 $SYS_epoll_create = 20; # (sys_epoll_create1)
184 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
188 $SYS_renameat2 //= 276;
190 $FS_IOC_GETFLAGS = 0x80086601;
191 $FS_IOC_SETFLAGS = 0x40086602;
192 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
194 $SYS_epoll_create = 250;
195 $SYS_epoll_ctl = 251;
196 $SYS_epoll_wait = 252;
198 $SYS_signalfd4 = 355;
199 $SYS_renameat2 //= 382;
201 } elsif ($machine =~ m/^mips64/) {
202 $SYS_epoll_create = 5207;
203 $SYS_epoll_ctl = 5208;
204 $SYS_epoll_wait = 5209;
206 $SYS_signalfd4 = 5283;
207 $SYS_renameat2 //= 5311;
209 $FS_IOC_GETFLAGS = 0x40046601;
210 $FS_IOC_SETFLAGS = 0x80046602;
211 } elsif ($machine =~ m/^mips/) {
212 $SYS_epoll_create = 4248;
213 $SYS_epoll_ctl = 4249;
214 $SYS_epoll_wait = 4250;
216 $SYS_signalfd4 = 4324;
217 $SYS_renameat2 //= 4351;
219 $FS_IOC_GETFLAGS = 0x40046601;
220 $FS_IOC_SETFLAGS = 0x80046602;
222 # as a last resort, try using the *.ph files which may not
223 # exist or may be wrong
225 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
226 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
227 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
229 # Note: do NOT add new syscalls to depend on *.ph, here.
230 # Better to miss syscalls (so we can fallback to IO::Poll)
231 # than to use wrong ones, since the names are not stable
232 # (at least not on FreeBSD), if the actual numbers are.
236 *epoll_wait = \&epoll_wait_mod8;
237 *epoll_ctl = \&epoll_ctl_mod8;
239 *epoll_wait = \&epoll_wait_mod4;
240 *epoll_ctl = \&epoll_ctl_mod4;
243 # use Inline::C for *BSD-only or general POSIX stuff.
244 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
245 # use scripts/syscall-list on Linux to detect new syscall numbers
247 ############################################################################
249 ############################################################################
251 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
254 syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
258 # ARGS: (epfd, op, fd, events_mask)
260 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
263 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
267 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
268 # arrayref: values modified to be [$fd, $event]
269 our $epoll_wait_events = '';
270 our $epoll_wait_size = 0;
271 sub epoll_wait_mod4 {
272 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
273 # resize our static buffer if maxevents bigger than we've ever done
274 if ($maxevents > $epoll_wait_size) {
275 $epoll_wait_size = $maxevents;
276 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
279 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
280 $maxevents, $timeout_msec);
282 # 12-byte struct epoll_event
283 # 4 bytes uint32_t events mask (skipped, useless to us)
284 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
285 # So we skip the first 4 bytes and take the middle 4:
286 $events->[$_] = unpack('L', substr($epoll_wait_events,
291 sub epoll_wait_mod8 {
292 my ($epfd, $maxevents, $timeout_msec, $events) = @_;
294 # resize our static buffer if maxevents bigger than we've ever done
295 if ($maxevents > $epoll_wait_size) {
296 $epoll_wait_size = $maxevents;
297 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
300 my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
301 $maxevents, $timeout_msec,
302 $no_deprecated ? undef : ());
304 # 16-byte struct epoll_event
305 # 4 bytes uint32_t events mask (skipped, useless to us)
306 # 4 bytes padding (skipped, useless)
307 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
308 # So skip the first 8 bytes, take 4, and ignore the last 4:
309 $events->[$_] = unpack('L', substr($epoll_wait_events,
315 my ($signos, $nonblock) = @_;
316 if ($SYS_signalfd4) {
317 my $set = POSIX::SigSet->new(@$signos);
318 syscall($SYS_signalfd4, -1, "$$set",
319 # $Config{sig_count} is NSIG, so this is NSIG/8:
320 int($Config{sig_count}/8),
321 # SFD_NONBLOCK == O_NONBLOCK for every architecture
322 ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
329 sub _rename_noreplace_racy ($$) {
330 my ($old, $new) = @_;
331 if (link($old, $new)) {
332 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
339 # TODO: support FD args?
340 sub rename_noreplace ($$) {
341 my ($old, $new) = @_;
342 if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
343 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
345 1; # like rename() perlop
346 } elsif ($! == ENOSYS || $! == EINVAL) {
347 undef $SYS_renameat2;
348 _rename_noreplace_racy($old, $new);
353 _rename_noreplace_racy($old, $new);
358 return if !defined($SYS_fstatfs);
360 vec($buf, 120 * 8 - 1, 1) = 0;
362 syscall($SYS_fstatfs, fileno($fh), $buf) == 0 or
363 return warn("fstatfs: $!\n");
364 my $f_type = unpack('l!', $buf); # statfs.f_type is a signed word
365 return if $f_type != 0x9123683E; # BTRFS_SUPER_MAGIC
368 return warn('FS_IOC_GETFLAGS undefined for platform');
369 ioctl($fh, $FS_IOC_GETFLAGS, $buf) //
370 return warn("FS_IOC_GETFLAGS: $!\n");
371 my $attr = unpack('l!', $buf);
372 return if ($attr & 0x00800000); # FS_NOCOW_FL;
373 ioctl($fh, $FS_IOC_SETFLAGS, pack('l', $attr | 0x00800000)) //
374 warn("FS_IOC_SETFLAGS: $!\n");
378 if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
385 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
389 Brad Fitzpatrick <brad@danga.com>