blib/lib/Linux/Event/Listen.pm

filter: Statementstmt 76.4% 156 / 204 Branchbran 58.2% 85 / 146 Conditioncond 44.6% 46 / 103 MC/DCmcdc 15.0% 9 / 60 Subroutinesub 65.7% 23 / 35 Podpod 66.6% 10 / 15 totaltotal 58.4% 329 / 563 CC 113 SCAR 51.0
CC
113
Cov
63%
new
342.0
__ANON__
78.5
__ANON__
12.0
CRAP 164.7
↑ Summary
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")
l!l&&r!l&&!r
800
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")
ABresult
1X1
011
000
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")
l!l&&r!l&&!r
800
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")
ABresult
1X1
011
000
48 8 ▶ croak 'on_accept must be a code reference' if ref($on_accept) ne 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $on_accept ne "CODE" 0 8 8
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
BranchTrueFalseTotal
if defined $on_error and ref $on_error ne "CODE" 0 8 8
Condition66%2/3
defined $on_error and ref $on_error ne "CODE"
!ll&&!rl&&r
440
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"
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if defined $on_emfile and ref $on_emfile ne "CODE" 0 8 8
Condition33%1/3
defined $on_emfile and ref $on_emfile ne "CODE"
!ll&&!rl&&r
800
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"
ABresult
0X0
100
111
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
l!l
08
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"}
Aresult
11
00
57  
58 8 ▶ my $user_edge = exists $args{edge_triggered} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'edge_triggered'} ? : 0 8 8
59 8 ▶ my $edge_triggered = $user_edge ? delete($args{edge_triggered}) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$user_edge ? : 0 8 8
60 8 ▶ my $oneshot = delete($args{oneshot}) // 0;
Condition50%1/2
delete $args{"oneshot"} // 0
l!l
08
MC/DC0%0/1
delete $args{"oneshot"} // 0
delete $args{"oneshot"}
Truth table50%1/2
A
A= delete $args{"oneshot"}
Aresult
11
00
61  
62 8 ▶ my $user_cap = exists $args{max_accept_per_tick} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'max_accept_per_tick'} ? : 1 7 8
63 8 ▶ my $max_accept_per_tick = $user_cap ? delete($args{max_accept_per_tick}) : 128;
Branch: 1 branch
BranchTrueFalseTotal
$user_cap ? : 1 7 8
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
BranchTrueFalseTotal
if ($user_cap and not $user_edge) 1 7 8
Condition66%2/3
$user_cap and not $user_edge
!ll&&!rl&&r
701
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
ABresult
0X0
100
111
69 1 $edge_triggered = 0;
70 }
71 8 ▶ croak 'max_accept_per_tick must be a positive integer'
Branch: 1 branch
BranchTrueFalseTotal
if not $max_accept_per_tick =~ /\A[0-9]+\z/u or $max_accept_per_tick < 1 0 8 8
Condition33%1/3
not $max_accept_per_tick =~ /\A[0-9]+\z/u or $max_accept_per_tick < 1
l!l&&r!l&&!r
008
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
ABresult
1X1
011
000
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
l!l
08
MC/DC0%0/1
delete $args{"cloexec"} // 1
delete $args{"cloexec"}
Truth table50%1/2
A
A= delete $args{"cloexec"}
Aresult
11
00
75 8 ▶ my $nonblocking = delete($args{nonblocking}) // 1; # best-effort; forced on in practice
Condition50%1/2
delete $args{"nonblocking"} // 1
l!l
08
MC/DC0%0/1
delete $args{"nonblocking"} // 1
delete $args{"nonblocking"}
Truth table50%1/2
A
A= delete $args{"nonblocking"}
Aresult
11
00
76  
77 8 ▶ my $backlog = delete($args{backlog}) // 4096;
Condition50%1/2
delete $args{"backlog"} // 4096
l!l
08
MC/DC0%0/1
delete $args{"backlog"} // 4096
delete $args{"backlog"}
Truth table50%1/2
A
A= delete $args{"backlog"}
Aresult
11
00
78 8 ▶ my $reuseaddr = delete($args{reuseaddr}) // 1;
Condition50%1/2
delete $args{"reuseaddr"} // 1
l!l
08
MC/DC0%0/1
delete $args{"reuseaddr"} // 1
delete $args{"reuseaddr"}
Truth table50%1/2
A
A= delete $args{"reuseaddr"}
Aresult
11
00
79 8 ▶ my $reuseport = delete($args{reuseport}) // 0;
Condition50%1/2
delete $args{"reuseport"} // 0
l!l
08
MC/DC0%0/1
delete $args{"reuseport"} // 0
delete $args{"reuseport"}
Truth table50%1/2
A
A= delete $args{"reuseport"}
Aresult
11
00
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
l!l
17
MC/DC100%1/1
delete $args{"unlink"} // 0
delete $args{"unlink"}
Truth table100%2/2
A
A= delete $args{"unlink"}
Aresult
11
00
87 8 ▶ my $unlink_on_cancel = exists $args{unlink_on_cancel} ? delete($args{unlink_on_cancel}) : undef;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'unlink_on_cancel'} ? : 0 8 8
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
BranchTrueFalseTotal
if %args 0 8 8
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
BranchTrueFalseTotal
if defined $fh and defined $host || defined $port || defined $path 0 8 8
Condition44%4/9
defined $fh and defined $host || defined $port || defined $path
!ll&&!rl&&r
620
defined $host or defined $port or defined $path
l!l&&r!l&&!r
002
defined $host or defined $port
l!l&&r!l&&!r
002
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
ABCDresult
0XXX0
10000
11XX1
101X1
10011
94 8 ▶ croak 'host and port must be provided together' if (defined($host) xor defined($port));
Branch: 1 branch
BranchTrueFalseTotal
if defined $host xor defined $port 0 8 8
Condition50%2/4
defined $host xor defined $port
l&&rl&&!r!l&&r!l&&!r
5003
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
ABresult
110
101
011
000
95 8 ▶ croak 'path cannot be combined with host/port' if defined($path) && defined($host);
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and defined $host 0 8 8
Condition66%2/3
defined $path and defined $host
!ll&&!rl&&r
710
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
ABresult
0X0
100
111
96  
97  
98 8 ▶ if (!defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fh) { } 6 2 8
99 6 ▶ croak 'either fh, host/port, or path is required' if !defined($host) && !defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host or defined $path 0 6 6
Condition66%2/3
defined $host or defined $path
l!l&&r!l&&!r
510
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
ABresult
1X1
011
000
100  
101 6 ▶ if (defined $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $path) { } 1 5 6
102 1 ▶ if ($unlink) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unlink) 1 0 1
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
BranchTrueFalseTotal
$reuseaddr ? : 5 0 5
$reuseport ? : 0 5 5
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
BranchTrueFalseTotal
unless ($fh) 0 6 6
123 0 my $err = {
124 op => 'setup',
125 errno => 0 + $!,
126 error => "$!",
127 fatal => 1,
128 };
129 0 ▶ if ($on_error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($on_error) 0 0 0
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
BranchTrueFalseTotal
if (defined $v6only) 0 6 6
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
BranchTrueFalseTotal
$v6only ? : 0 0 0
143 }
144  
145 6 ▶ $owns_socket = 1 if !defined $owns_socket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owns_socket 6 0 6
146 6 ▶ $unlink_on_cancel = 1 if defined($path) && !defined($unlink_on_cancel);
Branch: 1 branch
BranchTrueFalseTotal
if defined $path and not defined $unlink_on_cancel 1 5 6
Condition66%2/3
defined $path and not defined $unlink_on_cancel
!ll&&!rl&&r
501
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
ABresult
0X0
100
111
147 } else {
148 2 ▶ $owns_socket = 0 if !defined $owns_socket;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owns_socket 2 0 2
149 }
150  
151 # If caller provided fh, validate it is in listening state.
152 8 ▶ if (defined $fh && !defined $host) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh and not defined $host) 3 5 8
Condition66%2/3
defined $fh and not defined $host
!ll&&!rl&&r
053
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
ABresult
0X0
100
111
153 3 my $acceptconn = getsockopt($fh, SOL_SOCKET, SO_ACCEPTCONN);
154 3 ▶ if (!defined $acceptconn || !unpack('i', $acceptconn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $acceptconn and unpack "i", $acceptconn) 0 3 3
Condition33%1/3
defined $acceptconn and unpack "i", $acceptconn
!ll&&!rl&&r
003
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
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if $nonblocking 8 0 8
161 8 ▶ _set_cloexec($fh) if $cloexec;
Branch: 1 branch
BranchTrueFalseTotal
if $cloexec 8 0 8
162  
163 8 ▶ my $self = bless {
Branch: 8 branches
BranchTrueFalseTotal
$owns_socket ? : 6 2 8
$warn_on_unhandled_error ? : 0 8 8
$edge_triggered ? : 7 1 8
$oneshot ? : 0 8 8
defined $path ? : 1 7 8
$unlink_on_cancel ? : 1 7 8
$cloexec ? : 8 0 8
$nonblocking ? : 8 0 8
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
BranchTrueFalseTotal
if ($accepted < $self->{"max_accept_per_tick"}) 16 3 19
192 16 my $client;
193 16 my $peer_packed = accept($client, $listen_fh);
194  
195 16 ▶ if (!$peer_packed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($peer_packed) 0 16 16
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
BranchTrueFalseTotal
if ($errno == _EINTR() or $errno == _ECONNABORTED()) 0 0 0
Condition0%0/3
$errno == _EINTR() or $errno == _ECONNABORTED()
l!l&&r!l&&!r
000
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()
ABresult
1X1
011
000
200 0 next;
201 }
202 0 ▶ if ($errno == _EAGAIN() || $errno == _EWOULDBLOCK()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($errno == _EAGAIN() or $errno == _EWOULDBLOCK()) 0 0 0
Condition0%0/3
$errno == _EAGAIN() or $errno == _EWOULDBLOCK()
l!l&&r!l&&!r
000
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()
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($errno == _EMFILE() || $errno == _ENFILE() and $self->{'on_emfile'}) { } 0 0 0
elsif ($self->{'on_error'}) { } 0 0 0
elsif ($self->{'warn_on_unhandled_error'}) { } 0 0 0
Condition0%0/6
$errno == _EMFILE() || $errno == _ENFILE() and $self->{"on_emfile"}
!ll&&!rl&&r
000
$errno == _EMFILE() or $errno == _ENFILE()
l!l&&r!l&&!r
000
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"}
ABCresult
00X0
1X00
0100
1X11
0111
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
BranchTrueFalseTotal
if $self->{"nonblocking"} 16 0 16
225 16 ▶ _set_cloexec($client) if $self->{cloexec};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"cloexec"} 16 0 16
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
BranchTrueFalseTotal
unless ($ok) 1 15 16
231 1 ▶ my $e = $@ || 'unknown error';
Condition50%1/2
$@ || 'unknown error'
l!l
10
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
232 1 my $err = {
233 op => 'on_accept',
234 error => "$e",
235 fatal => 0,
236 };
237 1 ▶ if ($self->{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'on_error'}) { } 1 0 1
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
BranchTrueFalseTotal
if $self->{"_cancelled"} 7 9 16
247 }
248  
249 10 ▶ if ($self->{_defer_close} && $self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_defer_close"} and $self->{"fh"}) 5 5 10
Condition66%2/3
$self->{"_defer_close"} and $self->{"fh"}
!ll&&!rl&&r
505
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"}
ABresult
0X0
100
111
250 5 close($self->{fh});
251 5 delete $self->{fh};
252 5 ▶ if ($self->{_unix_path} && $self->{_unlink_on_cancel}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_unix_path"} and $self->{"_unlink_on_cancel"}) 1 4 5
Condition66%2/3
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
!ll&&!rl&&r
401
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
if ($self->{"on_error"} or $self->{"warn_on_unhandled_error"}) 4 4 8
Condition66%2/3
$self->{"on_error"} or $self->{"warn_on_unhandled_error"}
l!l&&r!l&&!r
404
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"}
ABresult
1X1
011
000
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
BranchTrueFalseTotal
if ($self->{'on_error'}) { } 0 0 0
elsif ($self->{'warn_on_unhandled_error'}) { } 0 0 0
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)
l!l
10
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
Aresult
11
00
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
BranchTrueFalseTotal
if $self->{"paused"} 0 1 1
298 1 ▶ $self->{watcher}->disable_read if $self->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} 1 0 1
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
BranchTrueFalseTotal
unless $self->{"paused"} 0 1 1
305 1 ▶ $self->{watcher}->enable_read if $self->{watcher};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"watcher"} 1 0 1
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
BranchTrueFalseTotal
$self->{'paused'} ? : 1 2 3
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
BranchTrueFalseTotal
$self->{'watcher'} ? : 1 1 2
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"
l!l
01
MC/DC0%0/1
$self->{"_family"} // "unknown"
$self->{"_family"}
Truth table50%1/2
A
A= $self->{"_family"}
Aresult
11
00
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
BranchTrueFalseTotal
($self->{'_family'} // '') eq 'unix' ? : 0 0 0
Condition0%0/2
$self->{"_family"} // ""
l!l
00
MC/DC0%0/1
$self->{"_family"} // ""
$self->{"_family"}
Truth table0%0/2
A
A= $self->{"_family"}
Aresult
11
00
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
BranchTrueFalseTotal
($self->{'_family'} // '') =~ /\Ainet/u ? : 0 0 0
Condition0%0/2
$self->{"_family"} // ""
l!l
00
MC/DC0%0/1
$self->{"_family"} // ""
$self->{"_family"}
Truth table0%0/2
A
A= $self->{"_family"}
Aresult
11
00
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)
l!l
00
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table0%0/2
A
A= $self->{"fh"}
Aresult
11
00
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)
l!l
20
MC/DC0%0/1
$self->{"fh"} // (return undef)
$self->{"fh"}
Truth table50%1/2
A
A= $self->{"fh"}
Aresult
11
00
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
BranchTrueFalseTotal
if $w 8 9 17
334  
335 17 ▶ if ($self->{owns_socket} && $self->{fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"owns_socket"} and $self->{"fh"}) 6 11 17
Condition100%3/3
$self->{"owns_socket"} and $self->{"fh"}
!ll&&!rl&&r
476
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"}
ABresult
0X0
100
111
336 6 ▶ if ($self->{_in_read_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_in_read_cb'}) { } 5 1 6
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
BranchTrueFalseTotal
if ($self->{"_unix_path"} and $self->{"_unlink_on_cancel"}) 0 1 1
Condition33%1/3
$self->{"_unix_path"} and $self->{"_unlink_on_cancel"}
!ll&&!rl&&r
100
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"}
ABresult
0X0
100
111
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
BranchTrueFalseTotal
unless defined $flags 0 24 24
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
BranchTrueFalseTotal
unless defined $fdflags 0 24 24
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
BranchTrueFalseTotal
if ($len == 16) 14 2 16
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
BranchTrueFalseTotal
if (defined $port6) 0 2 2
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
BranchTrueFalseTotal
if (defined $upath) 2 0 2
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__
↑ Summary