]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
Merge remote-tracking branch 'origin/danga-bundle'
[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 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 POSIX qw(ENOSYS SEEK_CUR);
17 use Config;
18
19 require Exporter;
20 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
21
22 $VERSION     = "0.25";
23 @ISA         = qw(Exporter);
24 @EXPORT_OK   = qw(sendfile epoll_ctl epoll_create epoll_wait
25                   EPOLLIN EPOLLOUT EPOLLERR EPOLLHUP EPOLLRDBAND
26                   EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
27                   EPOLLEXCLUSIVE);
28 %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
29                              EPOLLIN EPOLLOUT EPOLLERR EPOLLHUP EPOLLRDBAND
30                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
31                              EPOLLEXCLUSIVE)],
32                 sendfile => [qw(sendfile)],
33                 );
34
35 use constant EPOLLIN       => 1;
36 use constant EPOLLOUT      => 4;
37 use constant EPOLLERR      => 8;
38 use constant EPOLLHUP      => 16;
39 use constant EPOLLRDBAND   => 128;
40 use constant EPOLLEXCLUSIVE => (1 << 28);
41 use constant EPOLL_CTL_ADD => 1;
42 use constant EPOLL_CTL_DEL => 2;
43 use constant EPOLL_CTL_MOD => 3;
44
45 our $loaded_syscall = 0;
46
47 sub _load_syscall {
48     # props to Gaal for this!
49     return if $loaded_syscall++;
50     my $clean = sub {
51         delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
52                         _h2ph_pre.ph sys/syscall.ph>};
53     };
54     $clean->(); # don't trust modules before us
55     my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
56     $clean->(); # don't require modules after us trust us
57     return $rv;
58 }
59
60 our ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
61
62 our (
63      $SYS_epoll_create,
64      $SYS_epoll_ctl,
65      $SYS_epoll_wait,
66      $SYS_sendfile,
67      );
68
69 our $no_deprecated = 0;
70
71 if ($^O eq "linux") {
72     # whether the machine requires 64-bit numbers to be on 8-byte
73     # boundaries.
74     my $u64_mod_8 = 0;
75
76     # if we're running on an x86_64 kernel, but a 32-bit process,
77     # we need to use the i386 syscall numbers.
78     if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
79         $machine = "i386";
80     }
81
82     # Similarly for mips64 vs mips
83     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
84         $machine = "mips";
85     }
86
87     if ($machine =~ m/^i[3456]86$/) {
88         $SYS_epoll_create = 254;
89         $SYS_epoll_ctl    = 255;
90         $SYS_epoll_wait   = 256;
91         $SYS_sendfile     = 187;  # or 64: 239
92     } elsif ($machine eq "x86_64") {
93         $SYS_epoll_create = 213;
94         $SYS_epoll_ctl    = 233;
95         $SYS_epoll_wait   = 232;
96         $SYS_sendfile     =  40;
97     } elsif ($machine =~ m/^parisc/) {
98         $SYS_epoll_create = 224;
99         $SYS_epoll_ctl    = 225;
100         $SYS_epoll_wait   = 226;
101         $SYS_sendfile     = 122;  # sys_sendfile64=209
102         $u64_mod_8        = 1;
103     } elsif ($machine =~ m/^ppc64/) {
104         $SYS_epoll_create = 236;
105         $SYS_epoll_ctl    = 237;
106         $SYS_epoll_wait   = 238;
107         $SYS_sendfile     = 186;  # (sys32_sendfile).  sys32_sendfile64=226  (64 bit processes: sys_sendfile64=186)
108         $u64_mod_8        = 1;
109     } elsif ($machine eq "ppc") {
110         $SYS_epoll_create = 236;
111         $SYS_epoll_ctl    = 237;
112         $SYS_epoll_wait   = 238;
113         $SYS_sendfile     = 186;  # sys_sendfile64=226
114         $u64_mod_8        = 1;
115     } elsif ($machine =~ m/^s390/) {
116         $SYS_epoll_create = 249;
117         $SYS_epoll_ctl    = 250;
118         $SYS_epoll_wait   = 251;
119         $SYS_sendfile     = 187;  # sys_sendfile64=223
120         $u64_mod_8        = 1;
121     } elsif ($machine eq "ia64") {
122         $SYS_epoll_create = 1243;
123         $SYS_epoll_ctl    = 1244;
124         $SYS_epoll_wait   = 1245;
125         $SYS_sendfile     = 1187;
126         $u64_mod_8        = 1;
127     } elsif ($machine eq "alpha") {
128         # natural alignment, ints are 32-bits
129         $SYS_sendfile     = 370;  # (sys_sendfile64)
130         $SYS_epoll_create = 407;
131         $SYS_epoll_ctl    = 408;
132         $SYS_epoll_wait   = 409;
133         $u64_mod_8        = 1;
134     } elsif ($machine eq "aarch64") {
135         $SYS_epoll_create = 20;  # (sys_epoll_create1)
136         $SYS_epoll_ctl    = 21;
137         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
138         $SYS_sendfile     = 71;  # (sys_sendfile64)
139         $u64_mod_8        = 1;
140         $no_deprecated    = 1;
141     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
142         # ARM OABI
143         $SYS_epoll_create = 250;
144         $SYS_epoll_ctl    = 251;
145         $SYS_epoll_wait   = 252;
146         $SYS_sendfile     = 187;
147         $u64_mod_8        = 1;
148     } elsif ($machine =~ m/^mips64/) {
149         $SYS_sendfile     = 5039;
150         $SYS_epoll_create = 5207;
151         $SYS_epoll_ctl    = 5208;
152         $SYS_epoll_wait   = 5209;
153         $u64_mod_8        = 1;
154     } elsif ($machine =~ m/^mips/) {
155         $SYS_sendfile     = 4207;
156         $SYS_epoll_create = 4248;
157         $SYS_epoll_ctl    = 4249;
158         $SYS_epoll_wait   = 4250;
159         $u64_mod_8        = 1;
160     } else {
161         # as a last resort, try using the *.ph files which may not
162         # exist or may be wrong
163         _load_syscall();
164         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
165         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
166         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
167     }
168
169     if ($u64_mod_8) {
170         *epoll_wait = \&epoll_wait_mod8;
171         *epoll_ctl = \&epoll_ctl_mod8;
172     } else {
173         *epoll_wait = \&epoll_wait_mod4;
174         *epoll_ctl = \&epoll_ctl_mod4;
175     }
176 }
177
178 elsif ($^O eq "freebsd") {
179     if ($ENV{FREEBSD_SENDFILE}) {
180         # this is still buggy and in development
181         $SYS_sendfile = 393;  # old is 336
182     }
183 }
184
185 ############################################################################
186 # sendfile functions
187 ############################################################################
188
189 unless ($SYS_sendfile) {
190     _load_syscall();
191     $SYS_sendfile = eval { &SYS_sendfile; } || 0;
192 }
193
194 sub sendfile_defined { return $SYS_sendfile ? 1 : 0; }
195
196 if ($^O eq "linux" && $SYS_sendfile) {
197     *sendfile = \&sendfile_linux;
198 } elsif ($^O eq "freebsd" && $SYS_sendfile) {
199     *sendfile = \&sendfile_freebsd;
200 } else {
201     *sendfile = \&sendfile_noimpl;
202 }
203
204 sub sendfile_noimpl {
205     $! = ENOSYS;
206     return -1;
207 }
208
209 # C: ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
210 # Perl:  sendfile($write_fd, $read_fd, $max_count) --> $actually_sent
211 sub sendfile_linux {
212     return syscall(
213                    $SYS_sendfile,
214                    $_[0] + 0,  # fd
215                    $_[1] + 0,  # fd
216                    0,          # don't keep track of offset.  callers can lseek and keep track.
217                    $_[2] + 0   # count
218                    );
219 }
220
221 sub sendfile_freebsd {
222     my $offset = POSIX::lseek($_[1]+0, 0, SEEK_CUR) + 0;
223     my $ct = $_[2] + 0;
224     my $sbytes_buf = "\0" x 8;
225     my $rv = syscall(
226                      $SYS_sendfile,
227                      $_[1] + 0,   # fd     (from)
228                      $_[0] + 0,   # socket (to)
229                      $offset,
230                      $ct,
231                      0,           # struct sf_hdtr *hdtr
232                      $sbytes_buf, # off_t *sbytes
233                      0);          # flags
234     return $rv if $rv < 0;
235
236
237     my $set = unpack("L", $sbytes_buf);
238     POSIX::lseek($_[1]+0, SEEK_CUR, $set);
239     return $set;
240 }
241
242
243 ############################################################################
244 # epoll functions
245 ############################################################################
246
247 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
248
249 # ARGS: (size) -- but in modern Linux 2.6, the
250 # size doesn't even matter (radix tree now, not hash)
251 sub epoll_create {
252     return -1 unless defined $SYS_epoll_create;
253     my $epfd = eval { syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0) };
254     return -1 if $@;
255     return $epfd;
256 }
257
258 # epoll_ctl wrapper
259 # ARGS: (epfd, op, fd, events_mask)
260 sub epoll_ctl_mod4 {
261     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
262 }
263 sub epoll_ctl_mod8 {
264     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
265 }
266
267 # epoll_wait wrapper
268 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
269 #  arrayref: values modified to be [$fd, $event]
270 our $epoll_wait_events;
271 our $epoll_wait_size = 0;
272 sub epoll_wait_mod4 {
273     # resize our static buffer if requested size is bigger than we've ever done
274     if ($_[1] > $epoll_wait_size) {
275         $epoll_wait_size = $_[1];
276         $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
277     }
278     my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
279     for (0..$ct-1) {
280         @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
281     }
282     return $ct;
283 }
284
285 sub epoll_wait_mod8 {
286     # resize our static buffer if requested size is bigger than we've ever done
287     if ($_[1] > $epoll_wait_size) {
288         $epoll_wait_size = $_[1];
289         $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
290     }
291     my $ct;
292     if ($no_deprecated) {
293         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
294     } else {
295         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
296     }
297     for (0..$ct-1) {
298         # 16 byte epoll_event structs, with format:
299         #    4 byte mask [idx 1]
300         #    4 byte padding (we put it into idx 2, useless)
301         #    8 byte data (first 4 bytes are fd, into idx 0)
302         @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
303     }
304     return $ct;
305 }
306
307 1;
308
309 =head1 WARRANTY
310
311 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
312
313 =head1 AUTHORS
314
315 Brad Fitzpatrick <brad@danga.com>