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