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 EPOLLERR EPOLLHUP EPOLLRDBAND
26 EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
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
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 EPOLL_CTL_ADD => 1;
42 use constant EPOLL_CTL_DEL => 2;
43 use constant EPOLL_CTL_MOD => 3;
45 our $loaded_syscall = 0;
48 # props to Gaal for this!
49 return if $loaded_syscall++;
51 delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
52 _h2ph_pre.ph sys/syscall.ph>};
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
60 our ($sysname, $nodename, $release, $version, $machine) = POSIX::uname();
70 our $no_deprecated = 0;
73 # whether the machine requires 64-bit numbers to be on 8-byte
77 # if we're running on an x86_64 kernel, but a 32-bit process,
78 # we need to use the i386 syscall numbers.
79 if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
83 # Similarly for mips64 vs mips
84 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
88 if ($machine =~ m/^i[3456]86$/) {
89 $SYS_epoll_create = 254;
91 $SYS_epoll_wait = 256;
92 $SYS_sendfile = 187; # or 64: 239
94 } elsif ($machine eq "x86_64") {
95 $SYS_epoll_create = 213;
97 $SYS_epoll_wait = 232;
100 } elsif ($machine =~ m/^parisc/) {
101 $SYS_epoll_create = 224;
102 $SYS_epoll_ctl = 225;
103 $SYS_epoll_wait = 226;
104 $SYS_sendfile = 122; # sys_sendfile64=209
105 $SYS_readahead = 207;
107 } elsif ($machine =~ m/^ppc64/) {
108 $SYS_epoll_create = 236;
109 $SYS_epoll_ctl = 237;
110 $SYS_epoll_wait = 238;
111 $SYS_sendfile = 186; # (sys32_sendfile). sys32_sendfile64=226 (64 bit processes: sys_sendfile64=186)
112 $SYS_readahead = 191; # both 32-bit and 64-bit vesions
114 } elsif ($machine eq "ppc") {
115 $SYS_epoll_create = 236;
116 $SYS_epoll_ctl = 237;
117 $SYS_epoll_wait = 238;
118 $SYS_sendfile = 186; # sys_sendfile64=226
119 $SYS_readahead = 191;
121 } elsif ($machine =~ m/^s390/) {
122 $SYS_epoll_create = 249;
123 $SYS_epoll_ctl = 250;
124 $SYS_epoll_wait = 251;
125 $SYS_sendfile = 187; # sys_sendfile64=223
126 $SYS_readahead = 222;
128 } elsif ($machine eq "ia64") {
129 $SYS_epoll_create = 1243;
130 $SYS_epoll_ctl = 1244;
131 $SYS_epoll_wait = 1245;
132 $SYS_sendfile = 1187;
133 $SYS_readahead = 1216;
135 } elsif ($machine eq "alpha") {
136 # natural alignment, ints are 32-bits
137 $SYS_sendfile = 370; # (sys_sendfile64)
138 $SYS_epoll_create = 407;
139 $SYS_epoll_ctl = 408;
140 $SYS_epoll_wait = 409;
141 $SYS_readahead = 379;
143 } elsif ($machine eq "aarch64") {
144 $SYS_epoll_create = 20; # (sys_epoll_create1)
146 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
147 $SYS_sendfile = 71; # (sys_sendfile64)
148 $SYS_readahead = 213;
151 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
153 $SYS_epoll_create = 250;
154 $SYS_epoll_ctl = 251;
155 $SYS_epoll_wait = 252;
157 $SYS_readahead = 225;
159 } elsif ($machine =~ m/^mips64/) {
160 $SYS_sendfile = 5039;
161 $SYS_epoll_create = 5207;
162 $SYS_epoll_ctl = 5208;
163 $SYS_epoll_wait = 5209;
164 $SYS_readahead = 5179;
166 } elsif ($machine =~ m/^mips/) {
167 $SYS_sendfile = 4207;
168 $SYS_epoll_create = 4248;
169 $SYS_epoll_ctl = 4249;
170 $SYS_epoll_wait = 4250;
171 $SYS_readahead = 4223;
174 # as a last resort, try using the *.ph files which may not
175 # exist or may be wrong
177 $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
178 $SYS_epoll_ctl = eval { &SYS_epoll_ctl; } || 0;
179 $SYS_epoll_wait = eval { &SYS_epoll_wait; } || 0;
180 $SYS_readahead = eval { &SYS_readahead; } || 0;
184 *epoll_wait = \&epoll_wait_mod8;
185 *epoll_ctl = \&epoll_ctl_mod8;
187 *epoll_wait = \&epoll_wait_mod4;
188 *epoll_ctl = \&epoll_ctl_mod4;
192 elsif ($^O eq "freebsd") {
193 if ($ENV{FREEBSD_SENDFILE}) {
194 # this is still buggy and in development
195 $SYS_sendfile = 393; # old is 336
199 ############################################################################
201 ############################################################################
203 unless ($SYS_sendfile) {
205 $SYS_sendfile = eval { &SYS_sendfile; } || 0;
208 sub sendfile_defined { return $SYS_sendfile ? 1 : 0; }
210 if ($^O eq "linux" && $SYS_sendfile) {
211 *sendfile = \&sendfile_linux;
212 } elsif ($^O eq "freebsd" && $SYS_sendfile) {
213 *sendfile = \&sendfile_freebsd;
215 *sendfile = \&sendfile_noimpl;
218 sub sendfile_noimpl {
223 # C: ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count)
224 # Perl: sendfile($write_fd, $read_fd, $max_count) --> $actually_sent
230 0, # don't keep track of offset. callers can lseek and keep track.
235 sub sendfile_freebsd {
236 my $offset = POSIX::lseek($_[1]+0, 0, SEEK_CUR) + 0;
238 my $sbytes_buf = "\0" x 8;
241 $_[1] + 0, # fd (from)
242 $_[0] + 0, # socket (to)
245 0, # struct sf_hdtr *hdtr
246 $sbytes_buf, # off_t *sbytes
248 return $rv if $rv < 0;
251 my $set = unpack("L", $sbytes_buf);
252 POSIX::lseek($_[1]+0, SEEK_CUR, $set);
257 ############################################################################
259 ############################################################################
261 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
263 # ARGS: (size) -- but in modern Linux 2.6, the
264 # size doesn't even matter (radix tree now, not hash)
266 return -1 unless defined $SYS_epoll_create;
267 my $epfd = eval { syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0) };
273 # ARGS: (epfd, op, fd, events_mask)
275 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
278 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
282 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
283 # arrayref: values modified to be [$fd, $event]
284 our $epoll_wait_events;
285 our $epoll_wait_size = 0;
286 sub epoll_wait_mod4 {
287 # resize our static buffer if requested size is bigger than we've ever done
288 if ($_[1] > $epoll_wait_size) {
289 $epoll_wait_size = $_[1];
290 $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
292 my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
294 @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
299 sub epoll_wait_mod8 {
300 # resize our static buffer if requested size is bigger than we've ever done
301 if ($_[1] > $epoll_wait_size) {
302 $epoll_wait_size = $_[1];
303 $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
306 if ($no_deprecated) {
307 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
309 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
312 # 16 byte epoll_event structs, with format:
313 # 4 byte mask [idx 1]
314 # 4 byte padding (we put it into idx 2, useless)
315 # 8 byte data (first 4 bytes are fd, into idx 0)
316 @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
325 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
329 Brad Fitzpatrick <brad@danga.com>