]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
7ab4291119ea4deb84a7fe31ec6ed9e144a84d3d
[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 parent qw(Exporter);
17 use POSIX qw(ENOSYS O_NONBLOCK);
18 use Config;
19
20 # $VERSION = '0.25'; # Sys::Syscall version
21 our @EXPORT_OK = qw(epoll_ctl epoll_create epoll_wait
22                   EPOLLIN EPOLLOUT EPOLLET
23                   EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
24                   EPOLLONESHOT EPOLLEXCLUSIVE
25                   signalfd);
26 our %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
27                              EPOLLIN EPOLLOUT
28                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
29                              EPOLLONESHOT EPOLLEXCLUSIVE)],
30                 );
31
32 use constant {
33         EPOLLIN => 1,
34         EPOLLOUT => 4,
35         # EPOLLERR => 8,
36         # EPOLLHUP => 16,
37         # EPOLLRDBAND => 128,
38         EPOLLEXCLUSIVE => (1 << 28),
39         EPOLLONESHOT => (1 << 30),
40         EPOLLET => (1 << 31),
41         EPOLL_CTL_ADD => 1,
42         EPOLL_CTL_DEL => 2,
43         EPOLL_CTL_MOD => 3,
44 };
45
46 our $loaded_syscall = 0;
47
48 sub _load_syscall {
49     # props to Gaal for this!
50     return if $loaded_syscall++;
51     my $clean = sub {
52         delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
53                         _h2ph_pre.ph sys/syscall.ph>};
54     };
55     $clean->(); # don't trust modules before us
56     my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
57     $clean->(); # don't require modules after us trust us
58     $rv;
59 }
60
61
62 our (
63      $SYS_epoll_create,
64      $SYS_epoll_ctl,
65      $SYS_epoll_wait,
66      $SYS_signalfd4,
67      );
68
69 my $SFD_CLOEXEC = 02000000; # Perl does not expose O_CLOEXEC
70 our $no_deprecated = 0;
71
72 if ($^O eq "linux") {
73     my $machine = (POSIX::uname())[-1];
74     # whether the machine requires 64-bit numbers to be on 8-byte
75     # boundaries.
76     my $u64_mod_8 = 0;
77
78     # if we're running on an x86_64 kernel, but a 32-bit process,
79     # we need to use the x32 or i386 syscall numbers.
80     if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
81         $machine = $Config{cppsymbols} =~ /\b__ILP32__=1\b/ ? 'x32' : 'i386';
82     }
83
84     # Similarly for mips64 vs mips
85     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
86         $machine = "mips";
87     }
88
89     if ($machine =~ m/^i[3456]86$/) {
90         $SYS_epoll_create = 254;
91         $SYS_epoll_ctl    = 255;
92         $SYS_epoll_wait   = 256;
93         $SYS_signalfd4 = 327;
94     } elsif ($machine eq "x86_64") {
95         $SYS_epoll_create = 213;
96         $SYS_epoll_ctl    = 233;
97         $SYS_epoll_wait   = 232;
98         $SYS_signalfd4 = 289;
99     } elsif ($machine eq 'x32') {
100         $SYS_epoll_create = 1073742037;
101         $SYS_epoll_ctl = 1073742057;
102         $SYS_epoll_wait = 1073742056;
103         $SYS_signalfd4 = 1073742113;
104     } elsif ($machine eq 'sparc64') {
105         $SYS_epoll_create = 193;
106         $SYS_epoll_ctl = 194;
107         $SYS_epoll_wait = 195;
108         $u64_mod_8 = 1;
109         $SYS_signalfd4 = 317;
110         $SFD_CLOEXEC = 020000000;
111     } elsif ($machine =~ m/^parisc/) {
112         $SYS_epoll_create = 224;
113         $SYS_epoll_ctl    = 225;
114         $SYS_epoll_wait   = 226;
115         $u64_mod_8        = 1;
116         $SYS_signalfd4 = 309;
117     } elsif ($machine =~ m/^ppc64/) {
118         $SYS_epoll_create = 236;
119         $SYS_epoll_ctl    = 237;
120         $SYS_epoll_wait   = 238;
121         $u64_mod_8        = 1;
122         $SYS_signalfd4 = 313;
123     } elsif ($machine eq "ppc") {
124         $SYS_epoll_create = 236;
125         $SYS_epoll_ctl    = 237;
126         $SYS_epoll_wait   = 238;
127         $u64_mod_8        = 1;
128         $SYS_signalfd4 = 313;
129     } elsif ($machine =~ m/^s390/) {
130         $SYS_epoll_create = 249;
131         $SYS_epoll_ctl    = 250;
132         $SYS_epoll_wait   = 251;
133         $u64_mod_8        = 1;
134         $SYS_signalfd4 = 322;
135     } elsif ($machine eq "ia64") {
136         $SYS_epoll_create = 1243;
137         $SYS_epoll_ctl    = 1244;
138         $SYS_epoll_wait   = 1245;
139         $u64_mod_8        = 1;
140         $SYS_signalfd4 = 289;
141     } elsif ($machine eq "alpha") {
142         # natural alignment, ints are 32-bits
143         $SYS_epoll_create = 407;
144         $SYS_epoll_ctl    = 408;
145         $SYS_epoll_wait   = 409;
146         $u64_mod_8        = 1;
147         $SYS_signalfd4 = 484;
148         $SFD_CLOEXEC = 010000000;
149     } elsif ($machine eq "aarch64") {
150         $SYS_epoll_create = 20;  # (sys_epoll_create1)
151         $SYS_epoll_ctl    = 21;
152         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
153         $u64_mod_8        = 1;
154         $no_deprecated    = 1;
155         $SYS_signalfd4 = 74;
156     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
157         # ARM OABI
158         $SYS_epoll_create = 250;
159         $SYS_epoll_ctl    = 251;
160         $SYS_epoll_wait   = 252;
161         $u64_mod_8        = 1;
162         $SYS_signalfd4 = 355;
163     } elsif ($machine =~ m/^mips64/) {
164         $SYS_epoll_create = 5207;
165         $SYS_epoll_ctl    = 5208;
166         $SYS_epoll_wait   = 5209;
167         $u64_mod_8        = 1;
168         $SYS_signalfd4 = 5283;
169     } elsif ($machine =~ m/^mips/) {
170         $SYS_epoll_create = 4248;
171         $SYS_epoll_ctl    = 4249;
172         $SYS_epoll_wait   = 4250;
173         $u64_mod_8        = 1;
174         $SYS_signalfd4 = 4324;
175     } else {
176         # as a last resort, try using the *.ph files which may not
177         # exist or may be wrong
178         _load_syscall();
179         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
180         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
181         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
182
183         # Note: do NOT add new syscalls to depend on *.ph, here.
184         # Better to miss syscalls (so we can fallback to IO::Poll)
185         # than to use wrong ones, since the names are not stable
186         # (at least not on FreeBSD), if the actual numbers are.
187     }
188
189     if ($u64_mod_8) {
190         *epoll_wait = \&epoll_wait_mod8;
191         *epoll_ctl = \&epoll_ctl_mod8;
192     } else {
193         *epoll_wait = \&epoll_wait_mod4;
194         *epoll_ctl = \&epoll_ctl_mod4;
195     }
196 }
197 # use Inline::C for *BSD-only or general POSIX stuff.
198 # Linux guarantees stable syscall numbering, BSDs only offer a stable libc
199 # use scripts/syscall-list on Linux to detect new syscall numbers
200
201 ############################################################################
202 # epoll functions
203 ############################################################################
204
205 sub epoll_defined { $SYS_epoll_create ? 1 : 0; }
206
207 sub epoll_create {
208         syscall($SYS_epoll_create, $no_deprecated ? 0 : 100);
209 }
210
211 # epoll_ctl wrapper
212 # ARGS: (epfd, op, fd, events_mask)
213 sub epoll_ctl_mod4 {
214     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
215 }
216 sub epoll_ctl_mod8 {
217     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
218 }
219
220 # epoll_wait wrapper
221 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
222 #  arrayref: values modified to be [$fd, $event]
223 our $epoll_wait_events = '';
224 our $epoll_wait_size = 0;
225 sub epoll_wait_mod4 {
226         my ($epfd, $maxevents, $timeout_msec, $events) = @_;
227         # resize our static buffer if maxevents bigger than we've ever done
228         if ($maxevents > $epoll_wait_size) {
229                 $epoll_wait_size = $maxevents;
230                 vec($epoll_wait_events, $maxevents * 12 * 8 - 1, 1) = 0;
231         }
232         @$events = ();
233         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
234                         $maxevents, $timeout_msec);
235         for (0..$ct - 1) {
236                 # 12-byte struct epoll_event
237                 # 4 bytes uint32_t events mask (skipped, useless to us)
238                 # 8 bytes: epoll_data_t union (first 4 bytes are the fd)
239                 # So we skip the first 4 bytes and take the middle 4:
240                 $events->[$_] = unpack('L', substr($epoll_wait_events,
241                                                         12 * $_ + 4, 4));
242         }
243 }
244
245 sub epoll_wait_mod8 {
246         my ($epfd, $maxevents, $timeout_msec, $events) = @_;
247
248         # resize our static buffer if maxevents bigger than we've ever done
249         if ($maxevents > $epoll_wait_size) {
250                 $epoll_wait_size = $maxevents;
251                 vec($epoll_wait_events, $maxevents * 16 * 8 - 1, 1) = 0;
252         }
253         @$events = ();
254         my $ct = syscall($SYS_epoll_wait, $epfd, $epoll_wait_events,
255                         $maxevents, $timeout_msec,
256                         $no_deprecated ? undef : ());
257         for (0..$ct - 1) {
258                 # 16-byte struct epoll_event
259                 # 4 bytes uint32_t events mask (skipped, useless to us)
260                 # 4 bytes padding (skipped, useless)
261                 # 8 bytes epoll_data_t union (first 4 bytes are the fd)
262                 # So skip the first 8 bytes, take 4, and ignore the last 4:
263                 $events->[$_] = unpack('L', substr($epoll_wait_events,
264                                                         16 * $_ + 8, 4));
265         }
266 }
267
268 sub signalfd ($$) {
269         my ($signos, $nonblock) = @_;
270         if ($SYS_signalfd4) {
271                 my $set = POSIX::SigSet->new(@$signos);
272                 syscall($SYS_signalfd4, -1, "$$set",
273                         # $Config{sig_count} is NSIG, so this is NSIG/8:
274                         int($Config{sig_count}/8),
275                         # SFD_NONBLOCK == O_NONBLOCK for every architecture
276                         ($nonblock ? O_NONBLOCK : 0) |$SFD_CLOEXEC);
277         } else {
278                 $! = ENOSYS;
279                 undef;
280         }
281 }
282
283 1;
284
285 =head1 WARRANTY
286
287 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
288
289 =head1 AUTHORS
290
291 Brad Fitzpatrick <brad@danga.com>