| 1 | package Mail::Milter::Authentication::Net::ServerPatches; | ||||||||||||||||||||||||||||
| 2 | 143 | ▶ | use 5.20.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 143 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 143 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 143 | ▶ | use Mail::Milter::Authentication::Pragmas; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | # ABSTRACT: Patches to Net::Server::PreFork | ||||||||||||||||||||||||||||
| 7 | 143 | our $VERSION = '4.20261116'; # VERSION | |||||||||||||||||||||||||||
| 8 | 143 | ▶ | use base 'Net::Server::PreFork'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 143 | ▶ | use POSIX qw(EINTR); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 143 | ▶ | use SUPER; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 143 | ▶ | use Socket qw(AF_INET AF_UNIX SOCK_DGRAM SOCK_STREAM); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 23 | ▶ | sub run_child { | ||||||||||||||||||||||||||
|
Subroutine
run_child: called CC=8 SCAR=40.5
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 23 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 23 | ▶ | my $config = $self->{config} || get_config(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'config'} || get_config()
MC/DC0%0/2
$self->{'config'} || get_config()
$self->{'config'}
get_config()
Truth table33%1/3
A || B
A= $self->{'config'}B= get_config()
| |||||||||||||||||||||||||||||
| 18 | 23 | ▶ | return $self->SUPER unless $config->{'patch_net_server'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 0 | my $prop = $self->{'server'}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | ▶ | $SIG{'INT'} = $SIG{'TERM'} = $SIG{'QUIT'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 23 | 0 | $self->child_finish_hook; | |||||||||||||||||||||||||||
| 24 | 0 | exit; | |||||||||||||||||||||||||||
| 25 | 0 | }; | |||||||||||||||||||||||||||
| 26 | 0 | $SIG{'PIPE'} = 'IGNORE'; | |||||||||||||||||||||||||||
| 27 | 0 | $SIG{'CHLD'} = 'DEFAULT'; | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | $SIG{'HUP'} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | if (! $prop->{'connected'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | $self->child_finish_hook; | |||||||||||||||||||||||||||
| 31 | 0 | exit; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 0 | $prop->{'SigHUPed'} = 1; | |||||||||||||||||||||||||||
| 34 | 0 | }; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # Open in child at start | ||||||||||||||||||||||||||||
| 37 | 0 | ▶ | if ($prop->{'serialize'} eq 'flock') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | open $prop->{'lock_fh'}, ">", $prop->{'lock_file'} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | or $self->fatal("Couldn't open lock file \"$prop->{'lock_file'}\"[$!]"); | ||||||||||||||||||||||||||||
| 40 | # With flock() serialization, make things HUP safe | ||||||||||||||||||||||||||||
| 41 | 0 | pipe($prop->{'SigHUPReadPipe'}, $prop->{'SigHUPWritePipe'}); | |||||||||||||||||||||||||||
| 42 | 0 | $prop->{'select'}->add($prop->{'SigHUPReadPipe'}); | |||||||||||||||||||||||||||
| 43 | 0 | ▶ | $SIG{'HUP'} = sub { $prop->{'SigHUPed'} = 1; syswrite $prop->{'SigHUPWritePipe'}, "1" if !$prop->{SigHUPWriten}++; }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | $self->log(4, "Child Preforked ($$)"); | |||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | delete @{ $prop }{qw(children tally last_start last_process)}; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | $self->child_init_hook; | |||||||||||||||||||||||||||
| 51 | 0 | my $write = $prop->{'_WRITE'}; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 0 | ▶ | while ($self->accept()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $prop->{'connected'} = 1; | |||||||||||||||||||||||||||
| 55 | 0 | print $write "$$ processing\n"; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 0 | my $ok = eval { $self->run_client_connection; 1 }; | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (! $ok) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | print $write "$$ exiting\n"; | |||||||||||||||||||||||||||
| 60 | 0 | die $@; | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 0 | ▶ | last if $self->done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 0 | $prop->{'connected'} = 0; | |||||||||||||||||||||||||||
| 66 | 0 | print $write "$$ waiting\n"; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $self->child_finish_hook; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | print $write "$$ exiting\n"; | |||||||||||||||||||||||||||
| 72 | 0 | exit; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 103 | ▶ | sub accept { ## no critic | ||||||||||||||||||||||||||
|
Subroutine
accept: called CC=11 SCAR=45.6
Pod
documented
| |||||||||||||||||||||||||||||
| 77 | 103 | my $self = shift; | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 103 | ▶ | my $config = $self->{config} || get_config(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'config'} || get_config()
MC/DC0%0/2
$self->{'config'} || get_config()
$self->{'config'}
get_config()
Truth table33%1/3
A || B
A= $self->{'config'}B= get_config()
| |||||||||||||||||||||||||||||
| 80 | 103 | ▶ | return $self->SUPER unless $config->{'patch_net_server'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 0 | my $prop = $self->{'server'}; | |||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ($prop->{'serialize'} eq 'flock') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 85 | 0 | ▶ | while (! flock $prop->{'lock_fh'}, Fcntl::LOCK_EX()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 0 | ▶ | return undef if $prop->{'SigHUPed'}; ## no critic | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | 0 | ▶ | next if $! == EINTR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | $self->fatal("Couldn't get lock on file \"$prop->{'lock_file'}\" [$!]"); | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 0 | my $v = $self->super_accept(); | |||||||||||||||||||||||||||
| 91 | 0 | flock $prop->{'lock_fh'}, Fcntl::LOCK_UN(); | |||||||||||||||||||||||||||
| 92 | 0 | return $v; | |||||||||||||||||||||||||||
| 93 | } elsif ($prop->{'serialize'} eq 'semaphore') { | ||||||||||||||||||||||||||||
| 94 | 0 | ▶ | $prop->{'sem'}->op(0, -1, IPC::SysV::SEM_UNDO()) or $self->fatal("Semaphore Error [$!]"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | 0 | my $v = $self->super_accept(); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | $prop->{'sem'}->op(0, 1, IPC::SysV::SEM_UNDO()) or $self->fatal("Semaphore Error [$!]"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 0 | return $v; | |||||||||||||||||||||||||||
| 98 | } elsif ($prop->{'serialize'} eq 'pipe') { | ||||||||||||||||||||||||||||
| 99 | 0 | my $waiting = $prop->{'_WAITING'}; | |||||||||||||||||||||||||||
| 100 | 0 | scalar <$waiting>; # read one line - kernel says who gets it | |||||||||||||||||||||||||||
| 101 | 0 | my $v = $self->super_accept(); | |||||||||||||||||||||||||||
| 102 | 0 | print { $prop->{'_READY'} } "Next!\n"; | |||||||||||||||||||||||||||
| 103 | 0 | return $v; | |||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||
| 105 | 0 | my $v = $self->super_accept(); | |||||||||||||||||||||||||||
| 106 | 0 | return $v; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | sub super_accept { | ||||||||||||||||||||||||||
|
Subroutine
super_accept: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 112 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 113 | 0 | my $prop = $self->{'server'}; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | my $sock = undef; | |||||||||||||||||||||||||||
| 116 | 0 | my $retries = 30; | |||||||||||||||||||||||||||
| 117 | 0 | ▶ | while ($retries--) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($prop->{'multi_port'}) { # with more than one port, use select to get the next one | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | return 0 if $prop->{'_HUP'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ($sock, my $hup) = $self->accept_multi_port; # keep trying for the rest of retries | |||||||||||||||||||||||||||
| 121 | 0 | ▶ | return 0 if $hup || $prop->{'_HUP'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$hup or $prop->{"_HUP"}
MC/DC0%0/2
$hup or $prop->{"_HUP"}
$hup
$prop->{"_HUP"}
Truth table0%0/3
A or B
A= $hupB= $prop->{"_HUP"}
| |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | if ($self->can_read_hook($sock)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 0 | $retries++; | |||||||||||||||||||||||||||
| 124 | 0 | next; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | 0 | $sock = $prop->{'sock'}->[0]; # single port is bound - just accept | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $self->fatal("Received a bad sock!") if ! defined $sock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | if (SOCK_DGRAM == $sock->getsockopt(Socket::SOL_SOCKET(), Socket::SO_TYPE())) { # receive a udp packet | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 0 | $prop->{'client'} = $sock; | |||||||||||||||||||||||||||
| 133 | 0 | $prop->{'udp_true'} = 1; | |||||||||||||||||||||||||||
| 134 | 0 | $prop->{'udp_peer'} = $sock->recv($prop->{'udp_data'}, $sock->NS_recv_len, $sock->NS_recv_flags); | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | } else { # blocking accept per proto | ||||||||||||||||||||||||||||
| 137 | 0 | delete $prop->{'udp_true'}; | |||||||||||||||||||||||||||
| 138 | 0 | $prop->{'client'} = $sock->accept(); | |||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 0 | ▶ | return 0 if $prop->{'_HUP'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return 1 if $prop->{'client'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | $self->log(2,"Accept failed with $retries tries left: $!"); | |||||||||||||||||||||||||||
| 145 | 0 | sleep(1); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | $self->log(1,"Ran out of accept retries!"); | |||||||||||||||||||||||||||
| 149 | 0 | return undef; ## no critic | |||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 92 | ▶ | sub accept_multi_port { | ||||||||||||||||||||||||||
|
Subroutine
accept_multi_port: called CC=7 SCAR=34.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 154 | 92 | my $self = shift; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 92 | ▶ | my $config = $self->{config} || get_config(); | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{'config'} || get_config()
MC/DC0%0/2
$self->{'config'} || get_config()
$self->{'config'}
get_config()
Truth table33%1/3
A || B
A= $self->{'config'}B= get_config()
| |||||||||||||||||||||||||||||
| 157 | 92 | ▶ | return $self->SUPER unless $config->{'patch_net_server'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | my $prop = $self->{'server'}; | |||||||||||||||||||||||||||
| 160 | 0 | while (1) { | |||||||||||||||||||||||||||
| 161 | 0 | my @waiting = $prop->{'select'}->can_read(); | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | next if !@waiting && $! == EINTR; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not @waiting and $! == 4
MC/DC0%0/2
not @waiting and $! == 4
not @waiting
$! == 4
Truth table0%0/3
not(A) and B
A= not @waitingB= $! == 4
| |||||||||||||||||||||||||||||
| 163 | 0 | ▶ | return (undef, 1) if grep { fileno($_) == fileno($prop->{'SigHUPReadPipe'}) } @waiting; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | 0 | ▶ | return (undef, 0) if ! @waiting; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 165 | 0 | return ($waiting[rand @waiting], 0); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 143 | 1; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | __END__ | ||||||||||||||||||||||||||||