]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
syscall: get rid of sendfile wrappers for now
[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(epoll_ctl epoll_create epoll_wait
25                   EPOLLIN EPOLLOUT EPOLLET
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                 );
33
34 use constant EPOLLIN       => 1;
35 use constant EPOLLOUT      => 4;
36 # use constant EPOLLERR      => 8;
37 # use constant EPOLLHUP      => 16;
38 # use constant EPOLLRDBAND   => 128;
39 use constant EPOLLEXCLUSIVE => (1 << 28);
40 use constant EPOLLONESHOT => (1 << 30);
41 use constant EPOLLET => (1 << 31);
42 use constant EPOLL_CTL_ADD => 1;
43 use constant EPOLL_CTL_DEL => 2;
44 use constant EPOLL_CTL_MOD => 3;
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     return $rv;
59 }
60
61
62 our (
63      $SYS_epoll_create,
64      $SYS_epoll_ctl,
65      $SYS_epoll_wait,
66      );
67
68 our $no_deprecated = 0;
69
70 if ($^O eq "linux") {
71     my $machine = (POSIX::uname())[-1];
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     } elsif ($machine eq "x86_64") {
92         $SYS_epoll_create = 213;
93         $SYS_epoll_ctl    = 233;
94         $SYS_epoll_wait   = 232;
95     } elsif ($machine =~ m/^parisc/) {
96         $SYS_epoll_create = 224;
97         $SYS_epoll_ctl    = 225;
98         $SYS_epoll_wait   = 226;
99         $u64_mod_8        = 1;
100     } elsif ($machine =~ m/^ppc64/) {
101         $SYS_epoll_create = 236;
102         $SYS_epoll_ctl    = 237;
103         $SYS_epoll_wait   = 238;
104         $u64_mod_8        = 1;
105     } elsif ($machine eq "ppc") {
106         $SYS_epoll_create = 236;
107         $SYS_epoll_ctl    = 237;
108         $SYS_epoll_wait   = 238;
109         $u64_mod_8        = 1;
110     } elsif ($machine =~ m/^s390/) {
111         $SYS_epoll_create = 249;
112         $SYS_epoll_ctl    = 250;
113         $SYS_epoll_wait   = 251;
114         $u64_mod_8        = 1;
115     } elsif ($machine eq "ia64") {
116         $SYS_epoll_create = 1243;
117         $SYS_epoll_ctl    = 1244;
118         $SYS_epoll_wait   = 1245;
119         $u64_mod_8        = 1;
120     } elsif ($machine eq "alpha") {
121         # natural alignment, ints are 32-bits
122         $SYS_epoll_create = 407;
123         $SYS_epoll_ctl    = 408;
124         $SYS_epoll_wait   = 409;
125         $u64_mod_8        = 1;
126     } elsif ($machine eq "aarch64") {
127         $SYS_epoll_create = 20;  # (sys_epoll_create1)
128         $SYS_epoll_ctl    = 21;
129         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
130         $u64_mod_8        = 1;
131         $no_deprecated    = 1;
132     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
133         # ARM OABI
134         $SYS_epoll_create = 250;
135         $SYS_epoll_ctl    = 251;
136         $SYS_epoll_wait   = 252;
137         $u64_mod_8        = 1;
138     } elsif ($machine =~ m/^mips64/) {
139         $SYS_epoll_create = 5207;
140         $SYS_epoll_ctl    = 5208;
141         $SYS_epoll_wait   = 5209;
142         $u64_mod_8        = 1;
143     } elsif ($machine =~ m/^mips/) {
144         $SYS_epoll_create = 4248;
145         $SYS_epoll_ctl    = 4249;
146         $SYS_epoll_wait   = 4250;
147         $u64_mod_8        = 1;
148     } else {
149         # as a last resort, try using the *.ph files which may not
150         # exist or may be wrong
151         _load_syscall();
152         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
153         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
154         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
155     }
156
157     if ($u64_mod_8) {
158         *epoll_wait = \&epoll_wait_mod8;
159         *epoll_ctl = \&epoll_ctl_mod8;
160     } else {
161         *epoll_wait = \&epoll_wait_mod4;
162         *epoll_ctl = \&epoll_ctl_mod4;
163     }
164 }
165
166 elsif ($^O eq "freebsd") {
167     if ($ENV{FREEBSD_SENDFILE}) {
168         # this is still buggy and in development
169     }
170 }
171
172 ############################################################################
173 # epoll functions
174 ############################################################################
175
176 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
177
178 sub epoll_create {
179         syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
180 }
181
182 # epoll_ctl wrapper
183 # ARGS: (epfd, op, fd, events_mask)
184 sub epoll_ctl_mod4 {
185     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
186 }
187 sub epoll_ctl_mod8 {
188     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
189 }
190
191 # epoll_wait wrapper
192 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
193 #  arrayref: values modified to be [$fd, $event]
194 our $epoll_wait_events;
195 our $epoll_wait_size = 0;
196 sub epoll_wait_mod4 {
197     # resize our static buffer if requested size is bigger than we've ever done
198     if ($_[1] > $epoll_wait_size) {
199         $epoll_wait_size = $_[1];
200         $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
201     }
202     my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
203     for (0..$ct-1) {
204         @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
205     }
206     return $ct;
207 }
208
209 sub epoll_wait_mod8 {
210     # resize our static buffer if requested size is bigger than we've ever done
211     if ($_[1] > $epoll_wait_size) {
212         $epoll_wait_size = $_[1];
213         $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
214     }
215     my $ct;
216     if ($no_deprecated) {
217         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
218     } else {
219         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
220     }
221     for (0..$ct-1) {
222         # 16 byte epoll_event structs, with format:
223         #    4 byte mask [idx 1]
224         #    4 byte padding (we put it into idx 2, useless)
225         #    8 byte data (first 4 bytes are fd, into idx 0)
226         @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
227     }
228     return $ct;
229 }
230
231 1;
232
233 =head1 WARRANTY
234
235 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
236
237 =head1 AUTHORS
238
239 Brad Fitzpatrick <brad@danga.com>