]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
syscall: FS_IOC_*FLAGS: define on per-architecture basis
[public-inbox.git] / lib / PublicInbox / Syscall.pm
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.
4 #
5 # This license differs from the rest of public-inbox
6 #
7 # This module is Copyright (c) 2005 Six Apart, Ltd.
8 # Copyright (C) all contributors <meta@public-inbox.org>
9 #
10 # All rights reserved.
11 #
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;
15 use strict;
16 use v5.10.1;
17 use parent qw(Exporter);
18 use POSIX qw(ENOENT EEXIST ENOSYS EINVAL O_NONBLOCK);
19 use Config;
20
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
28                              EPOLLIN EPOLLOUT
29                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
30                              EPOLLONESHOT EPOLLEXCLUSIVE)],
31                 );
32
33 use constant {
34         EPOLLIN => 1,
35         EPOLLOUT => 4,
36         # EPOLLERR => 8,
37         # EPOLLHUP => 16,
38         # EPOLLRDBAND => 128,
39         EPOLLEXCLUSIVE => (1 << 28),
40         EPOLLONESHOT => (1 << 30),
41         EPOLLET => (1 << 31),
42         EPOLL_CTL_ADD => 1,
43         EPOLL_CTL_DEL => 2,
44         EPOLL_CTL_MOD => 3,
45 };
46
47 our $loaded_syscall = 0;
48
49 sub _load_syscall {
50     # props to Gaal for this!
51     return if $loaded_syscall++;
52     my $clean = sub {
53         delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
54                         _h2ph_pre.ph sys/syscall.ph>};
55     };
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
59     $rv;
60 }
61
62
63 our (
64      $SYS_epoll_create,
65      $SYS_epoll_ctl,
66      $SYS_epoll_wait,
67      $SYS_signalfd4,
68      $SYS_renameat2,
69      );
70
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;
75
76 if ($^O eq "linux") {
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
81     # boundaries.
82     my $u64_mod_8 = 0;
83
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';
88     }
89
90     # Similarly for mips64 vs mips
91     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
92         $machine = "mips";
93     }
94
95     if ($machine =~ m/^i[3456]86$/) {
96         $SYS_epoll_create = 254;
97         $SYS_epoll_ctl    = 255;
98         $SYS_epoll_wait   = 256;
99         $SYS_signalfd4 = 327;
100         $SYS_renameat2 //= 353;
101         $SYS_fstatfs = 100;
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;
110         $SYS_fstatfs = 138;
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;
119         $SYS_fstatfs = 138;
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;
126         $u64_mod_8 = 1;
127         $SYS_signalfd4 = 317;
128         $SYS_renameat2 //= 345;
129         $SFD_CLOEXEC = 020000000;
130         $SYS_fstatfs = 158;
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;
137         $u64_mod_8        = 1;
138         $SYS_signalfd4 = 309;
139     } elsif ($machine =~ m/^ppc64/) {
140         $SYS_epoll_create = 236;
141         $SYS_epoll_ctl    = 237;
142         $SYS_epoll_wait   = 238;
143         $u64_mod_8        = 1;
144         $SYS_signalfd4 = 313;
145         $SYS_renameat2 //= 357;
146         $SYS_fstatfs = 100;
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;
153         $u64_mod_8        = 1;
154         $SYS_signalfd4 = 313;
155         $SYS_renameat2 //= 357;
156         $SYS_fstatfs = 100;
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;
163         $u64_mod_8        = 1;
164         $SYS_signalfd4 = 322;
165         $SYS_renameat2 //= 347;
166         $SYS_fstatfs = 100;
167     } elsif ($machine eq "ia64") {
168         $SYS_epoll_create = 1243;
169         $SYS_epoll_ctl    = 1244;
170         $SYS_epoll_wait   = 1245;
171         $u64_mod_8        = 1;
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;
178         $u64_mod_8        = 1;
179         $SYS_signalfd4 = 484;
180         $SFD_CLOEXEC = 010000000;
181     } elsif ($machine eq "aarch64") {
182         $SYS_epoll_create = 20;  # (sys_epoll_create1)
183         $SYS_epoll_ctl    = 21;
184         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
185         $u64_mod_8        = 1;
186         $no_deprecated    = 1;
187         $SYS_signalfd4 = 74;
188         $SYS_renameat2 //= 276;
189         $SYS_fstatfs = 44;
190         $FS_IOC_GETFLAGS = 0x80086601;
191         $FS_IOC_SETFLAGS = 0x40086602;
192     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
193         # ARM OABI
194         $SYS_epoll_create = 250;
195         $SYS_epoll_ctl    = 251;
196         $SYS_epoll_wait   = 252;
197         $u64_mod_8        = 1;
198         $SYS_signalfd4 = 355;
199         $SYS_renameat2 //= 382;
200         $SYS_fstatfs = 100;
201     } elsif ($machine =~ m/^mips64/) {
202         $SYS_epoll_create = 5207;
203         $SYS_epoll_ctl    = 5208;
204         $SYS_epoll_wait   = 5209;
205         $u64_mod_8        = 1;
206         $SYS_signalfd4 = 5283;
207         $SYS_renameat2 //= 5311;
208         $SYS_fstatfs = 5135;
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;
215         $u64_mod_8        = 1;
216         $SYS_signalfd4 = 4324;
217         $SYS_renameat2 //= 4351;
218         $SYS_fstatfs = 4100;
219         $FS_IOC_GETFLAGS = 0x40046601;
220         $FS_IOC_SETFLAGS = 0x80046602;
221     } else {
222         # as a last resort, try using the *.ph files which may not
223         # exist or may be wrong
224         _load_syscall();
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;
228
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.
233     }
234
235     if ($u64_mod_8) {
236         *epoll_wait = \&epoll_wait_mod8;
237         *epoll_ctl = \&epoll_ctl_mod8;
238     } else {
239         *epoll_wait = \&epoll_wait_mod4;
240         *epoll_ctl = \&epoll_ctl_mod4;
241     }
242 }
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
246
247 ############################################################################
248 # epoll functions
249 ############################################################################
250
251 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
252
253 sub epoll_create {
254         syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
255 }
256
257 # epoll_ctl wrapper
258 # ARGS: (epfd, op, fd, events_mask)
259 sub epoll_ctl_mod4 {
260     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
261 }
262 sub epoll_ctl_mod8 {
263     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
264 }
265
266 # epoll_wait wrapper
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;
277         }
278         @$events = ();
279         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
280                         $maxevents, $timeout_msec);
281         for (0..$ct - 1) {
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,
287                                                         12 * $_ + 4, 4));
288         }
289 }
290
291 sub epoll_wait_mod8 {
292         my ($epfd, $maxevents, $timeout_msec, $events) = @_;
293
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;
298         }
299         @$events = ();
300         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
301                         $maxevents, $timeout_msec,
302                         $no_deprecated ? undef : ());
303         for (0..$ct - 1) {
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,
310                                                         16 * $_ + 8, 4));
311         }
312 }
313
314 sub signalfd ($$) {
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);
323         } else {
324                 $! = ENOSYS;
325                 undef;
326         }
327 }
328
329 sub _rename_noreplace_racy ($$) {
330         my ($old, $new) = @_;
331         if (link($old, $new)) {
332                 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
333                 1
334         } else {
335                 undef;
336         }
337 }
338
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);
344                 if ($ret == 0) {
345                         1; # like rename() perlop
346                 } elsif ($! == ENOSYS || $! == EINVAL) {
347                         undef $SYS_renameat2;
348                         _rename_noreplace_racy($old, $new);
349                 } else {
350                         undef
351                 }
352         } else {
353                 _rename_noreplace_racy($old, $new);
354         }
355 }
356
357 sub nodatacow_fh {
358         return if !defined($SYS_fstatfs);
359         my $buf = '';
360         vec($buf, 120 * 8 - 1, 1) = 0;
361         my ($fh) = @_;
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
366
367         $FS_IOC_GETFLAGS //
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");
375 }
376
377 sub nodatacow_dir {
378         if (open my $fh, '<', $_[0]) { nodatacow_fh($fh) }
379 }
380
381 1;
382
383 =head1 WARRANTY
384
385 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
386
387 =head1 AUTHORS
388
389 Brad Fitzpatrick <brad@danga.com>