blib/lib/IO/Async/OS.pm

filter: Statementstmt 96.2% 231 / 240 Branchbran 69.4% 75 / 108 Conditioncond 69.3% 43 / 62 MC/DCmcdc 38.2% 13 / 34 Subroutinesub 97.6% 42 / 43 Podpod n/a totaltotal 82.9% 404 / 487 CC 85 SCAR 19.9
CC
85
Cov
85%
socketpair
21.0
extract_addrinfo
11.0
socket
10.5
CRAP 7.3
« blib/lib/IO/Async/Timer/Periodic.pm ↑ Summary blib/lib/IO/Async/FileStream.pm »
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2012-2024 -- leonerd@leonerd.org.uk
5  
6 package IO::Async::OS 0.805;
7  
8 103 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 103 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 103 our @ISA = qw( IO::Async::OS::_Base );
12  
13 103 ▶if( eval { require "IO/Async/OS/$^O.pm" } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { require "IO/Async/OS/$^O.pm" } }) 103 0 103
14 103 @ISA = "IO::Async::OS::$^O";
15 }
16  
17 package # hide from CPAN
18 IO::Async::OS::_Base;
19  
20 103 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 103 use Socket 1.95 qw(
23 AF_INET AF_INET6 AF_UNIX INADDR_LOOPBACK SOCK_DGRAM SOCK_RAW SOCK_STREAM
24 pack_sockaddr_in inet_aton
25 pack_sockaddr_in6 inet_pton
26 pack_sockaddr_un
27 103 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 103 ▶use POSIX qw( sysconf _SC_OPEN_MAX );
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 # Win32 [and maybe other places] don't have an _SC_OPEN_MAX. About the best we
32 # can do really is just make up some largeish number and hope for the best.
33 103 ▶use constant OPEN_MAX_FD => eval { sysconf(_SC_OPEN_MAX) } || 1024;
Subroutine BEGIN: called CC=2 SCAR=7.0
Condition50%1/2
eval { do { sysconf(4) } } || 1024
l!l
1030
MC/DC0%0/1
eval { do { sysconf(4) } } || 1024
eval { do { sysconf(4) } }
Truth table50%1/2
A
A= eval { do { sysconf(4) } }
Aresult
11
00
34  
35 # Some constants that define features of the OS
36  
37 103 ▶use constant HAVE_SOCKADDR_IN6 => defined eval { pack_sockaddr_in6 0, inet_pton( AF_INET6, "2001::1" ) };
Subroutine BEGIN: called CC=1 SCAR=0.0
38 103 ▶use constant HAVE_SOCKADDR_UN => defined eval { pack_sockaddr_un "/foo" };
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 # Do we have to fake S_ISREG() files read/write-ready in select()?
41 103 ▶use constant HAVE_FAKE_ISREG_READY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # Do we have to select() for for evec to get connect() failures
44 103 ▶use constant HAVE_SELECT_CONNECT_EVEC => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 # Ditto; do we have to poll() for POLLPRI to get connect() failures
46 103 ▶use constant HAVE_POLL_CONNECT_POLLPRI => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # Does connect() yield EWOULDBLOCK for nonblocking in progress?
49 103 ▶use constant HAVE_CONNECT_EWOULDBLOCK => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51 # Can we rename() files that are open?
52 103 ▶use constant HAVE_RENAME_OPEN_FILES => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 # Can we reliably watch for POSIX signals, including SIGCHLD to reliably
55 # inform us that a fork()ed child has exit()ed?
56 103 ▶use constant HAVE_SIGNALS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # Do we support POSIX-style true fork()ed processes at all?
59 103 ▶use constant HAVE_POSIX_FORK => !$ENV{IO_ASYNC_NO_FORK};
Subroutine BEGIN: called CC=1 SCAR=0.0
60 # Can we potentially support threads? (would still need to 'require threads')
61 use constant HAVE_THREADS => !$ENV{IO_ASYNC_NO_THREADS} &&
62 103 ▶ eval { require Config && $Config::Config{useithreads} };
Subroutine BEGIN: called CC=3 SCAR=12.0
Branch: 1 branch
BranchTrueFalseTotal
require Config and $Config::Config{"useithreads"} 103 0 103
Condition33%1/3
!$ENV{'IO_ASYNC_NO_THREADS'} && eval { do { require Config and $Config::Config{'useithreads'} } }
!ll&&!rl&&r
01030
MC/DC0%0/2
!$ENV{'IO_ASYNC_NO_THREADS'} && eval { do { require Config and $Config::Config{'useithreads'} } }
!$ENV{'IO_ASYNC_NO_THREADS'} eval { do { require Config and $Config::Config{'useithreads'} } }
Truth table33%1/3
not(A) && B
A= !$ENV{'IO_ASYNC_NO_THREADS'}B= eval { do { require Config and $Config::Config{'useithreads'} } }
ABresult
0X0
100
111
63  
64 # Preferred trial order for built-in Loop classes
65 103 ▶use constant LOOP_BUILTIN_CLASSES => qw( Poll Select );
Subroutine BEGIN: called CC=1 SCAR=0.0
66  
67 # Should there be any other Loop classes we try before the builtin ones?
68 103 ▶use constant LOOP_PREFER_CLASSES => ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69  
70 =head1 NAME
71  
72 C<IO::Async::OS> - operating system abstractions for C<IO::Async>
73  
74 =head1 DESCRIPTION
75  
76 =for highlighter language=perl
77  
78 This module acts as a class to provide a number of utility methods whose exact
79 behaviour may depend on the type of OS it is running on. It is provided as a
80 class so that specific kinds of operating system can override methods in it.
81  
82 As well as these support functions it also provides a number of constants, all
83 with names beginning C<HAVE_> which describe various features that may or may
84 not be available on the OS or perl build. Most of these are either hard-coded
85 per OS, or detected at runtime.
86  
87 The following constants may be overridden by environment variables.
88  
89 =over 4
90  
91 =item * HAVE_POSIX_FORK
92  
93 True if the C<fork()> call has full POSIX semantics (full process separation).
94 This is true on most OSes but false on MSWin32.
95  
96 This may be overridden to be false by setting the environment variable
97 C<IO_ASYNC_NO_FORK>.
98  
99 =item * HAVE_THREADS
100  
101 True if C<ithreads> are available, meaning that the C<threads> module can be
102 used. This depends on whether perl was built with threading support.
103  
104 This may be overridable to be false by setting the environment variable
105 C<IO_ASYNC_NO_THREADS>.
106  
107 =back
108  
109 =cut
110  
111 =head2 getfamilybyname
112  
113 $family = IO::Async::OS->getfamilybyname( $name )
114  
115 Return a protocol family value based on the given name. If C<$name> looks like
116 a number it will be returned as-is. The string values C<inet>, C<inet6> and
117 C<unix> will be converted to the appropriate C<AF_*> constant.
118  
119 =cut
120  
121 sub getfamilybyname
122 {
123 156 ▶ shift;
Subroutine getfamilybyname: called CC=7 SCAR=19.7
124 156 my ( $name ) = @_;
125  
126 156 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 54 102 156
127  
128 102 ▶ return $name if $name =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\d+$/u 54 48 102
129  
130 48 ▶ return AF_INET if $name eq "inet";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "inet" 41 7 48
131 7 ▶ return AF_INET6() if $name eq "inet6" and defined &AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "inet6" and defined &IO::Async::OS::_Base::AF_INET6 2 5 7
Condition66%2/3
$name eq "inet6" and defined &IO::Async::OS::_Base::AF_INET6
!ll&&!rl&&r
502
MC/DC50%1/2
$name eq "inet6" and defined &IO::Async::OS::_Base::AF_INET6
$name eq "inet6" defined &IO::Async::OS::_Base::AF_INET6
Truth table66%2/3
A and B
A= $name eq "inet6"B= defined &IO::Async::OS::_Base::AF_INET6
ABresult
0X0
100
111
132 5 ▶ return AF_UNIX if $name eq "unix";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "unix" 5 0 5
133  
134 0 croak "Unrecognised socket family name '$name'";
135 }
136  
137 =head2 getsocktypebyname
138  
139 $socktype = IO::Async::OS->getsocktypebyname( $name );
140  
141 Return a socket type value based on the given name. If C<$name> looks like a
142 number it will be returned as-is. The string values C<stream>, C<dgram> and
143 C<raw> will be converted to the appropriate C<SOCK_*> constant.
144  
145 =cut
146  
147 sub getsocktypebyname
148 {
149 167 ▶ shift;
Subroutine getsocktypebyname: called CC=6 SCAR=19.1
150 167 my ( $name ) = @_;
151  
152 167 ▶ return undef unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 53 114 167
153  
154 114 ▶ return $name if $name =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /^\d+$/u 62 52 114
155  
156 52 ▶ return SOCK_STREAM if $name eq "stream";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "stream" 34 18 52
157 18 ▶ return SOCK_DGRAM if $name eq "dgram";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "dgram" 18 0 18
158 0 ▶ return SOCK_RAW if $name eq "raw";
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "raw" 0 0 0
159  
160 0 croak "Unrecognised socktype name '$name'";
161 }
162  
163 # This one isn't documented because it's not really overridable. It's largely
164 # here just for completeness
165 103 my $HAVE_IO_SOCKET_IP;
166  
167 sub socket
168 {
169 44 ▶ my $self = shift;
Subroutine socket: called CC=10 SCAR=23.5
170 44 my ( $family, $socktype, $proto ) = @_;
171  
172 44 require IO::Socket;
173 defined $HAVE_IO_SOCKET_IP or
174 44 ▶ $HAVE_IO_SOCKET_IP = defined eval { require IO::Socket::IP };
Branch: 1 branch
BranchTrueFalseTotal
defined $HAVE_IO_SOCKET_IP or $HAVE_IO_SOCKET_IP = defined eval { do { require IO::Socket::IP } } 12 32 44
175  
176 44 ▶ croak "Cannot create a new socket without a family" unless $family;
Branch: 1 branch
BranchTrueFalseTotal
unless $family 0 44 44
177 # PF_UNSPEC and undef are both false
178 44 ▶ $family = $self->getfamilybyname( $family ) || AF_UNIX;
Condition50%1/2
$self->getfamilybyname($family) || 1
l!l
440
MC/DC0%0/1
$self->getfamilybyname($family) || 1
$self->getfamilybyname($family)
Truth table50%1/2
A
A= $self->getfamilybyname($family)
Aresult
11
00
179  
180 # SOCK_STREAM is the most likely
181 44 ▶ $socktype = $self->getsocktypebyname( $socktype ) || SOCK_STREAM;
Condition50%1/2
$self->getsocktypebyname($socktype) || 1
l!l
440
MC/DC0%0/1
$self->getsocktypebyname($socktype) || 1
$self->getsocktypebyname($socktype)
Truth table50%1/2
A
A= $self->getsocktypebyname($socktype)
Aresult
11
00
182  
183 44 ▶ $proto //= 0;
Condition100%2/2
$proto //= 0
l!l
2024
MC/DC100%1/1
$proto //= 0
$proto
Truth table100%2/2
A
A= $proto
Aresult
11
00
184  
185 44 ▶ if( $HAVE_IO_SOCKET_IP and ( $family == AF_INET || $family == AF_INET6() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAVE_IO_SOCKET_IP and $family == 2 || $family == 10) 42 2 44
Condition83%5/6
$HAVE_IO_SOCKET_IP and $family == 2 || $family == 10
!ll&&!rl&&r
0422
$family == 2 or $family == 10
l!l&&r!l&&!r
4112
MC/DC66%2/3
$HAVE_IO_SOCKET_IP and $family == 2 || $family == 10
$HAVE_IO_SOCKET_IP $family == 2 $family == 10
Truth table75%3/4
A and B or C
A= $HAVE_IO_SOCKET_IPB= $family == 2C= $family == 10
ABCresult
0XX0
1000
11X1
1011
186 42 return IO::Socket::IP->new->socket( $family, $socktype, $proto );
187 }
188  
189 2 my $sock = eval {
190 2 IO::Socket->new(
191 Domain => $family,
192 Type => $socktype,
193 Proto => $proto,
194 );
195 };
196 2 ▶ return $sock if $sock;
Branch: 1 branch
BranchTrueFalseTotal
if $sock 2 0 2
197  
198 # That failed. Most likely because the Domain was unrecognised. This
199 # usually happens if getaddrinfo returns an AF_INET6 address but we don't
200 # have a suitable class loaded. In this case we'll return a generic one.
201 # It won't be in the specific subclass but that's the best we can do. And
202 # it will still work as a generic socket.
203 0 return IO::Socket->new->socket( $family, $socktype, $proto );
204 }
205  
206 =head2 socketpair
207  
208 ( $S1, $S2 ) = IO::Async::OS->socketpair( $family, $socktype, $proto );
209  
210 An abstraction of the C<socketpair(2)> syscall, where any argument may be
211 missing (or given as C<undef>).
212  
213 If C<$family> is not provided, a suitable value will be provided by the OS
214 (likely C<AF_UNIX> on POSIX-based platforms). If C<$socktype> is not provided,
215 then C<SOCK_STREAM> will be used.
216  
217 Additionally, this method supports building connected C<SOCK_STREAM> or
218 C<SOCK_DGRAM> pairs in the C<AF_INET> family even if the underlying platform's
219 C<socketpair(2)> does not, by connecting two normal sockets together.
220  
221 C<$family> and C<$socktype> may also be given symbolically as defined by
222 C<getfamilybyname> and C<getsocktypebyname>.
223  
224 =cut
225  
226 sub socketpair
227 {
228 51 ▶ my $self = shift;
Subroutine socketpair: called CC=17 SCAR=30.5
229 51 my ( $family, $socktype, $proto ) = @_;
230  
231 51 require IO::Socket;
232  
233 # PF_UNSPEC and undef are both false
234 51 ▶ $family = $self->getfamilybyname( $family ) || AF_UNIX;
Condition100%2/2
$self->getfamilybyname($family) || 1
l!l
1140
MC/DC100%1/1
$self->getfamilybyname($family) || 1
$self->getfamilybyname($family)
Truth table100%2/2
A
A= $self->getfamilybyname($family)
Aresult
11
00
235  
236 # SOCK_STREAM is the most likely
237 51 ▶ $socktype = $self->getsocktypebyname( $socktype ) || SOCK_STREAM;
Condition100%2/2
$self->getsocktypebyname($socktype) || 1
l!l
1239
MC/DC100%1/1
$self->getsocktypebyname($socktype) || 1
$self->getsocktypebyname($socktype)
Truth table100%2/2
A
A= $self->getsocktypebyname($socktype)
Aresult
11
00
238  
239 51 ▶ $proto ||= 0;
Condition50%1/2
$proto ||= 0
l!l
051
MC/DC0%0/1
$proto ||= 0
$proto
Truth table50%1/2
A
A= $proto
Aresult
11
00
240  
241 51 my ( $S1, $S2 ) = IO::Socket->new->socketpair( $family, $socktype, $proto );
242 51 ▶ return ( $S1, $S2 ) if defined $S1;
Branch: 1 branch
BranchTrueFalseTotal
if defined $S1 40 11 51
243  
244 11 ▶ return unless $family == AF_INET and ( $socktype == SOCK_STREAM or $socktype == SOCK_DGRAM );
Branch: 1 branch
BranchTrueFalseTotal
unless $family == 2 and $socktype == 1 || $socktype == 2 0 11 11
Condition50%3/6
$family == 2 and $socktype == 1 || $socktype == 2
!ll&&!rl&&r
0011
$socktype == 1 or $socktype == 2
l!l&&r!l&&!r
290
MC/DC0%0/3
$family == 2 and $socktype == 1 || $socktype == 2
$family == 2 $socktype == 1 $socktype == 2
Truth table25%1/4
A and B or C
A= $family == 2B= $socktype == 1C= $socktype == 2
ABCresult
0XX0
1000
11X1
1011
245  
246 # Now lets emulate an AF_INET socketpair call
247  
248 11 ▶ my $Stmp = IO::Async::OS->socket( $family, $socktype ) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $Stmp = "IO::Async::OS"->socket($family, $socktype) or return 0 11 11
249 11 ▶ $Stmp->bind( pack_sockaddr_in( 0, INADDR_LOOPBACK ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$Stmp->bind(pack_sockaddr_in(0, "\177\0\0\1")) or return 0 11 11
250  
251 11 ▶ $S1 = IO::Async::OS->socket( $family, $socktype ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$S1 = "IO::Async::OS"->socket($family, $socktype) or return 0 11 11
252  
253 11 ▶ if( $socktype == SOCK_STREAM ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($socktype == 1) { } 2 9 11
254 2 ▶ $Stmp->listen( 1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$Stmp->listen(1) or return 0 2 2
255 2 ▶ $S1->connect( getsockname $Stmp ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$S1->connect(getsockname $Stmp) or return 0 2 2
256 2 ▶ $S2 = $Stmp->accept or return;
Branch: 1 branch
BranchTrueFalseTotal
$S2 = $Stmp->accept or return 0 2 2
257  
258 # There's a bug in IO::Socket here, in that $S2 's ->socktype won't
259 # yet be set. We can apply a horribly hacky fix here
260 # defined $S2->socktype and $S2->socktype == $socktype or
261 # ${*$S2}{io_socket_type} = $socktype;
262 # But for now we'll skip the test for it instead
263 }
264 else {
265 9 $S2 = $Stmp;
266 9 ▶ $S1->connect( getsockname $S2 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$S1->connect(getsockname $S2) or return 0 9 9
267 9 ▶ $S2->connect( getsockname $S1 ) or return;
Branch: 1 branch
BranchTrueFalseTotal
$S2->connect(getsockname $S1) or return 0 9 9
268 }
269  
270 11 return ( $S1, $S2 );
271 }
272  
273 =head2 pipepair
274  
275 ( $rd, $wr ) = IO::Async::OS->pipepair;
276  
277 An abstraction of the C<pipe(2)> syscall, which returns the two new handles.
278  
279 =cut
280  
281 sub pipepair
282 {
283 794 ▶ my $self = shift;
Subroutine pipepair: called CC=2 SCAR=7.1
284  
285 794 ▶ pipe( my ( $rd, $wr ) ) or return;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $rd, my $wr or return 0 794 794
286 794 return ( $rd, $wr );
287 }
288  
289 =head2 pipequad
290  
291 ( $rdA, $wrA, $rdB, $wrB ) = IO::Async::OS->pipequad;
292  
293 This method is intended for creating two pairs of filehandles that are linked
294 together, suitable for passing as the STDIN/STDOUT pair to a child process.
295 After this function returns, C<$rdA> and C<$wrA> will be a linked pair, as
296 will C<$rdB> and C<$wrB>.
297  
298 On platforms that support C<socketpair(2)>, this implementation will be
299 preferred, in which case C<$rdA> and C<$wrB> will actually be the same
300 filehandle, as will C<$rdB> and C<$wrA>. This saves a file descriptor in the
301 parent process.
302  
303 When creating a L<IO::Async::Stream> or subclass of it, the C<read_handle>
304 and C<write_handle> parameters should always be used.
305  
306 my ( $childRd, $myWr, $myRd, $childWr ) = IO::Async::OS->pipequad;
307  
308 $loop->open_process(
309 stdin => $childRd,
310 stdout => $childWr,
311 ...
312 );
313  
314 my $str = IO::Async::Stream->new(
315 read_handle => $myRd,
316 write_handle => $myWr,
317 ...
318 );
319 $loop->add( $str );
320  
321 =cut
322  
323 sub pipequad
324 {
325 2 ▶ my $self = shift;
Subroutine pipequad: called CC=4 SCAR=21.7
326  
327 # Prefer socketpair
328 2 ▶ if( my ( $S1, $S2 ) = $self->socketpair ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $self->socketpair) 2 0 2
329 2 return ( $S1, $S2, $S2, $S1 );
330 }
331  
332 # Can't do that, fallback on pipes
333 0 ▶ my ( $rdA, $wrA ) = $self->pipepair or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->pipepair or return 0 0 0
334 0 ▶ my ( $rdB, $wrB ) = $self->pipepair or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $self->pipepair or return 0 0 0
335  
336 0 return ( $rdA, $wrA, $rdB, $wrB );
337 }
338  
339 =head2 signame2num
340  
341 $signum = IO::Async::OS->signame2num( $signame );
342  
343 This utility method converts a signal name (such as "TERM") into its system-
344 specific signal number. This may be useful to pass to C<POSIX::SigSet> or use
345 in other places which use numbers instead of symbolic names.
346  
347 =head2 signum2name
348  
349 $signame = IO::Async::OS->signum2name( $signum );
350  
351 The inverse of L<signame2num>; this method convers signal numbers into
352 readable names.
353  
354 =cut
355  
356 my %sig_name2num;
357 103 my %sig_num2name;
358  
359 sub _init_signum
360 {
361 57 ▶ my $self = shift;
Subroutine _init_signum: called CC=5 SCAR=17.0
362  
363 57 require Config;
364  
365 $Config::Config{sig_name} and $Config::Config{sig_num} or
366 57 ▶ die "No signals found";
Branch: 1 branch
BranchTrueFalseTotal
$Config::Config{"sig_name"} and $Config::Config{"sig_num"} or die "No signals found" 0 57 57
Condition33%1/3
$Config::Config{"sig_name"} and $Config::Config{"sig_num"}
!ll&&!rl&&r
0057
MC/DC0%0/2
$Config::Config{"sig_name"} and $Config::Config{"sig_num"}
$Config::Config{"sig_name"} $Config::Config{"sig_num"}
Truth table33%1/3
A and B
A= $Config::Config{"sig_name"}B= $Config::Config{"sig_num"}
ABresult
0X0
100
111
367  
368 57 my @names = split ' ', $Config::Config{sig_name};
369 57 my @nums = split ' ', $Config::Config{sig_num};
370  
371 57 @sig_name2num{ @names } = @nums;
372  
373 # Only take the first of each name, in case of aliased names
374 57 ▶ @sig_num2name{ $sig_name2num{$_} } //= $_ for @names;
Condition66%2/3
@sig_num2name{$sig_name2num{$_}} //= $_
l!l&&r!l&&!r
17137050
MC/DC0%0/2
@sig_num2name{$sig_name2num{$_}} //= $_
@sig_num2name{$sig_name2num{$_}} $_
Truth table66%2/3
A //= B
A= @sig_num2name{$sig_name2num{$_}}B= $_
ABresult
1X1
011
000
375 }
376  
377 sub signame2num
378 {
379 211 ▶ my $self = shift;
Subroutine signame2num: called CC=2 SCAR=6.9
380 211 my ( $signame ) = @_;
381  
382 211 ▶ %sig_name2num or $self->_init_signum;
Branch: 1 branch
BranchTrueFalseTotal
%sig_name2num or $self->_init_signum 57 154 211
383  
384 211 return $sig_name2num{$signame};
385 }
386  
387 sub signum2name
388 {
389 2 ▶ my $self = shift;
Subroutine signum2name: called CC=2 SCAR=7.0
390 2 my ( $signum ) = @_;
391  
392 2 ▶ %sig_num2name or $self->_init_signum;
Branch: 1 branch
BranchTrueFalseTotal
%sig_num2name or $self->_init_signum 0 2 2
393  
394 2 return $sig_num2name{$signum};
395 }
396  
397 =head2 extract_addrinfo
398  
399 ( $family, $socktype, $protocol, $addr ) = IO::Async::OS->extract_addrinfo( $ai );
400  
401 Given an ARRAY or HASH reference value containing an addrinfo, returns a
402 family, socktype and protocol argument suitable for a C<socket> call and an
403 address suitable for C<connect> or C<bind>.
404  
405 If given an ARRAY it should be in the following form:
406  
407 [ $family, $socktype, $protocol, $addr ]
408  
409 If given a HASH it should contain the following keys:
410  
411 family socktype protocol addr
412  
413 Each field in the result will be initialised to 0 (or empty string for the
414 address) if not defined in the C<$ai> value.
415  
416 The family type may also be given as a symbolic string as defined by
417 C<getfamilybyname>.
418  
419 The socktype may also be given as a symbolic string; C<stream>, C<dgram> or
420 C<raw>; this will be converted to the appropriate C<SOCK_*> constant.
421  
422 Note that the C<addr> field, if provided, must be a packed socket address,
423 such as returned by C<pack_sockaddr_in> or C<pack_sockaddr_un>.
424  
425 If the HASH form is used, rather than passing a packed socket address in the
426 C<addr> field, certain other hash keys may be used instead for convenience on
427 certain named families.
428  
429 =over 4
430  
431 =cut
432  
433 103 ▶use constant ADDRINFO_FAMILY => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
434 103 ▶use constant ADDRINFO_SOCKTYPE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
435 103 ▶use constant ADDRINFO_PROTOCOL => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
436 103 ▶use constant ADDRINFO_ADDR => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
437  
438 sub extract_addrinfo
439 {
440 55 ▶ my $self = shift;
Subroutine extract_addrinfo: called CC=11 SCAR=24.0
441 55 my ( $ai, $argname ) = @_;
442  
443 55 ▶ $argname ||= "addr";
Condition100%2/2
$argname ||= "addr"
l!l
1540
MC/DC100%1/1
$argname ||= "addr"
$argname
Truth table100%2/2
A
A= $argname
Aresult
11
00
444  
445 55 my @ai;
446  
447 55 ▶ if( ref $ai eq "ARRAY" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $ai eq 'ARRAY') { } 4 51 55
elsif (ref $ai eq 'HASH') { } 51 0 51
448 4 @ai = @$ai;
449 }
450 elsif( ref $ai eq "HASH" ) {
451 51 $ai = { %$ai }; # copy so we can delete from it
452 51 @ai = delete @{$ai}{qw( family socktype protocol addr )};
453  
454 51 ▶ if( defined $ai[ADDRINFO_FAMILY] and !defined $ai[ADDRINFO_ADDR] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ai[0] and not defined $ai[3]) 15 36 51
Condition100%3/3
defined $ai[0] and not defined $ai[3]
!ll&&!rl&&r
142215
MC/DC100%2/2
defined $ai[0] and not defined $ai[3]
defined $ai[0] not defined $ai[3]
Truth table100%3/3
A and not(B)
A= defined $ai[0]B= not defined $ai[3]
ABresult
0X0
100
111
455 15 my $family = $ai[ADDRINFO_FAMILY];
456 15 my $method = "_extract_addrinfo_$family";
457 15 ▶ my $code = $self->can( $method ) or croak "Cannot determine addr for extract_addrinfo on family='$family'";
Branch: 1 branch
BranchTrueFalseTotal
my $code = $self->can($method) or croak("Cannot determine addr for extract_addrinfo on family='${family}'") 1 14 15
458  
459 14 $ai[ADDRINFO_ADDR] = $code->( $self, $ai );
460  
461 14 ▶ keys %$ai and croak "Unrecognised '$family' addrinfo keys: " . join( ", ", keys %$ai );
Branch: 1 branch
BranchTrueFalseTotal
keys %$ai and croak("Unrecognised '${family}' addrinfo keys: " . join(", ", keys %$ai)) 1 13 14
462 }
463 }
464 else {
465 0 croak "Expected '$argname' to be an ARRAY or HASH reference";
466 }
467  
468 53 $ai[ADDRINFO_FAMILY] = $self->getfamilybyname( $ai[ADDRINFO_FAMILY] );
469 53 $ai[ADDRINFO_SOCKTYPE] = $self->getsocktypebyname( $ai[ADDRINFO_SOCKTYPE] );
470  
471 # Make sure all fields are defined
472 53 ▶ $ai[$_] ||= 0 for ADDRINFO_FAMILY, ADDRINFO_SOCKTYPE, ADDRINFO_PROTOCOL;
Condition100%2/2
$ai[$_] ||= 0
l!l
8871
MC/DC100%1/1
$ai[$_] ||= 0
$ai[$_]
Truth table100%2/2
A
A= $ai[$_]
Aresult
11
00
473 53 ▶ $ai[ADDRINFO_ADDR] = "" if !defined $ai[ADDRINFO_ADDR];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ai[3] 15 38 53
474  
475 53 return @ai;
476 }
477  
478 =item family => 'inet'
479  
480 Will pack an IP address and port number from keys called C<ip> and C<port>.
481 If C<ip> is missing it will be set to "0.0.0.0". If C<port> is missing it will
482 be set to 0.
483  
484 =cut
485  
486 sub _extract_addrinfo_inet
487 {
488 8 ▶ my $self = shift;
Subroutine _extract_addrinfo_inet: called CC=3 SCAR=11.0
489 8 my ( $ai ) = @_;
490  
491 8 ▶ my $port = delete $ai->{port} || 0;
Condition100%2/2
delete $ai->{'port'} || 0
l!l
44
MC/DC100%1/1
delete $ai->{'port'} || 0
delete $ai->{'port'}
Truth table100%2/2
A
A= delete $ai->{'port'}
Aresult
11
00
492 8 ▶ my $ip = delete $ai->{ip} || "0.0.0.0";
Condition100%2/2
delete $ai->{'ip'} || '0.0.0.0'
l!l
35
MC/DC100%1/1
delete $ai->{'ip'} || '0.0.0.0'
delete $ai->{'ip'}
Truth table100%2/2
A
A= delete $ai->{'ip'}
Aresult
11
00
493  
494 8 return pack_sockaddr_in( $port, inet_aton( $ip ) );
495 }
496  
497 =item family => 'inet6'
498  
499 Will pack an IP address and port number from keys called C<ip> and C<port>.
500 If C<ip> is missing it will be set to "::". If C<port> is missing it will be
501 set to 0. Optionally will also include values from C<scopeid> and C<flowinfo>
502 keys if provided.
503  
504 This will only work if a C<pack_sockaddr_in6> function can be found in
505 C<Socket>
506  
507 =cut
508  
509 sub _extract_addrinfo_inet6
510 {
511 1 ▶ my $self = shift;
Subroutine _extract_addrinfo_inet6: called CC=5 SCAR=16.8
512 1 my ( $ai ) = @_;
513  
514 1 ▶ my $port = delete $ai->{port} || 0;
Condition50%1/2
delete $ai->{'port'} || 0
l!l
10
MC/DC0%0/1
delete $ai->{'port'} || 0
delete $ai->{'port'}
Truth table50%1/2
A
A= delete $ai->{'port'}
Aresult
11
00
515 1 ▶ my $ip = delete $ai->{ip} || "::";
Condition50%1/2
delete $ai->{'ip'} || '::'
l!l
10
MC/DC0%0/1
delete $ai->{'ip'} || '::'
delete $ai->{'ip'}
Truth table50%1/2
A
A= delete $ai->{'ip'}
Aresult
11
00
516 1 ▶ my $scopeid = delete $ai->{scopeid} || 0;
Condition50%1/2
delete $ai->{'scopeid'} || 0
l!l
01
MC/DC0%0/1
delete $ai->{'scopeid'} || 0
delete $ai->{'scopeid'}
Truth table50%1/2
A
A= delete $ai->{'scopeid'}
Aresult
11
00
517 1 ▶ my $flowinfo = delete $ai->{flowinfo} || 0;
Condition50%1/2
delete $ai->{'flowinfo'} || 0
l!l
01
MC/DC0%0/1
delete $ai->{'flowinfo'} || 0
delete $ai->{'flowinfo'}
Truth table50%1/2
A
A= delete $ai->{'flowinfo'}
Aresult
11
00
518  
519 1 if( HAVE_SOCKADDR_IN6 ) {
520 1 return pack_sockaddr_in6( $port, inet_pton( AF_INET6, $ip ), $scopeid, $flowinfo );
521 }
522 else {
523 croak "Cannot pack_sockaddr_in6";
524 }
525 }
526  
527 =item family => 'unix'
528  
529 Will pack a UNIX socket path from a key called C<path>.
530  
531 =cut
532  
533 sub _extract_addrinfo_unix
534 {
535 5 ▶ my $self = shift;
Subroutine _extract_addrinfo_unix: called CC=2 SCAR=7.0
536 5 my ( $ai ) = @_;
537  
538 5 ▶ defined( my $path = delete $ai->{path} ) or croak "Expected 'path' for extract_addrinfo on family='unix'";
Branch: 1 branch
BranchTrueFalseTotal
defined(my $path = delete $ai->{"path"}) or croak("Expected 'path' for extract_addrinfo on family='unix'") 0 5 5
539  
540 5 return pack_sockaddr_un( $path );
541 }
542  
543 =pod
544  
545 =back
546  
547 =cut
548  
549 =head2 make_addr_for_peer
550  
551 $connectaddr = IO::Async::OS->make_addr_for_peer( $family, $listenaddr );
552  
553 Given the C<sockdomain> and C<sockname> of a listening socket. creates an
554 address suitable to C<connect()> to it.
555  
556 This method will handle specially any C<AF_INET> address bound to
557 C<INADDR_ANY> or any C<AF_INET6> address bound to C<IN6ADDR_ANY>, as some OSes
558 do not allow C<connect(2)>ing to those and would instead insist on receiving
559 C<INADDR_LOOPBACK> or C<IN6ADDR_LOOPBACK> respectively.
560  
561 This method is used by the C<< ->connect( peer => $sock ) >> parameter of
562 handle and loop connect methods.
563  
564 =cut
565  
566 sub make_addr_for_peer
567 {
568 5 ▶ shift;
Subroutine make_addr_for_peer: called CC=5 SCAR=16.1
569 5 my ( $p_family, $p_addr ) = @_;
570  
571 5 ▶ if( $p_family == Socket::AF_INET ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_family == 2) 2 3 5
572 2 my @params = Socket::unpack_sockaddr_in $p_addr;
573 2 ▶ $params[1] = Socket::INADDR_LOOPBACK if $params[1] eq Socket::INADDR_ANY;
Branch: 1 branch
BranchTrueFalseTotal
if $params[1] eq "\0\0\0\0" 1 1 2
574 2 return Socket::pack_sockaddr_in @params;
575 }
576 3 ▶ if( HAVE_SOCKADDR_IN6 and $p_family == Socket::AF_INET6 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p_family == 10) 2 1 3
577 2 my @params = Socket::unpack_sockaddr_in6 $p_addr;
578 2 ▶ $params[1] = Socket::IN6ADDR_LOOPBACK if $params[1] eq Socket::IN6ADDR_ANY;
Branch: 1 branch
BranchTrueFalseTotal
if $params[1] eq "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" 1 1 2
579 2 return Socket::pack_sockaddr_in6 @params;
580 }
581  
582 # Most other cases should be fine
583 1 return $p_addr;
584 }
585  
586 =head1 LOOP IMPLEMENTATION METHODS
587  
588 The following methods are provided on C<IO::Async::OS> because they are likely
589 to require OS-specific implementations, but are used by L<IO::Async::Loop> to
590 implement its functionality. It can use the HASH reference C<< $loop->{os} >>
591 to store other data it requires.
592  
593 =cut
594  
595 =head2 loop_watch_signal
596  
597 =head2 loop_unwatch_signal
598  
599 IO::Async::OS->loop_watch_signal( $loop, $signal, $code );
600  
601 IO::Async::OS->loop_unwatch_signal( $loop, $signal );
602  
603 Used to implement the C<watch_signal> / C<unwatch_signal> Loop pair.
604  
605 =cut
606  
607 sub _setup_sigpipe
608 {
609 58 ▶ my $self = shift;
Subroutine _setup_sigpipe: called CC=3 SCAR=11.1
610 58 my ( $loop ) = @_;
611  
612 58 require IO::Async::Handle;
613  
614 58 ▶ my ( $reader, $sigpipe ) = $self->pipepair or croak "Cannot pipe() - $!";
Branch: 1 branch
BranchTrueFalseTotal
() = $self->pipepair or croak("Cannot pipe() - $!") 0 58 58
615 58 $_->blocking( 0 ) for $reader, $sigpipe;
616  
617 58 $loop->{os}{sigpipe} = $sigpipe;
618  
619 58 my $sigwatch = $loop->{os}{sigwatch};
620  
621 $loop->add( $loop->{os}{sigpipe_reader} = IO::Async::Handle->new(
622 notifier_name => "sigpipe",
623 read_handle => $reader,
624 on_read_ready => sub {
625 318 ▶ sysread $reader, my $buffer, 8192 or return;
Subroutine __ANON__: called CC=4 SCAR=14.8
Branch: 1 branch
BranchTrueFalseTotal
sysread $reader, my $buffer, 8192 or return 0 318 318
626 318 foreach my $signum ( unpack "I*", $buffer ) {
627 326 ▶ $sigwatch->{$signum}->() if $sigwatch->{$signum};
Branch: 1 branch
BranchTrueFalseTotal
if $sigwatch->{$signum} 326 0 326
628 }
629 },
630 58 ) );
631  
632 58 return $sigpipe;
633 }
634  
635 sub loop_watch_signal
636 {
637 69 ▶ my $self = shift;
Subroutine loop_watch_signal: called CC=5 SCAR=16.1
638 69 my ( $loop, $signal, $code ) = @_;
639  
640 69 ▶ exists $SIG{$signal} or croak "Unrecognised signal name $signal";
Branch: 1 branch
BranchTrueFalseTotal
exists $SIG{$signal} or croak("Unrecognised signal name $signal") 3 66 69
641 66 ▶ ref $code or croak 'Expected $code as a reference';
Branch: 1 branch
BranchTrueFalseTotal
ref $code or croak("Expected \$code as a reference") 0 66 66
642  
643 66 my $signum = $self->signame2num( $signal );
644 66 ▶ my $sigwatch = $loop->{os}{sigwatch} ||= {}; # {$num} = $code
Condition100%2/2
$loop->{"os"}{"sigwatch"} ||= {}
l!l
1056
MC/DC100%1/1
$loop->{"os"}{"sigwatch"} ||= {}
$loop->{"os"}{"sigwatch"}
Truth table100%2/2
A
A= $loop->{"os"}{"sigwatch"}
Aresult
11
00
645  
646 66 ▶ my $sigpipe = $loop->{os}{sigpipe} // $self->_setup_sigpipe( $loop );
Condition66%2/3
$loop->{"os"}{"sigpipe"} // $self->_setup_sigpipe($loop)
l!l&&r!l&&!r
8580
MC/DC0%0/2
$loop->{"os"}{"sigpipe"} // $self->_setup_sigpipe($loop)
$loop->{"os"}{"sigpipe"} $self->_setup_sigpipe($loop)
Truth table66%2/3
A // B
A= $loop->{"os"}{"sigpipe"}B= $self->_setup_sigpipe($loop)
ABresult
1X1
011
000
647  
648 66 my $signum_str = pack "I", $signum;
649 66 ▶ $SIG{$signal} = sub { syswrite $sigpipe, $signum_str };
Subroutine __ANON__: called CC=1 SCAR=0.0
650  
651 66 $sigwatch->{$signum} = $code;
652 }
653  
654 sub loop_unwatch_signal
655 {
656 9 ▶ my $self = shift;
Subroutine loop_unwatch_signal: called CC=2 SCAR=7.0
657 9 my ( $loop, $signal ) = @_;
658  
659 9 my $signum = $self->signame2num( $signal );
660 9 ▶ my $sigwatch = $loop->{os}{sigwatch} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sigwatch = $loop->{"os"}{"sigwatch"} or return 0 9 9
661  
662 9 delete $sigwatch->{$signum};
663 9 undef $SIG{$signal};
664 }
665  
666 =head2 potentially_open_fds
667  
668 @fds = IO::Async::OS->potentially_open_fds;
669  
670 Returns a list of filedescriptors which might need closing. By default this
671 will return C<0 .. _SC_OPEN_MAX>. OS-specific subclasses may have a better
672 guess.
673  
674 =cut
675  
676 sub potentially_open_fds
677 {
678 0 ▶ return 0 .. OPEN_MAX_FD;
Subroutine potentially_open_fds: not called CC=1 SCAR=6.9
679 }
680  
681 sub post_fork
682 {
683 6 ▶ my $self = shift;
Subroutine post_fork: called CC=5 SCAR=16.1
684 6 my ( $loop ) = @_;
685  
686 6 ▶ if( $loop->{os}{sigpipe} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loop->{"os"}{"sigpipe"}) 2 4 6
687 2 $loop->remove( $loop->{os}{sigpipe_reader} );
688 2 undef $loop->{os}{sigpipe};
689  
690 2 my $sigwatch = $loop->{os}{sigwatch};
691  
692 2 foreach my $signal ( keys %SIG ) {
693 134 ▶ my $signum = $self->signame2num( $signal ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $signum = $self->signame2num($signal) or next 0 134 134
694 134 ▶ my $code = $sigwatch->{$signum} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $code = $sigwatch->{$signum} or next 132 2 134
695  
696 2 $self->loop_watch_signal( $loop, $signal, $code );
697 }
698 }
699 }
700  
701 =head1 AUTHOR
702  
703 Paul Evans <leonerd@leonerd.org.uk>
704  
705 =cut
706  
707 103 0x55AA;
« blib/lib/IO/Async/Timer/Periodic.pm ↑ Summary blib/lib/IO/Async/FileStream.pm »