Additional patch to qpsmtpd-prefork from Lars Roland:

Patch against current svn which removes references to highperf, and
  various other cleanups in the code.  

git-svn-id: https://svn.perl.org/qpsmtpd/branches/0.3x@641 958fd67b-6ff1-0310-b445-bb7760255be9
This commit is contained in:
John Peacock 2006-06-01 14:13:44 +00:00
parent e9e95dd09b
commit 9bb950d1d0

View File

@ -6,9 +6,6 @@
# See the LICENSE file for details. # See the LICENSE file for details.
# #
# For more information see http://develooper.com/code/qpsmtpd/ # For more information see http://develooper.com/code/qpsmtpd/
#
# Last updated: 05-05-2006
# Reviewed by: DA, LR
# safety guards # safety guards
use strict; use strict;
@ -40,7 +37,7 @@ my $ipcs = '/usr/bin/ipcs';
my $xargs = '/usr/bin/xargs'; my $xargs = '/usr/bin/xargs';
# vars we need # vars we need
my $chld_shmem; #shared memory to keep track of children (and their connections) my $chld_shmem; # shared mem to keep track of children (and their connections)
my %children; my %children;
my $chld_pool; my $chld_pool;
my $chld_busy; my $chld_busy;
@ -56,10 +53,8 @@ my $max_children = 15; #max number of child processes to spawn
my $idle_children = 5; # number of idle child processes to spawn my $idle_children = 5; # number of idle child processes to spawn
my $maxconnip = 10; my $maxconnip = 10;
my $child_lifetime = 100; # number of times a child may be reused my $child_lifetime = 100; # number of times a child may be reused
my $loop_sleep = my $loop_sleep = 30; # seconds main_loop sleeps before checking children
30; #max number of seconds main_loop sleeps before checking for busy children my $re_nice = 5; # substracted from parents current nice level
my $re_nice = 5
; #nice process (parent process is reniced with number substracted from current nice level)
my $d_start = 0; my $d_start = 0;
my $quiet = 0; my $quiet = 0;
my $status = 0; my $status = 0;
@ -69,7 +64,7 @@ my $user;
# help text # help text
sub usage { sub usage {
print <<"EOT"; print <<"EOT";
Usage: qpsmtpd-highperf [ options ] Usage: qpsmtpd-prefork [ options ]
--quiet : Be quiet (even errors are suppressed) --quiet : Be quiet (even errors are suppressed)
--version : Show version information --version : Show version information
--debug : Enable debug output --debug : Enable debug output
@ -99,24 +94,23 @@ GetOptions(
'user=s' => \$user, 'user=s' => \$user,
'renice-parent=i' => \$re_nice, 'renice-parent=i' => \$re_nice,
'help' => \&usage, 'help' => \&usage,
) ) || &usage;
|| &usage;
# misc checks # set max from ip to max number of children if option is set to disabled
$maxconnip = $max_children $maxconnip = $max_children if ($maxconnip == 0);
if ($maxconnip == 0)
; #set max from ip to max number of children if option is set to disabled #to fix limit counter error in plugin <hosts_allow>
$maxconnip++; #to fix limit counter error in plugin <hosts_allow> $maxconnip++;
#ensure that idle_children matches value given to max_children
$idle_children = $max_children $idle_children = $max_children
if (!$idle_children || $idle_children > $max_children || $idle_children < -1) if (!$idle_children || $idle_children > $max_children || $idle_children < -1);
; #ensure that idle_children matches value given to max_children
$chld_pool = $idle_children; $chld_pool = $idle_children;
run(); run();
#start daemon #start daemon
sub run { sub run {
# get UUID/GUID # get UUID/GUID
my ($uuid, $ugid, $group); my ($uuid, $ugid, $group);
if ($user) { if ($user) {
@ -148,9 +142,8 @@ sub run {
. "wait 20 secs before starting daemon again)\n" . "wait 20 secs before starting daemon again)\n"
unless $d; unless $d;
info( info("qpsmtpd-prefork daemon, version: $VERSION, staring on host: " .
"qpsmtpd-highperf daemon, version: $VERSION, staring on host: $d_addr, port: $d_port (user: $user [$<])" "$d_addr, port: $d_port (user: $user [$<])");
);
# reset priority # reset priority
my $old_nice = getpriority(0, 0); my $old_nice = getpriority(0, 0);
@ -160,8 +153,8 @@ sub run {
info("parent daemon nice level: $1"); info("parent daemon nice level: $1");
} }
else { else {
die die "FATAL: new nice level: $new_nice is not between -19 and 19 "
"FATAL: new nice level: $new_nice is not between -19 and 19 (old level = $old_nice, renice value = $re_nice)"; . "(old level = $old_nice, renice value = $re_nice)";
} }
if ($user) { if ($user) {
@ -178,18 +171,26 @@ sub run {
$chld_shmem = shmem("qpsmtpd", 1); $chld_shmem = shmem("qpsmtpd", 1);
untie $chld_shmem; untie $chld_shmem;
# Interrupt handler
$SIG{INT} = $SIG{TERM} = sub { $SIG{INT} = $SIG{TERM} = sub {
# terminate daemon (and children) # terminate daemon (and children)
my $sig = shift; my $sig = shift;
$SIG{$sig} = $SIG{CHLD} = $SIG{HUP} = 'IGNORE'; #prevent another signal and disable reaper
# prevent another signal and disable reaper
$SIG{$sig} = $SIG{CHLD} = $SIG{HUP} = 'IGNORE';
unlink("$PID"); unlink("$PID");
$d->close(); #close socket
# close socket
$d->close();
my $cnt = kill 'INT' => keys %children; my $cnt = kill 'INT' => keys %children;
IPC::Shareable->clean_up; #cleanup shared memory
# cleanup shared memory
IPC::Shareable->clean_up;
info("shutdown of daemon (and $cnt children)"); info("shutdown of daemon (and $cnt children)");
exit; exit;
}; };
# Hup handler
$SIG{HUP} = sub { $SIG{HUP} = sub {
# reload qpmstpd plugins # reload qpmstpd plugins
$qpsmtpd->load_plugins; $qpsmtpd->load_plugins;
@ -209,7 +210,6 @@ sub run {
# initialize children (only done at daemon startup) # initialize children (only done at daemon startup)
sub spawn_children { sub spawn_children {
# block signals while new children are being spawned # block signals while new children are being spawned
my $sigset = block_signal(SIGCHLD); my $sigset = block_signal(SIGCHLD);
for (1 .. $chld_pool) { for (1 .. $chld_pool) {
@ -228,14 +228,13 @@ sub reaper {
my $res = WEXITSTATUS($?); my $res = WEXITSTATUS($?);
info("child terminated, pid: $stiff (status $?, res: $res)"); info("child terminated, pid: $stiff (status $?, res: $res)");
delete $children{$stiff}; # delete pid from children delete $children{$stiff}; # delete pid from children
push @stiffs, $stiff # add pid to array so it later can be removed from shared memory
; #add pid to array so it later can be removed from shared memory push @stiffs, $stiff;
} }
#remove connection info from shared memory # remove connection info from shared memory and get number
$chld_busy = # of busy children (use by main_loop)
shmem_opt(undef, \@stiffs, undef, undef) $chld_busy = shmem_opt(undef, \@stiffs, undef, undef);
; #and get number of busy children (use by main_loop)
$SIG{CHLD} = \&reaper; $SIG{CHLD} = \&reaper;
} }
@ -244,22 +243,25 @@ sub reaper {
#ret0: void #ret0: void
sub main_loop { sub main_loop {
while (1) { while (1) {
# sleep EXPR seconds or until signal (i.e. child death) is received # sleep EXPR seconds or until signal (i.e. child death) is received
my $sleept = sleep $loop_sleep; my $sleept = sleep $loop_sleep;
# block CHLD signals to avoid race, anyway does it matter? # block CHLD signals to avoid race, anyway does it matter?
my $sigset = block_signal(SIGCHLD); my $sigset = block_signal(SIGCHLD);
# get number of busy children, if sleep wasn't interrupted by signal
$chld_busy = shmem_opt(undef, undef, undef, undef, 1) $chld_busy = shmem_opt(undef, undef, undef, undef, 1)
if ($sleept == $loop_sleep) if ($sleept == $loop_sleep);
; #get number of busy children, if sleep wasn't interrupted by signal
# calculate children in pool (if valid busy children number) # calculate children in pool (if valid busy children number)
if (defined($chld_busy)) { if (defined($chld_busy)) {
info("busy children: $chld_busy"); info("busy children: $chld_busy");
$chld_pool = $chld_busy + $idle_children; $chld_pool = $chld_busy + $idle_children;
} }
$chld_pool = $max_children
if ($chld_pool > $max_children); #ensure pool limit is max_children # ensure pool limit is max_children
$chld_pool = $max_children if ($chld_pool > $max_children);
# spawn children # spawn children
for (my $i = scalar(keys %children) ; $i < $chld_pool ; $i++) { for (my $i = scalar(keys %children) ; $i < $chld_pool ; $i++) {
new_child(); # add to the child pool new_child(); # add to the child pool
@ -287,7 +289,6 @@ sub block_signal {
or die "Could not block @signal signals: $!\n"; or die "Could not block @signal signals: $!\n";
return ($sigset); return ($sigset);
} }
# unblock_signal: unblock/reset and receive pending signals # unblock_signal: unblock/reset and receive pending signals
@ -295,22 +296,18 @@ sub block_signal {
# ret0: void # ret0: void
sub unblock_signal { sub unblock_signal {
my $sigset = shift; # arg0 my $sigset = shift; # arg0
sigprocmask(SIG_SETMASK, $sigset) sigprocmask(SIG_SETMASK, $sigset)
or die "Could not restore signals: $!\n"; or die "Could not restore signals: $!\n";
} }
# new_child: initialize new child # new_child: initialize new child
# arg0: void # arg0: void
# ret0: void # ret0: void
sub new_child { sub new_child {
# daemonize away from the parent process # daemonize away from the parent process
my $pid; my $pid;
die "Cannot fork child: $!\n" unless defined($pid = fork); die "Cannot fork child: $!\n" unless defined($pid = fork);
if ($pid) { if ($pid) {
# in parent # in parent
$children{$pid} = 1; $children{$pid} = 1;
info("new child, pid: $pid"); info("new child, pid: $pid");
@ -329,7 +326,8 @@ sub new_child {
or die "Could not unblock SIGHUP signal: $!\n"; or die "Could not unblock SIGHUP signal: $!\n";
$SIG{CHLD} = $SIG{INT} = $SIG{TERM} = $SIG{ALRM} = 'DEFAULT'; $SIG{CHLD} = $SIG{INT} = $SIG{TERM} = $SIG{ALRM} = 'DEFAULT';
# child should exit if it receives HUP signal (note: blocked while child is busy, but restored once done) # child should exit if it receives HUP signal (note: blocked while child
# is busy, but restored once done)
$SIG{HUP} = sub { $SIG{HUP} = sub {
info("signal HUP received, going to exit"); info("signal HUP received, going to exit");
exit 1; exit 1;
@ -337,7 +335,6 @@ sub new_child {
# continue to accept connections until "old age" is reached # continue to accept connections until "old age" is reached
for (my $i = 0 ; $i < $child_lifetime ; $i++) { for (my $i = 0 ; $i < $child_lifetime ; $i++) {
# accept a connection # accept a connection
$0 = 'qpsmtpd child'; # set pretty child name in process listing $0 = 'qpsmtpd child'; # set pretty child name in process listing
my ($client, $iinfo) = $d->accept() my ($client, $iinfo) = $d->accept()
@ -355,16 +352,14 @@ sub new_child {
# connection recieved, block signals # connection recieved, block signals
my $sigset = block_signal(SIGHUP); my $sigset = block_signal(SIGHUP);
#start new qpsmtpd session # start a session if connection looks valid
qpsmtpd_session($client, $qpsmtpd) qpsmtpd_session($client, $qpsmtpd) if ($iinfo);
if ($iinfo); #only start a session if connection looks valid
# close connection and cleanup # close connection and cleanup
$client->shutdown(2); $client->shutdown(2);
# unset block and receive pending signals # unset block and receive pending signals
unblock_signal($sigset); unblock_signal($sigset);
} }
exit; # this child has reached its end-of-life exit; # this child has reached its end-of-life
} }
@ -415,8 +410,8 @@ sub shmem {
my %shmem_hash; my %shmem_hash;
eval { eval {
tie %shmem_hash, 'IPC::Shareable', $glue, tie %shmem_hash, 'IPC::Shareable', $glue, {%options}
{%options} || die "unable to tie to shared memory - $!"; || die "unable to tie to shared memory - $!";
}; };
if ($@) { if ($@) {
info("$@"); info("$@");
@ -441,16 +436,17 @@ sub shmem_opt {
my $check = shift || 0; #arg4 my $check = shift || 0; #arg4
# check arguments # check arguments
return
if ( (defined($pid_add_key) && !defined($pid_add_value)) if ( (defined($pid_add_key) && !defined($pid_add_value))
|| (!defined($pid_add_key) && defined($pid_add_value))); || (!defined($pid_add_key) && defined($pid_add_value)))
{
return;
}
my ($chld_shmem, $chld_busy); my ($chld_shmem, $chld_busy);
eval { eval {
$chld_shmem = &shmem("qpsmtpd", 0); #connect to shared memory hash $chld_shmem = &shmem("qpsmtpd", 0); #connect to shared memory hash
if (tied %{$chld_shmem}) { if (tied %{$chld_shmem}) {
# perform options # perform options
(tied %{$chld_shmem})->shlock(LOCK_EX); (tied %{$chld_shmem})->shlock(LOCK_EX);
@ -460,15 +456,16 @@ sub shmem_opt {
delete $$chld_shmem{$pid_del}; delete $$chld_shmem{$pid_del};
} }
} }
$$chld_shmem{$pid_add_key} = $pid_add_value if ($pid_add_key); #add # add
%{$ref_shmem} = %{$chld_shmem} if($ref_shmem); #copy $$chld_shmem{$pid_add_key} = $pid_add_value if ($pid_add_key);
#loop through pid list and delete orphaned processes # copy
%{$ref_shmem} = %{$chld_shmem} if ($ref_shmem);
if ($check) { if ($check) {
# loop through pid list and delete orphaned processes
foreach my $pid (keys %{$chld_shmem}) { foreach my $pid (keys %{$chld_shmem}) {
if (!kill 0, $pid) { if (!kill 0, $pid) {
delete $$chld_shmem{$pid}; delete $$chld_shmem{$pid};
warn( warn("orphaned child, pid: $pid removed from memory");
"orphaned child, pid: $pid - removed from shared memory");
} }
} }
} }
@ -476,9 +473,9 @@ sub shmem_opt {
# count number of busy children # count number of busy children
$chld_busy = scalar(keys %{$chld_shmem}); $chld_busy = scalar(keys %{$chld_shmem});
(tied %{$chld_shmem})->shunlock; (tied %{$chld_shmem})->shunlock;
untie $chld_shmem
|| die # untie from shared memory
"unable to untie from shared memory"; #untie from shared memory untie $chld_shmem || die "unable to untie from shared memory";
} }
}; };
@ -536,8 +533,8 @@ sub qpsmtpd_session {
|| $rc == DENY || $rc == DENY
|| $rc == DENY_DISCONNECT) || $rc == DENY_DISCONNECT)
{ {
my $rc_reply = #smtp return code to reply client with (seed with soft deny)
451; #smtp return code to reply client with (seed with soft deny) my $rc_reply = 451;
unless ($msg[0]) { unless ($msg[0]) {
if ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) { if ($rc == DENYSOFT || $rc == DENYSOFT_DISCONNECT) {
@msg = ("Sorry, try again later"); @msg = ("Sorry, try again later");
@ -548,18 +545,24 @@ sub qpsmtpd_session {
} }
} }
respond_client($client, $rc_reply, @msg); respond_client($client, $rc_reply, @msg);
shmem_opt(undef, [$$], undef, undef); #remove pid from shared memory
return; #retur so child can be reused # remove pid from shared memory
shmem_opt(undef, [$$], undef, undef);
# retur so child can be reused
return;
} }
# all children should have different seeds, to prevent conflicts # all children should have different seeds, to prevent conflicts
srand(time ^ ($$ + ($$ << 15))); srand(time ^ ($$ + ($$ << 15)));
# $SIG{$_} = 'DEFAULT' for keys %SIG; # ALRM handler
$SIG{ALRM} = sub { $SIG{ALRM} = sub {
print $client "421 Connection Timed Out\n"; print $client "421 Connection Timed Out\n";
info("Connection Timed Out"); info("Connection Timed Out");
exit 1; #this will kill the child, but who cares?
# kill the child
exit 1;
}; };
# set enviroment variables # set enviroment variables
@ -583,10 +586,8 @@ sub qpsmtpd_session {
warn("$@"); warn("$@");
} }
#done - this child is now idle again # child is now idle again so remove it's pid from shared mem
shmem_opt(undef, [$$], undef, undef); #remove pid from shared memory shmem_opt(undef, [$$], undef, undef);
info("remote host: $ENV{TCPREMOTEIP} left..."); info("remote host: $ENV{TCPREMOTEIP} left...");
} }