| 1 | package Mail::POP3::Daemon; | ||||||||||||
| 2 | |||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 4 | ▶ | use IO::Socket; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 4 | ▶ | use IO::File; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | 4 | ▶ | use POSIX; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | # horrible globals! | ||||||||||||
| 9 | 4 | my $CONFIG_FILE = './mpopd.conf'; | |||||||||||
| 10 | |||||||||||||
| 11 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 12 | 0 | my ($class, $config, $connection_class) = @_; | |||||||||||
| 13 | 0 | my $self = {}; | |||||||||||
| 14 | 0 | bless $self, $class; | |||||||||||
| 15 | 0 | $self->{CONFIG} = $config; | |||||||||||
| 16 | 0 | $self; | |||||||||||
| 17 | } | ||||||||||||
| 18 | |||||||||||||
| 19 | # server daemon that will fork | ||||||||||||
| 20 | 0 | ▶ | sub start { | ||||||||||
|
Subroutine
start: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||
| 21 | 0 | my ($self) = @_; | |||||||||||
| 22 | 0 | $self->_setup_sighandlers; | |||||||||||
| 23 | # Counter used to track number of children | ||||||||||||
| 24 | 0 | $self->{KID_COUNT} = 0; | |||||||||||
| 25 | # Build the server socket and bind to $self->{CONFIG}->{port} | ||||||||||||
| 26 | 0 | $self->_build_server; | |||||||||||
| 27 | # Listen for a client and fork off a child server process | ||||||||||||
| 28 | 0 | while (1) { | |||||||||||
| 29 | # Trap errors caused by peer-resets etc. | ||||||||||||
| 30 | 0 | my $client; | |||||||||||
| 31 | 0 | ▶ | unless ( eval { $client = $self->{SERVER_SOCKET}->accept } ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 32 | 0 | eval { $client->close; }; | |||||||||||
| 33 | 0 | next; | |||||||||||
| 34 | } | ||||||||||||
| 35 | # Close the connection if this one would exceed the maximum | ||||||||||||
| 36 | # concurrent servers allowed, $self->{CONFIG}->{max_servers}. | ||||||||||||
| 37 | 0 | ▶ | unless ($self->{KID_COUNT} < $self->{CONFIG}->{max_servers}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 0 | eval { $client->close; }; | |||||||||||
| 39 | 0 | sleep 2; | |||||||||||
| 40 | 0 | next; | |||||||||||
| 41 | } | ||||||||||||
| 42 | # Try and fork | ||||||||||||
| 43 | 0 | my $kidpid; | |||||||||||
| 44 | 0 | ▶ | if ($kidpid = fork) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 45 | 0 | ++$self->{KID_COUNT}; | |||||||||||
| 46 | 0 | $self->{KID_PIDS}->{$kidpid} = 1; | |||||||||||
| 47 | 0 | next; | |||||||||||
| 48 | } | ||||||||||||
| 49 | # If fork fails log the event to a file | ||||||||||||
| 50 | 0 | ▶ | unless (defined $kidpid) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 51 | $self->_to_file( | ||||||||||||
| 52 | $self->{CONFIG}->{fork_alert}, | ||||||||||||
| 53 | 0 | "Fork failed at: " . localtime(time) . ": $!\n", | |||||||||||
| 54 | ); | ||||||||||||
| 55 | 0 | eval { $client->close; }; | |||||||||||
| 56 | 0 | next; | |||||||||||
| 57 | } | ||||||||||||
| 58 | # Everything below here (almost) belongs to the child server. | ||||||||||||
| 59 | 0 | $self->{SERVER_SOCKET}->close; # Close clone of SERVER handle. | |||||||||||
| 60 | my $server = Mail::POP3::Server->new( | ||||||||||||
| 61 | $self->{CONFIG}, | ||||||||||||
| 62 | 0 | ); | |||||||||||
| 63 | 0 | $server->start( | |||||||||||
| 64 | $client, | ||||||||||||
| 65 | $client, | ||||||||||||
| 66 | $client->peerhost, | ||||||||||||
| 67 | ); | ||||||||||||
| 68 | } | ||||||||||||
| 69 | } | ||||||||||||
| 70 | |||||||||||||
| 71 | # Create parent socket and bind to POP3 port (or custom port) | ||||||||||||
| 72 | 0 | ▶ | sub _build_server { | ||||||||||
|
Subroutine
_build_server: not called CC=2 SCAR=17.9
| |||||||||||||
| 73 | 0 | my $self = shift; | |||||||||||
| 74 | my $server_socket = IO::Socket::INET->new( | ||||||||||||
| 75 | LocalPort => $self->{CONFIG}->{port}, | ||||||||||||
| 76 | 0 | ▶ | Proto => 'tcp', | ||||||||||
|
Condition0%0/2
'IO::Socket::INET'->new('LocalPort', $self->{'CONFIG'}{'port'}, 'Proto', 'tcp', 'Reuse', 1, 'Listen', 4096) || die("Couldn't bind to port $self->{'CONFIG'}{'port'} : $!")
MC/DC0%0/1
'IO::Socket::INET'->new('LocalPort', $self->{'CONFIG'}{'port'}, 'Proto', 'tcp', 'Reuse', 1, 'Listen', 4096) || die("Couldn't bind to port $self->{'CONFIG'}{'port'} : $!")
'IO::Socket::INET'->new('LocalPort', $self->{'CONFIG'}{'port'}, 'Proto', 'tcp', 'Reuse', 1, 'Listen', 4096)
Truth table0%0/2
A
A= 'IO::Socket::INET'->new('LocalPort', $self->{'CONFIG'}{'port'}, 'Proto', 'tcp', 'Reuse', 1, 'Listen', 4096)
| |||||||||||||
| 77 | Reuse => 1, | ||||||||||||
| 78 | Listen => IO::Socket::SOMAXCONN, | ||||||||||||
| 79 | ) || die "Couldn't bind to port $self->{CONFIG}->{port} : $!"; | ||||||||||||
| 80 | 0 | $self->{SERVER_SOCKET} = $server_socket; | |||||||||||
| 81 | # Write a pid file with the port used on line 2 | ||||||||||||
| 82 | $self->_to_file( | ||||||||||||
| 83 | $self->{CONFIG}->{mpopd_pid_file}, | ||||||||||||
| 84 | 0 | "$$\n$self->{CONFIG}->{port}\n", | |||||||||||
| 85 | ); | ||||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 0 | ▶ | sub MPOPDQUIT { | ||||||||||
|
Subroutine
MPOPDQUIT: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||
| 90 | 0 | my $key; | |||||||||||
| 91 | 0 | foreach $key (keys %{ $self->{KID_PIDS} }) { | |||||||||||
| 92 | 0 | kill "USR1", $key; | |||||||||||
| 93 | 0 | $self->REAPER; | |||||||||||
| 94 | } | ||||||||||||
| 95 | 0 | $self->{SERVER_SOCKET}->close; | |||||||||||
| 96 | 0 | unlink $self->{CONFIG}->{mpopd_pid_file}; | |||||||||||
| 97 | 0 | exit; | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | sub READCONFIG { | ||||||||||
|
Subroutine
READCONFIG: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||
| 102 | 0 | $self->{CONFIG} = (ref $self)->read_config($CONFIG_FILE); | |||||||||||
| 103 | 0 | $SIG{USR1} = $self->_make_closure(\&READCONFIG); | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | sub MPOPDRESTART { | ||||||||||
|
Subroutine
MPOPDRESTART: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||
| 108 | 0 | kill "USR1", keys %{ $self->{KID_PIDS} }; | |||||||||||
| 109 | 0 | $self->REAPER; | |||||||||||
| 110 | 0 | $self->{SERVER_SOCKET}->close; | |||||||||||
| 111 | 0 | $self->READCONFIG; | |||||||||||
| 112 | 0 | $self->_build_server; | |||||||||||
| 113 | 0 | $SIG{HUP} = $self->_make_closure(\&MPOPDRESTART); | |||||||||||
| 114 | 0 | $SIG{PIPE} = $self->_make_closure(\&MPOPDRESTART); | |||||||||||
| 115 | 0 | $SIG{INT} = $self->_make_closure(\&MPOPDRESTART); | |||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | sub REAPER { | ||||||||||
|
Subroutine
REAPER: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 119 | 0 | my $self = shift; | |||||||||||
| 120 | 0 | ▶ | while ((my $kidpid = waitpid(-1, POSIX::WNOHANG)) > 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | 0 | --$self->{KID_COUNT}; | |||||||||||
| 122 | 0 | delete $self->{KID_PIDS}->{$kidpid}; | |||||||||||
| 123 | } | ||||||||||||
| 124 | 0 | $SIG{CHLD} = $self->_make_closure(\&REAPER); | |||||||||||
| 125 | } | ||||||||||||
| 126 | |||||||||||||
| 127 | 0 | ▶ | sub _setup_sighandlers { | ||||||||||
|
Subroutine
_setup_sighandlers: not called CC=1 SCAR=6.9
| |||||||||||||
| 128 | 0 | my $self = shift; | |||||||||||
| 129 | # do this with lexical closures | ||||||||||||
| 130 | # Try and rescue a broken pipe by rebuilding | ||||||||||||
| 131 | # the server-socket etc. | ||||||||||||
| 132 | 0 | $SIG{PIPE} = $self->_make_closure(\&MPOPDRESTART); | |||||||||||
| 133 | # Ignore alarm signals from kernel | ||||||||||||
| 134 | 0 | $SIG{ALRM} = "IGNORE"; | |||||||||||
| 135 | # If we get a plain kill then try and close down all child | ||||||||||||
| 136 | # servers, remove pid file and exit. | ||||||||||||
| 137 | 0 | $SIG{TERM} = $self->_make_closure(\&MPOPDQUIT); | |||||||||||
| 138 | 0 | $SIG{INT} = $self->_make_closure(\&MPOPDQUIT); | |||||||||||
| 139 | # Just re-read the config file on a SIGUSR1, don't restart. | ||||||||||||
| 140 | 0 | $SIG{USR1} = $self->_make_closure(\&READCONFIG); | |||||||||||
| 141 | # If we receive a SIGHUP kill off the forked servers gracefully(?) | ||||||||||||
| 142 | # with a SIGUSR1, close and re-open the server socket, reset as much | ||||||||||||
| 143 | # as possible and then re-read the config file. | ||||||||||||
| 144 | 0 | $SIG{HUP} = $self->_make_closure(\&MPOPDRESTART); | |||||||||||
| 145 | # Catch SIGCHLD | ||||||||||||
| 146 | 0 | $SIG{CHLD} = $self->_make_closure(\&REAPER); | |||||||||||
| 147 | } | ||||||||||||
| 148 | |||||||||||||
| 149 | 0 | ▶ | sub _make_closure { | ||||||||||
|
Subroutine
_make_closure: not called CC=1 SCAR=6.9
| |||||||||||||
| 150 | 0 | my ($self, $subref) = @_; | |||||||||||
| 151 | 0 | ▶ | sub { $subref->($self) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | 0 | ▶ | sub _to_file { | ||||||||||
|
Subroutine
_to_file: not called CC=1 SCAR=6.9
| |||||||||||||
| 155 | 0 | my ($self, $file, $data) = @_; | |||||||||||
| 156 | 0 | local *FH; | |||||||||||
| 157 | 0 | open FH, ">$file"; | |||||||||||
| 158 | 0 | print FH $data; | |||||||||||
| 159 | 0 | close FH; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 4 | 1; | |||||||||||
| 163 | |||||||||||||
| 164 | =head1 METHODS | ||||||||||||
| 165 | |||||||||||||
| 166 | =head2 new | ||||||||||||
| 167 | |||||||||||||
| 168 | Takes C<$config> and C<$connection_class>. | ||||||||||||
| 169 | |||||||||||||
| 170 | =head2 start | ||||||||||||
| 171 | |||||||||||||
| 172 | Starts forking server. | ||||||||||||
| 173 | |||||||||||||
| 174 | =head2 MPOPDQUIT | ||||||||||||
| 175 | |||||||||||||
| 176 | Signal handler, shuts down. | ||||||||||||
| 177 | |||||||||||||
| 178 | =head2 MPOPDRESTART | ||||||||||||
| 179 | |||||||||||||
| 180 | Signal handler, restarts. | ||||||||||||
| 181 | |||||||||||||
| 182 | =head2 READCONFIG | ||||||||||||
| 183 | |||||||||||||
| 184 | Signal handler, re-reads config. | ||||||||||||
| 185 | |||||||||||||
| 186 | =head2 REAPER | ||||||||||||
| 187 | |||||||||||||
| 188 | Signal handler, reaps zombies. | ||||||||||||