| 1 | package Linux::Event::Listen; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 9 | ▶ | use v5.36; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 9 | our $VERSION = '0.016'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK F_GETFD F_SETFD FD_CLOEXEC); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 9 | use Socket qw( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | SOL_SOCKET SO_ACCEPTCONN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | AF_INET6 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | IPPROTO_IPV6 IPV6_V6ONLY | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | unpack_sockaddr_in unpack_sockaddr_in6 unpack_sockaddr_un | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | inet_ntoa inet_ntop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 9 | ▶ | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 9 | ▶ | use IO::Socket::IP (); # core | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 9 | ▶ | use IO::Socket::UNIX (); # core | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | # new(%args) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Required: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # loop => $loop | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | # on_accept => sub ($loop, $client_fh, $peer, $listen) { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | # Socket source (choose one): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | # fh => $server_fh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | # host => $host, port => $port | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Optional: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | # backlog => 4096 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # reuseaddr => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # reuseport => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | # v6only => undef (OS default; only for IPv6 sockets we create) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | # nonblocking => 1 (best-effort; forced in practice) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | # cloexec => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | # edge_triggered => 1 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | # oneshot => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | # max_accept_per_tick => 128 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | # owns_socket => (default 1 if created, else 0 if fh provided) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | # on_error => sub ($loop, $err, $listen) { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | # warn_on_unhandled_error => 0 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ▶ | sub new ($class, %args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=65 SCAR=58.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | ▶ | my $loop = delete $args{loop} // croak 'loop is required'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $args{"loop"} // croak("loop is required")
MC/DC0%0/2
delete $args{"loop"} // croak("loop is required")
delete $args{"loop"}
croak("loop is required")
Truth table33%1/3
A // B
A= delete $args{"loop"}B= croak("loop is required")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | my $on_accept = delete $args{on_accept} // croak 'on_accept is required'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
delete $args{"on_accept"} // croak("on_accept is required")
MC/DC0%0/2
delete $args{"on_accept"} // croak("on_accept is required")
delete $args{"on_accept"}
croak("on_accept is required")
Truth table33%1/3
A // B
A= delete $args{"on_accept"}B= croak("on_accept is required")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | croak 'on_accept must be a code reference' if ref($on_accept) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | my $on_error = delete $args{on_error}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 8 | ▶ | croak 'on_error must be a code reference' if defined($on_error) && ref($on_error) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $on_error and ref $on_error ne "CODE"
MC/DC0%0/2
defined $on_error and ref $on_error ne "CODE"
defined $on_error
ref $on_error ne "CODE"
Truth table66%2/3
A and B
A= defined $on_errorB= ref $on_error ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 8 | my $on_emfile = delete $args{on_emfile}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 8 | ▶ | croak 'on_emfile must be a code reference' if defined($on_emfile) && ref($on_emfile) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $on_emfile and ref $on_emfile ne "CODE"
MC/DC0%0/2
defined $on_emfile and ref $on_emfile ne "CODE"
defined $on_emfile
ref $on_emfile ne "CODE"
Truth table33%1/3
A and B
A= defined $on_emfileB= ref $on_emfile ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 8 | ▶ | my $warn_on_unhandled_error = delete($args{warn_on_unhandled_error}) // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"warn_on_unhandled_error"} // 0
MC/DC0%0/1
delete $args{"warn_on_unhandled_error"} // 0
delete $args{"warn_on_unhandled_error"}
Truth table50%1/2
A
A= delete $args{"warn_on_unhandled_error"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 8 | ▶ | my $user_edge = exists $args{edge_triggered} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 8 | ▶ | my $edge_triggered = $user_edge ? delete($args{edge_triggered}) : 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 8 | ▶ | my $oneshot = delete($args{oneshot}) // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"oneshot"} // 0
MC/DC0%0/1
delete $args{"oneshot"} // 0
delete $args{"oneshot"}
Truth table50%1/2
A
A= delete $args{"oneshot"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 8 | ▶ | my $user_cap = exists $args{max_accept_per_tick} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 8 | ▶ | my $max_accept_per_tick = $user_cap ? delete($args{max_accept_per_tick}) : 128; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | # EPOLLET (edge-triggered) requires draining accept() until EAGAIN. If the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | # caller explicitly caps accepts-per-tick but did not explicitly request edge | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # triggering, default to level-triggered so the cap cannot stall the listener. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | if ($user_cap && !$user_edge) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$user_cap and not $user_edge
MC/DC50%1/2
$user_cap and not $user_edge
$user_cap
not $user_edge
Truth table66%2/3
A and not(B)
A= $user_capB= not $user_edge
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | $edge_triggered = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | croak 'max_accept_per_tick must be a positive integer' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $max_accept_per_tick =~ /\A[0-9]+\z/u or $max_accept_per_tick < 1
MC/DC0%0/2
not $max_accept_per_tick =~ /\A[0-9]+\z/u or $max_accept_per_tick < 1
not $max_accept_per_tick =~ /\A[0-9]+\z/u
$max_accept_per_tick < 1
Truth table33%1/3
not(A) or B
A= not $max_accept_per_tick =~ /\A[0-9]+\z/uB= $max_accept_per_tick < 1
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | if $max_accept_per_tick !~ /\A[0-9]+\z/ || $max_accept_per_tick < 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | ▶ | my $cloexec = delete($args{cloexec}) // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"cloexec"} // 1
MC/DC0%0/1
delete $args{"cloexec"} // 1
delete $args{"cloexec"}
Truth table50%1/2
A
A= delete $args{"cloexec"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | my $nonblocking = delete($args{nonblocking}) // 1; # best-effort; forced on in practice | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"nonblocking"} // 1
MC/DC0%0/1
delete $args{"nonblocking"} // 1
delete $args{"nonblocking"}
Truth table50%1/2
A
A= delete $args{"nonblocking"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | my $backlog = delete($args{backlog}) // 4096; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"backlog"} // 4096
MC/DC0%0/1
delete $args{"backlog"} // 4096
delete $args{"backlog"}
Truth table50%1/2
A
A= delete $args{"backlog"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 8 | ▶ | my $reuseaddr = delete($args{reuseaddr}) // 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"reuseaddr"} // 1
MC/DC0%0/1
delete $args{"reuseaddr"} // 1
delete $args{"reuseaddr"}
Truth table50%1/2
A
A= delete $args{"reuseaddr"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 8 | ▶ | my $reuseport = delete($args{reuseport}) // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"reuseport"} // 0
MC/DC0%0/1
delete $args{"reuseport"} // 0
delete $args{"reuseport"}
Truth table50%1/2
A
A= delete $args{"reuseport"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 8 | my $v6only = delete($args{v6only}); # undef means OS default | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 8 | my $fh = delete $args{fh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 8 | my $host = delete $args{host}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 8 | my $port = delete $args{port}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 8 | my $path = delete $args{path}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 8 | ▶ | my $unlink = delete($args{unlink}) // 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
delete $args{"unlink"} // 0
MC/DC100%1/1
delete $args{"unlink"} // 0
delete $args{"unlink"}
Truth table100%2/2
A
A= delete $args{"unlink"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 8 | ▶ | my $unlink_on_cancel = exists $args{unlink_on_cancel} ? delete($args{unlink_on_cancel}) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 8 | my $owns_socket = delete($args{owns_socket}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 8 | ▶ | croak "unknown option(s): @{[ sort keys %args ]}" if %args; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 8 | ▶ | croak 'provide either fh => $server_fh OR host/port OR path' if defined($fh) && (defined($host) || defined($port) || defined($path)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition44%4/9
defined $fh and defined $host || defined $port || defined $path
defined $host or defined $port or defined $path
defined $host or defined $port
MC/DC0%0/4
defined $fh and defined $host || defined $port || defined $path
defined $fh
defined $host
defined $port
defined $path
Truth table40%2/5
A and B or C or D
A= defined $fhB= defined $hostC= defined $portD= defined $path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | croak 'host and port must be provided together' if (defined($host) xor defined($port)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
defined $host xor defined $port
MC/DC0%0/2
defined $host xor defined $port
defined $host
defined $port
Truth table50%2/4
A xor B
A= defined $hostB= defined $port
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 8 | ▶ | croak 'path cannot be combined with host/port' if defined($path) && defined($host); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and defined $host
MC/DC0%0/2
defined $path and defined $host
defined $path
defined $host
Truth table66%2/3
A and B
A= defined $pathB= defined $host
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 8 | ▶ | if (!defined $fh) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | croak 'either fh, host/port, or path is required' if !defined($host) && !defined($path); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $host or defined $path
MC/DC0%0/2
defined $host or defined $path
defined $host
defined $path
Truth table66%2/3
A or B
A= defined $hostB= defined $path
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 6 | ▶ | if (defined $path) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | if ($unlink) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | unlink($path); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | $fh = IO::Socket::UNIX->new( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | Type => Socket::SOCK_STREAM(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | Local => $path, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | Listen => $backlog, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | 5 | ▶ | $fh = IO::Socket::IP->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | LocalHost => $host, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | LocalPort => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | Listen => $backlog, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | Proto => 'tcp', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | ReuseAddr => $reuseaddr ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | ($reuseport ? (ReusePort => 1) : ()), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 6 | ▶ | if (!$fh) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | my $err = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | op => 'setup', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | errno => 0 + $!, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | error => "$!", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | fatal => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | if ($on_error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | eval { $on_error->($loop, $err, undef) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | croak "listen setup failed: $err->{error}"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Optional: v6only on sockets we created. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 6 | ▶ | if (defined $v6only) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 0 | my $ok = eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | my $af = getsockopt($fh, Socket::SOL_SOCKET(), Socket::SO_DOMAIN()); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | # Some perls/platforms don't support SO_DOMAIN; best-effort: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | eval { setsockopt($fh, IPPROTO_IPV6, IPV6_V6ONLY, pack('I', $v6only ? 1 : 0)) }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 6 | ▶ | $owns_socket = 1 if !defined $owns_socket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 6 | ▶ | $unlink_on_cancel = 1 if defined($path) && !defined($unlink_on_cancel); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $path and not defined $unlink_on_cancel
MC/DC50%1/2
defined $path and not defined $unlink_on_cancel
defined $path
not defined $unlink_on_cancel
Truth table66%2/3
A and not(B)
A= defined $pathB= not defined $unlink_on_cancel
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 2 | ▶ | $owns_socket = 0 if !defined $owns_socket; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | # If caller provided fh, validate it is in listening state. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 8 | ▶ | if (defined $fh && !defined $host) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $fh and not defined $host
MC/DC50%1/2
defined $fh and not defined $host
defined $fh
not defined $host
Truth table66%2/3
A and not(B)
A= defined $fhB= not defined $host
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 3 | my $acceptconn = getsockopt($fh, SOL_SOCKET, SO_ACCEPTCONN); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 3 | ▶ | if (!defined $acceptconn || !unpack('i', $acceptconn)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $acceptconn and unpack "i", $acceptconn
MC/DC0%0/2
defined $acceptconn and unpack "i", $acceptconn
defined $acceptconn
unpack "i", $acceptconn
Truth table33%1/3
A and B
A= defined $acceptconnB= unpack "i", $acceptconn
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | croak 'fh is not a listening socket (SO_ACCEPTCONN is false)'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | # Force nonblocking + cloexec on the listening fh | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 8 | ▶ | _set_nonblocking($fh) if $nonblocking; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 8 | ▶ | _set_cloexec($fh) if $cloexec; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 8 | ▶ | my $self = bless { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | loop => $loop, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | fh => $fh, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | owns_socket => $owns_socket ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | on_accept => $on_accept, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | on_error => $on_error, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | on_emfile => $on_emfile, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | warn_on_unhandled_error => $warn_on_unhandled_error ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | edge_triggered => $edge_triggered ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | oneshot => $oneshot ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | max_accept_per_tick => $max_accept_per_tick, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | paused => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | watcher => undef, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | _unix_path => (defined $path ? $path : undef), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | _unlink_on_cancel => ($unlink_on_cancel ? 1 : 0), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | _in_read_cb => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | _defer_close => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | _cancelled => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | cloexec => $cloexec ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | nonblocking => $nonblocking ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | # Install watcher. Keep the accept loop in the callback to avoid layers. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | ▶ | my %watch = ( | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=22 SCAR=43.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | read => sub ($loop, $listen_fh, $w) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 10 | local $self->{_in_read_cb} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 10 | my $accepted = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 10 | ▶ | while ($accepted < $self->{max_accept_per_tick}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 16 | my $client; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 16 | my $peer_packed = accept($client, $listen_fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 16 | ▶ | if (!$peer_packed) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $errno = 0 + $!; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # EAGAIN/EWOULDBLOCK end-of-drain is the normal exit condition. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | # Treat EINTR/ECONNABORTED as retry. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if ($errno == _EINTR() || $errno == _ECONNABORTED()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$errno == _EINTR() or $errno == _ECONNABORTED()
MC/DC0%0/2
$errno == _EINTR() or $errno == _ECONNABORTED()
$errno == _EINTR()
$errno == _ECONNABORTED()
Truth table0%0/3
A or B
A= $errno == _EINTR()B= $errno == _ECONNABORTED()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | ▶ | if ($errno == _EAGAIN() || $errno == _EWOULDBLOCK()) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$errno == _EAGAIN() or $errno == _EWOULDBLOCK()
MC/DC0%0/2
$errno == _EAGAIN() or $errno == _EWOULDBLOCK()
$errno == _EAGAIN()
$errno == _EWOULDBLOCK()
Truth table0%0/3
A or B
A= $errno == _EAGAIN()B= $errno == _EWOULDBLOCK()
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | my $err = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | op => 'accept', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | errno => $errno, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | error => "$!", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | fatal => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | if (($errno == _EMFILE() || $errno == _ENFILE()) && $self->{on_emfile}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition0%0/6
$errno == _EMFILE() || $errno == _ENFILE() and $self->{"on_emfile"}
$errno == _EMFILE() or $errno == _ENFILE()
MC/DC0%0/3
$errno == _EMFILE() || $errno == _ENFILE() and $self->{"on_emfile"}
$errno == _EMFILE()
$errno == _ENFILE()
$self->{"on_emfile"}
Truth table0%0/5
A or B and C
A= $errno == _EMFILE()B= $errno == _ENFILE()C= $self->{"on_emfile"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | eval { $self->{on_emfile}->($loop, $err, $self) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | } elsif ($self->{on_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | eval { $self->{on_error}->($loop, $err, $self) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | } elsif ($self->{warn_on_unhandled_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 0 | warn "Linux::Event::Listen accept error: $err->{error}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 16 | ▶ | _set_nonblocking($client) if $self->{nonblocking}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 16 | ▶ | _set_cloexec($client) if $self->{cloexec}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 16 | my $peer = _peer_from_sockaddr($peer_packed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 16 | my $ok = eval { $self->{on_accept}->($loop, $client, $peer, $self); 1 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 16 | ▶ | if (!$ok) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | my $e = $@ || 'unknown error'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$@ || 'unknown error'
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | my $err = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | op => 'on_accept', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | error => "$e", | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | fatal => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 1 | ▶ | if ($self->{on_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 1 | eval { $self->{on_error}->($loop, $err, $self) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 0 | die $e; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 16 | $accepted++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | 16 | ▶ | last if $self->{_cancelled}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 10 | ▶ | if ($self->{_defer_close} && $self->{fh}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_defer_close"} and $self->{"fh"}
MC/DC50%1/2
$self->{"_defer_close"} and $self->{"fh"}
$self->{"_defer_close"}
$self->{"fh"}
Truth table66%2/3
A and B
A= $self->{"_defer_close"}B= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | 5 | close($self->{fh}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 5 | delete $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 5 | ▶ | if ($self->{_unix_path} && $self->{_unlink_on_cancel}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
MC/DC50%1/2
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
$self->{"_unix_path"}
$self->{"_unlink_on_cancel"}
Truth table66%2/3
A and B
A= $self->{"_unix_path"}B= $self->{"_unlink_on_cancel"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | unlink($self->{_unix_path}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 5 | $self->{_defer_close} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | edge_triggered => $self->{edge_triggered}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | oneshot => $self->{oneshot}, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 8 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # If user wants error visibility, wire error callback too. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 8 | ▶ | if ($self->{on_error} || $self->{warn_on_unhandled_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"on_error"} or $self->{"warn_on_unhandled_error"}
MC/DC50%1/2
$self->{"on_error"} or $self->{"warn_on_unhandled_error"}
$self->{"on_error"}
$self->{"warn_on_unhandled_error"}
Truth table66%2/3
A or B
A= $self->{"on_error"}B= $self->{"warn_on_unhandled_error"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | ▶ | $watch{error} = sub ($loop, $listen_fh, $w) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 0 | my $err = { op => 'watch', error => 'listener socket error/hup', fatal => 0 }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | if ($self->{on_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | eval { $self->{on_error}->($loop, $err, $self) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } elsif ($self->{warn_on_unhandled_error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | warn "Linux::Event::Listen watcher error: $err->{error}\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 8 | $self->{watcher} = $loop->watch($fh, %watch); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 8 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | # Separate entry-point to keep call sites short: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | # my $listen = Linux::Event::Listen->listen($loop, host => ..., port => ..., on_accept => ...); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | ▶ | sub listen ($class, $loop, %args) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
listen: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | return $class->new(loop => $loop, %args); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | # --- Control --------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | sub watcher ($self) { return $self->{watcher} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
watcher: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 3 | ▶ | sub fh ($self) { return $self->{fh} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fh: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | ▶ | sub fd ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fd: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | ▶ | my $fh = $self->{fh} // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"fh"} // (return undef)
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 1 | return fileno($fh); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | ▶ | sub pause ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pause: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 1 | ▶ | return $self if $self->{paused}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 1 | ▶ | $self->{watcher}->disable_read if $self->{watcher}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 1 | $self->{paused} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | ▶ | sub resume ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resume: called CC=3 SCAR=11.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 1 | ▶ | return $self if !$self->{paused}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 1 | ▶ | $self->{watcher}->enable_read if $self->{watcher}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 1 | $self->{paused} = 0; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | ▶ | sub is_paused ($self) { return $self->{paused} ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_paused: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 2 | ▶ | sub is_running ($self) { return $self->{watcher} ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_running: called CC=2 SCAR=6.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 1 | ▶ | sub family ($self) { return $self->{_family} // 'unknown' } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
family: called CC=2 SCAR=7.6
Pod
undocumented
Condition50%1/2
$self->{"_family"} // "unknown"
MC/DC0%0/1
$self->{"_family"} // "unknown"
$self->{"_family"}
Truth table50%1/2
A
A= $self->{"_family"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 0 | ▶ | sub is_unix ($self) { return ($self->{_family} // '') eq 'unix' ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_unix: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
Condition0%0/2
$self->{"_family"} // ""
MC/DC0%0/1
$self->{"_family"} // ""
$self->{"_family"}
Truth table0%0/2
A
A= $self->{"_family"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | 0 | ▶ | sub is_tcp ($self) { return ($self->{_family} // '') =~ /\Ainet/ ? 1 : 0 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_tcp: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
Condition0%0/2
$self->{"_family"} // ""
MC/DC0%0/1
$self->{"_family"} // ""
$self->{"_family"}
Truth table0%0/2
A
A= $self->{"_family"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | ▶ | sub sockhost ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sockhost: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | ▶ | my $fh = $self->{fh} // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$self->{"fh"} // (return undef)
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table0%0/2
A
A= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | return eval { $fh->sockhost }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | ▶ | sub sockport ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sockport: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 2 | ▶ | my $fh = $self->{fh} // return undef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{"fh"} // (return undef)
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 2 | return eval { $fh->sockport }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | ▶ | sub cancel ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cancel: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | 17 | $self->{_cancelled} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 17 | my $w = delete $self->{watcher}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 17 | ▶ | $w->cancel if $w; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | 17 | ▶ | if ($self->{owns_socket} && $self->{fh}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"owns_socket"} and $self->{"fh"}
MC/DC100%2/2
$self->{"owns_socket"} and $self->{"fh"}
$self->{"owns_socket"}
$self->{"fh"}
Truth table100%3/3
A and B
A= $self->{"owns_socket"}B= $self->{"fh"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | 6 | ▶ | if ($self->{_in_read_cb}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 5 | $self->{_defer_close} = 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 1 | close($self->{fh}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | 1 | delete $self->{fh}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 1 | ▶ | if ($self->{_unix_path} && $self->{_unlink_on_cancel}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
MC/DC0%0/2
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
$self->{"_unix_path"}
$self->{"_unlink_on_cancel"}
Truth table33%1/3
A and B
A= $self->{"_unix_path"}B= $self->{"_unlink_on_cancel"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | unlink($self->{_unix_path}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | 17 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | ▶ | sub DESTROY ($self) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | 8 | eval { $self->cancel }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | # --- Internal utilities ---------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | ▶ | sub _set_nonblocking ($fh) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_nonblocking: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 24 | my $flags = fcntl($fh, F_GETFL, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 24 | ▶ | return if !defined $flags; # best-effort | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 24 | fcntl($fh, F_SETFL, $flags | O_NONBLOCK); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 24 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | ▶ | sub _set_cloexec ($fh) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_set_cloexec: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 24 | my $fdflags = fcntl($fh, F_GETFD, 0); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 24 | ▶ | return if !defined $fdflags; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | 24 | fcntl($fh, F_SETFD, $fdflags | FD_CLOEXEC); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 24 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | ▶ | sub _peer_from_sockaddr ($packed) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_peer_from_sockaddr: called CC=4 SCAR=14.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | 16 | my $len = length($packed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | # IPv4 sockaddr_in length is typically 16; IPv6 is typically 28. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 16 | ▶ | if ($len == 16) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 14 | my ($port, $addr) = unpack_sockaddr_in($packed); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | 14 | family => 'inet', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | host => inet_ntoa($addr), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | port => $port, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | 2 | my ($port6, $addr6, $scope, $flow) = eval { unpack_sockaddr_in6($packed) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | 2 | ▶ | if (defined $port6) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | 0 | my $host6 = inet_ntop(AF_INET6, $addr6); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | 0 | family => 'inet6', | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | host => $host6, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | port => $port6, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | scope_id => $scope, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | 2 | my $upath = eval { unpack_sockaddr_un($packed) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 2 | ▶ | if (defined $upath) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | 2 | return { family => 'unix', path => $upath }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 0 | return { family => 'unknown' }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # Errno constants without depending on POSIX::Errno exports. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 0 | ▶ | sub _EINTR () { 4 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_EINTR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | sub _EAGAIN () { 11 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_EAGAIN: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | 0 | ▶ | sub _EWOULDBLOCK () { 11 } # Linux: same as EAGAIN | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_EWOULDBLOCK: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | 0 | ▶ | sub _ECONNABORTED () { 103 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ECONNABORTED: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 0 | ▶ | sub _EMFILE () { 24 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_EMFILE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 0 | ▶ | sub _ENFILE () { 23 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_ENFILE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | 9 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||