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.
5 # This license differs from the rest of public-inbox
7 # This module is Copyright (c) 2005 Six Apart, Ltd.
8 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
10 # All rights reserved.
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;
16 use POSIX qw(ENOSYS SEEK_CUR);
20 use vars qw(@ISA @EXPORT_OK %EXPORT_TAGS $VERSION);
24 @EXPORT_OK = qw(sendfile 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
30 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
31 EPOLLONESHOT EPOLLEXCLUSIVE)],
32 sendfile => [qw(sendfile)],
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;
47 our $loaded_syscall = 0;
50 # props to Gaal for this!
51 return if $loaded_syscall++;
53 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
54 _h2ph_pre.ph sys/syscall.ph>};
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
70 our $no_deprecated = 0;
73 my $machine = (POSIX::uname())[-1];
74 # whether the machine requires 64-bit numbers to be on 8-byte
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) {
84 # Similarly for mips64 vs mips
85 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
89 if ($machine =~ m/^i[3456]86$/) {
90 $SYS_epoll_create = 254;
92 $SYS_epoll_wait = 256;
93 $SYS_sendfile = 187; # or 64: 239
94 } elsif ($machine eq "x86_64") {
95 $SYS_epoll_create = 213;
97 $SYS_epoll_wait = 232;
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
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)
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
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
123 } elsif ($machine eq "ia64") {
124 $SYS_epoll_create = 1243;
125 $SYS_epoll_ctl = 1244;
126 $SYS_epoll_wait = 1245;
127 $SYS_sendfile = 1187;
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;
136 } elsif ($machine eq "aarch64") {
137 $SYS_epoll_create = 20; # (sys_epoll_create1)
139 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
140 $SYS_sendfile = 71; # (sys_sendfile64)
143 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
145 $SYS_epoll_create = 250;
146 $SYS_epoll_ctl = 251;
147 $SYS_epoll_wait = 252;
150 } elsif ($machine =~ m/^mips64/) {
151 $SYS_sendfile = 5039;
152 $SYS_epoll_create = 5207;
153 $SYS_epoll_ctl = 5208;
154 $SYS_epoll_wait = 5209;
156 } elsif ($machine =~ m/^mips/) {
157 $SYS_sendfile = 4207;
158 $SYS_epoll_create = 4248;
159 $SYS_epoll_ctl = 4249;
160 $SYS_epoll_wait = 4250;
163 # as a last resort, try using the *.ph files which may not
164 # exist or may be wrong
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;
172 *epoll_wait = \&epoll_wait_mod8;
173 *epoll_ctl = \&epoll_ctl_mod8;
175 *epoll_wait = \&epoll_wait_mod4;
176 *epoll_ctl = \&epoll_ctl_mod4;
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
187 ############################################################################
189 ############################################################################
191 unless ($SYS_sendfile) {
193 $SYS_sendfile = eval { &SYS_sendfile; } || 0;
196 sub sendfile_defined { return $SYS_sendfile ? 1 : 0; }
198 if ($^O eq "linux" && $SYS_sendfile) {
199 *sendfile = \&sendfile_linux;
200 } elsif ($^O eq "freebsd" && $SYS_sendfile) {
201 *sendfile = \&sendfile_freebsd;
203 *sendfile = \&sendfile_noimpl;
206 sub sendfile_noimpl {
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
218 0, # don't keep track of offset. callers can lseek and keep track.
223 sub sendfile_freebsd {
224 my $offset = POSIX::lseek($_[1]+0, 0, SEEK_CUR) + 0;
226 my $sbytes_buf = "\0" x 8;
229 $_[1] + 0, # fd (from)
230 $_[0] + 0, # socket (to)
233 0, # struct sf_hdtr *hdtr
234 $sbytes_buf, # off_t *sbytes
236 return $rv if $rv < 0;
239 my $set = unpack("L", $sbytes_buf);
240 POSIX::lseek($_[1]+0, SEEK_CUR, $set);
245 ############################################################################
247 ############################################################################
249 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
252 syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
256 # ARGS: (epfd, op, fd, events_mask)
258 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
261 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
265 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
266 # arrayref: values modified to be [$fd, $event]
267 our $epoll_wait_events;
268 our $epoll_wait_size = 0;
269 sub epoll_wait_mod4 {
270 # resize our static buffer if requested size is bigger than we've ever done
271 if ($_[1] > $epoll_wait_size) {
272 $epoll_wait_size = $_[1];
273 $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
275 my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
277 @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
282 sub epoll_wait_mod8 {
283 # resize our static buffer if requested size is bigger than we've ever done
284 if ($_[1] > $epoll_wait_size) {
285 $epoll_wait_size = $_[1];
286 $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
289 if ($no_deprecated) {
290 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
292 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
295 # 16 byte epoll_event structs, with format:
296 # 4 byte mask [idx 1]
297 # 4 byte padding (we put it into idx 2, useless)
298 # 8 byte data (first 4 bytes are fd, into idx 0)
299 @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
308 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
312 Brad Fitzpatrick <brad@danga.com>