]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
lei: use RENAME_NOREPLACE on Linux 3.15+
[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) 2019-2021 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 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 $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
72 our $no_deprecated = 0;
73
74 if ($^O eq "linux") {
75     my (undef, undef, $release, undef, $machine) = POSIX::uname();
76     my ($maj, $min) = ($release =~ /\A([0-9]+)\.([0-9]+)/);
77     $SYS_renameat2 = 0 if "$maj.$min" < 3.15;
78     # whether the machine requires 64-bit numbers to be on 8-byte
79     # boundaries.
80     my $u64_mod_8 = 0;
81
82     # if we're running on an x86_64 kernel, but a 32-bit process,
83     # we need to use the x32 or i386 syscall numbers.
84     if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
85         $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
86     }
87
88     # Similarly for mips64 vs mips
89     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
90         $machine = "mips";
91     }
92
93     if ($machine =~ m/^i[3456]86$/) {
94         $SYS_epoll_create = 254;
95         $SYS_epoll_ctl    = 255;
96         $SYS_epoll_wait   = 256;
97         $SYS_signalfd4 = 327;
98         $SYS_renameat2 //= 353;
99     } elsif ($machine eq "x86_64") {
100         $SYS_epoll_create = 213;
101         $SYS_epoll_ctl    = 233;
102         $SYS_epoll_wait   = 232;
103         $SYS_signalfd4 = 289;
104         $SYS_renameat2 //= 316;
105     } elsif ($machine eq 'x32') {
106         $SYS_epoll_create = 1073742037;
107         $SYS_epoll_ctl = 1073742057;
108         $SYS_epoll_wait = 1073742056;
109         $SYS_signalfd4 = 1073742113;
110         $SYS_renameat2 //= 0x40000000 + 316;
111     } elsif ($machine eq 'sparc64') {
112         $SYS_epoll_create = 193;
113         $SYS_epoll_ctl = 194;
114         $SYS_epoll_wait = 195;
115         $u64_mod_8 = 1;
116         $SYS_signalfd4 = 317;
117         $SYS_renameat2 //= 345;
118         $SFD_CLOEXEC = 020000000;
119     } elsif ($machine =~ m/^parisc/) {
120         $SYS_epoll_create = 224;
121         $SYS_epoll_ctl    = 225;
122         $SYS_epoll_wait   = 226;
123         $u64_mod_8        = 1;
124         $SYS_signalfd4 = 309;
125     } elsif ($machine =~ m/^ppc64/) {
126         $SYS_epoll_create = 236;
127         $SYS_epoll_ctl    = 237;
128         $SYS_epoll_wait   = 238;
129         $u64_mod_8        = 1;
130         $SYS_signalfd4 = 313;
131         $SYS_renameat2 //= 357;
132     } elsif ($machine eq "ppc") {
133         $SYS_epoll_create = 236;
134         $SYS_epoll_ctl    = 237;
135         $SYS_epoll_wait   = 238;
136         $u64_mod_8        = 1;
137         $SYS_signalfd4 = 313;
138         $SYS_renameat2 //= 357;
139     } elsif ($machine =~ m/^s390/) {
140         $SYS_epoll_create = 249;
141         $SYS_epoll_ctl    = 250;
142         $SYS_epoll_wait   = 251;
143         $u64_mod_8        = 1;
144         $SYS_signalfd4 = 322;
145         $SYS_renameat2 //= 347;
146     } elsif ($machine eq "ia64") {
147         $SYS_epoll_create = 1243;
148         $SYS_epoll_ctl    = 1244;
149         $SYS_epoll_wait   = 1245;
150         $u64_mod_8        = 1;
151         $SYS_signalfd4 = 289;
152     } elsif ($machine eq "alpha") {
153         # natural alignment, ints are 32-bits
154         $SYS_epoll_create = 407;
155         $SYS_epoll_ctl    = 408;
156         $SYS_epoll_wait   = 409;
157         $u64_mod_8        = 1;
158         $SYS_signalfd4 = 484;
159         $SFD_CLOEXEC = 010000000;
160     } elsif ($machine eq "aarch64") {
161         $SYS_epoll_create = 20;  # (sys_epoll_create1)
162         $SYS_epoll_ctl    = 21;
163         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
164         $u64_mod_8        = 1;
165         $no_deprecated    = 1;
166         $SYS_signalfd4 = 74;
167         $SYS_renameat2 //= 276;
168     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
169         # ARM OABI
170         $SYS_epoll_create = 250;
171         $SYS_epoll_ctl    = 251;
172         $SYS_epoll_wait   = 252;
173         $u64_mod_8        = 1;
174         $SYS_signalfd4 = 355;
175         $SYS_renameat2 //= 382;
176     } elsif ($machine =~ m/^mips64/) {
177         $SYS_epoll_create = 5207;
178         $SYS_epoll_ctl    = 5208;
179         $SYS_epoll_wait   = 5209;
180         $u64_mod_8        = 1;
181         $SYS_signalfd4 = 5283;
182         $SYS_renameat2 //= 5311;
183     } elsif ($machine =~ m/^mips/) {
184         $SYS_epoll_create = 4248;
185         $SYS_epoll_ctl    = 4249;
186         $SYS_epoll_wait   = 4250;
187         $u64_mod_8        = 1;
188         $SYS_signalfd4 = 4324;
189         $SYS_renameat2 //= 4351;
190     } else {
191         # as a last resort, try using the *.ph files which may not
192         # exist or may be wrong
193         _load_syscall();
194         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
195         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
196         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
197
198         # Note: do NOT add new syscalls to depend on *.ph, here.
199         # Better to miss syscalls (so we can fallback to IO::Poll)
200         # than to use wrong ones, since the names are not stable
201         # (at least not on FreeBSD), if the actual numbers are.
202     }
203
204     if ($u64_mod_8) {
205         *epoll_wait = \&epoll_wait_mod8;
206         *epoll_ctl = \&epoll_ctl_mod8;
207     } else {
208         *epoll_wait = \&epoll_wait_mod4;
209         *epoll_ctl = \&epoll_ctl_mod4;
210     }
211 }
212 # use Inline::C for *BSD-only or general POSIX stuff.
213 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
214 # use scripts/syscall-list on Linux to detect new syscall numbers
215
216 ############################################################################
217 # epoll functions
218 ############################################################################
219
220 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
221
222 sub epoll_create {
223         syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
224 }
225
226 # epoll_ctl wrapper
227 # ARGS: (epfd, op, fd, events_mask)
228 sub epoll_ctl_mod4 {
229     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
230 }
231 sub epoll_ctl_mod8 {
232     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
233 }
234
235 # epoll_wait wrapper
236 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
237 #  arrayref: values modified to be [$fd, $event]
238 our $epoll_wait_events = '';
239 our $epoll_wait_size = 0;
240 sub epoll_wait_mod4 {
241         my ($epfd, $maxevents, $timeout_msec, $events) = @_;
242         # resize our static buffer if maxevents bigger than we've ever done
243         if ($maxevents > $epoll_wait_size) {
244                 $epoll_wait_size = $maxevents;
245                 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
246         }
247         @$events = ();
248         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
249                         $maxevents, $timeout_msec);
250         for (0..$ct - 1) {
251                 # 12-byte struct epoll_event
252                 # 4 bytes uint32_t events mask (skipped, useless to us)
253                 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
254                 # So we skip the first 4 bytes and take the middle 4:
255                 $events->[$_] = unpack('L', substr($epoll_wait_events,
256                                                         12 * $_ + 4, 4));
257         }
258 }
259
260 sub epoll_wait_mod8 {
261         my ($epfd, $maxevents, $timeout_msec, $events) = @_;
262
263         # resize our static buffer if maxevents bigger than we've ever done
264         if ($maxevents > $epoll_wait_size) {
265                 $epoll_wait_size = $maxevents;
266                 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
267         }
268         @$events = ();
269         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
270                         $maxevents, $timeout_msec,
271                         $no_deprecated ? undef : ());
272         for (0..$ct - 1) {
273                 # 16-byte struct epoll_event
274                 # 4 bytes uint32_t events mask (skipped, useless to us)
275                 # 4 bytes padding (skipped, useless)
276                 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
277                 # So skip the first 8 bytes, take 4, and ignore the last 4:
278                 $events->[$_] = unpack('L', substr($epoll_wait_events,
279                                                         16 * $_ + 8, 4));
280         }
281 }
282
283 sub signalfd ($$) {
284         my ($signos, $nonblock) = @_;
285         if ($SYS_signalfd4) {
286                 my $set = POSIX::SigSet->new(@$signos);
287                 syscall($SYS_signalfd4, -1, "$$set",
288                         # $Config{sig_count} is NSIG, so this is NSIG/8:
289                         int($Config{sig_count}/8),
290                         # SFD_NONBLOCK == O_NONBLOCK for every architecture
291                         ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
292         } else {
293                 $! = ENOSYS;
294                 undef;
295         }
296 }
297
298 sub _rename_noreplace_racy ($$) {
299         my ($old, $new) = @_;
300         if (link($old, $new)) {
301                 warn "unlink $old: $!\n" if !unlink($old) && $! != ENOENT;
302                 1
303         } else {
304                 undef;
305         }
306 }
307
308 # TODO: support FD args?
309 sub rename_noreplace ($$) {
310         my ($old, $new) = @_;
311         if ($SYS_renameat2) { # RENAME_NOREPLACE = 1, AT_FDCWD = -100
312                 my $ret = syscall($SYS_renameat2, -100, $old, -100, $new, 1);
313                 if ($ret == 0) {
314                         1; # like rename() perlop
315                 } elsif ($! == ENOSYS) {
316                         undef $SYS_renameat2;
317                         _rename_noreplace_racy($old, $new);
318                 } else {
319                         undef
320                 }
321         } else {
322                 _rename_noreplace_racy($old, $new);
323         }
324 }
325
326 1;
327
328 =head1 WARRANTY
329
330 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
331
332 =head1 AUTHORS
333
334 Brad Fitzpatrick <brad@danga.com>