Annotation of loncom/loncnew, revision 1.15

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

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