blib/lib/Feersum/Runner.pm

filter: Statementstmt 41.6% 448 / 1076 Branchbran 29.3% 183 / 624 Conditioncond 19.0% 62 / 326 MC/DCmcdc 9.9% 18 / 181 Subroutinesub 55.1% 64 / 116 Podpod 100% 4 / 4 totaltotal 33.4% 779 / 2327 CC 453 SCAR 51.7
CC
453
Cov
34%
__ANON__
1122.0
__ANON__
650.0
_run_hot_restart_master
408.8
CRAP 175.5
↑ Summary blib/lib/Feersum.pm »
1 package Feersum::Runner;
2 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 16 ▶use EV;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 16 ▶use Feersum;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use Socket qw/SOMAXCONN SOL_SOCKET SO_REUSEADDR AF_INET SOCK_STREAM
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 inet_aton pack_sockaddr_in/;
9 16 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.0
10 # Socket exports these even when unsupported; only a call croaks, so call it.
11 16 eval { Socket->import(qw/AF_INET6 inet_pton pack_sockaddr_in6/); AF_INET6(); 1 }
12 16 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "Socket"->import("AF_INET6", "inet_pton", "pack_sockaddr_in6"); &AF_INET6(); 1 } } or do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} *AF_INET6 = sub () { undef } ; *inet_pton = sub { undef; } ; *pack_sockaddr_in6 = sub { undef; } } 15 1 16
13 16 ▶ no warnings 'redefine', 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 *AF_INET6 = sub () { undef };
15 0 ▶ *inet_pton = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
16 0 ▶ *pack_sockaddr_in6 = sub { undef };
Subroutine __ANON__: not called CC=1 SCAR=1.2
17 };
18 }
19 16 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
20 # Same croak-on-call trap as AF_INET6 above.
21 16 eval { Socket->import('SO_REUSEPORT'); SO_REUSEPORT(); 1 }
22 16 ▶ or do { no warnings 'redefine', 'prototype';
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval { do { "Socket"->import("SO_REUSEPORT"); &SO_REUSEPORT(); 1 } } or do { BEGIN {${^WARNING_BITS} = "\x55\x55\x55\x55\x15\x55\x55\x55\x15\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55\x55"} *SO_REUSEPORT = sub () { undef } } 15 1 16
23 1 *SO_REUSEPORT = sub () { undef } };
24 }
25 16 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 16 ▶use Scalar::Util qw/weaken blessed reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 16 ▶use List::Util qw/any/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 16 ▶use Carp qw/carp croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 16 ▶use File::Spec::Functions 'rel2abs';
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 16 ▶use constant DEATH_TIMER => 5.0; # seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
32 16 ▶use constant DEATH_TIMER_INCR => 2.0; # seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
33 # sun_path is 108 bytes on Linux and 104 on the BSDs/macOS, minus the NUL.
34 16 ▶use constant UNIX_PATH_MAX => ($^O =~ /linux/i ? 108 : 104) - 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$^O =~ /linux/i ? : 16 0 16
35 16 ▶use constant DEFAULT_HOST => 'localhost';
Subroutine BEGIN: called CC=1 SCAR=0.0
36 16 ▶use constant DEFAULT_PORT => 5000;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 16 ▶use constant MAX_PRE_FORK => $ENV{FEERSUM_MAX_PRE_FORK} || 1000;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'FEERSUM_MAX_PRE_FORK'} || 1000
l!l
016
MC/DC0%0/1
$ENV{'FEERSUM_MAX_PRE_FORK'} || 1000
$ENV{'FEERSUM_MAX_PRE_FORK'}
Truth table50%1/2
A
A= $ENV{'FEERSUM_MAX_PRE_FORK'}
Aresult
11
00
38 # Respawn backoff: a worker dying within RESPAWN_INSTANT_DEATH is crash-looping.
39 16 ▶use constant RESPAWN_INSTANT_DEATH => 1.0; # seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
40 16 ▶use constant RESPAWN_BACKOFF_BASE => 0.1; # seconds, doubled per failure
Subroutine BEGIN: called CC=1 SCAR=0.0
41 16 ▶use constant RESPAWN_BACKOFF_MAX => 30.0; # seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
42 16 ▶use constant RESPAWN_MAX_FAILS => 10; # then retry at BACKOFF_MAX only
Subroutine BEGIN: called CC=1 SCAR=0.0
43 16 ▶use constant WORKER_READY_BUFSZ => 256; # readiness bytes read per wakeup
Subroutine BEGIN: called CC=1 SCAR=0.0
44 16 ▶use constant MAX_PORT => 65_535;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 16 ▶use constant MIN_LIKELY_PORT => 1024; # below this, a bare number is ambiguous
Subroutine BEGIN: called CC=1 SCAR=0.0
46 16 ▶use constant PORT_DIGITS => 5; # a 5-digit tail is definitely a port
Subroutine BEGIN: called CC=1 SCAR=0.0
47 16 ▶use constant PRIORITY_MIN => -2; # libev EV_MINPRI; max is +2
Subroutine BEGIN: called CC=1 SCAR=0.0
48 16 ▶use constant SYSCALL_ERROR => -1; # the usual failure return
Subroutine BEGIN: called CC=1 SCAR=0.0
49 16 ▶use constant DEFAULT_STARTUP_TIMEOUT => 10; # seconds
Subroutine BEGIN: called CC=1 SCAR=0.0
50 16 ▶use constant READY_TOKEN_SIZE => 8; # daemonize readiness handshake
Subroutine BEGIN: called CC=1 SCAR=0.0
51 16 ▶use constant WSTATUS_SIGNAL_MASK => 127; # low 7 bits of a wait() status
Subroutine BEGIN: called CC=1 SCAR=0.0
52 16 ▶use constant WSTATUS_EXIT_SHIFT => 8; # high bits hold the exit code
Subroutine BEGIN: called CC=1 SCAR=0.0
53 16 ▶use constant WAIT_ANY_PID => -1; # waitpid(): any child
Subroutine BEGIN: called CC=1 SCAR=0.0
54 # Retirement exit status; keeps a fast retirement out of the respawn backoff.
55 16 ▶use constant EXIT_RETIRED => 42;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 16 ▶use constant FINAL_REAP_ATTEMPTS => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # warn() is the daemon logging itself; the unchecked close() calls are cleanup.
59 ## no critic (ErrorHandling::RequireCarping, InputOutput::RequireCheckedClose)
60  
61 16 our $INSTANCE;
62 31 ▶sub new { ## no critic (RequireArgUnpacking)
Subroutine new: called CC=3 SCAR=11.0
Pod documented
63 31 my $c = shift;
64 31 ▶ if ($INSTANCE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Feersum::Runner::INSTANCE) 3 28 31
65 croak "Only one Feersum::Runner instance can be active at a time"
66 3 ▶ if $INSTANCE->{running};
Branch: 1 branch
BranchTrueFalseTotal
if $Feersum::Runner::INSTANCE->{"running"} 0 3 3
67 3 $INSTANCE->_cleanup();
68 3 undef $INSTANCE;
69 }
70 31 $INSTANCE = bless {quiet=>1, @_, running=>0}, $c;
71 31 return $INSTANCE;
72 }
73  
74 44 ▶sub _cleanup {
Subroutine _cleanup: called CC=3 SCAR=11.0
75 44 my $self = shift;
76 44 ▶ return if $self->{_cleaned_up};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_cleaned_up"} 3 41 44
77 41 $self->{_cleaned_up} = 1;
78 41 ▶ if (my $f = $self->{endjinn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $self->{"endjinn"}) 5 36 41
79 5 ▶ $f->request_handler(sub{});
Subroutine __ANON__: not called CC=1 SCAR=0.0
80 5 $f->unlisten();
81 }
82 41 $self->{_quit} = undef;
83 41 $self->{_term} = undef;
84 41 $self->{_int} = undef;
85 41 $self->{_hup} = undef;
86 # Left armed, _death would _exit(1) an embedder after run() has returned.
87 41 $self->{_death} = undef;
88 41 $self->{_kids} = undef;
89 41 $self->{_respawn_timers} = undef;
90 41 $self->{running} = 0;
91 41 $self->_remove_pid_file;
92 41 return;
93 }
94  
95 # Only if it still holds our pid; a failed second instance must not unlink it.
96 41 ▶sub _remove_pid_file {
Subroutine _remove_pid_file: called CC=6 SCAR=33.4
97 41 my $self = shift;
98 41 ▶ my $file = $self->{pid_file} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->{"pid_file"} or return 0 41 41
99 0 ▶ open my $fh, '<', $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 0 0
100 0 my $owner = <$fh>;
101 0 close $fh;
102 0 ▶ $owner = '' unless defined $owner;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $owner 0 0 0
103 0 $owner =~ s/\s+//g;
104 0 ▶ return unless $owner eq $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $owner eq $$ 0 0 0
105 # unlink needs directory write access, which the dropped-to user often lacks.
106 0 ▶ unlink($file) or warn "Feersum [$$]: cannot remove pid_file '$file': $!\n";
Branch: 1 branch
BranchTrueFalseTotal
unlink $file or warn "Feersum [${$}]: cannot remove pid_file '${file}': $!\n" 0 0 0
107 0 return;
108 }
109  
110 40 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
111 40 my ($self) = @_;
112 40 local $@ = undef;
113 40 $self->_cleanup();
114 40 return;
115 }
116  
117 31 ▶sub _create_socket { ## no critic (ProhibitExcessComplexity)
Subroutine _create_socket: called CC=44 SCAR=42.5
118 31 my ($self, $listen, $use_reuseport, $no_listen) = @_;
119 31 ▶ my $backlog = $self->{backlog} || SOMAXCONN;
Condition100%2/2
$self->{'backlog'} || 4096
l!l
328
MC/DC100%1/1
$self->{'backlog'} || 4096
$self->{'backlog'}
Truth table100%2/2
A
A= $self->{'backlog'}
Aresult
11
00
120  
121 31 my $sock;
122 31 ▶ if (_is_unix_listen($listen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unix_listen($listen)) { } 2 29 31
123 2 require IO::Socket::UNIX;
124 2 my $path = rel2abs($listen);
125 # pack_sockaddr_un only warns before truncating, binding a prefix of the path.
126 2 ▶ croak "listen path '$path' is too long (" . length($path)
Branch: 1 branch
BranchTrueFalseTotal
if length $path > 107 1 1 2
127 . " bytes; the platform limit for a unix socket is "
128 . UNIX_PATH_MAX . ")"
129 if length($path) > UNIX_PATH_MAX;
130 1 ▶ if (-S $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (-S $path) 1 0 1
131 1 ▶ if (IO::Socket::UNIX->new(Peer => $path)) {
Branch: 1 branch
BranchTrueFalseTotal
if ("IO::Socket::UNIX"->new("Peer", $path)) 0 1 1
132 0 croak "listen path '$path' is already served by a live server";
133 }
134 1 ▶ unlink $path or carp "unlink stale socket '$path': $!";
Branch: 1 branch
BranchTrueFalseTotal
unlink $path or carp("unlink stale socket '${path}': $!") 1 0 1
135 }
136 1 my $saved = umask(0);
137 1 $sock = eval {
138 1 IO::Socket::UNIX->new(
139 Local => $path,
140 Listen => $backlog,
141 );
142 };
143 1 my $err = $@;
144 1 umask($saved);
145 1 ▶ die $err if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 1 1
146 1 ▶ croak "couldn't bind to socket '$path': $!" unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 1 1
147 0 ▶ $sock->blocking(0) || do { close($sock); croak "couldn't unblock socket: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
$sock->blocking(0) or do { close $sock; croak("couldn't unblock socket: $!") } 1 0 1
148 }
149 else {
150 29 require IO::Socket::INET;
151 # IO::Socket::INET is IPv4-only, so IPv6 takes the manual socket() path.
152 29 ▶ my $is_ipv6_form = ($listen =~ /^\[/ || $listen =~ /:.*:/) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$listen =~ /^\[/ || $listen =~ /:.*:/ ? : 8 21 29
Condition100%3/3
$listen =~ /^\[/ or $listen =~ /:.*:/
l!l&&r!l&&!r
6221
MC/DC100%2/2
$listen =~ /^\[/ or $listen =~ /:.*:/
$listen =~ /^\[/ $listen =~ /:.*:/
Truth table100%3/3
A or B
A= $listen =~ /^\[/B= $listen =~ /:.*:/
ABresult
1X1
011
000
153 29 ▶ my $want_reuseport = ($use_reuseport && defined SO_REUSEPORT) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$use_reuseport && !0 ? : 7 22 29
Condition100%2/2
$use_reuseport and
!ll
218
MC/DC100%1/1
$use_reuseport and
$use_reuseport
Truth table100%2/2
A
A= $use_reuseport
Aresult
00
11
154 # SO_REUSEPORT must be set before bind.
155 29 ▶ if ($want_reuseport || $is_ipv6_form) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_reuseport or $is_ipv6_form) { } 12 17 29
Condition100%3/3
$want_reuseport or $is_ipv6_form
l!l&&r!l&&!r
7517
MC/DC100%2/2
$want_reuseport or $is_ipv6_form
$want_reuseport $is_ipv6_form
Truth table100%3/3
A or B
A= $want_reuseportB= $is_ipv6_form
ABresult
1X1
011
000
156 12 my ($host, $port, $is_ipv6);
157 12 ▶ if ($listen =~ /^\[([^\]]+)\]:(\d*)$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($listen =~ /^\[([^\]]+)\]:(\d*)$/) { } 4 8 12
elsif ($listen =~ /^\[([^\]]+)\]$/) { } 2 6 8
elsif ($listen =~ /:.*:/) { } 2 4 6
158 4 ▶ ($host, $port, $is_ipv6) = ($1, $2 || 0, 1);
Condition50%1/2
$2 || 0
l!l
04
MC/DC0%0/1
$2 || 0
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
159 } elsif ($listen =~ /^\[([^\]]+)\]$/) {
160 2 ($host, $port, $is_ipv6) = ($1, 0, 1);
161 } elsif ($listen =~ /:.*:/) {
162 2 ▶ if ($listen =~ /:(\d{1,5})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($listen =~ /:(\d{1,5})$/) 2 0 2
163 2 my $maybe_port = $1;
164 2 ▶ if ($maybe_port <= MAX_PORT && (length($maybe_port) == PORT_DIGITS || $maybe_port >= MIN_LIKELY_PORT)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($maybe_port <= 65535 and length $maybe_port == 5 || $maybe_port >= 1024) 2 0 2
Condition33%2/6
$maybe_port <= 65535 and length $maybe_port == 5 || $maybe_port >= 1024
!ll&&!rl&&r
002
length $maybe_port == 5 or $maybe_port >= 1024
l!l&&r!l&&!r
020
MC/DC0%0/3
$maybe_port <= 65535 and length $maybe_port == 5 || $maybe_port >= 1024
$maybe_port <= 65535 length $maybe_port == 5 $maybe_port >= 1024
Truth table25%1/4
A and B or C
A= $maybe_port <= 65535B= length $maybe_port == 5C= $maybe_port >= 1024
ABCresult
0XX0
1000
11X1
1011
165 2 croak "ambiguous IPv6 address '$listen': use bracket notation [host]:port " .
166 "(e.g., [::1]:$maybe_port or [2001:db8::1]:$maybe_port)";
167 }
168 }
169 0 ($host, $port, $is_ipv6) = ($listen, 0, 1);
170 } else {
171 4 ($host, $port) = split /:/, $listen, 2;
172 4 ▶ $host ||= '0.0.0.0';
Condition50%1/2
$host ||= "0.0.0.0"
l!l
40
MC/DC0%0/1
$host ||= "0.0.0.0"
$host
Truth table50%1/2
A
A= $host
Aresult
11
00
173 4 ▶ $port ||= 0;
Condition100%2/2
$port ||= 0
l!l
22
MC/DC100%1/1
$port ||= 0
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
174 4 $is_ipv6 = 0;
175 }
176  
177 10 ▶ if ($port !~ /^\d+$/ || $port > MAX_PORT) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $port =~ /^\d+$/ or $port > 65535) 0 10 10
Condition33%1/3
not $port =~ /^\d+$/ or $port > 65535
l!l&&r!l&&!r
0010
MC/DC0%0/2
not $port =~ /^\d+$/ or $port > 65535
not $port =~ /^\d+$/ $port > 65535
Truth table33%1/3
not(A) or B
A= not $port =~ /^\d+$/B= $port > 65535
ABresult
1X1
011
000
178 0 croak "invalid port '$port': must be 0-" . MAX_PORT;
179 }
180  
181 10 my ($domain, $sockaddr);
182 10 ▶ if ($is_ipv6) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_ipv6) { } 6 4 10
183 6 defined AF_INET6()
184 or croak "IPv6 not supported on this system";
185 5 ▶ my $addr = inet_pton(AF_INET6(), $host)
Branch: 1 branch
BranchTrueFalseTotal
my $addr = inet_pton(10, $host) or croak("couldn't resolve IPv6 address '${host}'") 5 0 5
186 or croak "couldn't resolve IPv6 address '$host'";
187 5 $domain = AF_INET6();
188 5 $sockaddr = pack_sockaddr_in6($port, $addr);
189 } else {
190 4 ▶ my $addr = inet_aton($host)
Branch: 1 branch
BranchTrueFalseTotal
my $addr = inet_aton($host) or croak("couldn't resolve address '${host}'") 4 0 4
191 or croak "couldn't resolve address '$host'";
192 4 $domain = AF_INET();
193 4 $sockaddr = pack_sockaddr_in($port, $addr);
194 }
195  
196 9 ▶ socket($sock, $domain, SOCK_STREAM(), 0)
Branch: 1 branch
BranchTrueFalseTotal
socket $sock, $domain, 1, 0 or croak("couldn't create socket: $!") 9 0 9
197 or croak "couldn't create socket: $!";
198 setsockopt($sock, SOL_SOCKET, SO_REUSEADDR, pack("i", 1))
199 0 ▶ or do { close($sock); croak "setsockopt SO_REUSEADDR failed: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 1, 2, "\1\0\0\0" or do { close $sock; croak("setsockopt SO_REUSEADDR failed: $!") } 9 0 9
200 9 ▶ if ($want_reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_reuseport) 6 3 9
201 setsockopt($sock, SOL_SOCKET, SO_REUSEPORT, pack("i", 1))
202 0 ▶ or do { close($sock); croak "setsockopt SO_REUSEPORT failed: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 1, 15, "\1\0\0\0" or do { close $sock; croak("setsockopt SO_REUSEPORT failed: $!") } 6 0 6
203 }
204 bind($sock, $sockaddr)
205 9 ▶ or do { close($sock); croak "couldn't bind to socket: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
bind $sock, $sockaddr or do { close $sock; croak("couldn't bind to socket: $!") } 8 1 9
206 # Probe: listen() would join the reuseport group and attract connections.
207 8 ▶ return $sock if $no_listen;
Branch: 1 branch
BranchTrueFalseTotal
if $no_listen 1 7 8
208 listen($sock, $backlog)
209 0 ▶ or do { close($sock); croak "couldn't listen: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
listen $sock, $backlog or do { close $sock; croak("couldn't listen: $!") } 7 0 7
210  
211 7 require IO::Handle;
212 7 bless $sock, 'IO::Handle';
213 $sock->blocking(0)
214 0 ▶ || do { close($sock); croak "couldn't unblock socket: $!"; };
Branch: 1 branch
BranchTrueFalseTotal
$sock->blocking(0) or do { close $sock; croak("couldn't unblock socket: $!") } 7 0 7
215 }
216 else {
217 17 ▶ if ($listen =~ /:(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($listen =~ /:(\d+)$/) { } 16 1 17
elsif ($listen =~ /:(\S+)$/) { } 1 0 1
218 16 my $port = $1;
219 16 ▶ croak "invalid port '$port': must be 0-" . MAX_PORT if $port > MAX_PORT;
Branch: 1 branch
BranchTrueFalseTotal
if $port > 65535 1 15 16
220 } elsif ($listen =~ /:(\S+)$/) {
221 1 my $port = $1;
222 1 ▶ croak "invalid port '$port': must be numeric" unless $port =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $port =~ /^\d+$/ 1 0 1
223 }
224 15 $sock = IO::Socket::INET->new(
225 LocalAddr => $listen,
226 ReuseAddr => 1,
227 Proto => 'tcp',
228 Listen => $backlog,
229 Blocking => 0,
230 );
231 15 ▶ croak "couldn't bind to socket: $!" unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 15 15
232 }
233 }
234 23 return $sock;
235 }
236  
237  
238 # One list for cold start and hot_restart, so a new setting cannot miss either.
239 16 my @SIMPLE_SETTINGS = (
240 [keepalive => 'set_keepalive'],
241 [reverse_proxy => 'set_reverse_proxy'],
242 [proxy_protocol => 'set_proxy_protocol'],
243 [psgix_io => 'set_psgix_io'],
244 [read_timeout => 'read_timeout'],
245 [header_timeout => 'header_timeout'],
246 [write_timeout => 'write_timeout'],
247 [max_connection_reqs => 'max_connection_reqs'],
248 [read_priority => 'read_priority'],
249 [write_priority => 'write_priority'],
250 [accept_priority => 'accept_priority'],
251 [max_accept_per_loop => 'max_accept_per_loop'],
252 [max_connections => 'max_connections'],
253 [max_read_buf => 'max_read_buf'],
254 [max_body_len => 'max_body_len'],
255 [max_uri_len => 'max_uri_len'],
256 [wbuf_low_water => 'wbuf_low_water'],
257 [max_h2_concurrent_streams => 'max_h2_concurrent_streams'],
258 [max_h2_conn_body => 'max_h2_conn_body'],
259 );
260  
261 10 ▶sub _apply_simple_settings {
Subroutine _apply_simple_settings: called CC=4 SCAR=13.9
262 10 my ($self, $f, $consume) = @_;
263 10 for my $pair (@SIMPLE_SETTINGS) {
264 190 my ($opt, $meth) = @$pair;
265 190 ▶ my $val = $consume ? delete $self->{$opt} : $self->{$opt};
Branch: 1 branch
BranchTrueFalseTotal
$consume ? : 190 0 190
266 190 ▶ $f->$meth($val) if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2 188 190
267 }
268 10 return;
269 }
270  
271 # File-scoped: a watcher in the callback's own pad dies with the callback.
272 16 my $RETIRE_DEADLINE;
273  
274 0 ▶sub _set_max_requests_per_worker {
Subroutine _set_max_requests_per_worker: not called CC=4 SCAR=30.0
275 0 my ($self, $f) = @_;
276 0 ▶ my $max = $self->{max_requests_per_worker} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $max = $self->{"max_requests_per_worker"} or return 0 0 0
277 my $gt = $self->{graceful_timeout}
278 // $ENV{FEERSUM_GRACEFUL_TIMEOUT}
279 0 ▶ // DEATH_TIMER;
Condition0%0/5
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
l!l
00
$self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
$self->{"graceful_timeout"} $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
Truth table0%0/3
A // B
A= $self->{"graceful_timeout"}B= $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
ABresult
1X1
011
000
280 0 my $quiet = $self->{quiet};
281 $f->max_requests_per_worker($max,
282 0 ▶ sub { POSIX::_exit(EXIT_RETIRED) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
284 # The replacement is forked only after exit, so the drain needs a deadline.
285 0 ▶ $RETIRE_DEADLINE = EV::timer($gt, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
286 0 ▶ $quiet or warn "Feersum [$$]: retirement drain exceeded "
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: retirement drain exceeded " . "${gt}s, forcing exit\n" 0 0 0
287 . "${gt}s, forcing exit\n";
288 0 POSIX::_exit(EXIT_RETIRED);
289 0 });
290 0 });
291 0 return;
292 }
293  
294 0 ▶sub _apply_tls_to_listeners {
Subroutine _apply_tls_to_listeners: not called CC=6 SCAR=37.4
295 0 my ($self, $f, $n_listeners, $tls, $sni) = @_;
296 0 for my $i (0 .. $n_listeners - 1) {
297 0 $f->set_tls(listener => $i, %$tls);
298 }
299 0 ▶ if ($sni) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sni) 0 0 0
300 0 ▶ croak "sni must be an array reference" unless ref $sni eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sni eq "ARRAY" 0 0 0
301 0 for my $entry (@$sni) {
302 0 for my $i (0 .. $n_listeners - 1) {
303 0 $f->set_tls(listener => $i, %$entry);
304 }
305 }
306 }
307 0 return;
308 }
309  
310 26 ▶sub _normalize_listen {
Subroutine _normalize_listen: called CC=15 SCAR=30.6
311 26 my $self = shift;
312 26 ▶ if (defined $self->{listen} && !ref $self->{listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"listen"} and not ref $self->{"listen"}) 0 26 26
Condition33%1/3
defined $self->{"listen"} and not ref $self->{"listen"}
!ll&&!rl&&r
0260
MC/DC0%0/2
defined $self->{"listen"} and not ref $self->{"listen"}
defined $self->{"listen"} not ref $self->{"listen"}
Truth table33%1/3
A and not(B)
A= defined $self->{"listen"}B= not ref $self->{"listen"}
ABresult
0X0
100
111
313 0 $self->{listen} = [ $self->{listen} ];
314 }
315 $self->{listen} ||=
316 26 ▶ [ ($self->{host}||DEFAULT_HOST) . q{:} . ($self->{port}||DEFAULT_PORT) ];
Condition16%1/6
$self->{"listen"} ||= [($self->{"host"} || "localhost") . ':' . ($self->{"port"} || 5000)]
l!l
260
$self->{'host'} || 'localhost'
l!l
00
$self->{'port'} || 5000
l!l
00
MC/DC0%0/1
$self->{"listen"} ||= [($self->{"host"} || "localhost") . ':' . ($self->{"port"} || 5000)]
$self->{"listen"}
MC/DC0%0/1
$self->{'host'} || 'localhost'
$self->{'host'}
MC/DC0%0/1
$self->{'port'} || 5000
$self->{'port'}
Truth table50%1/2
A
A= $self->{"listen"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'host'}
Aresult
11
00
Truth table0%0/2
A
A= $self->{'port'}
Aresult
11
00
317 croak "listen must be an array reference"
318 26 ▶ if ref $self->{listen} ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"listen"} ne "ARRAY" 0 26 26
319 croak "listen array cannot be empty"
320 26 ▶ if @{$self->{listen}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"listen"};} == 0 0 26 26
321 # An empty or bare-numeric entry would bind 0.0.0.0 on a random port.
322 26 for my $i (0 .. $#{$self->{listen}}) {
323 27 my $l = $self->{listen}[$i];
324 27 ▶ croak "listen[$i] is undefined" unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 0 27 27
325 27 ▶ croak "listen[$i] is empty" unless length $l;
Branch: 1 branch
BranchTrueFalseTotal
unless length $l 0 27 27
326 27 ▶ croak "listen[$i] '$l' is not an address: use host:port, [ipv6]:port, "
Branch: 1 branch
BranchTrueFalseTotal
if $l =~ /\A\d+\z/ 0 27 27
327 . "or a socket path" if $l =~ /\A\d+\z/;
328 27 ▶ next if _is_unix_listen($l)
Branch: 2 branches
BranchTrueFalseTotal
if _is_unix_listen($l) or $l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/ 22 5 27
$l =~ /\A\[/ ? : 2 22 24
Condition100%3/3
_is_unix_listen($l) or $l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/
l!l&&r!l&&!r
3195
MC/DC100%2/2
_is_unix_listen($l) or $l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/
_is_unix_listen($l) $l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/
Truth table100%3/3
A or B
A= _is_unix_listen($l)B= $l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/
ABresult
1X1
011
000
329 || ($l =~ /\A\[/ ? $l =~ /\]:/ : $l =~ /:/);
330 5 croak "listen[$i] '$l' has no port: use $l:PORT "
331 . "(a portless address binds an arbitrary port)";
332 }
333 21 $self->{_listen_addrs} = [ @{$self->{listen}} ];
334 21 return;
335 }
336  
337 # Must not consume $self->{tls}: hot_restart re-reads it for every generation.
338 14 ▶sub _normalize_tls_config {
Subroutine _normalize_tls_config: called CC=13 SCAR=25.8
339 14 my $self = shift;
340 14 ▶ if (!$self->{tls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'tls'}) { } 11 3 14
341 11 ▶ if (my $cert = delete $self->{tls_cert_file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $cert = delete $self->{'tls_cert_file'}) { } 2 9 11
elsif (delete $self->{'tls_key_file'}) { } 1 8 9
342 my $key = delete $self->{tls_key_file}
343 2 ▶ or croak "tls_cert_file requires tls_key_file";
Branch: 1 branch
BranchTrueFalseTotal
my $key = delete $self->{"tls_key_file"} or croak("tls_cert_file requires tls_key_file") 1 1 2
344 1 $self->{tls} = { cert_file => $cert, key_file => $key };
345 } elsif (delete $self->{tls_key_file}) {
346 1 croak "tls_key_file requires tls_cert_file";
347 }
348 } else {
349 3 delete $self->{tls_cert_file};
350 3 delete $self->{tls_key_file};
351 }
352 12 ▶ if (my $tls = $self->{tls}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $tls = $self->{'tls'}) { } 4 8 12
elsif (delete $self->{'h2'}) { } 1 7 8
353 4 ▶ croak "tls must be a hash reference" unless ref $tls eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $tls eq "HASH" 0 4 4
354 4 ▶ croak "tls requires cert_file" unless $tls->{cert_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $tls->{"cert_file"} 0 4 4
355 4 ▶ croak "tls requires key_file" unless $tls->{key_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $tls->{"key_file"} 0 4 4
356 4 ▶ $tls->{h2} = 1 if delete $self->{h2};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"h2"} 1 3 4
357 } elsif (delete $self->{h2}) {
358 1 croak "h2 requires TLS (provide tls_cert_file and tls_key_file, or a tls hash)";
359 }
360 croak "sni requires TLS (provide tls_cert_file and tls_key_file, or a tls hash)"
361 11 ▶ if $self->{sni} && !$self->{tls};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sni"} and not $self->{"tls"} 1 10 11
Condition66%2/3
$self->{"sni"} and not $self->{"tls"}
!ll&&!rl&&r
1001
MC/DC50%1/2
$self->{"sni"} and not $self->{"tls"}
$self->{"sni"} not $self->{"tls"}
Truth table66%2/3
A and not(B)
A= $self->{"sni"}B= not $self->{"tls"}
ABresult
0X0
100
111
362 10 return;
363 }
364  
365 14 ▶sub _validate_pre_fork {
Subroutine _validate_pre_fork: called CC=5 SCAR=16.2
366 14 my $self = shift;
367 14 ▶ return unless $self->{pre_fork};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pre_fork"} 10 4 14
368 4 my $n = $self->{pre_fork};
369 4 ▶ croak "pre_fork must be a positive integer" if $n !~ /^\d+$/ || $n < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $n =~ /^\d+$/ or $n < 1 1 3 4
Condition66%2/3
not $n =~ /^\d+$/ or $n < 1
l!l&&r!l&&!r
103
MC/DC50%1/2
not $n =~ /^\d+$/ or $n < 1
not $n =~ /^\d+$/ $n < 1
Truth table66%2/3
not(A) or B
A= not $n =~ /^\d+$/B= $n < 1
ABresult
1X1
011
000
370 3 ▶ croak "pre_fork=$n exceeds maximum of " . MAX_PRE_FORK if $n > MAX_PRE_FORK;
Branch: 1 branch
BranchTrueFalseTotal
if $n > 1000 0 3 3
371 3 return;
372 }
373  
374 # The one test for a socket-path spec; every listener path must agree on it.
375 62 ▶sub _is_unix_listen {
Subroutine _is_unix_listen: called CC=3 SCAR=11.1
376 62 my ($listen) = @_;
377 62 ▶ return defined($listen) && $listen =~ m{\A[/.]} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $listen && $listen =~ m[\A[/.]] ? : 7 55 62
Condition66%2/3
defined $listen and $listen =~ m[\A[/.]]
!ll&&!rl&&r
0557
MC/DC50%1/2
defined $listen and $listen =~ m[\A[/.]]
defined $listen $listen =~ m[\A[/.]]
Truth table66%2/3
A and B
A= defined $listenB= $listen =~ m[\A[/.]]
ABresult
0X0
100
111
378 }
379  
380 # Blessed coderefs and &{} overloads count too; call_sv() invokes any of them.
381 1 ▶sub _is_callable {
Subroutine _is_callable: called CC=6 SCAR=28.1
382 1 my ($app) = @_;
383 1 ▶ return 0 unless ref $app;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $app 0 1 1
384 1 ▶ return 1 if (reftype($app) || q{}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::reftype($app) || "") eq "CODE" 1 0 1
Condition50%1/2
builtin::reftype($app) || ''
l!l
10
MC/DC0%0/1
builtin::reftype($app) || ''
builtin::reftype($app)
Truth table50%1/2
A
A= builtin::reftype($app)
Aresult
11
00
385 0 ▶ return 1 if blessed($app) && do { require overload;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($app) and do { require overload; overload::Method($app, "&{}") } 0 0 0
Condition0%0/3
builtin::blessed($app) and do { require overload; overload::Method($app, "&{}") }
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($app) and do { require overload; overload::Method($app, "&{}") }
builtin::blessed($app) do { require overload; overload::Method($app, "&{}") }
Truth table0%0/3
A and B
A= builtin::blessed($app)B= do { require overload; overload::Method($app, "&{}") }
ABresult
0X0
100
111
386 0 overload::Method($app, '&{}') };
387 0 return 0;
388 }
389  
390 my $SO_REUSEPORT_WORKS; # probed once: a kernel property
391  
392 13 ▶sub _reuseport_enabled {
Subroutine _reuseport_enabled: called CC=7 SCAR=33.6
393 13 my $self = shift;
394 13 ▶ return 0 unless $self->{reuseport} && $self->{pre_fork} && defined SO_REUSEPORT;
Branch: 1 branch
BranchTrueFalseTotal
unless 13 0 13
Condition40%2/5
$self->{"reuseport"} and $self->{"pre_fork"} and
!ll
130
$self->{"reuseport"} and $self->{"pre_fork"}
!ll&&!rl&&r
1300
MC/DC0%0/2
$self->{"reuseport"} and $self->{"pre_fork"} and
$self->{"reuseport"} $self->{"pre_fork"}
Truth table33%1/3
A and B
A= $self->{"reuseport"}B= $self->{"pre_fork"}
ABresult
0X0
100
111
395 # The constant only proves the headers had it; old kernels fail the setsockopt.
396 0 ▶ $SO_REUSEPORT_WORKS //= do {
Condition0%0/3
$SO_REUSEPORT_WORKS //= do { my $probe; socket($probe, 2, 1, 0) && setsockopt($probe, 1, 15, "\1\0\0\0") ? 1 : 0 }
l!l&&r!l&&!r
000
MC/DC0%0/2
$SO_REUSEPORT_WORKS //= do { my $probe; socket($probe, 2, 1, 0) && setsockopt($probe, 1, 15, "\1\0\0\0") ? 1 : 0 }
$SO_REUSEPORT_WORKS do { my $probe; socket($probe, 2, 1, 0) && setsockopt($probe, 1, 15, "\1\0\0\0") ? 1 : 0 }
Truth table0%0/3
A //= B
A= $SO_REUSEPORT_WORKSB= do { my $probe; socket($probe, 2, 1, 0) && setsockopt($probe, 1, 15, "\1\0\0\0") ? 1 : 0 }
ABresult
1X1
011
000
397 0 my $probe;
398 0 ▶ socket($probe, AF_INET, SOCK_STREAM, 0)
Branch: 1 branch
BranchTrueFalseTotal
socket($probe, 2, 1, 0) && setsockopt($probe, 1, 15, "\cA\000\000\000") ? : 0 0 0
Condition0%0/3
socket $probe, 2, 1, 0 and setsockopt $probe, 1, 15, "\1\0\0\0"
!ll&&!rl&&r
000
MC/DC0%0/2
socket $probe, 2, 1, 0 and setsockopt $probe, 1, 15, "\1\0\0\0"
socket $probe, 2, 1, 0 setsockopt $probe, 1, 15, "\1\0\0\0"
Truth table0%0/3
A and B
A= socket $probe, 2, 1, 0B= setsockopt $probe, 1, 15, "\1\0\0\0"
ABresult
0X0
100
111
399 && setsockopt($probe, SOL_SOCKET, SO_REUSEPORT, pack('i', 1))
400 ? 1 : 0;
401 };
402 0 return $SO_REUSEPORT_WORKS;
403 }
404  
405 13 ▶sub _prepare { ## no critic (ProhibitExcessComplexity)
Subroutine _prepare: called CC=23 SCAR=40.9
406 13 my $self = shift;
407  
408 13 $self->_normalize_listen();
409 13 $self->_validate_pre_fork;
410  
411 13 my $use_reuseport = $self->_reuseport_enabled;
412 13 $self->{_use_reuseport} = $use_reuseport;
413  
414 13 my $f = Feersum->endjinn;
415  
416 # Before use_socket(): each accept watcher captures accept_priority at creation.
417 13 ▶ if (defined $self->{accept_priority}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"accept_priority"}) 1 12 13
418 1 my $val = $self->{accept_priority};
419 1 ▶ croak "accept_priority must be an integer" unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 1 1
420 1 ▶ croak "accept_priority must be between -2 and 2" if $val < PRIORITY_MIN || $val > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $val < -2 or $val > 2 1 0 1
Condition33%1/3
$val < -2 or $val > 2
l!l&&r!l&&!r
010
MC/DC0%0/2
$val < -2 or $val > 2
$val < -2 $val > 2
Truth table33%1/3
A or B
A= $val < -2B= $val > 2
ABresult
1X1
011
000
421 0 $f->accept_priority($val);
422 }
423  
424 12 my @socks;
425 12 for my $listen (@{$self->{_listen_addrs}}) {
426 13 my $sock = $self->_create_socket($listen, $use_reuseport);
427 13 push @socks, $sock;
428 13 $f->use_socket($sock);
429 }
430 12 $self->{sock} = $socks[0]; # backward compat: primary socket
431 12 $self->{_socks} = \@socks;
432  
433 12 for my $prio_name (qw/read_priority write_priority/) {
434 23 my $val = $self->{$prio_name};
435 23 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 19 4 23
436 4 ▶ croak "$prio_name must be an integer" unless $val =~ /^-?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^-?\d+$/ 0 4 4
437 4 ▶ croak "$prio_name must be between -2 and 2" if $val < PRIORITY_MIN || $val > 2;
Branch: 1 branch
BranchTrueFalseTotal
if $val < -2 or $val > 2 2 2 4
Condition100%3/3
$val < -2 or $val > 2
l!l&&r!l&&!r
112
MC/DC100%2/2
$val < -2 or $val > 2
$val < -2 $val > 2
Truth table100%3/3
A or B
A= $val < -2B= $val > 2
ABresult
1X1
011
000
438 }
439 10 ▶ if (defined(my $val = $self->{max_accept_per_loop})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $val = $self->{"max_accept_per_loop"})) 0 10 10
440 0 ▶ croak "max_accept_per_loop must be a positive integer"
Branch: 1 branch
BranchTrueFalseTotal
if not $val =~ /^\d+$/ or $val < 1 0 0 0
Condition0%0/3
not $val =~ /^\d+$/ or $val < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $val =~ /^\d+$/ or $val < 1
not $val =~ /^\d+$/ $val < 1
Truth table0%0/3
not(A) or B
A= not $val =~ /^\d+$/B= $val < 1
ABresult
1X1
011
000
441 if $val !~ /^\d+$/ || $val < 1;
442 }
443 10 ▶ if (defined(my $val = $self->{max_connections})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $val = $self->{"max_connections"})) 0 10 10
444 0 ▶ croak "max_connections must be a non-negative integer"
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^\d+$/ 0 0 0
445 unless $val =~ /^\d+$/;
446 }
447 10 $self->_apply_simple_settings($f, 1); # consume from $self
448  
449 10 $self->_normalize_tls_config;
450  
451 8 ▶ if (my $tls = delete $self->{tls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tls = delete $self->{"tls"}) 2 6 8
452 2 ▶ (-f $tls->{cert_file} && -r _)
Branch: 1 branch
BranchTrueFalseTotal
-f $tls->{"cert_file"} and -r _ or croak(qq[tls cert_file '$tls->{"cert_file"}': not found or not readable]) 0 2 2
Condition33%1/3
-f $tls->{"cert_file"} and -r _
!ll&&!rl&&r
200
MC/DC0%0/2
-f $tls->{"cert_file"} and -r _
-f $tls->{"cert_file"} -r _
Truth table33%1/3
A and B
A= -f $tls->{"cert_file"}B= -r _
ABresult
0X0
100
111
453 or croak "tls cert_file '$tls->{cert_file}': not found or not readable";
454 0 ▶ (-f $tls->{key_file} && -r _)
Branch: 1 branch
BranchTrueFalseTotal
-f $tls->{"key_file"} and -r _ or croak(qq[tls key_file '$tls->{"key_file"}': not found or not readable]) 0 0 0
Condition0%0/3
-f $tls->{"key_file"} and -r _
!ll&&!rl&&r
000
MC/DC0%0/2
-f $tls->{"key_file"} and -r _
-f $tls->{"key_file"} -r _
Truth table0%0/3
A and B
A= -f $tls->{"key_file"}B= -r _
ABresult
0X0
100
111
455 or croak "tls key_file '$tls->{key_file}': not found or not readable";
456  
457 0 ▶ if ($f->has_tls()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->has_tls) { } 0 0 0
458 0 $self->_apply_tls_to_listeners($f, scalar(@socks), $tls, $self->{sni});
459 0 $self->{_tls_config} = $tls; # for reuseport workers
460 0 ▶ $self->{quiet} or warn "Feersum [$$]: TLS enabled on "
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: TLS enabled on " . scalar(@socks) . " listener(s)\n" 0 0 0
461 . scalar(@socks) . " listener(s)\n";
462 } else {
463 0 croak "tls option requires Feersum compiled with TLS support (need picotls submodule + OpenSSL; see Alien::OpenSSL)";
464 }
465 }
466  
467 6 $self->{endjinn} = $f;
468 6 return;
469 }
470  
471 # for overriding:
472 1 ▶sub assign_request_handler {
Subroutine assign_request_handler: called CC=1 SCAR=0.0
Pod documented
473 1 my ($self, $app) = @_;
474 1 $self->{endjinn}->access_log($self->{access_log});
475 1 return $self->{endjinn}->request_handler($app);
476 }
477  
478 3 ▶sub run { ## no critic (ProhibitExcessComplexity)
Subroutine run: called CC=26 SCAR=51.3
Pod documented
479 3 my $self = shift;
480 3 weaken $self;
481  
482 # the hot master's EV watcher still takes USR2 over this
483 3 local $SIG{USR1} = 'IGNORE';
484 3 local $SIG{USR2} = 'IGNORE';
485  
486 3 $self->{running} = 1;
487 3 ▶ my $app = shift || $self->{app};
Condition100%3/3
shift() || $self->{'app'}
l!l&&r!l&&!r
111
MC/DC100%2/2
shift() || $self->{'app'}
shift() $self->{'app'}
Truth table100%3/3
A || B
A= shift()B= $self->{'app'}
ABresult
1X1
011
000
488 3 ▶ $self->{quiet} or warn "Feersum [$$]: starting...\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: starting...\n" 3 0 3
489  
490 3 ▶ if ($self->{hot_restart}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"hot_restart"}) 0 3 3
491 0 ▶ croak "hot_restart requires app_file" unless $self->{app_file};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"app_file"} 0 0 0
492 # Bind before daemonizing so a bind failure reaches the exit status.
493 0 $self->_hot_restart_bind();
494 0 $self->_daemonize_and_write_pid();
495 0 $self->_run_hot_restart_master();
496 # DESTROY at interpreter exit misses embedders that _exit after run().
497 0 $self->_cleanup();
498 0 return;
499 }
500  
501 3 $self->_prepare();
502 3 $self->_daemonize_and_write_pid();
503 3 $self->_drop_privs(); # after bind, before app load
504  
505 # Daemonize + pre_fork gates the ready token on the pool actually coming up.
506 3 my $workers_ok = 1;
507 3 ▶ if ($self->{pre_fork} && defined $self->{preload_app} && !$self->{preload_app}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pre_fork'} and defined $self->{'preload_app'} and not $self->{'preload_app'}) { } 2 1 3
Condition66%4/6
$self->{"pre_fork"} and defined $self->{"preload_app"} and not $self->{"preload_app"}
!ll&&!rl&&r
102
$self->{"pre_fork"} and defined $self->{"preload_app"}
!ll&&!rl&&r
012
MC/DC33%1/3
$self->{"pre_fork"} and defined $self->{"preload_app"} and not $self->{"preload_app"}
$self->{"pre_fork"} defined $self->{"preload_app"} not $self->{"preload_app"}
Truth table50%2/4
A and B and not(C)
A= $self->{"pre_fork"}B= defined $self->{"preload_app"}C= not $self->{"preload_app"}
ABCresult
0XX0
10X0
1100
1111
508 croak "preload_app => 0 requires app_file and no app coderef (a coderef is "
509 . "compiled before the fork and cannot be loaded independently per worker)"
510 2 ▶ if ($app || $self->{app}) && !$self->{app_file};
Branch: 1 branch
BranchTrueFalseTotal
if $app || $self->{"app"} and not $self->{"app_file"} 2 0 2
Condition33%2/6
$app || $self->{"app"} and not $self->{"app_file"}
!ll&&!rl&&r
002
$app or $self->{"app"}
l!l&&r!l&&!r
200
MC/DC0%0/3
$app || $self->{"app"} and not $self->{"app_file"}
$app $self->{"app"} not $self->{"app_file"}
Truth table20%1/5
A or B and not(C)
A= $appB= $self->{"app"}C= not $self->{"app_file"}
ABCresult
00X0
1X00
0100
1X11
0111
511 0 ▶ $self->{_app_loader} = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
512 0 ▶ my $handler = $app || $self->{app};
Condition0%0/3
$app || $self->{'app'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$app || $self->{'app'}
$app $self->{'app'}
Truth table0%0/3
A || B
A= $appB= $self->{'app'}
ABresult
1X1
011
000
513 0 ▶ if (!$handler && $self->{app_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $handler and $self->{"app_file"}) 0 0 0
Condition0%0/3
not $handler and $self->{"app_file"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $handler and $self->{"app_file"}
not $handler $self->{"app_file"}
Truth table0%0/3
not(A) and B
A= not $handlerB= $self->{"app_file"}
ABresult
0X0
100
111
514 0 local ($@, $!) = (undef, undef);
515 0 $handler = do(rel2abs($self->{app_file}));
516 0 ▶ warn "couldn't load $self->{app_file}: " . ($@ || $!)
Branch: 1 branch
BranchTrueFalseTotal
if $@ or not $handler 0 0 0
Condition0%0/6
$@ or not $handler
l!l&&r!l&&!r
000
$@ || $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ or not $handler
$@ not $handler
MC/DC0%0/2
$@ || $!
$@ $!
Truth table0%0/3
A or not(B)
A= $@B= not $handler
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $@B= $!
ABresult
1X1
011
000
517 if $@ || !$handler;
518 }
519 0 ▶ croak "app not defined or failed to compile" unless $handler;
Branch: 1 branch
BranchTrueFalseTotal
unless $handler 0 0 0
520 0 ▶ croak "app must be callable, got " . (ref($handler) || 'a plain scalar')
Branch: 1 branch
BranchTrueFalseTotal
unless _is_callable($handler) 0 0 0
Condition0%0/2
ref $handler || 'a plain scalar'
l!l
00
MC/DC0%0/1
ref $handler || 'a plain scalar'
ref $handler
Truth table0%0/2
A
A= ref $handler
Aresult
11
00
521 unless _is_callable($handler);
522 0 $self->assign_request_handler($handler);
523 0 };
524 0 ▶ $self->{_quit} = EV::signal 'QUIT', sub { $self && $self->quit };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 0 0 0
525 # Unhandled TERM/INT would orphan the workers with the port still bound.
526 0 ▶ $self->{_term} = EV::signal 'TERM', sub { $self && $self->quit };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 0 0 0
527 0 ▶ $self->{_int} = EV::signal 'INT', sub { $self && $self->quit };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 0 0 0
528 # Unhandled HUP would kill the supervisor and orphan the workers.
529 0 ▶ $self->{_hup} = EV::signal 'HUP', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
530 0 warn "Feersum [$$]: SIGHUP ignored (reload requires hot_restart)\n";
531 0 };
532 0 $workers_ok = $self->_start_pre_fork;
533 } else {
534 1 ▶ $app ||= delete $self->{app};
Condition33%1/3
$app ||= delete $self->{"app"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$app ||= delete $self->{"app"}
$app delete $self->{"app"}
Truth table33%1/3
A ||= B
A= $appB= delete $self->{"app"}
ABresult
1X1
011
000
535 1 ▶ if (!$app && $self->{app_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $app and $self->{"app_file"}) 1 0 1
Condition33%1/3
not $app and $self->{"app_file"}
!ll&&!rl&&r
001
MC/DC0%0/2
not $app and $self->{"app_file"}
not $app $self->{"app_file"}
Truth table33%1/3
not(A) and B
A= not $appB= $self->{"app_file"}
ABresult
0X0
100
111
536 1 local ($@, $!) = (undef, undef);
537 1 $app = do(rel2abs($self->{app_file}));
538 1 ▶ warn "couldn't parse $self->{app_file}: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
539 1 ▶ warn "couldn't do $self->{app_file}: $!" if ($! && !defined $app);
Branch: 1 branch
BranchTrueFalseTotal
if $! and not defined $app 0 1 1
Condition33%1/3
$! and not defined $app
!ll&&!rl&&r
100
MC/DC0%0/2
$! and not defined $app
$! not defined $app
Truth table33%1/3
A and not(B)
A= $!B= not defined $app
ABresult
0X0
100
111
540 1 ▶ warn "couldn't run $self->{app_file}: didn't return anything"
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 1 1
541 unless $app;
542 }
543 1 ▶ croak "app not defined or failed to compile" unless $app;
Branch: 1 branch
BranchTrueFalseTotal
unless $app 0 1 1
544 1 ▶ croak "app must be callable, got " . (ref($app) || 'a plain scalar')
Branch: 1 branch
BranchTrueFalseTotal
unless _is_callable($app) 0 1 1
Condition0%0/2
ref $app || 'a plain scalar'
l!l
00
MC/DC0%0/1
ref $app || 'a plain scalar'
ref $app
Truth table0%0/2
A
A= ref $app
Aresult
11
00
545 unless _is_callable($app);
546  
547 1 $self->assign_request_handler($app);
548  
549 1 ▶ $self->{_quit} = EV::signal 'QUIT', sub { $self && $self->quit };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 2 0 2
550 # Unhandled TERM/INT would orphan the workers with the port still bound.
551 0 ▶ $self->{_term} = EV::signal 'TERM', sub { $self && $self->quit };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 0 0 0
552 0 ▶ $self->{_int} = EV::signal 'INT', sub { $self && $self->quit };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
$self and $self->quit 0 0 0
553 # Unhandled HUP would kill the supervisor and orphan the workers.
554 0 ▶ $self->{_hup} = EV::signal 'HUP', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
555 0 warn "Feersum [$$]: SIGHUP ignored (reload requires hot_restart)\n";
556 1 };
557  
558 1 ▶ $workers_ok = $self->_start_pre_fork if $self->{pre_fork};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pre_fork"} 1 0 1
559 }
560 # _exit without the ready token: the daemonize parent sees EOF and fails.
561 1 ▶ if (!$workers_ok && !$self->{_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($workers_ok or $self->{"_shutdown"}) 0 1 1
Condition33%1/3
$workers_ok or $self->{"_shutdown"}
l!l&&r!l&&!r
100
MC/DC0%0/2
$workers_ok or $self->{"_shutdown"}
$workers_ok $self->{"_shutdown"}
Truth table33%1/3
A or B
A= $workers_okB= $self->{"_shutdown"}
ABresult
1X1
011
000
562 0 ▶ warn "Feersum [$$]: workers failed to start\n" unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 0 0 0
563 0 POSIX::_exit(1);
564 }
565 # Everything that can fail at startup has now succeeded.
566 1 $self->_daemon_ready;
567 1 EV::run;
568 1 ▶ $self->{quiet} or warn "Feersum [$$]: done\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: done\n" 1 0 1
569 1 $self->_cleanup();
570 1 return;
571 }
572  
573 # Runs before daemonizing so a bind failure reaches the exit status; idempotent.
574 1 ▶sub _hot_restart_bind {
Subroutine _hot_restart_bind: called CC=3 SCAR=17.8
575 1 my ($self) = @_;
576 1 ▶ return if $self->{_master_socks};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_master_socks"} 0 1 1
577  
578 1 $self->_normalize_listen();
579 # Generation children never run _prepare, so validate and fold here.
580 1 $self->_validate_pre_fork;
581 0 $self->_normalize_tls_config();
582  
583 0 my $use_reuseport = $self->_reuseport_enabled;
584 0 $self->{_use_reuseport} = $use_reuseport;
585 0 my @socks;
586 0 for my $listen (@{$self->{_listen_addrs}}) {
587 0 my $sock = $self->_create_socket($listen, $use_reuseport);
588 0 push @socks, $sock;
589 }
590 0 $self->{_master_socks} = \@socks;
591 0 return;
592 }
593  
594 1 ▶sub _run_hot_restart_master { ## no critic (ProhibitExcessComplexity)
Subroutine _run_hot_restart_master: called CC=20 SCAR=60.1
595 1 my ($self) = @_;
596 1 my $quiet = $self->{quiet};
597  
598 1 ▶ $quiet or warn "Feersum [$$]: hot restart master starting\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: hot restart master starting\n" 1 0 1
599  
600 1 $self->_hot_restart_bind();
601 0 my @socks = @{ $self->{_master_socks} };
602  
603 0 $self->_drop_privs(); # after bind, before app load
604 # Read after the drop: _verify_reuseport_bind may have turned it off.
605 0 my $use_reuseport = $self->{_use_reuseport};
606  
607 0 my $gen = 0;
608 0 my $current_pid;
609 my $pending_pid; # generation being started (not yet $current_pid)
610 0 my $shutting_down = 0;
611 0 ▶ my $startup_timeout = $self->{startup_timeout} // DEFAULT_STARTUP_TIMEOUT;
Condition0%0/2
$self->{"startup_timeout"} // 10
l!l
00
MC/DC0%0/1
$self->{"startup_timeout"} // 10
$self->{"startup_timeout"}
Truth table0%0/2
A
A= $self->{"startup_timeout"}
Aresult
11
00
612  
613 # Declared here so the child can undef them: EV watchers survive fork.
614 0 my ($hup, $quit, $int, $term, $reap, $usr2, $master_death);
615 # Fed by a persistent USR2 watcher; a late USR2 with no handler kills the master.
616 0 my $gen_ready = 0;
617 # Restart backoff for a crashed generation.
618 0 my ($gen_ready_at, $gen_restart_fails, $gen_restart_timer);
619 0 my %retiring;
620 my $drain_deadline = ($self->{graceful_timeout}
621 // $ENV{FEERSUM_GRACEFUL_TIMEOUT}
622 0 ▶ // DEATH_TIMER) + 2 * DEATH_TIMER_INCR;
Condition0%0/5
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
l!l
00
$self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
$self->{"graceful_timeout"} $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
Truth table0%0/3
A // B
A= $self->{"graceful_timeout"}B= $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
ABresult
1X1
011
000
623  
624 # Only the master holds the write end, so a generation sees EOF when it dies.
625 0 ▶ pipe(my $master_alive_r, my $master_alive_w)
Branch: 1 branch
BranchTrueFalseTotal
pipe my $master_alive_r, my $master_alive_w or croak("master liveness pipe: $!") 0 0 0
626 or croak "master liveness pipe: $!";
627  
628 0 ▶ my $fork_generation = sub {
Subroutine __ANON__: not called CC=33 SCAR=70.2
629 0 $gen++;
630 0 my $pid = fork;
631 0 ▶ croak "fork generation: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
632  
633 0 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 0 0
634 # === Generation child ===
635 # An exception here would unwind into the caller's script in the child.
636 eval {
637 0 EV::default_loop()->loop_fork;
638 # Drop the master's watchers before any loop iteration here.
639 0 undef $hup; undef $quit; undef $int; undef $term; undef $reap;
640 0 undef $usr2; undef $master_death; undef $gen_restart_timer;
641 0 %retiring = (); # inherited master deadlines must not run in a generation
642 # Inherited daemonize pipe end; held, it hides a master death from the parent.
643 0 ▶ if (my $rdy = delete $self->{_daemon_ready_fh}) { close $rdy }
Branch: 1 branch
BranchTrueFalseTotal
if (my $rdy = delete $self->{"_daemon_ready_fh"}) 0 0 0
644 0 ▶ $quiet or warn "Feersum [$$]: gen $gen loading app\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: gen $gen loading app\n" 0 0 0
645  
646 0 my $f = Feersum->endjinn;
647 # Before use_socket, as in _prepare.
648 0 ▶ if (defined $self->{accept_priority}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"accept_priority"}) 0 0 0
649 0 $f->accept_priority($self->{accept_priority});
650 }
651 0 ▶ if ($use_reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_reuseport) { } 0 0 0
652 # A reuseport socket nobody accepts on black-holes connections; close
653 # the TCP ones. UNIX listeners stay: workers inherit, not rebind.
654 0 ▶ my $addrs = $self->{_listen_addrs} || [];
Condition0%0/2
$self->{'_listen_addrs'} || []
l!l
00
MC/DC0%0/1
$self->{'_listen_addrs'} || []
$self->{'_listen_addrs'}
Truth table0%0/2
A
A= $self->{'_listen_addrs'}
Aresult
11
00
655 0 my @keep;
656 0 for my $i (0 .. $#socks) {
657 0 ▶ if (_is_unix_listen($addrs->[$i])) { $keep[$i] = $socks[$i]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unix_listen($addrs->[$i])) 0 0 0
658 0 ▶ close($socks[$i])
Branch: 1 branch
BranchTrueFalseTotal
if defined $socks[$i] 0 0 0
Condition0%0/5
defined $socks[$i] && (close $socks[$i] || warn("Feersum [${$}]: close inherited socket: $!\n"))
!ll&&!rl&&r
000
close $socks[$i] or warn "Feersum [${$}]: close inherited socket: $!\n"
l!l
00
MC/DC0%0/2
defined $socks[$i] && (close $socks[$i] || warn("Feersum [${$}]: close inherited socket: $!\n"))
defined $socks[$i] close $socks[$i]
Truth table0%0/3
A && B
A= defined $socks[$i]B= close $socks[$i]
ABresult
0X0
100
111
659 or warn "Feersum [$$]: close inherited socket: $!\n"
660 if defined $socks[$i];
661 }
662 0 @socks = @keep;
663 0 $self->{_master_socks} = [];
664 0 $self->{_socks} = \@keep;
665 0 ($self->{sock}) = grep { defined } @keep;
666 }
667 else {
668 0 for my $sock (@socks) {
669 0 $f->use_socket($sock);
670 }
671 0 $self->{_socks} = \@socks;
672 0 $self->{sock} = $socks[0];
673 }
674  
675 0 $self->_apply_settings($f);
676  
677 0 my $app_file = rel2abs($self->{app_file});
678 0 local ($@, $!) = (undef, undef);
679 0 my $app = do $app_file;
680 0 ▶ if ($@ || !$app || !_is_callable($app)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ or not $app or not _is_callable($app)) 0 0 0
Condition0%0/6
$@ or not $app or not _is_callable($app)
l!l&&r!l&&!r
000
$@ or not $app
l!l&&r!l&&!r
000
MC/DC0%0/3
$@ or not $app or not _is_callable($app)
$@ not $app not _is_callable($app)
Truth table0%0/4
A or not(B) or not(C)
A= $@B= not $appC= not _is_callable($app)
ABCresult
1XX1
01X1
0011
0000
681 0 ▶ warn "Feersum [$$]: gen $gen: failed to load $app_file: "
Condition0%0/5
$@ || $! || 'not a coderef'
l!l
00
$@ or $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $! || 'not a coderef'
$@ $!
Truth table0%0/3
A or B
A= $@B= $!
ABresult
1X1
011
000
682 . ($@ || $! || "not a coderef") . "\n";
683 0 POSIX::_exit(1);
684 }
685  
686 0 $self->{endjinn} = $f;
687 0 $self->assign_request_handler($app);
688  
689 0 my ($quit_w, $term_w, $int_w, $hup_w, $death_w);
690 0 ▶ my $begin_gen_shutdown = sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
691 0 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 0 0 0
692 0 $self->{_shutdown} = 1;
693 my $gt = $self->{graceful_timeout}
694 // $ENV{FEERSUM_GRACEFUL_TIMEOUT}
695 0 ▶ // DEATH_TIMER;
Condition0%0/5
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
l!l
00
$self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
$self->{"graceful_timeout"} $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
Truth table0%0/3
A // B
A= $self->{"graceful_timeout"}B= $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
ABresult
1X1
011
000
696 0 ▶ if ($self->{_n_kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_n_kids'}) { } 0 0 0
697 # Group QUIT includes self (guarded above); the reaper ends the loop.
698 # graceful_shutdown here would _exit(0) before the workers drain.
699 0 kill POSIX::SIGQUIT, -$$;
700 0 $gt += DEATH_TIMER_INCR; # outlast the workers' deadline
701 }
702 else {
703 # A croak here (already draining a retirement) would skip arming $death_w.
704 0 ▶ my $ok = eval { $f->graceful_shutdown(sub { POSIX::_exit(0) }); 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
705 0 ▶ $self->{quiet} or $ok
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or $ok or warn "Feersum [${$}]: gen ${gen}: already draining, " . "forcing exit in ${gt}s\n" 0 0 0
Condition0%0/3
$self->{"quiet"} or $ok
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"quiet"} or $ok
$self->{"quiet"} $ok
Truth table0%0/3
A or B
A= $self->{"quiet"}B= $ok
ABresult
1X1
011
000
706 or warn "Feersum [$$]: gen $gen: already draining, "
707 . "forcing exit in ${gt}s\n";
708 }
709 0 ▶ $death_w = EV::timer($gt, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
710 # Exiting alone leaves orphaned workers holding the listen socket.
711 # The pid file is the master's; do not remove it here.
712 0 ▶ my @alive = grep { defined } @{ $self->{_kid_pids} || [] };
Condition0%0/2
$self->{'_kid_pids'} || []
l!l
00
MC/DC0%0/1
$self->{'_kid_pids'} || []
$self->{'_kid_pids'}
Truth table0%0/2
A
A= $self->{'_kid_pids'}
Aresult
11
00
713 0 ▶ kill 'KILL', @alive if @alive;
Branch: 1 branch
BranchTrueFalseTotal
if @alive 0 0 0
714 0 POSIX::_exit(1);
715 0 });
716 0 };
717 # systemd delivers TERM straight here; unhandled it would skip the drain.
718 0 $quit_w = EV::signal 'QUIT', $begin_gen_shutdown;
719 0 $term_w = EV::signal 'TERM', $begin_gen_shutdown;
720 0 $int_w = EV::signal 'INT', $begin_gen_shutdown;
721 # A group or mis-aimed HUP would otherwise kill the generation.
722 0 ▶ $hup_w = EV::signal 'HUP', sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
723 0 ▶ $quiet or warn "Feersum [$$]: gen $gen ignoring SIGHUP "
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: gen $gen ignoring SIGHUP " . "(reload is the master's job)\n" 0 0 0
724 . "(reload is the master's job)\n";
725 0 };
726  
727 # Close our write-end copy so only the master holds one. The watcher
728 # clears itself: a level-triggered EV::io on a dead pipe busy-spins.
729 0 close $master_alive_w;
730 0 my $master_gone_watch;
731 0 ▶ $master_gone_watch = EV::io($master_alive_r, EV::READ, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
732 0 $master_gone_watch = undef;
733 0 ▶ $quiet or warn "Feersum [$$]: master is gone, draining\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: master is gone, draining\n" 0 0 0
734 0 $begin_gen_shutdown->();
735 0 });
736  
737 0 ▶ if ($self->{pre_fork}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pre_fork"}) 0 0 0
738 0 $f->set_multiprocess(1);
739 # The master's post-privdrop decision, not a recompute from config.
740 0 $self->{_use_reuseport} = $use_reuseport;
741 # setsid() returns -1 (true) on failure; EPERM means already a session leader.
742 0 ▶ POSIX::setsid() == SYSCALL_ERROR and $! != POSIX::EPERM()
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() == -1 and $! != 1 and warn "Feersum [${$}]: setsid failed: $!\n" 0 0 0
Condition0%0/3
POSIX::setsid() == -1 and $! != 1
!ll&&!rl&&r
000
MC/DC0%0/2
POSIX::setsid() == -1 and $! != 1
POSIX::setsid() == -1 $! != 1
Truth table0%0/3
A and B
A= POSIX::setsid() == -1B= $! != 1
ABresult
0X0
100
111
743 and warn "Feersum [$$]: setsid failed: $!\n";
744 0 $self->{_kids} = [];
745 0 $self->{_kid_pids} = []; # inherited: another generation's
746 0 $self->{_n_kids} = 0;
747 0 ▶ pipe(my $wready_r, my $wready_w)
Branch: 1 branch
BranchTrueFalseTotal
pipe my $wready_r, my $wready_w or croak("worker readiness pipe: $!") 0 0 0
748 or croak "worker readiness pipe: $!";
749 0 $self->{_worker_ready_pipe} = [$wready_r, $wready_w];
750 0 $self->_fork_another($_) for (1 .. $self->{pre_fork});
751 0 $f->unlisten(); # parent of workers doesn't accept
752 # Gate on live workers: a failed reload must not retire the old generation.
753 my $up = $self->_wait_for_workers_ready(
754 0 $self->{pre_fork}, $startup_timeout);
755 0 ▶ if ($up < $self->{pre_fork} && !$self->{_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($up < $self->{"pre_fork"} and not $self->{"_shutdown"}) 0 0 0
Condition0%0/3
$up < $self->{"pre_fork"} and not $self->{"_shutdown"}
!ll&&!rl&&r
000
MC/DC0%0/2
$up < $self->{"pre_fork"} and not $self->{"_shutdown"}
$up < $self->{"pre_fork"} not $self->{"_shutdown"}
Truth table0%0/3
A and not(B)
A= $up < $self->{"pre_fork"}B= not $self->{"_shutdown"}
ABresult
0X0
100
111
756 0 warn "Feersum [$$]: gen $gen: only $up of "
757 . "$self->{pre_fork} workers came up; failing\n";
758 0 POSIX::_exit(1);
759 }
760 }
761  
762 0 ▶ if (!$self->{pre_fork}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"pre_fork"}) 0 0 0
763 0 ▶ if (my $cb = $self->{after_fork}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"after_fork"}) 0 0 0
764 0 ▶ unless (eval { $cb->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &$cb(); 1 } }) 0 0 0
765 0 warn "Feersum [$$]: after_fork failed: $@";
766 0 POSIX::_exit(1);
767 }
768 }
769 0 $self->_set_max_requests_per_worker($f);
770 }
771  
772 0 ▶ kill 'USR2', getppid() unless $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_shutdown"} 0 0 0
773  
774 $quiet or warn "Feersum [$$]: gen $gen ready"
775 0 ▶ . ($self->{pre_fork} ? " ($self->{pre_fork} workers)" : "") . "\n";
Branch: 2 branches
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: gen $gen ready" . ($self->{"pre_fork"} ? qq[ ($self->{"pre_fork"} workers)] : "") . "\n" 0 0 0
$self->{'pre_fork'} ? : 0 0 0
776 0 EV::run;
777 0 1;
778 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &EV::default_loop()->loop_fork; undef $hup; undef $quit; undef $int; undef $term; undef $reap; undef $usr2; undef $master_death; undef $gen_restart_timer; %retiring = (); if (my $rdy = delete $self->{"_daemon_ready_fh"}) { close $rdy; } ; $quiet or warn "Feersum [${$}]: gen $gen loading app\n"; my $f = "Feersum"->endjinn; if (defined $self->{"accept_priority"}) { $f->accept_priority($self->{"accept_priority"}); } ; if ($use_reuseport) { my $addrs = $self->{"_listen_addrs"} || []; my @keep; foreach my $i (0 .. $#socks) { if (_is_unix_listen($addrs->[$i])) { $keep[$i] = $socks[$i]; next; } ; close $socks[$i] or warn "Feersum [${$}]: close inherited socket: $!\n" if defined $socks[$i]; } ; @socks = @keep; $self->{"_master_socks"} = []; $self->{"_socks"} = \@keep; ($self->{"sock"}) = grep({defined $_;} @keep); } else { foreach my $sock (@socks) { $f->use_socket($sock); } ; $self->{"_socks"} = \@socks; $self->{"sock"} = $socks[0]; } ; $self->_apply_settings($f); my $app_file = rel2abs($self->{"app_file"}); local($@, $!) = (undef, undef); my $app = do $app_file; if ($@ or not $app or not _is_callable($app)) { warn "Feersum [${$}]: gen ${gen}: failed to load ${app_file}: " . ($@ || $! || "not a coderef") . "\n"; POSIX::_exit(1); } ; $self->{"endjinn"} = $f; $self->assign_request_handler($app); my($quit_w, $term_w, $int_w, $hup_w, $death_w); my $begin_gen_shutdown = sub { return if $self->{"_shutdown"}; $self->{"_shutdown"} = 1; my $gt = ($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5; if ($self->{"_n_kids"}) { kill 3, -$$; $gt += 2; } else { my $ok = eval { do { $f->graceful_shutdown(sub { POSIX::_exit(0); } ); 1 } }; $self->{"quiet"} or $ok or warn "Feersum [${$}]: gen ${gen}: already draining, " . "forcing exit in ${gt}s\n"; } ; $death_w = &EV::timer($gt, 0, sub { my(@alive) = grep({defined $_;} @{$self->{"_kid_pids"} or [];}); kill "KILL", @alive if @alive; POSIX::_exit(1); } ); } ; $quit_w = &EV::signal("QUIT", $begin_gen_shutdown); $term_w = &EV::signal("TERM", $begin_gen_shutdown); $int_w = &EV::signal("INT", $begin_gen_shutdown); $hup_w = &EV::signal("HUP", sub { $quiet or warn "Feersum [${$}]: gen $gen ignoring SIGHUP " . "(reload is the master's job)\n"; } ); close $master_alive_w; my $master_gone_watch; $master_gone_watch = &EV::io($master_alive_r, 1, sub { $master_gone_watch = undef; $quiet or warn "Feersum [${$}]: master is gone, draining\n"; &$begin_gen_shutdown(); } ); if ($self->{"pre_fork"}) { $f->set_multiprocess(1); $self->{"_use_reuseport"} = $use_reuseport; POSIX::setsid() == -1 and $! != 1 and warn "Feersum [${$}]: setsid failed: $!\n"; $self->{"_kids"} = []; $self->{"_kid_pids"} = []; $self->{"_n_kids"} = 0; pipe my $wready_r, my $wready_w or croak("worker readiness pipe: $!"); $self->{"_worker_ready_pipe"} = [$wready_r, $wready_w]; $self->_fork_another($_) foreach (1 .. $self->{"pre_fork"}); $f->unlisten; my $up = $self->_wait_for_workers_ready($self->{"pre_fork"}, $startup_timeout); if ($up < $self->{"pre_fork"} and not $self->{"_shutdown"}) { warn "Feersum [${$}]: gen ${gen}: only $up of " . qq[$self->{"pre_fork"} workers came up; failing\n]; POSIX::_exit(1); } ; } ; unless ($self->{"pre_fork"}) { if (my $cb = $self->{"after_fork"}) { unless (eval { do { &$cb(); 1 } }) { warn "Feersum [${$}]: after_fork failed: $@"; POSIX::_exit(1); } ; } ; $self->_set_max_requests_per_worker($f); } ; kill "USR2", getppid unless $self->{"_shutdown"}; $quiet or warn "Feersum [${$}]: gen $gen ready" . ($self->{"pre_fork"} ? qq[ ($self->{"pre_fork"} workers)] : "") . "\n"; &EV::run(); 1 } } or do { my $err = $@ || "unknown error"; warn "Feersum [${$}]: gen $gen failed to start: $err"; POSIX::_exit(1) } 0 0 0
779 0 ▶ my $err = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
780 0 warn "Feersum [$$]: gen $gen failed to start: $err";
781 0 POSIX::_exit(1);
782 };
783 0 POSIX::_exit(0);
784 }
785  
786 0 return $pid;
787 0 };
788  
789 0 ▶ my $begin_shutdown = sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
790 0 my ($why) = @_;
791 0 ▶ return if $shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down 0 0 0
792 0 $shutting_down = 1;
793 0 $gen_restart_timer = undef;
794 0 ▶ $quiet or warn "Feersum [$$]: master $why\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: master $why\n" 0 0 0
795 0 ▶ kill 'QUIT', $current_pid if $current_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $current_pid 0 0 0
796 0 ▶ kill 'QUIT', $pending_pid if $pending_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pending_pid 0 0 0
797 0 ▶ kill 'QUIT', keys %retiring if %retiring;
Branch: 1 branch
BranchTrueFalseTotal
if %retiring 0 0 0
798 # With nothing to reap the reap watcher never breaks the loop; break here.
799 0 ▶ EV::break unless $current_pid || $pending_pid || %retiring;
Branch: 1 branch
BranchTrueFalseTotal
unless $current_pid or $pending_pid or %retiring 0 0 0
Condition0%0/6
$current_pid or $pending_pid or %retiring
l!l&&r!l&&!r
000
$current_pid or $pending_pid
l!l&&r!l&&!r
000
MC/DC0%0/3
$current_pid or $pending_pid or %retiring
$current_pid $pending_pid %retiring
Truth table0%0/4
A or B or C
A= $current_pidB= $pending_pidC= %retiring
ABCresult
1XX1
01X1
0011
0000
800  
801 # Backstop for a generation wedged in app code, whose own deadline never
802 # runs; set later than that deadline so the graceful path goes first.
803 0 ▶ $master_death = EV::timer $drain_deadline, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
804 0 ▶ $quiet or warn "Feersum [$$]: master shutdown timed out, forcing\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: master shutdown timed out, forcing\n" 0 0 0
805 0 for my $p (grep { $_ } $current_pid, $pending_pid, keys %retiring) {
806 0 kill 'KILL', $p;
807 # A pre-fork generation setsid()s; its workers are reachable only by group.
808 0 kill 'KILL', -$p;
809 }
810 0 $self->_remove_pid_file;
811 0 POSIX::_exit(1);
812 0 };
813 0 };
814  
815 # EV swallows a croak inside a watcher callback; return undef instead.
816 0 ▶ my $try_fork_generation = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
817 0 my $pid = eval { $fork_generation->() };
818 0 ▶ return $pid if $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pid 0 0 0
819 0 ▶ my $why = $@ || $!;
Condition0%0/3
$@ || $!
l!l&&r!l&&!r
000
MC/DC0%0/2
$@ || $!
$@ $!
Truth table0%0/3
A || B
A= $@B= $!
ABresult
1X1
011
000
820 0 $why =~ s/\s+\z//;
821 0 warn "Feersum [$$]: could not fork a generation: $why\n";
822 0 return;
823 0 };
824  
825 # Before generation 1 forks, or a QUIT during startup kills the master.
826 0 ▶ $hup = EV::signal 'HUP', sub {
Subroutine __ANON__: not called CC=18 SCAR=58.3
827 0 ▶ return if $shutting_down || $pending_pid; # debounce rapid HUPs
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down or $pending_pid 0 0 0
Condition0%0/3
$shutting_down or $pending_pid
l!l&&r!l&&!r
000
MC/DC0%0/2
$shutting_down or $pending_pid
$shutting_down $pending_pid
Truth table0%0/3
A or B
A= $shutting_downB= $pending_pid
ABresult
1X1
011
000
828 0 ▶ $quiet or warn "Feersum [$$]: HUP - spawning gen " . ($gen + 1) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: HUP - spawning gen " . ($gen + 1) . "\n" 0 0 0
829 0 $gen_restart_timer = undef;
830 0 $gen_restart_fails = 0;
831  
832 0 my $old_pid = $current_pid;
833 # The old generation is still serving, so a failed reload is survivable.
834 0 ▶ $pending_pid = $try_fork_generation->() or return;
Branch: 1 branch
BranchTrueFalseTotal
$pending_pid = &$try_fork_generation() or return 0 0 0
835  
836 0 ▶ if (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready,
Branch: 1 branch
BranchTrueFalseTotal
if (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready, \$shutting_down, $startup_timeout)) { } 0 0 0
837 \$shutting_down, $startup_timeout)) {
838 0 ▶ $quiet or warn "Feersum [$$]: gen $gen ready (pid $pending_pid), retiring old (pid "
Branch: 2 branches
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: gen $gen ready (pid $pending_pid), retiring old (pid " . (defined $old_pid ? $old_pid : "none") . ")\n" 0 0 0
defined $old_pid ? : 0 0 0
839 . (defined $old_pid ? $old_pid : q{none}) . ")\n";
840 # $current_pid may have been reaped and reused during the reload.
841 0 ▶ my $retire = ($current_pid && $current_pid == $old_pid) ? $old_pid : undef;
Branch: 1 branch
BranchTrueFalseTotal
$current_pid && $current_pid == $old_pid ? : 0 0 0
Condition0%0/3
$current_pid and $current_pid == $old_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$current_pid and $current_pid == $old_pid
$current_pid $current_pid == $old_pid
Truth table0%0/3
A and B
A= $current_pidB= $current_pid == $old_pid
ABresult
0X0
100
111
842 0 $current_pid = $pending_pid;
843 0 $pending_pid = undef;
844 0 $gen_ready_at = EV::time();
845 0 ▶ if ($retire) {
Branch: 1 branch
BranchTrueFalseTotal
if ($retire) 0 0 0
846 0 ▶ $retiring{$retire} = EV::timer($drain_deadline, 0, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
847 0 $retiring{$retire} = undef;
848 0 ▶ $quiet or warn "Feersum [$$]: retired generation $retire "
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: retired generation $retire " . "drain timed out, forcing\n" 0 0 0
849 . "drain timed out, forcing\n";
850 0 kill 'KILL', $retire;
851 0 kill 'KILL', -$retire;
852 0 });
853 0 kill 'QUIT', $retire;
854 }
855 } else {
856 0 ▶ kill 'KILL', $pending_pid if kill(0, $pending_pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pending_pid 0 0 0
857 0 waitpid($pending_pid, 0);
858 0 $pending_pid = undef;
859 # The reaper may have cleared $current_pid mid-reload without restarting.
860 0 ▶ if ($current_pid) {
Branch: 2 branches
BranchTrueFalseTotal
if ($current_pid) { } 0 0 0
elsif ($shutting_down) { } 0 0 0
861 0 warn "Feersum [$$]: gen $gen failed, keeping old (pid $current_pid)\n";
862 }
863 elsif ($shutting_down) {
864 0 ▶ EV::break unless %retiring;
Branch: 1 branch
BranchTrueFalseTotal
unless %retiring 0 0 0
865 }
866 else {
867 0 warn "Feersum [$$]: gen $gen failed and the running generation "
868 . "died during the reload - starting a replacement\n";
869 # Nothing is serving, so a failed fork is terminal.
870 0 $pending_pid = $try_fork_generation->();
871 0 ▶ if (!$pending_pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $pending_pid) { } 0 0 0
elsif (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready, \$shutting_down, $startup_timeout)) { } 0 0 0
872 0 EV::break;
873 }
874 elsif (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready,
875 \$shutting_down, $startup_timeout)) {
876 0 $current_pid = $pending_pid;
877 } else {
878 0 warn "Feersum [$$]: replacement generation also failed, "
879 . "giving up\n";
880 0 ▶ kill 'KILL', $pending_pid if kill(0, $pending_pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pending_pid 0 0 0
881 0 waitpid($pending_pid, 0);
882 0 EV::break;
883 }
884 0 $pending_pid = undef;
885 }
886 }
887 0 };
888  
889 0 ▶ $quit = EV::signal 'QUIT', sub { $begin_shutdown->('shutting down') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
890 0 ▶ $int = EV::signal 'INT', sub { $begin_shutdown->('interrupted') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
891 # systemctl stop sends TERM; unhandled it orphans the generation.
892 0 ▶ $term = EV::signal 'TERM', sub { $begin_shutdown->('terminated') };
Subroutine __ANON__: not called CC=1 SCAR=6.9
893 0 ▶ $usr2 = EV::signal 'USR2', sub { $gen_ready = 1 };
Subroutine __ANON__: not called CC=1 SCAR=6.9
894  
895 0 ▶ my $restart_generation = sub {
Subroutine __ANON__: not called CC=8 SCAR=42.8
896 0 ▶ return if $shutting_down || $pending_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down or $pending_pid 0 0 0
Condition0%0/3
$shutting_down or $pending_pid
l!l&&r!l&&!r
000
MC/DC0%0/2
$shutting_down or $pending_pid
$shutting_down $pending_pid
Truth table0%0/3
A or B
A= $shutting_downB= $pending_pid
ABresult
1X1
011
000
897 0 ▶ warn "Feersum [$$]: active generation died, restarting\n" unless $quiet;
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 0 0 0
898 0 $pending_pid = $try_fork_generation->();
899 0 ▶ if (!$pending_pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $pending_pid) { } 0 0 0
elsif (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready, \$shutting_down, $startup_timeout)) { } 0 0 0
900 0 EV::break;
901 }
902 elsif (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready,
903 \$shutting_down, $startup_timeout)) {
904 0 $current_pid = $pending_pid;
905 0 $gen_ready_at = EV::time();
906 } else {
907 0 ▶ warn "Feersum [$$]: replacement generation also failed, giving up\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 0 0 0
908 unless $quiet;
909 0 ▶ kill 'KILL', $pending_pid if kill(0, $pending_pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pending_pid 0 0 0
910 0 waitpid($pending_pid, 0);
911 0 EV::break;
912 }
913 0 $pending_pid = undef;
914 0 };
915  
916 0 ▶ $reap = EV::child 0, 0, sub {
Subroutine __ANON__: not called CC=25 SCAR=64.8
917 0 my $kid = $_[0]->rpid;
918 0 my $rstatus = $_[0]->rstatus;
919 0 ▶ my $how = ($rstatus & WSTATUS_SIGNAL_MASK)
Branch: 1 branch
BranchTrueFalseTotal
$rstatus & 127 ? : 0 0 0
920 ? "killed by signal " . ($rstatus & WSTATUS_SIGNAL_MASK)
921 : "exited (" . ($rstatus >> WSTATUS_EXIT_SHIFT) . ")";
922 0 ▶ $quiet or warn "Feersum [$$]: child $kid $how\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: child $kid $how\n" 0 0 0
923 # _wait_for_ready owns the pending generation.
924 0 ▶ return if $pending_pid && $kid == $pending_pid;
Branch: 1 branch
BranchTrueFalseTotal
if $pending_pid and $kid == $pending_pid 0 0 0
Condition0%0/3
$pending_pid and $kid == $pending_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$pending_pid and $kid == $pending_pid
$pending_pid $kid == $pending_pid
Truth table0%0/3
A and B
A= $pending_pidB= $kid == $pending_pid
ABresult
0X0
100
111
925 0 ▶ if (exists $retiring{$kid}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $retiring{$kid}) 0 0 0
926 0 delete $retiring{$kid};
927 0 ▶ EV::break if $shutting_down && !$current_pid && !$pending_pid && !%retiring;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down and not $current_pid and not $pending_pid and not %retiring 0 0 0
Condition0%0/9
$shutting_down and not $current_pid and not $pending_pid and not %retiring
!ll&&!rl&&r
000
$shutting_down and not $current_pid and not $pending_pid
!ll&&!rl&&r
000
$shutting_down and not $current_pid
!ll&&!rl&&r
000
MC/DC0%0/4
$shutting_down and not $current_pid and not $pending_pid and not %retiring
$shutting_down not $current_pid not $pending_pid not %retiring
Truth table0%0/5
A and not(B) and not(C) and not(D)
A= $shutting_downB= not $current_pidC= not $pending_pidD= not %retiring
ABCDresult
0XXX0
10XX0
110X0
11100
11111
928 0 return;
929 }
930 0 ▶ if ($current_pid && $kid == $current_pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_pid and $kid == $current_pid) 0 0 0
Condition0%0/3
$current_pid and $kid == $current_pid
!ll&&!rl&&r
000
MC/DC0%0/2
$current_pid and $kid == $current_pid
$current_pid $kid == $current_pid
Truth table0%0/3
A and B
A= $current_pidB= $kid == $current_pid
ABresult
0X0
100
111
931 0 $current_pid = undef;
932 0 ▶ EV::break if $shutting_down && !$pending_pid && !%retiring;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down and not $pending_pid and not %retiring 0 0 0
Condition0%0/6
$shutting_down and not $pending_pid and not %retiring
!ll&&!rl&&r
000
$shutting_down and not $pending_pid
!ll&&!rl&&r
000
MC/DC0%0/3
$shutting_down and not $pending_pid and not %retiring
$shutting_down not $pending_pid not %retiring
Truth table0%0/4
A and not(B) and not(C)
A= $shutting_downB= not $pending_pidC= not %retiring
ABCresult
0XX0
10X0
1100
1111
933 0 ▶ unless ($shutting_down || $pending_pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($shutting_down or $pending_pid) 0 0 0
Condition0%0/3
$shutting_down or $pending_pid
l!l&&r!l&&!r
000
MC/DC0%0/2
$shutting_down or $pending_pid
$shutting_down $pending_pid
Truth table0%0/3
A or B
A= $shutting_downB= $pending_pid
ABresult
1X1
011
000
934 0 ▶ my $retired = ($rstatus & WSTATUS_SIGNAL_MASK) == 0
Condition0%0/3
($rstatus & 127) == 0 && $rstatus >> 8 == 42
!ll&&!rl&&r
000
MC/DC0%0/2
($rstatus & 127) == 0 && $rstatus >> 8 == 42
($rstatus & 127) == 0 $rstatus >> 8 == 42
Truth table0%0/3
A && B
A= ($rstatus & 127) == 0B= $rstatus >> 8 == 42
ABresult
0X0
100
111
935 && ($rstatus >> WSTATUS_EXIT_SHIFT) == EXIT_RETIRED;
936 0 ▶ my $lifetime = defined $gen_ready_at
Branch: 1 branch
BranchTrueFalseTotal
defined $gen_ready_at ? : 0 0 0
937 ? EV::time() - $gen_ready_at : undef;
938 0 ▶ if (!$retired && defined $lifetime && $lifetime < RESPAWN_INSTANT_DEATH) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retired and defined $lifetime and $lifetime < 1.0) { } 0 0 0
Condition0%0/6
not $retired and defined $lifetime and $lifetime < 1
!ll&&!rl&&r
000
not $retired and defined $lifetime
!ll&&!rl&&r
000
MC/DC0%0/3
not $retired and defined $lifetime and $lifetime < 1
not $retired defined $lifetime $lifetime < 1
Truth table0%0/4
not(A) and B and C
A= not $retiredB= defined $lifetimeC= $lifetime < 1
ABCresult
0XX0
10X0
1100
1111
939 0 my $n = ++$gen_restart_fails;
940 0 ▶ my $delay = $n > RESPAWN_MAX_FAILS ? RESPAWN_BACKOFF_MAX
Branch: 1 branch
BranchTrueFalseTotal
$n > 10 ? : 0 0 0
941 : RESPAWN_BACKOFF_BASE * (2 ** ($n - 1));
942 0 ▶ $delay = RESPAWN_BACKOFF_MAX if $delay > RESPAWN_BACKOFF_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 30 0 0 0
943 0 ▶ warn sprintf "Feersum [%d]: active generation died after "
Branch: 1 branch
BranchTrueFalseTotal
unless $quiet 0 0 0
944 . "%.2fs, restarting in %gs (failure %d)\n",
945 $$, $lifetime, $delay, $n unless $quiet;
946 0 ▶ $gen_restart_timer = EV::timer($delay, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
947 0 $gen_restart_timer = undef;
948 0 $restart_generation->();
949 0 });
950 }
951 else {
952 0 $gen_restart_fails = 0;
953 0 $restart_generation->();
954 }
955 }
956 }
957 0 };
958  
959 0 $pending_pid = $fork_generation->();
960  
961 # Held open, the master's copies black-hole a share of connections and,
962 # root-owned, keep privdropped workers out of the reuseport group.
963 0 ▶ if ($use_reuseport) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_reuseport) 0 0 0
964 # UNIX listeners stay: workers inherit them rather than rebinding.
965 0 ▶ my $addrs = $self->{_listen_addrs} || [];
Condition0%0/2
$self->{'_listen_addrs'} || []
l!l
00
MC/DC0%0/1
$self->{'_listen_addrs'} || []
$self->{'_listen_addrs'}
Truth table0%0/2
A
A= $self->{'_listen_addrs'}
Aresult
11
00
966 0 my @keep;
967 0 for my $i (0 .. $#socks) {
968 0 ▶ if (_is_unix_listen($addrs->[$i])) { $keep[$i] = $socks[$i]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unix_listen($addrs->[$i])) 0 0 0
969 0 ▶ close($socks[$i]) or warn "Feersum [$$]: close master socket: $!\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $socks[$i] 0 0 0
Condition0%0/5
defined $socks[$i] && (close $socks[$i] || warn("Feersum [${$}]: close master socket: $!\n"))
!ll&&!rl&&r
000
close $socks[$i] or warn "Feersum [${$}]: close master socket: $!\n"
l!l
00
MC/DC0%0/2
defined $socks[$i] && (close $socks[$i] || warn("Feersum [${$}]: close master socket: $!\n"))
defined $socks[$i] close $socks[$i]
Truth table0%0/3
A && B
A= defined $socks[$i]B= close $socks[$i]
ABresult
0X0
100
111
970 if defined $socks[$i];
971 }
972 0 @socks = @keep;
973 0 $self->{_master_socks} = \@keep;
974 }
975  
976 0 ▶ unless (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready,
Branch: 1 branch
BranchTrueFalseTotal
unless (_wait_for_ready($pending_pid, $quiet, $gen, \$gen_ready, \$shutting_down, $startup_timeout)) 0 0 0
977 \$shutting_down, $startup_timeout)) {
978 0 ▶ kill 'KILL', $pending_pid if kill(0, $pending_pid);
Branch: 1 branch
BranchTrueFalseTotal
if kill 0, $pending_pid 0 0 0
979 0 waitpid($pending_pid, 0);
980 0 $pending_pid = undef;
981 # A QUIT/INT/TERM during startup is a shutdown request, not a failure.
982 0 ▶ return if $shutting_down;
Branch: 1 branch
BranchTrueFalseTotal
if $shutting_down 0 0 0
983 0 croak "first generation failed to start";
984 }
985 0 $current_pid = $pending_pid;
986 0 $pending_pid = undef;
987 0 $gen_ready_at = EV::time();
988  
989 # Generation 1 is serving: only now is a daemonized start a success.
990 0 $self->_daemon_ready;
991  
992 0 ▶ $quiet or warn "Feersum [$$]: master ready (gen $gen, pid $current_pid)\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: master ready (gen $gen, pid $current_pid)\n" 0 0 0
993  
994 0 EV::run;
995 # A failed replacement can end the loop while older generations still drain.
996 0 for my $pid (keys %retiring) {
997 0 kill 'KILL', $pid;
998 0 kill 'KILL', -$pid;
999 0 waitpid $pid, 0;
1000 }
1001 0 %retiring = ();
1002 # @socks is sparse under reuseport (indices line up with _listen_addrs).
1003 0 for my $sock (grep { defined } @socks) { close($sock) }
1004 0 waitpid(WAIT_ANY_PID, POSIX::WNOHANG()) for 1 .. FINAL_REAP_ATTEMPTS;
1005 0 ▶ $quiet or warn "Feersum [$$]: master done\n";
Branch: 1 branch
BranchTrueFalseTotal
$quiet or warn "Feersum [${$}]: master done\n" 0 0 0
1006 0 return;
1007 }
1008  
1009 # RUN_ONCE loop so an EV::break cannot propagate to the outer EV::run.
1010 0 ▶sub _wait_for_ready { ## no critic (ProhibitManyArgs)
Subroutine _wait_for_ready: not called CC=8 SCAR=42.8
1011 0 my ($pid, $quiet, $gen, $ready_ref, $shutdown_ref, $timeout) = @_;
1012 0 ▶ $timeout //= DEFAULT_STARTUP_TIMEOUT;
Condition0%0/2
$timeout //= 10
l!l
00
MC/DC0%0/1
$timeout //= 10
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
1013 # Clear first: a stale USR2 from a timed-out generation must not count.
1014 0 $$ready_ref = 0;
1015 0 my ($done, $died) = (0, 0);
1016 0 ▶ my $fail = EV::child $pid, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1017 0 warn "Feersum [$$]: gen $gen (pid $pid) died during startup\n";
1018 0 $done = 1; $died = 1;
1019 0 };
1020 0 ▶ my $to = EV::timer($timeout, 0, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1021 0 warn "Feersum [$$]: gen $gen startup timeout\n";
1022 0 $done = 1;
1023 0 });
1024 0 ▶ EV::run(EV::RUN_ONCE)
Branch: 1 branch
BranchTrueFalseTotal
($done or $$ready_ref or $shutdown_ref and $$shutdown_ref) or do { &EV::run(2) } 0 0 0
Condition0%0/9
$done or $$ready_ref or $shutdown_ref and $$shutdown_ref
l!l&&r!l&&!r
000
$done or $$ready_ref
l!l&&r!l&&!r
000
$shutdown_ref and $$shutdown_ref
!ll&&!rl&&r
000
MC/DC0%0/4
$done or $$ready_ref or $shutdown_ref and $$shutdown_ref
$done $$ready_ref $shutdown_ref $$shutdown_ref
Truth table0%0/5
A or B or C and D
A= $doneB= $$ready_refC= $shutdown_refD= $$shutdown_ref
ABCDresult
1XXX1
01XX1
00111
000X0
00100
1025 until $done || $$ready_ref || ($shutdown_ref && $$shutdown_ref);
1026 # Ready then dead in one batch is dead: adopting a reaped pid wedges the master.
1027 0 ▶ return ($$ready_ref && !$died) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$$ready_ref && !$died ? : 0 0 0
Condition0%0/3
$$ready_ref and not $died
!ll&&!rl&&r
000
MC/DC0%0/2
$$ready_ref and not $died
$$ready_ref not $died
Truth table0%0/3
A and not(B)
A= $$ready_refB= not $died
ABresult
0X0
100
111
1028 }
1029  
1030 # The reaper runs in this loop, so an early death can respawn and still report.
1031 0 ▶sub _wait_for_workers_ready {
Subroutine _wait_for_workers_ready: not called CC=6 SCAR=37.4
1032 0 my ($self, $count, $timeout) = @_;
1033 0 ▶ my $pipe = $self->{_worker_ready_pipe} or return $count;
Branch: 1 branch
BranchTrueFalseTotal
my $pipe = $self->{"_worker_ready_pipe"} or return $count 0 0 0
1034 0 ▶ $timeout //= DEFAULT_STARTUP_TIMEOUT;
Condition0%0/2
$timeout //= 10
l!l
00
MC/DC0%0/1
$timeout //= 10
$timeout
Truth table0%0/2
A
A= $timeout
Aresult
11
00
1035 0 my $ready = 0;
1036 0 my $rfh = $pipe->[0];
1037 0 my $expired = 0;
1038 # Our write end stays open for respawns, so gate on count and deadline, not EOF.
1039 0 ▶ my $io = EV::io($rfh, EV::READ, sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1040 0 my $n = sysread($rfh, my $buf, WORKER_READY_BUFSZ);
1041 0 ▶ $ready += $n if $n;
Branch: 1 branch
BranchTrueFalseTotal
if $n 0 0 0
1042 0 });
1043 0 ▶ my $to = EV::timer($timeout, 0, sub { $expired = 1 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
1044 0 ▶ while (!$expired && $ready < $count && !$self->{_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $expired and $ready < $count and not $self->{"_shutdown"}) 0 0 0
Condition0%0/6
not $expired and $ready < $count and not $self->{"_shutdown"}
!ll&&!rl&&r
000
not $expired and $ready < $count
!ll&&!rl&&r
000
MC/DC0%0/3
not $expired and $ready < $count and not $self->{"_shutdown"}
not $expired $ready < $count not $self->{"_shutdown"}
Truth table0%0/4
not(A) and B and not(C)
A= not $expiredB= $ready < $countC= not $self->{"_shutdown"}
ABCresult
0XX0
10X0
1100
1111
1045 0 EV::run(EV::RUN_ONCE);
1046 }
1047 0 undef $io; undef $to;
1048 0 delete $self->{_worker_ready_pipe}; # later respawns must not write here
1049 0 return $ready;
1050 }
1051  
1052 0 ▶sub _apply_settings {
Subroutine _apply_settings: not called CC=5 SCAR=34.0
1053 0 my ($self, $f) = @_;
1054 0 $self->_apply_simple_settings($f, 0); # preserve $self for re-use
1055  
1056 0 ▶ if (my $tls = $self->{tls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tls = $self->{"tls"}) 0 0 0
1057 0 ▶ if ($f->has_tls()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->has_tls) { } 0 0 0
1058 # 0 under reuseport: each worker applies TLS to the socket it binds.
1059 0 ▶ my $n = scalar @{$self->{_master_socks} || $self->{_socks} || []};
Condition0%0/5
$self->{'_master_socks'} || $self->{'_socks'} || []
l!l
00
$self->{"_master_socks"} or $self->{"_socks"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'_master_socks'} || $self->{'_socks'} || []
$self->{"_master_socks"} $self->{"_socks"}
Truth table0%0/3
A or B
A= $self->{"_master_socks"}B= $self->{"_socks"}
ABresult
1X1
011
000
1060 0 $self->_apply_tls_to_listeners($f, $n, $tls, $self->{sni});
1061 0 $self->{_tls_config} = $tls; # for reuseport workers
1062 } else {
1063 0 warn "Feersum [$$]: tls configured but TLS not compiled in"
1064 . " - aborting generation\n";
1065 0 POSIX::_exit(1);
1066 }
1067 }
1068 0 return;
1069 }
1070  
1071 # Close our write-end copy so only the parent holds one; drain on its EOF.
1072 0 ▶sub _watch_for_parent_death {
Subroutine _watch_for_parent_death: not called CC=2 SCAR=17.9
1073 0 my $self = shift;
1074 0 ▶ my $pipe = $self->{_parent_death_pipe} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pipe = $self->{"_parent_death_pipe"} or return 0 0 0
1075 0 close $pipe->[1];
1076 0 my $s = $self;
1077 0 weaken $s;
1078 0 ▶ $self->{_parent_death_w} = EV::io($pipe->[0], EV::READ, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1079 0 ▶ return unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
1080 0 ▶ $s->{quiet} or warn "Feersum [$$]: parent is gone, draining\n";
Branch: 1 branch
BranchTrueFalseTotal
$s->{"quiet"} or warn "Feersum [${$}]: parent is gone, draining\n" 0 0 0
1081 0 $s->{_parent_death_w} = undef;
1082 0 $s->quit;
1083 0 });
1084 0 return;
1085 }
1086  
1087 2 ▶sub _fork_another { ## no critic (ProhibitExcessComplexity)
Subroutine _fork_another: called CC=24 SCAR=58.4
1088 2 my ($self, $slot) = @_;
1089  
1090 # Only the parent keeps the write end, so a worker sees EOF however it died.
1091 2 ▶ $self->{_parent_death_pipe} ||= do {
Condition66%2/3
$self->{"_parent_death_pipe"} ||= do { pipe my $r, my $w or croak("worker death pipe: $!"); [$r, $w] }
l!l&&r!l&&!r
110
MC/DC0%0/2
$self->{"_parent_death_pipe"} ||= do { pipe my $r, my $w or croak("worker death pipe: $!"); [$r, $w] }
$self->{"_parent_death_pipe"} do { pipe my $r, my $w or croak("worker death pipe: $!"); [$r, $w] }
Truth table66%2/3
A ||= B
A= $self->{"_parent_death_pipe"}B= do { pipe my $r, my $w or croak("worker death pipe: $!"); [$r, $w] }
ABresult
1X1
011
000
1092 1 ▶ pipe(my $r, my $w) or croak "worker death pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or croak("worker death pipe: $!") 1 0 1
1093 1 [$r, $w];
1094 };
1095  
1096 2 my $pid = fork;
1097 2 ▶ croak "failed to fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 2 2
1098 2 ▶ unless ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pid) 0 2 2
1099 0 EV::default_loop()->loop_fork;
1100 0 ▶ $self->{quiet} or warn "Feersum [$$]: starting\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: starting\n" 0 0 0
1101 0 delete $self->{_kids};
1102 0 delete $self->{_kid_pids}; # the parent's workers, not ours to signal
1103 0 delete $self->{pre_fork};
1104 # Inherited daemonize pipe end; held, it hides a master death from the parent.
1105 0 ▶ if (my $rdy = delete $self->{_daemon_ready_fh}) { close $rdy }
Branch: 1 branch
BranchTrueFalseTotal
if (my $rdy = delete $self->{"_daemon_ready_fh"}) 0 0 0
1106 0 $self->_watch_for_parent_death;
1107 # EV timers survive fork: an inherited respawn timer would fork grandchildren.
1108 0 delete $self->{_respawn_timers};
1109 0 delete $self->{_kid_started};
1110 0 delete $self->{_respawn_fails};
1111 0 $self->{_n_kids} = 0;
1112  
1113 0 ▶ if ($self->{_use_reuseport}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_use_reuseport"}) 0 0 0
1114 0 my @addrs = @{$self->{_listen_addrs}};
1115 0 ▶ my @old = @{$self->{_socks} || []};
Condition0%0/2
$self->{'_socks'} || []
l!l
00
MC/DC0%0/1
$self->{'_socks'} || []
$self->{'_socks'}
Truth table0%0/2
A
A= $self->{'_socks'}
Aresult
11
00
1116 0 $self->{endjinn}->unlisten();
1117 0 my @new_socks;
1118 eval {
1119 0 for my $i (0 .. $#addrs) {
1120 0 my $listen = $addrs[$i];
1121 # Rebinding a UNIX path would unlink the previous worker's socket.
1122 0 ▶ if (_is_unix_listen($listen)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unix_listen($listen)) 0 0 0
1123 0 push @new_socks, $old[$i];
1124 0 $self->{endjinn}->use_socket($old[$i]);
1125 0 next;
1126 }
1127 0 ▶ if (defined $old[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old[$i]) 0 0 0
1128 0 ▶ close($old[$i])
Branch: 1 branch
BranchTrueFalseTotal
close $old[$i] or die "close parent socket in child: $!\n" 0 0 0
1129 or die "close parent socket in child: $!\n";
1130 }
1131 0 my $sock = $self->_create_socket($listen, 1);
1132 0 push @new_socks, $sock;
1133 0 $self->{endjinn}->use_socket($sock);
1134 }
1135 0 1;
1136 0 ▶ } or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { foreach my $i (0 .. $#addrs) { my $listen = $addrs[$i]; if (_is_unix_listen($listen)) { push @new_socks, $old[$i]; $self->{"endjinn"}->use_socket($old[$i]); next; } ; if (defined $old[$i]) { close $old[$i] or die "close parent socket in child: $!\n"; } ; my $sock = $self->_create_socket($listen, 1); push @new_socks, $sock; $self->{"endjinn"}->use_socket($sock); } ; 1 } } or do { warn "Feersum [${$}]: child socket creation failed: $@"; POSIX::_exit(1) } 0 0 0
1137 0 warn "Feersum [$$]: child socket creation failed: $@";
1138 0 POSIX::_exit(1);
1139 };
1140 0 $self->{sock} = $new_socks[0];
1141 0 $self->{_socks} = \@new_socks;
1142  
1143 # An unguarded croak here unwinds the child into the supervisor's frames.
1144 0 ▶ if (my $tls = $self->{_tls_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tls = $self->{"_tls_config"}) 0 0 0
1145 0 ▶ unless (eval {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_apply_tls_to_listeners($self->{"endjinn"}, scalar @new_socks, $tls, $self->{"sni"}); 1 } }) 0 0 0
1146 $self->_apply_tls_to_listeners(
1147 0 $self->{endjinn}, scalar(@new_socks), $tls, $self->{sni});
1148 0 1;
1149 }) {
1150 0 warn "Feersum [$$]: worker TLS setup failed: $@";
1151 0 POSIX::_exit(1);
1152 }
1153 }
1154  
1155 # An owned listener's accept queue dies with it; a shared one passes to a sibling.
1156 0 $self->{endjinn}->set_drain_accept_queue(1);
1157 }
1158  
1159 0 ▶ if (my $loader = $self->{_app_loader}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $loader = $self->{"_app_loader"}) 0 0 0
1160 0 ▶ unless (eval { $loader->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &$loader(); 1 } }) 0 0 0
1161 0 warn "Feersum [$$]: worker app load failed: $@";
1162 0 POSIX::_exit(1);
1163 }
1164 }
1165  
1166 # On a respawn this runs inside an EV callback, which would swallow a die.
1167 0 ▶ if (my $cb = $self->{after_fork}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"after_fork"}) 0 0 0
1168 0 ▶ unless (eval { $cb->(); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { &$cb(); 1 } }) 0 0 0
1169 0 warn "Feersum [$$]: after_fork failed: $@";
1170 0 POSIX::_exit(1);
1171 }
1172 }
1173  
1174 0 ▶ unless (eval { $self->_set_max_requests_per_worker($self->{endjinn}); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_set_max_requests_per_worker($self->{"endjinn"}); 1 } }) 0 0 0
1175 0 warn "Feersum [$$]: worker max-requests setup failed: $@";
1176 0 POSIX::_exit(1);
1177 }
1178  
1179 # One readiness byte each; the parent may already have closed its end.
1180 0 ▶ if (my $rp = delete $self->{_worker_ready_pipe}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $rp = delete $self->{"_worker_ready_pipe"}) 0 0 0
1181 0 local $SIG{PIPE} = 'IGNORE';
1182 0 syswrite $rp->[1], 'R';
1183 0 close $rp->[1];
1184 0 close $rp->[0];
1185 }
1186  
1187 0 eval { EV::run; }; ## no critic (RequireCheckingReturnValueOfEval)
1188 0 ▶ carp $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
1189 0 ▶ POSIX::_exit($@ ? 1 : 0); # _exit avoids running parent's END blocks
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
1190 }
1191  
1192 2 weaken $self; # prevent circular ref with watcher callback
1193 2 $self->{_n_kids}++;
1194 2 $self->{_kid_started}[$slot] = EV::time(); # for respawn backoff
1195 2 $self->{_kid_pids}[$slot] = $pid; # for the force-exit backstop
1196 2 ▶ $self->{_kids}[$slot] = EV::child $pid, 0, sub {
Subroutine __ANON__: called CC=13 SCAR=44.2
1197 2 my $w = shift;
1198 2 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 2 2
1199 2 ▶ $self->{quiet} or warn "Feersum [$$]: child $pid exited ".
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: child $pid exited " . 'with rstatus ' . $w->rstatus . "\n" 2 0 2
1200 "with rstatus ".$w->rstatus."\n";
1201 2 $self->{_n_kids}--;
1202 2 undef $self->{_kid_pids}[$slot]; # reaped; never signal a reused pid
1203 2 ▶ if ($self->{_shutdown}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_shutdown"}) 2 0 2
1204 2 ▶ unless ($self->{_n_kids}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_n_kids"}) 1 1 2
1205 1 $self->{_death} = undef;
1206 1 EV::break(EV::BREAK_ALL());
1207 }
1208 2 return;
1209 }
1210 0 my $rstatus = $w->rstatus;
1211 0 ▶ my $retired = ($rstatus & WSTATUS_SIGNAL_MASK) == 0
Condition0%0/3
($rstatus & 127) == 0 && $rstatus >> 8 == 42
!ll&&!rl&&r
000
MC/DC0%0/2
($rstatus & 127) == 0 && $rstatus >> 8 == 42
($rstatus & 127) == 0 $rstatus >> 8 == 42
Truth table0%0/3
A && B
A= ($rstatus & 127) == 0B= $rstatus >> 8 == 42
ABresult
0X0
100
111
1212 && ($rstatus >> WSTATUS_EXIT_SHIFT) == EXIT_RETIRED;
1213 0 my $started = $self->{_kid_started}[$slot];
1214 0 ▶ my $lifetime = defined $started ? EV::time() - $started : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $started ? : 0 0 0
1215 0 ▶ if (!$retired && defined $lifetime && $lifetime < RESPAWN_INSTANT_DEATH) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $retired and defined $lifetime and $lifetime < 1) 0 0 0
Condition0%0/6
not $retired and defined $lifetime and $lifetime < 1
!ll&&!rl&&r
000
not $retired and defined $lifetime
!ll&&!rl&&r
000
MC/DC0%0/3
not $retired and defined $lifetime and $lifetime < 1
not $retired defined $lifetime $lifetime < 1
Truth table0%0/4
not(A) and B and C
A= not $retiredB= defined $lifetimeC= $lifetime < 1
ABCresult
0XX0
10X0
1100
1111
1216 0 my $n = ++$self->{_respawn_fails}[$slot];
1217 0 ▶ my $delay = $n > RESPAWN_MAX_FAILS
Branch: 1 branch
BranchTrueFalseTotal
$n > 10 ? : 0 0 0
1218 ? RESPAWN_BACKOFF_MAX
1219 : RESPAWN_BACKOFF_BASE * (2 ** ($n - 1));
1220 0 ▶ $delay = RESPAWN_BACKOFF_MAX if $delay > RESPAWN_BACKOFF_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 30 0 0 0
1221 0 ▶ warn "Feersum [$$]: worker slot $slot has died immediately $n "
Branch: 1 branch
BranchTrueFalseTotal
if $n == 11 0 0 0
1222 . "times, still retrying every ${delay}s "
1223 . "(check the app and after_fork)\n"
1224 if $n == RESPAWN_MAX_FAILS + 1;
1225 0 warn sprintf "Feersum [%d]: worker slot %d died after %.2fs, "
1226 . "respawning in %gs (failure %d)\n", $$, $slot, $lifetime,
1227 $delay, $n;
1228 0 ▶ $self->{_respawn_timers}[$slot] = EV::timer($delay, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1229 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
1230 0 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 0 0 0
1231 0 $self->{_respawn_timers}[$slot] = undef;
1232 0 $self->_respawn_worker($slot);
1233 0 });
1234 0 return;
1235 }
1236 0 $self->{_respawn_fails}[$slot] = 0;
1237 0 $self->_respawn_worker($slot);
1238 2 };
1239 2 return;
1240 }
1241  
1242 # Without reuseport the parent must be listening across the fork.
1243 1 ▶sub _respawn_worker {
Subroutine _respawn_worker: called CC=10 SCAR=34.0
1244 1 my ($self, $slot) = @_;
1245 1 ▶ unless ($self->{_use_reuseport}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_use_reuseport'}) { } 0 1 1
1246 1 my $feersum = $self->{endjinn};
1247 1 ▶ my @socks = @{$self->{_socks} || [$self->{sock}]};
Condition50%1/2
$self->{'_socks'} || [$self->{'sock'}]
l!l
10
MC/DC0%0/1
$self->{'_socks'} || [$self->{'sock'}]
$self->{'_socks'}
Truth table50%1/2
A
A= $self->{'_socks'}
Aresult
11
00
1248 1 my $all_valid = 1;
1249 1 for my $sock (@socks) {
1250 1 ▶ unless (defined fileno $sock) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined fileno $sock) 1 0 1
1251 1 $all_valid = 0;
1252 1 last;
1253 }
1254 }
1255 1 ▶ if ($all_valid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($all_valid) { } 0 1 1
1256 # unlisten() dropped the TLS context, so rebuild the listeners fully.
1257 # Guarded: EV would swallow a croak and leave the supervisor listening.
1258 0 my $forked = eval {
1259 0 for my $sock (@socks) {
1260 0 $feersum->use_socket($sock);
1261 }
1262 0 ▶ if (my $tls = $self->{_tls_config}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $tls = $self->{"_tls_config"}) 0 0 0
1263 $self->_apply_tls_to_listeners(
1264 0 $feersum, scalar(@socks), $tls, $self->{sni});
1265 }
1266 0 $self->_fork_another($slot);
1267 0 1;
1268 };
1269 0 my $err = $@;
1270 0 $feersum->unlisten;
1271 0 ▶ $self->_retry_respawn($slot, $err) unless $forked;
Branch: 1 branch
BranchTrueFalseTotal
unless $forked 0 0 0
1272 } else {
1273 1 $self->_retry_respawn($slot, 'listen socket has no fileno');
1274 }
1275 }
1276 else {
1277 0 my $forked = eval { $self->_fork_another($slot); 1 };
1278 0 ▶ $self->_retry_respawn($slot, $@) unless $forked;
Branch: 1 branch
BranchTrueFalseTotal
unless $forked 0 0 0
1279 }
1280 1 return;
1281 }
1282  
1283 # EV swallows a fork failure in a callback; retry or the slot is stranded for good.
1284 0 ▶sub _retry_respawn {
Subroutine _retry_respawn: not called CC=5 SCAR=34.0
1285 0 my ($self, $slot, $err) = @_;
1286 0 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 0 0 0
1287 0 my $n = ++$self->{_respawn_fails}[$slot];
1288 0 ▶ my $delay = $n > RESPAWN_MAX_FAILS
Branch: 1 branch
BranchTrueFalseTotal
$n > 10 ? : 0 0 0
1289 ? RESPAWN_BACKOFF_MAX
1290 : RESPAWN_BACKOFF_BASE * (2 ** ($n - 1));
1291 0 ▶ $delay = RESPAWN_BACKOFF_MAX if $delay > RESPAWN_BACKOFF_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if $delay > 30 0 0 0
1292 0 ▶ my $msg = defined $err ? $err : 'unknown error';
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 0 0 0
1293 0 $msg =~ s/\s+\z//;
1294 0 warn sprintf "Feersum [%d]: could not fork worker slot %d (%s), "
1295 . "retrying in %gs (failure %d)\n", $$, $slot, $msg, $delay, $n;
1296 0 ▶ $self->{_respawn_timers}[$slot] = EV::timer($delay, 0, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1297 0 ▶ return unless $self;
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 0 0
1298 0 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 0 0 0
1299 0 $self->{_respawn_timers}[$slot] = undef;
1300 0 $self->_respawn_worker($slot);
1301 0 });
1302 0 return;
1303 }
1304  
1305 1 ▶sub _start_pre_fork {
Subroutine _start_pre_fork: called CC=14 SCAR=41.8
1306 1 my $self = shift;
1307  
1308 1 $self->{endjinn}->set_multiprocess(1);
1309  
1310 # setsid() returns -1 (true) on failure; EPERM means already a session leader.
1311 1 ▶ POSIX::setsid() == SYSCALL_ERROR and $! != POSIX::EPERM()
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setsid() == -1 and $! != 1 and croak("setsid() failed: $!") 0 1 1
Condition33%1/3
POSIX::setsid() == -1 and $! != 1
!ll&&!rl&&r
100
MC/DC0%0/2
POSIX::setsid() == -1 and $! != 1
POSIX::setsid() == -1 $! != 1
Truth table33%1/3
A and B
A= POSIX::setsid() == -1B= $! != 1
ABresult
0X0
100
111
1312 and croak "setsid() failed: $!";
1313  
1314 1 $self->{_kids} = [];
1315 1 $self->{_kid_pids} = []; # inherited entries belong to another generation
1316 1 $self->{_n_kids} = 0;
1317 # Close before forking: a root-owned member locks privdropped workers out of the group.
1318 1 ▶ if ($self->{_use_reuseport}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_use_reuseport"}) 0 1 1
1319 0 $self->{endjinn}->unlisten();
1320 # UNIX listeners stay: workers inherit them rather than rebinding.
1321 0 ▶ my $addrs = $self->{_listen_addrs} || [];
Condition0%0/2
$self->{'_listen_addrs'} || []
l!l
00
MC/DC0%0/1
$self->{'_listen_addrs'} || []
$self->{'_listen_addrs'}
Truth table0%0/2
A
A= $self->{'_listen_addrs'}
Aresult
11
00
1322 0 ▶ my $socks = $self->{_socks} || [];
Condition0%0/2
$self->{'_socks'} || []
l!l
00
MC/DC0%0/1
$self->{'_socks'} || []
$self->{'_socks'}
Truth table0%0/2
A
A= $self->{'_socks'}
Aresult
11
00
1323 0 my @keep;
1324 0 for my $i (0 .. $#$socks) {
1325 0 ▶ if (_is_unix_listen($addrs->[$i])) { $keep[$i] = $socks->[$i]; next }
Branch: 1 branch
BranchTrueFalseTotal
if (_is_unix_listen($addrs->[$i])) 0 0 0
1326 0 ▶ close($socks->[$i]) or warn "close parent socket before fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $socks->[$i] or warn "close parent socket before fork: $!" 0 0 0
1327 }
1328 0 $self->{_socks} = \@keep;
1329 0 ($self->{sock}) = grep { defined } @keep;
1330 }
1331  
1332 # Under daemonize, gate the ready token on the workers actually coming up.
1333 1 my $gate = $self->{daemonize};
1334 1 ▶ if ($gate) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gate) 0 1 1
1335 0 ▶ pipe(my $wready_r, my $wready_w)
Branch: 1 branch
BranchTrueFalseTotal
pipe my $wready_r, my $wready_w or croak("worker readiness pipe: $!") 0 0 0
1336 or croak "worker readiness pipe: $!";
1337 0 $self->{_worker_ready_pipe} = [$wready_r, $wready_w];
1338 }
1339  
1340 1 $self->_fork_another($_) for (1 .. $self->{pre_fork});
1341  
1342 1 ▶ $self->{endjinn}->unlisten() unless $self->{_use_reuseport};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_use_reuseport"} 1 0 1
1343  
1344 1 ▶ return 1 unless $gate;
Branch: 1 branch
BranchTrueFalseTotal
unless $gate 1 0 1
1345 return $self->_wait_for_workers_ready(
1346 0 $self->{pre_fork}, $self->{startup_timeout}) >= $self->{pre_fork};
1347 }
1348  
1349 # False only on EOF before the token; a timeout counts as success.
1350 0 ▶sub _await_daemon_ready {
Subroutine _await_daemon_ready: not called CC=10 SCAR=47.0
1351 0 my ($self, $rdy_r) = @_;
1352 0 my $rin = '';
1353 0 vec($rin, fileno($rdy_r), 1) = 1;
1354 0 ▶ my $wait = $self->{startup_timeout} // DEFAULT_STARTUP_TIMEOUT;
Condition0%0/2
$self->{"startup_timeout"} // 10
l!l
00
MC/DC0%0/1
$self->{"startup_timeout"} // 10
$self->{"startup_timeout"}
Truth table0%0/2
A
A= $self->{"startup_timeout"}
Aresult
11
00
1355 0 my $deadline = EV::time() + $wait;
1356 0 my $nready = 0;
1357 # A SIGCHLD EINTR would read as a timeout, i.e. success; resume instead.
1358 0 while (1) {
1359 0 my $remaining = $deadline - EV::time();
1360 0 ▶ last if $remaining <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $remaining <= 0 0 0 0
1361 0 $nready = select(my $rout = $rin, undef, undef, $remaining);
1362 0 ▶ last if !defined $nready || $nready >= 0 || $! != POSIX::EINTR();
Branch: 1 branch
BranchTrueFalseTotal
if not defined $nready or $nready >= 0 or $! != 4 0 0 0
Condition0%0/6
not defined $nready or $nready >= 0 or $! != 4
l!l&&r!l&&!r
000
not defined $nready or $nready >= 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $nready or $nready >= 0 or $! != 4
not defined $nready $nready >= 0 $! != 4
Truth table0%0/4
not(A) or B or C
A= not defined $nreadyB= $nready >= 0C= $! != 4
ABCresult
1XX1
01X1
0011
0000
1363 }
1364 0 ▶ return 1 if !$nready || $nready < 1;
Branch: 1 branch
BranchTrueFalseTotal
if not $nready or $nready < 1 0 0 0
Condition0%0/3
not $nready or $nready < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $nready or $nready < 1
not $nready $nready < 1
Truth table0%0/3
not(A) or B
A= not $nreadyB= $nready < 1
ABresult
1X1
011
000
1365 0 my $n = sysread($rdy_r, my $tok, READY_TOKEN_SIZE);
1366 0 ▶ return (defined $n && $n == 0) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $n && $n == 0 ? : 0 0 0
Condition0%0/3
defined $n and $n == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $n and $n == 0
defined $n $n == 0
Truth table0%0/3
A and B
A= defined $nB= $n == 0
ABresult
0X0
100
111
1367 }
1368  
1369 # Idempotent; the parent reads EOF without a token as a failed start.
1370 1 ▶sub _daemon_ready {
Subroutine _daemon_ready: called CC=2 SCAR=10.9
1371 1 my $self = shift;
1372 1 ▶ my $fh = delete $self->{_daemon_ready_fh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $fh = delete $self->{"_daemon_ready_fh"} or return 0 1 1
1373 # The parent may have timed out and closed its end; a bare write takes SIGPIPE.
1374 0 local $SIG{PIPE} = 'IGNORE';
1375 0 syswrite $fh, 'R';
1376 0 close $fh;
1377 0 return;
1378 }
1379  
1380 # Records whether the probe created the file; cleanup must not remove a live one.
1381 0 ▶sub _probe_pid_file_writable {
Subroutine _probe_pid_file_writable: not called CC=4 SCAR=30.0
1382 0 my $self = shift;
1383 0 ▶ my $file = $self->{pid_file} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->{"pid_file"} or return 0 0 0
1384 0 $self->{_pid_file_probed} = !-e $file;
1385 0 ▶ open my $probe, '>>', $file or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $probe, ">>", $file or croak("Cannot write pid_file '${file}': $!") 0 0 0
1386 0 ▶ close $probe or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $probe or croak("Cannot write pid_file '${file}': $!") 0 0 0
1387 0 return;
1388 }
1389  
1390 # A zero-byte pid file left behind reads to a health check as a running server.
1391 0 ▶sub _discard_probed_pid_file {
Subroutine _discard_probed_pid_file: not called CC=4 SCAR=30.0
1392 0 my $self = shift;
1393 0 ▶ my $file = $self->{_pid_file_probed} ? $self->{pid_file} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'_pid_file_probed'} ? : 0 0 0
1394 0 ▶ unlink $file if $file && -z $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file and -z $file 0 0 0
Condition0%0/3
$file and -z $file
!ll&&!rl&&r
000
MC/DC0%0/2
$file and -z $file
$file -z $file
Truth table0%0/3
A and B
A= $fileB= -z $file
ABresult
0X0
100
111
1395 0 return;
1396 }
1397  
1398 3 ▶sub _daemonize_and_write_pid {
Subroutine _daemonize_and_write_pid: called CC=17 SCAR=55.1
1399 3 my $self = shift;
1400  
1401 3 ▶ if ($self->{daemonize}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'daemonize'}) { } 0 3 3
elsif (my $file = $self->{'pid_file'}) { } 0 3 3
1402 # Before forking, or the daemon is already serving when the parent croaks.
1403 0 $self->_probe_pid_file_writable;
1404 0 ▶ pipe(my $rdy_r, my $rdy_w) or croak "daemonize pipe: $!";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $rdy_r, my $rdy_w or croak("daemonize pipe: $!") 0 0 0
1405 0 my $pid = fork;
1406 0 ▶ croak "daemonize fork: $!" unless defined $pid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid 0 0 0
1407 0 ▶ if ($pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid) 0 0 0
1408 0 close $rdy_w;
1409 0 my $ready = $self->_await_daemon_ready($rdy_r);
1410 0 close $rdy_r;
1411 0 ▶ unless ($ready) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ready) 0 0 0
1412 0 waitpid $pid, 0;
1413 0 $self->_discard_probed_pid_file;
1414 warn "Feersum: startup failed, see the log for the reason\n"
1415 0 ▶ unless $self->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"quiet"} 0 0 0
1416 0 POSIX::_exit(1);
1417 }
1418 0 ▶ if (my $file = $self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->{"pid_file"}) 0 0 0
1419 0 ▶ open my $fh, '>', $file or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("Cannot write pid_file '${file}': $!") 0 0 0
1420 0 print {$fh} "$pid\n";
1421 # a failed close truncates the pid file
1422 0 ▶ close $fh or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Cannot write pid_file '${file}': $!") 0 0 0
1423 }
1424 0 POSIX::_exit(0);
1425 }
1426 0 close $rdy_r;
1427 0 $self->{_daemon_ready_fh} = $rdy_w;
1428 0 POSIX::setsid();
1429 0 ▶ open STDIN, '<', '/dev/null' or croak "redirect stdin: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, "<", "/dev/null" or croak("redirect stdin: $!") 0 0 0
1430 0 ▶ open STDOUT, '>', '/dev/null' or croak "redirect stdout: $!";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">", "/dev/null" or croak("redirect stdout: $!") 0 0 0
1431 open STDERR, '>', '/dev/null' or croak "redirect stderr: $!"
1432 0 ▶ unless $ENV{FEERSUM_DEBUG};
Branch: 1 branch
BranchTrueFalseTotal
unless $ENV{"FEERSUM_DEBUG"} 0 0 0
Condition0%0/5
$ENV{'FEERSUM_DEBUG'} || (open(STDERR, '>', '/dev/null') || croak("redirect stderr: $!"))
l!l&&r!l&&!r
000
open STDERR, ">", "/dev/null" or croak("redirect stderr: $!")
l!l
00
MC/DC0%0/2
$ENV{'FEERSUM_DEBUG'} || (open(STDERR, '>', '/dev/null') || croak("redirect stderr: $!"))
$ENV{'FEERSUM_DEBUG'} open STDERR, ">", "/dev/null"
Truth table0%0/3
A || B
A= $ENV{'FEERSUM_DEBUG'}B= open STDERR, ">", "/dev/null"
ABresult
1X1
011
000
1433 } elsif (my $file = $self->{pid_file}) {
1434 0 ▶ open my $fh, '>', $file or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or croak("Cannot write pid_file '${file}': $!") 0 0 0
1435 0 print {$fh} "$$\n";
1436 0 ▶ close $fh or croak "Cannot write pid_file '$file': $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Cannot write pid_file '${file}': $!") 0 0 0
1437 }
1438 3 return;
1439 }
1440  
1441 6 ▶sub _drop_privs { ## no critic (ProhibitExcessComplexity)
Subroutine _drop_privs: called CC=22 SCAR=39.1
1442 6 my $self = shift;
1443  
1444 # The user's primary group is the default when no group is configured.
1445 6 my ($uid, $gid);
1446 6 ▶ if (my $user = $self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $user = $self->{"user"}) 2 4 6
1447 2 (undef, undef, $uid, my $pw_gid) = getpwnam($user);
1448 2 ▶ croak "Unknown user '$user'" unless defined $uid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $uid 1 1 2
1449 1 $gid = $pw_gid;
1450 }
1451 5 ▶ if (my $group = $self->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $self->{"group"}) 1 4 5
1452 1 $gid = getgrnam($group);
1453 1 ▶ croak "Unknown group '$group'" unless defined $gid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $gid 1 0 1
1454 }
1455  
1456 4 ▶ if (defined $gid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gid) 1 3 4
1457 # Assigning $) also clears supplemental groups (setgroups), which needs
1458 # root; a non-root start has none to shed and would only EPERM.
1459 1 my $root = ($> == 0);
1460 1 ▶ if ($root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($root) 0 1 1
1461 # $) hides the syscall results; errno is the only signal, so clear it first.
1462 0 $! = 0;
1463 0 $) = "$gid $gid";
1464 0 ▶ croak "setgroups/setegid($gid): $!" if $!;
Branch: 1 branch
BranchTrueFalseTotal
if $! 0 0 0
1465 }
1466 1 ▶ POSIX::setgid($gid) or croak "setgid($gid): $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setgid($gid) or croak("setgid($gid): $!") 1 0 1
1467 # As root $) = "g g" set setgroups([g]), so every token of $( must be $gid;
1468 # non-root could only set the real GID, the first token.
1469 1 my @rg = split ' ', $(;
1470 croak "setgid($gid) verification failed: real GID list is @rg"
1471 0 ▶ if !@rg || ($root ? (any { $_ != $gid } @rg) : $rg[0] != $gid);
Subroutine __ANON__: not called CC=1 SCAR=1.6
Branch: 2 branches
BranchTrueFalseTotal
if not @rg or $root ? &any(sub { $_ != $gid; } , @rg) : $rg[0] != $gid 0 1 1
$root ? : 0 1 1
Condition33%1/3
not @rg or $root ? &any(sub { $_ != $gid; } , @rg) : $rg[0] != $gid
l!l&&r!l&&!r
001
MC/DC0%0/2
not @rg or $root ? &any(sub { $_ != $gid; } , @rg) : $rg[0] != $gid
not @rg $root ? &any(sub { $_ != $gid; } , @rg) : $rg[0] != $gid
Truth table33%1/3
not(A) or B
A= not @rgB= $root ? &any(sub { $_ != $gid; } , @rg) : $rg[0] != $gid
ABresult
1X1
011
000
1472 }
1473 4 ▶ if (defined $uid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uid) 1 3 4
1474 1 ▶ POSIX::setuid($uid) or croak "setuid($uid): $!";
Branch: 1 branch
BranchTrueFalseTotal
POSIX::setuid($uid) or croak("setuid($uid): $!") 1 0 1
1475 1 ▶ croak "setuid($uid) verification failed: \$<=$<, \$>=$>"
Branch: 1 branch
BranchTrueFalseTotal
unless $< == $uid and $> == $uid 0 1 1
Condition33%1/3
$< == $uid and $> == $uid
!ll&&!rl&&r
001
MC/DC0%0/2
$< == $uid and $> == $uid
$< == $uid $> == $uid
Truth table33%1/3
A and B
A= $< == $uidB= $> == $uid
ABresult
0X0
100
111
1476 unless $< == $uid && $> == $uid;
1477 }
1478 4 ▶ if (defined $uid || defined $gid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uid or defined $gid) 1 3 4
Condition66%2/3
defined $uid or defined $gid
l!l&&r!l&&!r
103
MC/DC50%1/2
defined $uid or defined $gid
defined $uid defined $gid
Truth table66%2/3
A or B
A= defined $uidB= defined $gid
ABresult
1X1
011
000
1479 1 $self->_verify_reuseport_bind;
1480 # Warn now: at shutdown stderr may already be closed.
1481 1 ▶ if (my $file = $self->{pid_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $file = $self->{"pid_file"}) 0 1 1
1482 0 my ($dir) = $file =~ m{^(.*)/[^/]+\z};
1483 0 ▶ $dir = q{.} unless defined $dir && length $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 0 0
Condition0%0/3
defined $dir and length $dir
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table0%0/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
1484 0 ▶ warn "Feersum [$$]: pid_file '$file' cannot be removed at shutdown: "
Branch: 1 branch
BranchTrueFalseTotal
unless -w $dir 0 0 0
1485 . "'$dir' is not writable after dropping privileges\n"
1486 unless -w $dir;
1487 }
1488 }
1489 4 return;
1490 }
1491  
1492 # Probe the post-drop rebind while the inherited-socket fallback is still open.
1493 4 ▶sub _verify_reuseport_bind {
Subroutine _verify_reuseport_bind: called CC=8 SCAR=21.3
1494 4 my $self = shift;
1495 4 ▶ return unless $self->{_use_reuseport};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_use_reuseport"} 1 3 4
1496  
1497 # EADDRINUSE is a pass: the kernel checks bind permission before the address
1498 # conflict, and we still hold the address. Built so it matches any locale.
1499 3 my $in_use = do { local $! = POSIX::EADDRINUSE(); qq{$!} };
1500  
1501 3 ▶ for my $listen (@{ $self->{_listen_addrs} || [] }) {
Condition50%1/2
$self->{'_listen_addrs'} || []
l!l
30
MC/DC0%0/1
$self->{'_listen_addrs'} || []
$self->{'_listen_addrs'}
Truth table50%1/2
A
A= $self->{'_listen_addrs'}
Aresult
11
00
1502 3 ▶ next if _is_unix_listen($listen); # inherited, never rebound
Branch: 1 branch
BranchTrueFalseTotal
if _is_unix_listen($listen) 1 2 3
1503 2 my $probe = eval { $self->_create_socket($listen, 1, 1) };
1504 2 ▶ if ($probe) { close $probe; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($probe) 1 1 2
1505 1 ▶ my $why = $@ || "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
1506 1 $why =~ s/\s+at\s+\S+\s+line\s+\d+\.?\s*\z//;
1507 1 ▶ next if index($why, $in_use) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($why, $in_use) != -1 1 0 1
1508 0 warn "Feersum [$$]: cannot rebind '$listen' after dropping privileges "
1509 . "($why); disabling reuseport - workers will share the inherited "
1510 . "listen socket instead\n";
1511 0 $self->{_use_reuseport} = 0;
1512 0 last;
1513 }
1514 3 return;
1515 }
1516  
1517 2 ▶sub quit {
Subroutine quit: called CC=8 SCAR=31.4
Pod documented
1518 2 my $self = shift;
1519 2 ▶ return if $self->{_shutdown};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_shutdown"} 1 1 2
1520  
1521 1 $self->{_shutdown} = 1;
1522 1 ▶ $self->{quiet} or warn "Feersum [$$]: shutting down...\n";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or warn "Feersum [${$}]: shutting down...\n" 1 0 1
1523 my $death = $self->{graceful_timeout}
1524 // $ENV{FEERSUM_GRACEFUL_TIMEOUT}
1525 1 ▶ // DEATH_TIMER;
Condition40%2/5
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
l!l
01
$self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
l!l&&r!l&&!r
001
MC/DC0%0/2
($self->{"graceful_timeout"} // $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}) // 5
$self->{"graceful_timeout"} $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
Truth table33%1/3
A // B
A= $self->{"graceful_timeout"}B= $ENV{"FEERSUM_GRACEFUL_TIMEOUT"}
ABresult
1X1
011
000
1526  
1527 1 ▶ if ($self->{_n_kids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_n_kids'}) { } 1 0 1
1528 # Group QUIT includes self; guarded by _shutdown above.
1529 1 kill POSIX::SIGQUIT, -$$;
1530 1 $death += DEATH_TIMER_INCR;
1531 }
1532 else {
1533 # _exit skips END and DESTROY, so remove the pid file here. Guarded: a
1534 # croak (already draining a retirement) would skip arming the backstop.
1535 0 my $ok = eval {
1536 0 ▶ $self->{endjinn}->graceful_shutdown(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
1537 0 $self->_remove_pid_file;
1538 0 POSIX::_exit(0);
1539 0 });
1540 0 1;
1541 };
1542 0 ▶ $self->{quiet} or $ok
Branch: 1 branch
BranchTrueFalseTotal
$self->{"quiet"} or $ok or warn "Feersum [${$}]: already draining, forcing exit in ${death}s\n" 0 0 0
Condition0%0/3
$self->{"quiet"} or $ok
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"quiet"} or $ok
$self->{"quiet"} $ok
Truth table0%0/3
A or B
A= $self->{"quiet"}B= $ok
ABresult
1X1
011
000
1543 or warn "Feersum [$$]: already draining, forcing exit in ${death}s\n";
1544 }
1545  
1546 # Backstop; exiting alone leaves orphaned workers holding the listen socket.
1547 1 my $s = $self;
1548 1 weaken $s; # the watcher is stored on $self; a strong ref here leaks it
1549 0 ▶ $self->{_death} = EV::timer $death, 0, sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
1550 0 ▶ return unless $s;
Branch: 1 branch
BranchTrueFalseTotal
unless $s 0 0 0
1551 0 $s->_remove_pid_file;
1552 0 ▶ my @alive = grep { defined } @{ $s->{_kid_pids} || [] };
Condition0%0/2
$s->{'_kid_pids'} || []
l!l
00
MC/DC0%0/1
$s->{'_kid_pids'} || []
$s->{'_kid_pids'}
Truth table0%0/2
A
A= $s->{'_kid_pids'}
Aresult
11
00
1553 0 ▶ kill 'KILL', @alive if @alive;
Branch: 1 branch
BranchTrueFalseTotal
if @alive 0 0 0
1554 0 POSIX::_exit(1);
1555 1 };
1556 1 return;
1557 }
1558  
1559 16 1;
1560 __END__
↑ Summary blib/lib/Feersum.pm »