--- loncom/loncnew 2003/06/25 01:54:44 1.11 +++ loncom/loncnew 2003/07/02 01:31:55 1.13 @@ -2,7 +2,7 @@ # The LearningOnline Network with CAPA # lonc maintains the connections to remote computers # -# $Id: loncnew,v 1.11 2003/06/25 01:54:44 foxr Exp $ +# $Id: loncnew,v 1.13 2003/07/02 01:31:55 foxr Exp $ # # Copyright Michigan State University Board of Trustees # @@ -46,6 +46,9 @@ # Change log: # $Log: loncnew,v $ +# Revision 1.13 2003/07/02 01:31:55 foxr +# Added kill -HUP logic (restart). +# # Revision 1.11 2003/06/25 01:54:44 foxr # Fix more problems with transaction failure. # @@ -71,6 +74,7 @@ use lib "/home/httpd/lib/perl/"; use lib "/home/foxr/newloncapa/types"; use Event qw(:DEFAULT ); use POSIX qw(:signal_h); +use POSIX; use IO::Socket; use IO::Socket::INET; use IO::Socket::UNIX; @@ -287,11 +291,7 @@ Invoked each timer tick. sub Tick { my $client; ShowStatus(GetServerHost()." Connection count: ".$ConnectionCount); - Debug(10,"Tick"); - Debug(10," Current connection count: ".$ConnectionCount); - foreach $client (keys %ActiveClients) { - Debug(10," Have client: with id: ".$ActiveClients{$client}); - } + # Is it time to prune connection count: @@ -523,6 +523,7 @@ sub StartClientReply { my $Transaction = shift; my $data = shift; + my $Client = $Transaction->getClient(); &Debug(8," Reply was: ".$data); @@ -716,8 +717,10 @@ sub LondReadable { my $State = $Socket->GetState(); # All action depends on the state. SocketDump(6, $Socket); + my $status = $Socket->Readable(); + &Debug(2, "Socket->Readable returned: $status"); - if($Socket->Readable() != 0) { + if($status != 0) { # bad return from socket read. Currently this means that # The socket has become disconnected. We fail the transaction. @@ -982,7 +985,7 @@ sub QueueDelayed { my $Handle = IO::File->new($reqfile); my $cmd = <$Handle>; chomp $cmd; # There may or may not be a newline... - $cmd = $cmd."\ny"; # now for sure there's exactly one newline. + $cmd = $cmd."\n"; # now for sure there's exactly one newline. my $Transaction = LondTransaction->new($cmd); $Transaction->SetDeferred($reqfile); QueueTransaction($Transaction); @@ -1293,9 +1296,9 @@ Called in response to a signal that caus =cut -=pod sub SignalledToDeath { + Debug(2,"Signalled to death!"); my ($signal) = @_; chomp($signal); Log("CRITICAL", "Abnormal exit. Child $$ for $RemoteHost " @@ -1303,6 +1306,7 @@ sub SignalledToDeath { LogPerm("F:lonc: $$ on $RemoteHost signalled to death: " ."\"$signal\""); die("Signal abnormal end"); + exit 0; } =head2 ChildProcess @@ -1319,7 +1323,7 @@ sub ChildProcess { $SIG{QUIT} = \&SignalledToDeath; $SIG{HUP} = IGNORE; $SIG{USR1} = IGNORE; - $SIG{INT} = IGNORE; + $SIG{INT} = DEFAULT; $SIG{CHLD} = IGNORE; $SIG{__DIE__} = \&SignalledToDeath; @@ -1346,15 +1350,21 @@ sub ChildProcess { # Create a new child for host passed in: sub CreateChild { + my $sigset = POSIX::SigSet->new(SIGINT); + sigprocmask(SIG_BLOCK, $sigset); my $host = shift; $RemoteHost = $host; Log("CRITICAL", "Forking server for ".$host); $pid = fork; if($pid) { # Parent $ChildHash{$pid} = $RemoteHost; + sigprocmask(SIG_UNBLOCK, $sigset); + } else { # child. ShowStatus("Connected to ".$RemoteHost); - ChildProcess; + $SIG{INT} = DEFAULT; + sigprocmask(SIG_UNBLOCK, $sigset); + ChildProcess; # Does not return. } } @@ -1410,6 +1420,7 @@ while (! $HostIterator->end()) { CreateChild($hostentryref->[0]); $HostIterator->next(); } +$RemoteHost = "Parent Server"; # Maintain the population: @@ -1418,8 +1429,10 @@ ShowStatus("Parent keeping the flock"); # # Set up parent signals: # -$SIG{INT} = &KillThemAll; -$SIG{TERM} = &KillThemAll; + +$SIG{INT} = \&KillThemAll; +$SIG{TERM} = \&KillThemAll; +$SIG{HUP} = \&Restart; while(1) { $deadchild = wait(); @@ -1432,9 +1445,50 @@ while(1) { CreateChild($deadhost); } } + +=pod + +=head1 Restart + +Signal handler for HUP... all children are killed and +we self restart. This is an el-cheapo way to re read +the config file. + +=cut + +sub Restart { + KillThemAll; # First kill all the children. + Log("CRITICAL", "Restarting"); + my $execdir = $perlvar{'lonDaemons'}; + unlink("$execdir/logs/lonc.pid"); + exec("$execdir/lonc"); +} + +=pod + +=head1 KillThemAll + +Signal handler that kills all children by sending them a +SIGINT. Responds to sigint and sigterm. + +=cut + sub KillThemAll { + Debug(2, "Kill them all!!"); + local($SIG{CHLD}) = 'IGNORE'; # Our children >will< die. + foreach $pid (keys %ChildHash) { + my $serving = $ChildHash{$pid}; + Debug(2, "Killing lonc for $serving pid = $pid"); + ShowStatus("Killing lonc for $serving pid = $pid"); + Log("CRITICAL", "Killing lonc for $serving pid = $pid"); + kill('INT', $pid); + } + Log("CRITICAL", "Killing the master process."); + exit } +=pod + =head1 Theory The event class is used to build this as a single process with an