Annotation of loncom/loncnew, revision 1.23

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

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