File:  [LON-CAPA] / loncom / loncnew
Revision 1.12: download - view: text, annotated - select for diffs
Wed Jul 2 01:12:35 2003 UTC (20 years, 10 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
- Add some debugging to killthemall
- Add better error handling to LondReadable
- Remove tick logging in the timer handler.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: # lonc maintains the connections to remote computers
    4: #
    5: # $Id: loncnew,v 1.12 2003/07/02 01:12:35 foxr Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: #
   11: # LON-CAPA is free software; you can redistribute it and/or modify
   12: # it under the terms of the GNU General Public License as published by
   13: # the Free Software Foundation; either version 2 of the License, or
   14: # (at your option) any later version.
   15: #
   16: # LON-CAPA is distributed in the hope that it will be useful,
   17: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   18: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   19: # GNU General Public License for more details.
   20: #
   21: # You should have received a copy of the GNU General Public License
   22: # along with LON-CAPA; if not, write to the Free Software
   23: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   24: #
   25: # /home/httpd/html/adm/gpl.txt
   26: #
   27: # http://www.lon-capa.org/
   28: #
   29: #
   30: # new lonc handles n requestors spread out bver m connections to londs.
   31: # This module is based on the Event class.
   32: #   Development iterations:
   33: #    - Setup basic event loop.   (done)
   34: #    - Add timer dispatch.       (done)
   35: #    - Add ability to accept lonc UNIX domain sockets.  (done)
   36: #    - Add ability to create/negotiate lond connections (done).
   37: #    - Add general logic for dispatching requests and timeouts. (done).
   38: #    - Add support for the lonc/lond requests.          (done).
   39: #    - Add logging/status monitoring.
   40: #    - Add Signal handling - HUP restarts. USR1 status report.
   41: #    - Add Configuration file I/O                       (done).
   42: #    - Add management/status request interface.
   43: #    - Add deferred request capability.                  (done)
   44: #    - Detect transmission timeouts.
   45: #
   46: 
   47: # Change log:
   48: #    $Log: loncnew,v $
   49: #    Revision 1.12  2003/07/02 01:12:35  foxr
   50: #    - Add some debugging to killthemall
   51: #    - Add better error handling to LondReadable
   52: #    - Remove tick logging in the timer handler.
   53: #
   54: #    Revision 1.11  2003/06/25 01:54:44  foxr
   55: #    Fix more problems with transaction failure.
   56: #
   57: #    Revision 1.10  2003/06/24 02:46:04  foxr
   58: #    Put a limit on  the number of times we'll retry a connection.
   59: #    Start getting the signal stuff put in as well...note that need to get signals
   60: #    going or else 6the client will permanently give up on dead servers.
   61: #
   62: #    Revision 1.9  2003/06/13 02:38:43  foxr
   63: #    Add logging in 'expected format'
   64: #
   65: #    Revision 1.8  2003/06/11 02:04:35  foxr
   66: #    Support delayed transactions... this is done uniformly by encapsulating
   67: #    transactions in an object ... a LondTransaction that is implemented by
   68: #    LondTransaction.pm
   69: #
   70: #    Revision 1.7  2003/06/03 01:59:39  foxr
   71: #    complete coding to support deferred transactions.
   72: #
   73: #
   74: 
   75: use lib "/home/httpd/lib/perl/";
   76: use lib "/home/foxr/newloncapa/types";
   77: use Event qw(:DEFAULT );
   78: use POSIX qw(:signal_h);
   79: use POSIX;
   80: use IO::Socket;
   81: use IO::Socket::INET;
   82: use IO::Socket::UNIX;
   83: use IO::File;
   84: use IO::Handle;
   85: use Socket;
   86: use Crypt::IDEA;
   87: use LONCAPA::Queue;
   88: use LONCAPA::Stack;
   89: use LONCAPA::LondConnection;
   90: use LONCAPA::LondTransaction;
   91: use LONCAPA::Configuration;
   92: use LONCAPA::HashIterator;
   93: 
   94: 
   95: #
   96: #   Disable all signals we might receive from outside for now.
   97: #
   98: $SIG{QUIT}  = IGNORE;
   99: $SIG{HUP}   = IGNORE;
  100: $SIG{USR1}  = IGNORE;
  101: $SIG{INT}   = IGNORE;
  102: $SIG{CHLD}  = IGNORE;
  103: $SIG{__DIE__}  = IGNORE;
  104: 
  105: 
  106: # Read the httpd configuration file to get perl variables
  107: # normally set in apache modules:
  108: 
  109: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
  110: my %perlvar    = %{$perlvarref};
  111: 
  112: #
  113: #  parent and shared variables.
  114: 
  115: my %ChildHash;			# by pid -> host.
  116: 
  117: 
  118: my $MaxConnectionCount = 10;	# Will get from config later.
  119: my $ClientConnection = 0;	# Uniquifier for client events.
  120: 
  121: my $DebugLevel = 0;
  122: my $IdleTimeout= 3600;		# Wait an hour before pruning connections.
  123: 
  124: #
  125: #  The variables below are only used by the child processes.
  126: #
  127: my $RemoteHost;			# Name of host child is talking to.
  128: my $UnixSocketDir= "/home/httpd/sockets"; 
  129: my $IdleConnections = Stack->new(); # Set of idle connections
  130: my %ActiveConnections;		# Connections to the remote lond.
  131: my %ActiveTransactions;		# LondTransactions in flight.
  132: my %ActiveClients;		# Serial numbers of active clients by socket.
  133: my $WorkQueue       = Queue->new(); # Queue of pending transactions.
  134: my $ConnectionCount = 0;
  135: my $IdleSeconds     = 0;	# Number of seconds idle.
  136: my $Status          = "";	# Current status string.
  137: my $ConnectionRetries=5;	# Number of connection retries allowed.
  138: my $ConnectionRetriesLeft=5;	# Number of connection retries remaining.
  139: 
  140: #
  141: #   The hash below gives the HTML format for log messages
  142: #   given a severity.
  143: #    
  144: my %LogFormats;
  145: 
  146: $LogFormats{"CRITICAL"} = "<font color=red>CRITICAL: %s</font>";
  147: $LogFormats{"SUCCESS"}  = "<font color=green>SUCCESS: %s</font>";
  148: $LogFormats{"INFO"}     = "<font color=yellow>INFO: %s</font>";
  149: $LogFormats{"WARNING"}  = "<font color=blue>WARNING: %s</font>";
  150: $LogFormats{"DEFAULT"}  = " %s ";
  151: 
  152: 
  153: 
  154: =pod
  155: 
  156: =head2 LogPerm
  157: 
  158: Makes an entry into the permanent log file.
  159: 
  160: =cut
  161: sub LogPerm {
  162:     my $message=shift;
  163:     my $execdir=$perlvar{'lonDaemons'};
  164:     my $now=time;
  165:     my $local=localtime($now);
  166:     my $fh=IO::File->new(">>$execdir/logs/lonnet.perm.log");
  167:     print $fh "$now:$message:$local\n";
  168: }
  169: 
  170: =pod
  171: 
  172: =head2 Log
  173: 
  174: Logs a message to the log file.
  175: Parameters:
  176: 
  177: =item severity
  178: 
  179: One of CRITICAL, WARNING, INFO, SUCCESS used to select the
  180: format string used to format the message.  if the severity is
  181: not a defined severity the Default format string is used.
  182: 
  183: =item message
  184: 
  185: The base message.  In addtion to the format string, the message
  186: will be appended to a string containing the name of our remote
  187: host and the time will be formatted into the message.
  188: 
  189: =cut
  190: 
  191: sub Log {
  192:     my $severity = shift;
  193:     my $message  = shift;
  194:    
  195:     if(!$LogFormats{$severity}) {
  196: 	$severity = "DEFAULT";
  197:     }
  198: 
  199:     my $format = $LogFormats{$severity};
  200:     
  201:     #  Put the window dressing in in front of the message format:
  202: 
  203:     my $now   = time;
  204:     my $local = localtime($now);
  205:     my $finalformat = "$local ($$) [$RemoteHost] [$Status] ";
  206:     my $finalformat = $finalformat.$format."\n";
  207: 
  208:     # open the file and put the result.
  209: 
  210:     my $execdir = $perlvar{'lonDaemons'};
  211:     my $fh      = IO::File->new(">>$execdir/logs/lonc.log");
  212:     my $msg = sprintf($finalformat, $message);
  213:     print $fh $msg;
  214:     
  215:     
  216: }
  217: 
  218: 
  219: =pod
  220: 
  221: =head2 GetPeerName
  222: 
  223: Returns the name of the host that a socket object is connected to.
  224: 
  225: =cut
  226: 
  227: sub GetPeername {
  228:     my $connection = shift;
  229:     my $AdrFamily  = shift;
  230:     my $peer       = $connection->peername();
  231:     my $peerport;
  232:     my $peerip;
  233:     if($AdrFamily == AF_INET) {
  234: 	($peerport, $peerip) = sockaddr_in($peer);
  235: 	my $peername    = gethostbyaddr($iaddr, $AdrFamily);
  236: 	return $peername;
  237:     } elsif ($AdrFamily == AF_UNIX) {
  238: 	my $peerfile;
  239: 	($peerfile) = sockaddr_un($peer);
  240: 	return $peerfile;
  241:     }
  242: }
  243: #----------------------------- Timer management ------------------------
  244: =pod
  245: 
  246: =head2 Debug
  247: 
  248: Invoked to issue a debug message.
  249: 
  250: =cut
  251: 
  252: sub Debug {
  253:     my $level   = shift;
  254:     my $message = shift;
  255:     if ($level <= $DebugLevel) {
  256: 	print $message." host = ".$RemoteHost."\n";
  257:     }
  258: }
  259: 
  260: sub SocketDump {
  261:     my $level = shift;
  262:     my $socket= shift;
  263:     if($level <= $DebugLevel) {
  264: 	$socket->Dump();
  265:     }
  266: }
  267: 
  268: =pod
  269: 
  270: =head2 ShowStatus
  271: 
  272:  Place some text as our pid status.
  273:  and as what we return in a SIGUSR1
  274: 
  275: =cut
  276: sub ShowStatus {
  277:     my $state = shift;
  278:     my $now = time;
  279:     my $local = localtime($now);
  280:     $Status   = $local.": ".$state;
  281:     $0='lonc: '.$state.' '.$local;
  282: }
  283: 
  284: =pod
  285: 
  286: =head2 Tick
  287: 
  288: Invoked  each timer tick.
  289: 
  290: =cut
  291: 
  292: 
  293: sub Tick {
  294:     my $client;
  295:     ShowStatus(GetServerHost()." Connection count: ".$ConnectionCount);
  296: 
  297:     # Is it time to prune connection count:
  298: 
  299: 
  300:     if($IdleConnections->Count()  && 
  301:        ($WorkQueue->Count() == 0)) { # Idle connections and nothing to do?
  302: 	$IdleSeconds++;
  303: 	if($IdleSeconds > $IdleTimeout) { # Prune a connection...
  304: 	    $Socket = $IdleConnections->pop();
  305: 	    KillSocket($Socket);
  306: 	}
  307:     } else {
  308: 	$IdleSeconds = 0;	# Reset idle count if not idle.
  309:     }
  310: 
  311:     # Do we have work in the queue, but no connections to service them?
  312:     # If so, try to make some new connections to get things going again.
  313:     #
  314:     
  315:     my $Requests = $WorkQueue->Count();
  316:     if (($ConnectionCount == 0)  && ($Requests > 0)) { 
  317: 	if ($ConnectionRetriesLeft > 0) {
  318: 	    my $Connections = ($Requests <= $MaxConnectionCount) ?
  319: 		$Requests : $MaxConnectionCount;
  320: 	    Debug(1,"Work but no connections, start ".$Connections." of them");
  321: 	    for ($i =0; $i < $Connections; $i++) {
  322: 		MakeLondConnection();
  323: 	    }
  324: 	} else {
  325: 	    Debug(1,"Work in queue, but gave up on connections..flushing\n");
  326: 	    EmptyQueue();	# Connections can't be established.
  327: 	}
  328:        
  329:     }
  330: }
  331: 
  332: =pod
  333: 
  334: =head2 SetupTimer
  335: 
  336: Sets up a 1 per sec recurring timer event.  The event handler is used to:
  337: 
  338: =item
  339: 
  340: Trigger timeouts on communications along active sockets.
  341: 
  342: =item
  343: 
  344: Trigger disconnections of idle sockets.
  345: 
  346: =cut
  347: 
  348: sub SetupTimer {
  349:     Debug(6, "SetupTimer");
  350:     Event->timer(interval => 1, debug => 1, cb => \&Tick );
  351: }
  352: 
  353: =pod
  354: 
  355: =head2 ServerToIdle
  356: 
  357: This function is called when a connection to the server is
  358: ready for more work.
  359: 
  360: If there is work in the Work queue the top element is dequeued
  361: and the connection will start to work on it.  If the work queue is
  362: empty, the connection is pushed on the idle connection stack where
  363: it will either get another work unit, or alternatively, if it sits there
  364: long enough, it will be shut down and released.
  365: 
  366: =cut
  367: 
  368: sub ServerToIdle {
  369:     my $Socket   = shift;	# Get the socket.
  370:     delete($ActiveTransactions{$Socket}); # Server has no transaction
  371: 
  372:     &Debug(6, "Server to idle");
  373: 
  374:     #  If there's work to do, start the transaction:
  375: 
  376:     $reqdata = $WorkQueue->dequeue(); # This is a LondTransaction
  377:     unless($reqdata eq undef)  {
  378: 	Debug(9, "Queue gave request data: ".$reqdata->getRequest());
  379: 	&StartRequest($Socket,  $reqdata);
  380: 
  381:     } else {
  382: 	
  383:     #  There's no work waiting, so push the server to idle list.
  384: 	&Debug(8, "No new work requests, server connection going idle");
  385: 	$IdleConnections->push($Socket);
  386:     }
  387: }
  388: 
  389: =pod
  390: 
  391: =head2 ClientWritable
  392: 
  393: Event callback for when a client socket is writable.
  394: 
  395: This callback is established when a transaction reponse is
  396: avaiable from lond.  The response is forwarded to the unix socket
  397: as it becomes writable in this sub.
  398: 
  399: Parameters:
  400: 
  401: =item Event
  402: 
  403: The event that has been triggered. Event->w->data is
  404: the data and Event->w->fd is the socket to write.
  405: 
  406: =cut
  407: 
  408: sub ClientWritable {
  409:     my $Event    = shift;
  410:     my $Watcher  = $Event->w;
  411:     my $Data     = $Watcher->data;
  412:     my $Socket   = $Watcher->fd;
  413: 
  414:     # Try to send the data:
  415: 
  416:     &Debug(6, "ClientWritable writing".$Data);
  417:     &Debug(9, "Socket is: ".$Socket);
  418: 
  419:     if($Socket->connected) {
  420: 	my $result = $Socket->send($Data, 0);
  421: 	
  422: 	# $result undefined: the write failed.
  423: 	# otherwise $result is the number of bytes written.
  424: 	# Remove that preceding string from the data.
  425: 	# If the resulting data is empty, destroy the watcher
  426: 	# and set up a read event handler to accept the next
  427: 	# request.
  428: 	
  429: 	&Debug(9,"Send result is ".$result." Defined: ".defined($result));
  430: 	if(defined($result)) {
  431: 	    &Debug(9, "send result was defined");
  432: 	    if($result == length($Data)) { # Entire string sent.
  433: 		&Debug(9, "ClientWritable data all written");
  434: 		$Watcher->cancel();
  435: 		#
  436: 		#  Set up to read next request from socket:
  437: 		
  438: 		my $descr     = sprintf("Connection to lonc client %d",
  439: 					$ActiveClients{$Socket});
  440: 		Event->io(cb    => \&ClientRequest,
  441: 			  poll  => 'r',
  442: 			  desc  => $descr,
  443: 			  data  => "",
  444: 			  fd    => $Socket);
  445: 		
  446: 	    } else {		# Partial string sent.
  447: 		$Watcher->data(substr($Data, $result));
  448: 	    }
  449: 	    
  450: 	} else {			# Error of some sort...
  451: 	    
  452: 	    # Some errnos are possible:
  453: 	    my $errno = $!;
  454: 	    if($errno == POSIX::EWOULDBLOCK   ||
  455: 	       $errno == POSIX::EAGAIN        ||
  456: 	       $errno == POSIX::EINTR) {
  457: 		# No action taken?
  458: 	    } else {		# Unanticipated errno.
  459: 		&Debug(5,"ClientWritable error or peer shutdown".$RemoteHost);
  460: 		$Watcher->cancel;	# Stop the watcher.
  461: 		$Socket->shutdown(2); # Kill connection
  462: 		$Socket->close();	# Close the socket.
  463: 	    }
  464: 	    
  465: 	}
  466:     } else {
  467: 	$Watcher->cancel();	# A delayed request...just cancel.
  468:     }
  469: }
  470: 
  471: =pod
  472: 
  473: =head2 CompleteTransaction
  474: 
  475: Called when the reply data has been received for a lond 
  476: transaction.   The reply data must now be sent to the
  477: ultimate client on the other end of the Unix socket.  This is
  478: done by setting up a writable event for the socket with the
  479: data the reply data.
  480: 
  481: Parameters:
  482: 
  483: =item Socket
  484: 
  485: Socket on which the lond transaction occured.  This is a
  486: LondConnection. The data received is in the TransactionReply member.
  487: 
  488: =item Transaction
  489: 
  490: The transaction that is being completed.
  491: 
  492: =cut
  493: 
  494: sub CompleteTransaction {
  495:     &Debug(6,"Complete transaction");
  496:     my $Socket = shift;
  497:     my $Transaction = shift;
  498: 
  499:     if (!$Transaction->isDeferred()) { # Normal transaction
  500: 	my $data   = $Socket->GetReply(); # Data to send.
  501: 	StartClientReply($Transaction, $data);
  502:     } else {			# Delete deferred transaction file.
  503: 	Log("SUCCESS", "A delayed transaction was completed");
  504: 	LogPerm("S:$Client:".$Transaction->getRequest());
  505: 	unlink $Transaction->getFile();
  506:     }
  507: }
  508: =pod
  509: =head1 StartClientReply
  510: 
  511:    Initiates a reply to a client where the reply data is a parameter.
  512: 
  513: =head2  parameters:
  514: 
  515: =item Transaction
  516: 
  517:     The transaction for which we are responding to the client.
  518: 
  519: =item data
  520: 
  521:     The data to send to apached client.
  522: 
  523: =cut
  524: sub StartClientReply {
  525:     my $Transaction   = shift;
  526:     my $data     = shift;
  527: 
  528: 
  529:     my $Client   = $Transaction->getClient();
  530: 
  531:     &Debug(8," Reply was: ".$data);
  532:     my $Serial         = $ActiveClients{$Client};
  533:     my $desc           = sprintf("Connection to lonc client %d",
  534: 
  535: 				 $Serial);
  536:     Event->io(fd       => $Client,
  537: 	      poll     => "w",
  538: 	      desc     => $desc,
  539: 	      cb       => \&ClientWritable,
  540: 	      data     => $data);
  541: }
  542: =pod
  543: =head2 FailTransaction
  544: 
  545:   Finishes a transaction with failure because the associated lond socket
  546:   disconnected.  There are two possibilities:
  547:   - The transaction is deferred: in which case we just quietly
  548:     delete the transaction since there is no client connection.
  549:   - The transaction is 'live' in which case we initiate the sending
  550:     of "con_lost" to the client.
  551: 
  552: Deleting the transaction means killing it from the 
  553: %ActiveTransactions hash.
  554: 
  555: Parameters:
  556: 
  557: =item client  
  558:  
  559:    The LondTransaction we are failing.
  560:  
  561: =cut
  562: 
  563: sub FailTransaction {
  564:     my $transaction = shift;
  565:     Debug(1, "Failing transaction: ".$transaction->getRequest());
  566:     if (!$transaction->isDeferred()) { # If the transaction is deferred we'll get to it.
  567: 	my $client  = $transaction->getClient();
  568: 	Debug(1," Replying con_lost to ".$transaction->getRequest());
  569: 	StartClientReply($transaction, "con_lost\n");
  570:     }
  571: 
  572: }
  573: 
  574: =pod
  575: =head1  EmptyQueue
  576: 
  577:   Fails all items in the work queue with con_lost.
  578:   Note that each item in the work queue is a transaction.
  579: 
  580: =cut
  581: sub EmptyQueue {
  582:     while($WorkQueue->Count()) {
  583: 	my $request = $WorkQueue->dequeue(); # This is a transaction
  584: 	FailTransaction($request);
  585:     }
  586: }
  587: 
  588: =pod
  589: 
  590: =head2 CloseAllLondConnections
  591: 
  592: Close all connections open on lond prior to exit e.g.
  593: 
  594: =cut
  595: sub CloseAllLondConnections {
  596:     foreach $Socket (keys %ActiveConnections) {
  597: 	KillSocket($Socket);
  598:     }
  599: }
  600: =cut
  601: 
  602: =pod
  603: 
  604: =head2 KillSocket
  605:  
  606: Destroys a socket.  This function can be called either when a socket
  607: has died of 'natural' causes or because a socket needs to be pruned due to
  608: idleness.  If the socket has died naturally, if there are no longer any 
  609: live connections a new connection is created (in case there are transactions
  610: in the queue).  If the socket has been pruned, it is never re-created.
  611: 
  612: Parameters:
  613: 
  614: =item Socket
  615:  
  616:   The socket to kill off.
  617: 
  618: =item Restart
  619: 
  620: nonzero if we are allowed to create a new connection.
  621: 
  622: 
  623: =cut
  624: sub KillSocket {
  625:     my $Socket = shift;
  626: 
  627:     $Socket->Shutdown();
  628: 
  629:     #  If the socket came from the active connection set,
  630:     #  delete its transaction... note that FailTransaction should
  631:     #  already have been called!!!
  632:     #  otherwise it came from the idle set.
  633:     #  
  634:     
  635:     if(exists($ActiveTransactions{$Socket})) {
  636: 	delete ($ActiveTransactions{$Socket});
  637:     }
  638:     if(exists($ActiveConnections{$Socket})) {
  639: 	delete($ActiveConnections{$Socket});
  640:     }
  641:     $ConnectionCount--;
  642: 
  643:     #  If the connection count has gone to zero and there is work in the
  644:     #  work queue, the work all gets failed with con_lost.
  645:     #
  646:     if($ConnectionCount == 0) {
  647: 	EmptyQueue;
  648:     }
  649: }
  650: 
  651: =pod
  652: 
  653: =head2 LondReadable
  654: 
  655: This function is called whenever a lond connection
  656: is readable.  The action is state dependent:
  657: 
  658: =head3 State=Initialized
  659: 
  660: We''re waiting for the challenge, this is a no-op until the
  661: state changes.
  662: 
  663: =head3 State=Challenged 
  664: 
  665: The challenge has arrived we need to transition to Writable.
  666: The connection must echo the challenge back.
  667: 
  668: =head3 State=ChallengeReplied
  669: 
  670: The challenge has been replied to.  The we are receiveing the 
  671: 'ok' from the partner.
  672: 
  673: =head3 State=RequestingKey
  674: 
  675: The ok has been received and we need to send the request for
  676: an encryption key.  Transition to writable for that.
  677: 
  678: =head3 State=ReceivingKey
  679: 
  680: The the key has been requested, now we are reading the new key.
  681: 
  682: =head3 State=Idle 
  683: 
  684: The encryption key has been negotiated or we have finished 
  685: reading data from the a transaction.   If the callback data has
  686: a client as well as the socket iformation, then we are 
  687: doing a transaction and the data received is relayed to the client
  688: before the socket is put on the idle list.
  689: 
  690: =head3 State=SendingRequest
  691: 
  692: I do not think this state can be received here, but if it is,
  693: the appropriate thing to do is to transition to writable, and send
  694: the request.
  695: 
  696: =head3 State=ReceivingReply
  697: 
  698: We finished sending the request to the server and now transition
  699: to readable to receive the reply. 
  700: 
  701: The parameter to this function are:
  702: 
  703: The event. Implicit in this is the watcher and its data.  The data 
  704: contains at least the lond connection object and, if a 
  705: transaction is in progress, the socket attached to the local client.
  706: 
  707: =cut
  708: 
  709: sub LondReadable {
  710: 
  711:     my $Event      = shift;
  712:     my $Watcher    = $Event->w;
  713:     my $Socket     = $Watcher->data;
  714:     my $client     = undef;
  715: 
  716:     &Debug(6,"LondReadable called state = ".$State);
  717: 
  718: 
  719:     my $State = $Socket->GetState(); # All action depends on the state.
  720: 
  721:     SocketDump(6, $Socket);
  722:     my $status = $Socket->Readable();
  723:     &Debug(2, "Socket->Readable returned: $status");
  724: 
  725:     if($status != 0) {
  726: 	 # bad return from socket read. Currently this means that
  727: 	# The socket has become disconnected. We fail the transaction.
  728: 
  729: 	if(exists($ActiveTransactions{$Socket})) {
  730: 	    Debug(3,"Lond connection lost failing transaction");
  731: 	    FailTransaction($ActiveTransactions{$Socket});
  732: 	}
  733: 	$Watcher->cancel();
  734: 	KillSocket($Socket);
  735: 	return;
  736:     }
  737:     SocketDump(6,$Socket);
  738: 
  739:     $State = $Socket->GetState(); # Update in case of transition.
  740:     &Debug(6, "After read, state is ".$State);
  741: 
  742:    if($State eq "Initialized") {
  743: 
  744: 
  745:     } elsif ($State eq "ChallengeReceived") {
  746: 	#  The challenge must be echoed back;  The state machine
  747: 	# in the connection takes care of setting that up.  Just
  748: 	# need to transition to writable:
  749: 
  750: 	$Watcher->cb(\&LondWritable);
  751: 	$Watcher->poll("w");
  752: 
  753:     } elsif ($State eq "ChallengeReplied") {
  754: 
  755: 
  756:     } elsif ($State eq "RequestingKey") {
  757: 	#  The ok was received.  Now we need to request the key
  758: 	#  That requires us to be writable:
  759: 
  760: 	$Watcher->cb(\&LondWritable);
  761: 	$Watcher->poll("w");
  762: 
  763:     } elsif ($State eq "ReceivingKey") {
  764: 
  765:     } elsif ($State eq "Idle") {
  766: 	# If necessary, complete a transaction and then go into the
  767: 	# idle queue.
  768: 	$Watcher->cancel();
  769: 	if(exists($ActiveTransactions{$Socket})) {
  770: 	    Debug(8,"Completing transaction!!");
  771: 	    CompleteTransaction($Socket, 
  772: 				$ActiveTransactions{$Socket});
  773: 	} else {
  774: 	    Log("SUCCESS", "Connection ".$ConnectionCount." to "
  775: 		.$RemoteHost." now ready for action");
  776: 	}
  777: 	ServerToIdle($Socket);	# Next work unit or idle.
  778: 	
  779:     } elsif ($State eq "SendingRequest") {
  780: 	#  We need to be writable for this and probably don't belong
  781: 	#  here inthe first place.
  782: 
  783: 	Deubg(6, "SendingRequest state encountered in readable");
  784: 	$Watcher->poll("w");
  785: 	$Watcher->cb(\&LondWritable);
  786: 
  787:     } elsif ($State eq "ReceivingReply") {
  788: 
  789: 
  790:     } else {
  791: 	 # Invalid state.
  792: 	Debug(4, "Invalid state in LondReadable");
  793:     }
  794: }
  795: 
  796: =pod
  797: 
  798: =head2 LondWritable
  799: 
  800: This function is called whenever a lond connection
  801: becomes writable while there is a writeable monitoring
  802: event.  The action taken is very state dependent:
  803: 
  804: =head3 State = Connected 
  805: 
  806: The connection is in the process of sending the 'init' hailing to the
  807: lond on the remote end.  The connection object''s Writable member is
  808: called.  On error, ConnectionError is called to destroy the connection
  809: and remove it from the ActiveConnections hash
  810: 
  811: =head3 Initialized
  812: 
  813: 'init' has been sent, writability monitoring is removed and
  814: readability monitoring is started with LondReadable as the callback.
  815: 
  816: =head3 ChallengeReceived
  817: 
  818: The connection has received the who are you challenge from the remote
  819: system, and is in the process of sending the challenge
  820: response. Writable is called.
  821: 
  822: =head3 ChallengeReplied
  823: 
  824: The connection has replied to the initial challenge The we switch to
  825: monitoring readability looking for the server to reply with 'ok'.
  826: 
  827: =head3 RequestingKey
  828: 
  829: The connection is in the process of requesting its encryption key.
  830: Writable is called.
  831: 
  832: =head3 ReceivingKey
  833: 
  834: The connection has sent the request for a key.  Switch to readability
  835: monitoring to accept the key
  836: 
  837: =head3 SendingRequest
  838: 
  839: The connection is in the process of sending a request to the server.
  840: This request is part of a client transaction.  All the states until
  841: now represent the client setup protocol. Writable is called.
  842: 
  843: =head3 ReceivingReply
  844: 
  845: The connection has sent a request.  Now it must receive a reply.
  846: Readability monitoring is requested.
  847: 
  848: This function is an event handler and therefore receives as
  849: a parameter the event that has fired.  The data for the watcher
  850: of this event is a reference to a list of one or two elements,
  851: depending on state. The first (and possibly only) element is the
  852: socket.  The second (present only if a request is in progress)
  853: is the socket on which to return a reply to the caller.
  854: 
  855: =cut
  856: 
  857: sub LondWritable {
  858:     my $Event   = shift;
  859:     my $Watcher = $Event->w;
  860:     my $Socket  = $Watcher->data;
  861:     my $State   = $Socket->GetState();
  862: 
  863:     Debug(6,"LondWritable State = ".$State."\n");
  864: 
  865:  
  866:     #  Figure out what to do depending on the state of the socket:
  867:     
  868: 
  869: 
  870: 
  871:     SocketDump(6,$Socket);
  872: 
  873:     if      ($State eq "Connected")         {
  874: 
  875: 	if ($Socket->Writable() != 0) {
  876: 	    #  The write resulted in an error.
  877: 	    # We'll treat this as if the socket got disconnected:
  878: 	    Log("WARNING", "Connection to ".$RemoteHost.
  879: 		" has been disconnected");
  880: 	    $Watcher->cancel();
  881: 	    KillSocket($Socket);
  882: 	    return;
  883: 	}
  884: 	#  "init" is being sent...
  885: 
  886: 	
  887:     } elsif ($State eq "Initialized")       {
  888: 
  889: 	# Now that init was sent, we switch 
  890: 	# to watching for readability:
  891: 
  892: 	$Watcher->cb(\&LondReadable);
  893: 	$Watcher->poll("r");
  894: 
  895:     } elsif ($State eq "ChallengeReceived") {
  896: 	# We received the challenge, now we 
  897: 	# are echoing it back. This is a no-op,
  898: 	# we're waiting for the state to change
  899: 	
  900: 	if($Socket->Writable() != 0) {
  901: 
  902: 	    $Watcher->cancel();
  903: 	    KillSocket($Socket);
  904: 	    return;
  905: 	}
  906: 	
  907:     } elsif ($State eq "ChallengeReplied")  {
  908: 	# The echo was sent back, so we switch
  909: 	# to watching readability.
  910: 
  911: 	$Watcher->cb(\&LondReadable);
  912: 	$Watcher->poll("r");
  913: 
  914:     } elsif ($State eq "RequestingKey")     {
  915: 	# At this time we're requesting the key.
  916: 	# again, this is essentially a no-op.
  917: 	# we'll write the next chunk until the
  918: 	# state changes.
  919: 
  920: 	if($Socket->Writable() != 0) {
  921: 	    # Write resulted in an error.
  922: 
  923: 	    $Watcher->cancel();
  924: 	    KillSocket($Socket);
  925: 	    return;
  926: 
  927: 	}
  928:     } elsif ($State eq "ReceivingKey")      {
  929: 	# Now we need to wait for the key
  930: 	# to come back from the peer:
  931: 
  932: 	$Watcher->cb(\&LondReadable);
  933: 	$Watcher->poll("r");
  934: 
  935:     } elsif ($State eq "SendingRequest")    {
  936: 	# At this time we are sending a request to the
  937: 	# peer... write the next chunk:
  938: 
  939: 	if($Socket->Writable() != 0) {
  940: 
  941: 	    if(exists($ActiveTransactions{$Socket})) {
  942: 		Debug(3, "Lond connection lost, failing transactions");
  943: 		FailTransaction($ActiveTransactions{$Socket});
  944: 	    }
  945: 	    $Watcher->cancel();
  946: 	    KillSocket($Socket);
  947: 	    return;
  948: 	    
  949: 	}
  950: 
  951:     } elsif ($State eq "ReceivingReply")    {
  952: 	# The send has completed.  Wait for the
  953: 	# data to come in for a reply.
  954: 	Debug(8,"Writable sent request/receiving reply");
  955: 	$Watcher->cb(\&LondReadable);
  956: 	$Watcher->poll("r");
  957: 
  958:     } else {
  959: 	#  Control only passes here on an error: 
  960: 	#  the socket state does not match any
  961: 	#  of the known states... so an error
  962: 	#  must be logged.
  963: 
  964: 	&Debug(4, "Invalid socket state ".$State."\n");
  965:     }
  966:     
  967: }
  968: =pod
  969:     
  970: =cut
  971: sub QueueDelayed {
  972:     Debug(3,"QueueDelayed called");
  973: 
  974:     my $path = "$perlvar{'lonSockDir'}/delayed";
  975: 
  976:     Debug(4, "Delayed path: ".$path);
  977:     opendir(DIRHANDLE, $path);
  978:     
  979:     @alldelayed = grep /\.$RemoteHost$/, readdir DIRHANDLE;
  980:     Debug(4, "Got ".$alldelayed." delayed files");
  981:     closedir(DIRHANDLE);
  982:     my $dfname;
  983:     my $reqfile;
  984:     foreach $dfname (sort  @alldelayed) {
  985: 	$reqfile = "$path/$dfname";
  986: 	Debug(4, "queueing ".$reqfile);
  987: 	my $Handle = IO::File->new($reqfile);
  988: 	my $cmd    = <$Handle>;
  989: 	chomp $cmd;		# There may or may not be a newline...
  990: 	$cmd = $cmd."\n";	# now for sure there's exactly one newline.
  991: 	my $Transaction = LondTransaction->new($cmd);
  992: 	$Transaction->SetDeferred($reqfile);
  993: 	QueueTransaction($Transaction);
  994:     }
  995:     
  996: }
  997: 
  998: =pod
  999: 
 1000: =head2 MakeLondConnection
 1001: 
 1002: Create a new lond connection object, and start it towards its initial
 1003: idleness.  Once idle, it becomes elligible to receive transactions
 1004: from the work queue.  If the work queue is not empty when the
 1005: connection is completed and becomes idle, it will dequeue an entry and
 1006: start off on it.
 1007: 
 1008: =cut
 1009: 
 1010: sub MakeLondConnection {     
 1011:     Debug(4,"MakeLondConnection to ".GetServerHost()." on port "
 1012: 	  .GetServerPort());
 1013: 
 1014:     my $Connection = LondConnection->new(&GetServerHost(),
 1015: 					 &GetServerPort());
 1016: 
 1017:     if($Connection == undef) {	# Needs to be more robust later.
 1018: 	Log("CRITICAL","Failed to make a connection with lond.");
 1019: 	$ConnectionRetriesLeft--;
 1020: 	return 0;		# Failure.
 1021:     }  else {
 1022: 	$ConnectionRetriesLeft = $ConnectionRetries; # success resets the count
 1023: 	# The connection needs to have writability 
 1024: 	# monitored in order to send the init sequence
 1025: 	# that starts the whole authentication/key
 1026: 	# exchange underway.
 1027: 	#
 1028: 	my $Socket = $Connection->GetSocket();
 1029: 	if($Socket == undef) {
 1030: 	    die "did not get a socket from the connection";
 1031: 	} else {
 1032: 	    &Debug(9,"MakeLondConnection got socket: ".$Socket);
 1033: 	}
 1034: 	
 1035: 	
 1036: 	$event = Event->io(fd       => $Socket,
 1037: 			   poll     => 'w',
 1038: 			   cb       => \&LondWritable,
 1039: 			   data     => $Connection,
 1040: 			   desc => 'Connection to lond server');
 1041: 	$ActiveConnections{$Connection} = $event;
 1042: 	
 1043: 	$ConnectionCount++;
 1044: 	Debug(4, "Connection count = ".$ConnectionCount);
 1045: 	if($ConnectionCount == 1) { # First Connection:
 1046: 	    QueueDelayed;
 1047: 	}
 1048: 	Log("SUCESS", "Created connection ".$ConnectionCount
 1049: 	    ." to host ".GetServerHost());
 1050: 	return 1;		# Return success.
 1051:     }
 1052:     
 1053: }
 1054: 
 1055: =pod
 1056: 
 1057: =head2 StartRequest
 1058: 
 1059: Starts a lond request going on a specified lond connection.
 1060: parameters are:
 1061: 
 1062: =item $Lond
 1063: 
 1064: Connection to the lond that will send the transaction and receive the
 1065: reply.
 1066: 
 1067: =item $Client
 1068: 
 1069: Connection to the client that is making this request We got the
 1070: request from this socket, and when the request has been relayed to
 1071: lond and we get a reply back from lond it will get sent to this
 1072: socket.
 1073: 
 1074: =item $Request
 1075: 
 1076: The text of the request to send.
 1077: 
 1078: =cut
 1079: 
 1080: sub StartRequest {
 1081:     my $Lond     = shift;
 1082:     my $Request  = shift;	# This is a LondTransaction.
 1083:     
 1084:     Debug(6, "StartRequest: ".$Request->getRequest());
 1085: 
 1086:     my $Socket = $Lond->GetSocket();
 1087:     
 1088:     $Request->Activate($Lond);
 1089:     $ActiveTransactions{$Lond} = $Request;
 1090: 
 1091:     $Lond->InitiateTransaction($Request->getRequest());
 1092:     $event = Event->io(fd      => $Socket,
 1093: 		       poll    => "w",
 1094: 		       cb      => \&LondWritable,
 1095: 		       data    => $Lond,
 1096: 		       desc    => "lond transaction connection");
 1097:     $ActiveConnections{$Lond} = $event;
 1098:     Debug(8," Start Request made watcher data with ".$event->data."\n");
 1099: }
 1100: 
 1101: =pod
 1102: 
 1103: =head2 QueueTransaction
 1104: 
 1105: If there is an idle lond connection, it is put to work doing this
 1106: transaction.  Otherwise, the transaction is placed in the work queue.
 1107: If placed in the work queue and the maximum number of connections has
 1108: not yet been created, a new connection will be started.  Our goal is
 1109: to eventually have a sufficient number of connections that the work
 1110: queue will typically be empty.  parameters are:
 1111: 
 1112: =item Socket
 1113: 
 1114: open on the lonc client.
 1115: 
 1116: =item Request
 1117: 
 1118: data to send to the lond.
 1119: 
 1120: =cut
 1121: 
 1122: sub QueueTransaction {
 1123: 
 1124:     my $requestData   = shift;	# This is a LondTransaction.
 1125:     my $cmd           = $requestData->getRequest();
 1126: 
 1127:     Debug(6,"QueueTransaction: ".$cmd);
 1128: 
 1129:     my $LondSocket    = $IdleConnections->pop();
 1130:     if(!defined $LondSocket) {	# Need to queue request.
 1131: 	Debug(8,"Must queue...");
 1132: 	$WorkQueue->enqueue($requestData);
 1133: 	if($ConnectionCount < $MaxConnectionCount) {
 1134: 	    Debug(4,"Starting additional lond connection");
 1135: 	    MakeLondConnection();
 1136: 	}
 1137:     } else {			# Can start the request:
 1138: 	Debug(8,"Can start...");
 1139: 	StartRequest($LondSocket,  $requestData);
 1140:     }
 1141: }
 1142: 
 1143: #-------------------------- Lonc UNIX socket handling ---------------------
 1144: 
 1145: =pod
 1146: 
 1147: =head2 ClientRequest
 1148: Callback that is called when data can be read from the UNIX domain
 1149: socket connecting us with an apache server process.
 1150: 
 1151: =cut
 1152: 
 1153: sub ClientRequest {
 1154:     Debug(6, "ClientRequest");
 1155:     my $event   = shift;
 1156:     my $watcher = $event->w;
 1157:     my $socket  = $watcher->fd;
 1158:     my $data    = $watcher->data;
 1159:     my $thisread;
 1160: 
 1161:     Debug(9, "  Watcher named: ".$watcher->desc);
 1162: 
 1163:     my $rv = $socket->recv($thisread, POSIX::BUFSIZ, 0);
 1164:     Debug(8, "rcv:  data length = ".length($thisread)
 1165: 	  ." read =".$thisread);
 1166:     unless (defined $rv && length($thisread)) {
 1167: 	 # Likely eof on socket.
 1168: 	Debug(5,"Client Socket closed on lonc for ".$RemoteHost);
 1169: 	close($socket);
 1170: 	$watcher->cancel();
 1171: 	delete($ActiveClients{$socket});
 1172: 	return;
 1173:     }
 1174:     Debug(8,"Data: ".$data." this read: ".$thisread);
 1175:     $data = $data.$thisread;	# Append new data.
 1176:     $watcher->data($data);
 1177:     if($data =~ /(.*\n)/) {	# Request entirely read.
 1178: 	if($data eq "close_connection_exit\n") {
 1179: 	    Log("CRITICAL",
 1180: 		"Request Close Connection ... exiting");
 1181: 	    CloseAllLondConnections();
 1182: 	    exit;
 1183: 	}
 1184: 	Debug(8, "Complete transaction received: ".$data);
 1185: 	my $Transaction = LondTransaction->new($data);
 1186: 	$Transaction->SetClient($socket);
 1187: 	QueueTransaction($Transaction);
 1188: 	$watcher->cancel();	# Done looking for input data.
 1189:     }
 1190: 
 1191: }
 1192: 
 1193: 
 1194: =pod
 1195: 
 1196: =head2  NewClient
 1197: 
 1198: Callback that is called when a connection is received on the unix
 1199: socket for a new client of lonc.  The callback is parameterized by the
 1200: event.. which is a-priori assumed to be an io event, and therefore has
 1201: an fd member that is the Listener socket.  We Accept the connection
 1202: and register a new event on the readability of that socket:
 1203: 
 1204: =cut
 1205: 
 1206: sub NewClient {
 1207:     Debug(6, "NewClient");
 1208:     my $event      = shift;		# Get the event parameters.
 1209:     my $watcher    = $event->w; 
 1210:     my $socket     = $watcher->fd;	# Get the event' socket.
 1211:     my $connection = $socket->accept();	# Accept the client connection.
 1212:     Debug(8,"Connection request accepted from "
 1213: 	  .GetPeername($connection, AF_UNIX));
 1214: 
 1215: 
 1216:     my $description = sprintf("Connection to lonc client %d",
 1217: 			      $ClientConnection);
 1218:     Debug(9, "Creating event named: ".$description);
 1219:     Event->io(cb      => \&ClientRequest,
 1220: 	      poll    => 'r',
 1221: 	      desc    => $description,
 1222: 	      data    => "",
 1223: 	      fd      => $connection);
 1224:     $ActiveClients{$connection} = $ClientConnection;
 1225:     $ClientConnection++;
 1226: }
 1227: 
 1228: =pod
 1229: 
 1230: =head2 GetLoncSocketPath
 1231: 
 1232: Returns the name of the UNIX socket on which to listen for client
 1233: connections.
 1234: 
 1235: =cut
 1236: 
 1237: sub GetLoncSocketPath {
 1238:     return $UnixSocketDir."/".GetServerHost();
 1239: }
 1240: 
 1241: =pod
 1242: 
 1243: =head2 GetServerHost
 1244: 
 1245: Returns the host whose lond we talk with.
 1246: 
 1247: =cut
 1248: 
 1249: sub GetServerHost {
 1250:     return $RemoteHost;		# Setup by the fork.
 1251: }
 1252: 
 1253: =pod
 1254: 
 1255: =head2 GetServerPort
 1256: 
 1257: Returns the lond port number.
 1258: 
 1259: =cut
 1260: 
 1261: sub GetServerPort {
 1262:     return $perlvar{londPort};
 1263: }
 1264: 
 1265: =pod
 1266: 
 1267: =head2 SetupLoncListener
 1268: 
 1269: Setup a lonc listener event.  The event is called when the socket
 1270: becomes readable.. that corresponds to the receipt of a new
 1271: connection.  The event handler established will accept the connection
 1272: (creating a communcations channel), that int turn will establish
 1273: another event handler to subess requests.
 1274: 
 1275: =cut
 1276: 
 1277: sub SetupLoncListener {
 1278: 
 1279:     my $socket;
 1280:     my $SocketName = GetLoncSocketPath();
 1281:     unlink($SocketName);
 1282:     unless ($socket =IO::Socket::UNIX->new(Local  => $SocketName,
 1283: 					    Listen => 10, 
 1284: 					    Type   => SOCK_STREAM)) {
 1285: 	die "Failed to create a lonc listner socket";
 1286:     }
 1287:     Event->io(cb     => \&NewClient,
 1288: 	      poll   => 'r',
 1289: 	      desc   => 'Lonc listener Unix Socket',
 1290: 	      fd     => $socket);
 1291: }
 1292: 
 1293: =pod
 1294: 
 1295: =head2 SignalledToDeath
 1296: 
 1297: Called in response to a signal that causes a chid process to die.
 1298: 
 1299: =cut
 1300: 
 1301: 
 1302: sub SignalledToDeath {
 1303:     Debug(2,"Signalled to death!");
 1304:     my ($signal) = @_;
 1305:     chomp($signal);
 1306:     Log("CRITICAL", "Abnormal exit.  Child $$ for $RemoteHost "
 1307: 	."died through "."\"$signal\"");
 1308:     LogPerm("F:lonc: $$ on $RemoteHost signalled to death: "
 1309: 	    ."\"$signal\"");
 1310:     die("Signal abnormal end");
 1311:     exit 0;
 1312: 
 1313: }
 1314: =head2 ChildProcess
 1315: 
 1316: This sub implements a child process for a single lonc daemon.
 1317: 
 1318: =cut
 1319: 
 1320: sub ChildProcess {
 1321: 
 1322: 
 1323:     # For now turn off signals.
 1324:     
 1325:     $SIG{QUIT}  = \&SignalledToDeath;
 1326:     $SIG{HUP}   = IGNORE;
 1327:     $SIG{USR1}  = IGNORE;
 1328:     $SIG{INT}   = DEFAULT;
 1329:     $SIG{CHLD}  = IGNORE;
 1330:     $SIG{__DIE__}  = \&SignalledToDeath;
 1331: 
 1332:     SetupTimer();
 1333:     
 1334:     SetupLoncListener();
 1335:     
 1336:     $Event::Debuglevel = $DebugLevel;
 1337:     
 1338:     Debug(9, "Making initial lond connection for ".$RemoteHost);
 1339: 
 1340: # Setup the initial server connection:
 1341:     
 1342:      # &MakeLondConnection(); // let first work requirest do it.
 1343: 
 1344: 
 1345:     Debug(9,"Entering event loop");
 1346:     my $ret = Event::loop();		#  Start the main event loop.
 1347:     
 1348:     
 1349:     die "Main event loop exited!!!";
 1350: }
 1351: 
 1352: #  Create a new child for host passed in:
 1353: 
 1354: sub CreateChild {
 1355:     my $sigset = POSIX::SigSet->new(SIGINT);
 1356:     sigprocmask(SIG_BLOCK, $sigset);
 1357:     my $host = shift;
 1358:     $RemoteHost = $host;
 1359:     Log("CRITICAL", "Forking server for ".$host);
 1360:     $pid          = fork;
 1361:     if($pid) {			# Parent
 1362: 	$ChildHash{$pid} = $RemoteHost;
 1363: 	sigprocmask(SIG_UNBLOCK, $sigset);
 1364: 
 1365:     } else {			# child.
 1366: 	ShowStatus("Connected to ".$RemoteHost);
 1367: 	$SIG{INT} = DEFAULT;
 1368: 	sigprocmask(SIG_UNBLOCK, $sigset);
 1369: 	ChildProcess;		# Does not return.
 1370:     }
 1371: 
 1372: }
 1373: #
 1374: #  Parent process logic pass 1:
 1375: #   For each entry in the hosts table, we will
 1376: #  fork off an instance of ChildProcess to service the transactions
 1377: #  to that host.  Each pid will be entered in a global hash
 1378: #  with the value of the key, the host.
 1379: #  The parent will then enter a loop to wait for process exits.
 1380: #  Each exit gets logged and the child gets restarted.
 1381: #
 1382: 
 1383: #
 1384: #   Fork and start in new session so hang-up isn't going to 
 1385: #   happen without intent.
 1386: #
 1387: 
 1388: 
 1389: 
 1390: 
 1391: 
 1392: 
 1393: ShowStatus("Forming new session");
 1394: my $childpid = fork;
 1395: if ($childpid != 0) {
 1396:     sleep 4;			# Give child a chacne to break to
 1397:     exit 0;			# a new sesion.
 1398: }
 1399: #
 1400: #   Write my pid into the pid file so I can be located
 1401: #
 1402: 
 1403: ShowStatus("Parent writing pid file:");
 1404: $execdir = $perlvar{'lonDaemons'};
 1405: open (PIDSAVE, ">$execdir/logs/lonc.pid");
 1406: print PIDSAVE "$$\n";
 1407: close(PIDSAVE);
 1408: 
 1409: if (POSIX::setsid() < 0) {
 1410:     print "Could not create new session\n";
 1411:     exit -1;
 1412: }
 1413: 
 1414: ShowStatus("Forking node servers");
 1415: 
 1416: Log("CRITICAL", "--------------- Starting children ---------------");
 1417: 
 1418: my $HostIterator = LondConnection::GetHostIterator;
 1419: while (! $HostIterator->end()) {
 1420: 
 1421:     $hostentryref = $HostIterator->get();
 1422:     CreateChild($hostentryref->[0]);
 1423:     $HostIterator->next();
 1424: }
 1425: $RemoteHost = "Parent Server";
 1426: 
 1427: # Maintain the population:
 1428: 
 1429: ShowStatus("Parent keeping the flock");
 1430: 
 1431: #
 1432: #   Set up parent signals:
 1433: #
 1434: 
 1435: $SIG{INT}  = \&KillThemAll;
 1436: $SIG{TERM} = \&KillThemAll; 
 1437: 
 1438: 
 1439: while(1) {
 1440:     $deadchild = wait();
 1441:     if(exists $ChildHash{$deadchild}) {	# need to restart.
 1442: 	$deadhost = $ChildHash{$deadchild};
 1443: 	delete($ChildHash{$deadchild});
 1444: 	Log("WARNING","Lost child pid= ".$deadchild.
 1445: 	      "Connected to host ".$deadhost);
 1446: 	Log("INFO", "Restarting child procesing ".$deadhost);
 1447: 	CreateChild($deadhost);
 1448:     }
 1449: }
 1450: 
 1451: =pod
 1452: 
 1453: =head1 KillThemAll
 1454: 
 1455: Signal handler that kills all children by sending them a 
 1456: SIGINT.  Responds to sigint and sigterm.
 1457: 
 1458: =cut
 1459: 
 1460: sub KillThemAll {
 1461:     Debug(2, "Kill them all!!");
 1462:     local($SIG{CHLD}) = 'IGNORE';      # Our children >will< die.
 1463:     foreach $pid (keys %ChildHash) {
 1464: 	my $serving = $ChildHash{$pid};
 1465: 	Debug(2, "Killing lonc for $serving pid = $pid");
 1466: 	ShowStatus("Killing lonc for $serving pid = $pid");
 1467: 	Log("CRITICAL", "Killing lonc for $serving pid = $pid");
 1468: 	kill('INT', $pid);
 1469:     }
 1470:     Log("CRITICAL", "Killing the master process.");
 1471:     exit
 1472: }
 1473: 
 1474: =pod
 1475: 
 1476: =head1 Theory
 1477: 
 1478: The event class is used to build this as a single process with an
 1479: event driven model.  The following events are handled:
 1480: 
 1481: =item UNIX Socket connection Received
 1482: 
 1483: =item Request data arrives on UNIX data transfer socket.
 1484: 
 1485: =item lond connection becomes writable.
 1486: 
 1487: =item timer fires at 1 second intervals.
 1488: 
 1489: All sockets are run in non-blocking mode.  Timeouts managed by the timer
 1490: handler prevents hung connections.
 1491: 
 1492: Key data structures:
 1493: 
 1494: =item RequestQueue
 1495: 
 1496: A queue of requests received from UNIX sockets that are
 1497: waiting for a chance to be forwarded on a lond connection socket.
 1498: 
 1499: =item ActiveConnections
 1500: 
 1501: A hash of lond connections that have transactions in process that are
 1502: available to be timed out.
 1503: 
 1504: =item ActiveTransactions
 1505: 
 1506: A hash indexed by lond connections that contain the client reply
 1507: socket for each connection that has an active transaction on it.
 1508: 
 1509: =item IdleConnections
 1510: 
 1511: A hash of lond connections that have no work to do.  These connections
 1512: can be closed if they are idle for a long enough time.
 1513: 
 1514: =cut

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>