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();
69 our $no_deprecated = 0;
72 # whether the machine requires 64-bit numbers to be on 8-byte
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) {
82 # Similarly for mips64 vs mips
83 if ($machine eq "mips64" && $Config{ptrsize} == 4) {
87 if ($machine =~ m/^i[3456]86$/) {
88 $SYS_epoll_create = 254;
90 $SYS_epoll_wait = 256;
91 $SYS_sendfile = 187; # or 64: 239
92 } elsif ($machine eq "x86_64") {
93 $SYS_epoll_create = 213;
95 $SYS_epoll_wait = 232;
97 } elsif ($machine =~ m/^parisc/) {
98 $SYS_epoll_create = 224;
100 $SYS_epoll_wait = 226;
101 $SYS_sendfile = 122; # sys_sendfile64=209
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)
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
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
121 } elsif ($machine eq "ia64") {
122 $SYS_epoll_create = 1243;
123 $SYS_epoll_ctl = 1244;
124 $SYS_epoll_wait = 1245;
125 $SYS_sendfile = 1187;
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;
134 } elsif ($machine eq "aarch64") {
135 $SYS_epoll_create = 20; # (sys_epoll_create1)
137 $SYS_epoll_wait = 22; # (sys_epoll_pwait)
138 $SYS_sendfile = 71; # (sys_sendfile64)
141 } elsif ($machine =~ m/arm(v\d+)?.*l/) {
143 $SYS_epoll_create = 250;
144 $SYS_epoll_ctl = 251;
145 $SYS_epoll_wait = 252;
148 } elsif ($machine =~ m/^mips64/) {
149 $SYS_sendfile = 5039;
150 $SYS_epoll_create = 5207;
151 $SYS_epoll_ctl = 5208;
152 $SYS_epoll_wait = 5209;
154 } elsif ($machine =~ m/^mips/) {
155 $SYS_sendfile = 4207;
156 $SYS_epoll_create = 4248;
157 $SYS_epoll_ctl = 4249;
158 $SYS_epoll_wait = 4250;
161 # as a last resort, try using the *.ph files which may not
162 # exist or may be wrong
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;
170 *epoll_wait = \&epoll_wait_mod8;
171 *epoll_ctl = \&epoll_ctl_mod8;
173 *epoll_wait = \&epoll_wait_mod4;
174 *epoll_ctl = \&epoll_ctl_mod4;
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
185 ############################################################################
187 ############################################################################
189 unless ($SYS_sendfile) {
191 $SYS_sendfile = eval { &SYS_sendfile; } || 0;
194 sub sendfile_defined { return $SYS_sendfile ? 1 : 0; }
196 if ($^O eq "linux" && $SYS_sendfile) {
197 *sendfile = \&sendfile_linux;
198 } elsif ($^O eq "freebsd" && $SYS_sendfile) {
199 *sendfile = \&sendfile_freebsd;
201 *sendfile = \&sendfile_noimpl;
204 sub sendfile_noimpl {
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
216 0, # don't keep track of offset. callers can lseek and keep track.
221 sub sendfile_freebsd {
222 my $offset = POSIX::lseek($_[1]+0, 0, SEEK_CUR) + 0;
224 my $sbytes_buf = "\0" x 8;
227 $_[1] + 0, # fd (from)
228 $_[0] + 0, # socket (to)
231 0, # struct sf_hdtr *hdtr
232 $sbytes_buf, # off_t *sbytes
234 return $rv if $rv < 0;
237 my $set = unpack("L", $sbytes_buf);
238 POSIX::lseek($_[1]+0, SEEK_CUR, $set);
243 ############################################################################
245 ############################################################################
247 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
249 # ARGS: (size) -- but in modern Linux 2.6, the
250 # size doesn't even matter (radix tree now, not hash)
252 return -1 unless defined $SYS_epoll_create;
253 my $epfd = eval { syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0) };
259 # ARGS: (epfd, op, fd, events_mask)
261 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
264 syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
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;
278 my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
280 @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
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;
292 if ($no_deprecated) {
293 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
295 $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
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));
311 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
315 Brad Fitzpatrick <brad@danga.com>