]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Syscall.pm
httpd|nntpd: avoid missed signal wakeups
[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                   signalfd SFD_NONBLOCK);
29 %EXPORT_TAGS = (epoll => [qw(epoll_ctl epoll_create epoll_wait
30                              EPOLLIN EPOLLOUT
31                              EPOLL_CTL_ADD EPOLL_CTL_DEL EPOLL_CTL_MOD
32                              EPOLLONESHOT EPOLLEXCLUSIVE)],
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 use constant {
47         SFD_CLOEXEC => 02000000,
48         SFD_NONBLOCK => 00004000,
49 };
50
51
52 our $loaded_syscall = 0;
53
54 sub _load_syscall {
55     # props to Gaal for this!
56     return if $loaded_syscall++;
57     my $clean = sub {
58         delete @INC{qw<syscall.ph asm/unistd.ph bits/syscall.ph
59                         _h2ph_pre.ph sys/syscall.ph>};
60     };
61     $clean->(); # don't trust modules before us
62     my $rv = eval { require 'syscall.ph'; 1 } || eval { require 'sys/syscall.ph'; 1 };
63     $clean->(); # don't require modules after us trust us
64     return $rv;
65 }
66
67
68 our (
69      $SYS_epoll_create,
70      $SYS_epoll_ctl,
71      $SYS_epoll_wait,
72      $SYS_signalfd4,
73      );
74
75 our $no_deprecated = 0;
76
77 if ($^O eq "linux") {
78     my $machine = (POSIX::uname())[-1];
79     # whether the machine requires 64-bit numbers to be on 8-byte
80     # boundaries.
81     my $u64_mod_8 = 0;
82
83     # if we're running on an x86_64 kernel, but a 32-bit process,
84     # we need to use the i386 syscall numbers.
85     if ($machine eq "x86_64" && $Config{ptrsize} == 4) {
86         $machine = "i386";
87     }
88
89     # Similarly for mips64 vs mips
90     if ($machine eq "mips64" && $Config{ptrsize} == 4) {
91         $machine = "mips";
92     }
93
94     if ($machine =~ m/^i[3456]86$/) {
95         $SYS_epoll_create = 254;
96         $SYS_epoll_ctl    = 255;
97         $SYS_epoll_wait   = 256;
98         $SYS_signalfd4 = 327;
99     } elsif ($machine eq "x86_64") {
100         $SYS_epoll_create = 213;
101         $SYS_epoll_ctl    = 233;
102         $SYS_epoll_wait   = 232;
103         $SYS_signalfd4 = 289;
104     } elsif ($machine =~ m/^parisc/) {
105         $SYS_epoll_create = 224;
106         $SYS_epoll_ctl    = 225;
107         $SYS_epoll_wait   = 226;
108         $u64_mod_8        = 1;
109         $SYS_signalfd4 = 309;
110     } elsif ($machine =~ m/^ppc64/) {
111         $SYS_epoll_create = 236;
112         $SYS_epoll_ctl    = 237;
113         $SYS_epoll_wait   = 238;
114         $u64_mod_8        = 1;
115         $SYS_signalfd4 = 313;
116     } elsif ($machine eq "ppc") {
117         $SYS_epoll_create = 236;
118         $SYS_epoll_ctl    = 237;
119         $SYS_epoll_wait   = 238;
120         $u64_mod_8        = 1;
121         $SYS_signalfd4 = 313;
122     } elsif ($machine =~ m/^s390/) {
123         $SYS_epoll_create = 249;
124         $SYS_epoll_ctl    = 250;
125         $SYS_epoll_wait   = 251;
126         $u64_mod_8        = 1;
127         $SYS_signalfd4 = 322;
128     } elsif ($machine eq "ia64") {
129         $SYS_epoll_create = 1243;
130         $SYS_epoll_ctl    = 1244;
131         $SYS_epoll_wait   = 1245;
132         $u64_mod_8        = 1;
133         $SYS_signalfd4 = 289;
134     } elsif ($machine eq "alpha") {
135         # natural alignment, ints are 32-bits
136         $SYS_epoll_create = 407;
137         $SYS_epoll_ctl    = 408;
138         $SYS_epoll_wait   = 409;
139         $u64_mod_8        = 1;
140         $SYS_signalfd4 = 484;
141     } elsif ($machine eq "aarch64") {
142         $SYS_epoll_create = 20;  # (sys_epoll_create1)
143         $SYS_epoll_ctl    = 21;
144         $SYS_epoll_wait   = 22;  # (sys_epoll_pwait)
145         $u64_mod_8        = 1;
146         $no_deprecated    = 1;
147         $SYS_signalfd4 = 74;
148     } elsif ($machine =~ m/arm(v\d+)?.*l/) {
149         # ARM OABI
150         $SYS_epoll_create = 250;
151         $SYS_epoll_ctl    = 251;
152         $SYS_epoll_wait   = 252;
153         $u64_mod_8        = 1;
154         $SYS_signalfd4 = 355;
155     } elsif ($machine =~ m/^mips64/) {
156         $SYS_epoll_create = 5207;
157         $SYS_epoll_ctl    = 5208;
158         $SYS_epoll_wait   = 5209;
159         $u64_mod_8        = 1;
160         $SYS_signalfd4 = 5283;
161     } elsif ($machine =~ m/^mips/) {
162         $SYS_epoll_create = 4248;
163         $SYS_epoll_ctl    = 4249;
164         $SYS_epoll_wait   = 4250;
165         $u64_mod_8        = 1;
166         $SYS_signalfd4 = 4324;
167     } else {
168         # as a last resort, try using the *.ph files which may not
169         # exist or may be wrong
170         _load_syscall();
171         $SYS_epoll_create = eval { &SYS_epoll_create; } || 0;
172         $SYS_epoll_ctl    = eval { &SYS_epoll_ctl;    } || 0;
173         $SYS_epoll_wait   = eval { &SYS_epoll_wait;   } || 0;
174
175         # Note: do NOT add new syscalls to depend on *.ph, here.
176         # Better to miss syscalls (so we can fallback to IO::Poll)
177         # than to use wrong ones, since the names are not stable
178         # (at least not on FreeBSD), if the actual numbers are.
179     }
180
181     if ($u64_mod_8) {
182         *epoll_wait = \&epoll_wait_mod8;
183         *epoll_ctl = \&epoll_ctl_mod8;
184     } else {
185         *epoll_wait = \&epoll_wait_mod4;
186         *epoll_ctl = \&epoll_ctl_mod4;
187     }
188 }
189
190 elsif ($^O eq "freebsd") {
191     if ($ENV{FREEBSD_SENDFILE}) {
192         # this is still buggy and in development
193     }
194 }
195
196 ############################################################################
197 # epoll functions
198 ############################################################################
199
200 sub epoll_defined { return $SYS_epoll_create ? 1 : 0; }
201
202 sub epoll_create {
203         syscall($SYS_epoll_create, $no_deprecated ? 0 : ($_[0]||100)+0);
204 }
205
206 # epoll_ctl wrapper
207 # ARGS: (epfd, op, fd, events_mask)
208 sub epoll_ctl_mod4 {
209     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLL", $_[3], $_[2], 0));
210 }
211 sub epoll_ctl_mod8 {
212     syscall($SYS_epoll_ctl, $_[0]+0, $_[1]+0, $_[2]+0, pack("LLLL", $_[3], 0, $_[2], 0));
213 }
214
215 # epoll_wait wrapper
216 # ARGS: (epfd, maxevents, timeout (milliseconds), arrayref)
217 #  arrayref: values modified to be [$fd, $event]
218 our $epoll_wait_events;
219 our $epoll_wait_size = 0;
220 sub epoll_wait_mod4 {
221     # resize our static buffer if requested size is bigger than we've ever done
222     if ($_[1] > $epoll_wait_size) {
223         $epoll_wait_size = $_[1];
224         $epoll_wait_events = "\0" x 12 x $epoll_wait_size;
225     }
226     my $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
227     for (0..$ct-1) {
228         @{$_[3]->[$_]}[1,0] = unpack("LL", substr($epoll_wait_events, 12*$_, 8));
229     }
230     return $ct;
231 }
232
233 sub epoll_wait_mod8 {
234     # resize our static buffer if requested size is bigger than we've ever done
235     if ($_[1] > $epoll_wait_size) {
236         $epoll_wait_size = $_[1];
237         $epoll_wait_events = "\0" x 16 x $epoll_wait_size;
238     }
239     my $ct;
240     if ($no_deprecated) {
241         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0, undef);
242     } else {
243         $ct = syscall($SYS_epoll_wait, $_[0]+0, $epoll_wait_events, $_[1]+0, $_[2]+0);
244     }
245     for (0..$ct-1) {
246         # 16 byte epoll_event structs, with format:
247         #    4 byte mask [idx 1]
248         #    4 byte padding (we put it into idx 2, useless)
249         #    8 byte data (first 4 bytes are fd, into idx 0)
250         @{$_[3]->[$_]}[1,2,0] = unpack("LLL", substr($epoll_wait_events, 16*$_, 12));
251     }
252     return $ct;
253 }
254
255 sub signalfd ($$$) {
256         my ($fd, $signos, $flags) = @_;
257         if ($SYS_signalfd4) {
258                 # Not sure if there's a way to get pack/unpack to get the
259                 # contents of POSIX::SigSet to a buffer, but prepping the
260                 # bitmap like one would for select() works:
261                 my $buf = "\0" x 8;
262                 vec($buf, $_ - 1, 1) = 1 for @$signos;
263
264                 syscall($SYS_signalfd4, $fd, $buf, 8, $flags|SFD_CLOEXEC);
265         } else {
266                 $! = ENOSYS;
267                 undef;
268         }
269 }
270
271 1;
272
273 =head1 WARRANTY
274
275 This is free software. IT COMES WITHOUT WARRANTY OF ANY KIND.
276
277 =head1 AUTHORS
278
279 Brad Fitzpatrick <brad@danga.com>