File:  [LON-CAPA] / loncom / loncnew
Revision 1.107: download - view: text, annotated - select for diffs
Mon Dec 10 17:34:22 2018 UTC (5 years, 4 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Verification of CN when creating SSL tunnel on multi-domain servers.
  CN is default lonid, i.e., one listed last for the node's hostname in
  hosts.tab and/or dns_hosts.tab.

    1: #!/usr/bin/perl
    2: # The LearningOnline Network with CAPA
    3: # lonc maintains the connections to remote computers
    4: #
    5: # $Id: loncnew,v 1.107 2018/12/10 17:34:22 raeburn Exp $
    6: #
    7: # Copyright Michigan State University Board of Trustees
    8: #
    9: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   10: ## LON-CAPA is free software; you can redistribute it and/or modify
   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: #
   28: #
   29: # new lonc handles n request out over m connections to londs.
   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).
   36: #    - Add general logic for dispatching requests and timeouts. (done).
   37: #    - Add support for the lonc/lond requests.          (done).
   38: #    - Add logging/status monitoring.                    (done)
   39: #    - Add Signal handling - HUP restarts. USR1 status report. (done)
   40: #    - Add Configuration file I/O                       (done).
   41: #    - Add management/status request interface.         (done)
   42: #    - Add deferred request capability.                  (done)
   43: #    - Detect transmission timeouts.                     (done)
   44: #
   45: 
   46: use strict;
   47: use lib "/home/httpd/lib/perl/";
   48: use Event qw(:DEFAULT );
   49: use POSIX qw(:signal_h);
   50: use POSIX;
   51: use IO::Socket;
   52: use IO::Socket::INET;
   53: use IO::Socket::UNIX;
   54: use IO::File;
   55: use IO::Handle;
   56: use Socket;
   57: use Crypt::IDEA;
   58: use LONCAPA::Queue;
   59: use LONCAPA::Stack;
   60: use LONCAPA::LondConnection;
   61: use LONCAPA::LondTransaction;
   62: use LONCAPA::Configuration;
   63: use Fcntl qw(:flock);
   64: 
   65: 
   66: # Read the httpd configuration file to get perl variables
   67: # normally set in apache modules:
   68: 
   69: my $perlvarref = LONCAPA::Configuration::read_conf('loncapa.conf');
   70: my %perlvar    = %{$perlvarref};
   71: 
   72: #
   73: #  parent and shared variables.
   74: 
   75: my %ChildPid;			# by pid -> host.
   76: my %ChildHost;			# by host.
   77: my %ChildKeyMode;               # by pid -> keymode
   78: my %listening_to;		# Socket->host table for who the parent
   79:                                 # is listening to.
   80: my %parent_dispatchers;         # host-> listener watcher events. 
   81: 
   82: my %parent_handlers;		# Parent signal handlers...
   83: 
   84: my $MaxConnectionCount = 10;	# Will get from config later.
   85: my $ClientConnection = 0;	# Uniquifier for client events.
   86: 
   87: my $DebugLevel = 0;
   88: my $NextDebugLevel= 2;		# So Sigint can toggle this.
   89: my $IdleTimeout= 5*60;		# Seconds to wait prior to pruning connections.
   90: 
   91: my $LogTransactions = 0;	# When True, all transactions/replies get logged.
   92: my $executable      = $0;	# Get the full path to me.
   93: 
   94: #
   95: #  The variables below are only used by the child processes.
   96: #
   97: my $RemoteHost;			# Hostname of host child is talking to.
   98: my $RemoteHostId;		# lonid of host child is talking to.
   99: my $RemoteDefHostId;		# default lonhostID of host child is talking to.
  100: my @all_host_ids;
  101: my $UnixSocketDir= $perlvar{'lonSockDir'};
  102: my $IdleConnections = Stack->new(); # Set of idle connections
  103: my %ActiveConnections;		# Connections to the remote lond.
  104: my %ActiveTransactions;		# LondTransactions in flight.
  105: my %ActiveClients;		# Serial numbers of active clients by socket.
  106: my $WorkQueue       = Queue->new(); # Queue of pending transactions.
  107: my $ConnectionCount = 0;
  108: my $IdleSeconds     = 0;	# Number of seconds idle.
  109: my $Status          = "";	# Current status string.
  110: my $RecentLogEntry  = "";
  111: my $ConnectionRetries=5;	# Number of connection retries allowed.
  112: my $ConnectionRetriesLeft=5;	# Number of connection retries remaining.
  113: my $LondVersion     = "unknown"; # Version of lond we talk with.
  114: my $KeyMode         = "";       # e.g. ssl, local, insecure from last connect.
  115: my $LondConnecting  = 0;       # True when a connection is being built.
  116: 
  117: 
  118: 
  119: my $I_am_child      = 0;	# True if this is the child process.
  120: 
  121: #
  122: #   The hash below gives the HTML format for log messages
  123: #   given a severity.
  124: #    
  125: my %LogFormats;
  126: 
  127: $LogFormats{"CRITICAL"} = "<font color='red'>CRITICAL: %s</font>";
  128: $LogFormats{"SUCCESS"}  = "<font color='green'>SUCCESS: %s</font>";
  129: $LogFormats{"INFO"}     = "<font color='yellow'>INFO: %s</font>";
  130: $LogFormats{"WARNING"}  = "<font color='blue'>WARNING: %s</font>";
  131: $LogFormats{"DEFAULT"}  = " %s ";
  132: 
  133: 
  134: #  UpdateStatus;
  135: #    Update the idle status display to show how many connections
  136: #    are left, retries and other stuff.
  137: #
  138: sub UpdateStatus {
  139:     if ($ConnectionRetriesLeft > 0) {
  140: 	ShowStatus(GetServerHost()." Connection count: ".$ConnectionCount
  141: 		   ." Retries remaining: ".$ConnectionRetriesLeft
  142: 		   ." ($KeyMode)");
  143:     } else {
  144: 	ShowStatus(GetServerHost()." >> DEAD <<");
  145:     }
  146: }
  147: 
  148: 
  149: =pod
  150: 
  151: =head2 LogPerm
  152: 
  153: Makes an entry into the permanent log file.
  154: 
  155: =cut
  156: 
  157: sub LogPerm {
  158:     my $message=shift;
  159:     my $execdir=$perlvar{'lonDaemons'};
  160:     my $now=time;
  161:     my $local=localtime($now);
  162:     my $fh=IO::File->new(">>$execdir/logs/lonnet.perm.log");
  163:     chomp($message);
  164:     print $fh "$now:$message:$local\n";
  165: }
  166: 
  167: =pod
  168: 
  169: =head2 Log
  170: 
  171: Logs a message to the log file.
  172: Parameters:
  173: 
  174: =item severity
  175: 
  176: One of CRITICAL, WARNING, INFO, SUCCESS used to select the
  177: format string used to format the message.  if the severity is
  178: not a defined severity the Default format string is used.
  179: 
  180: =item message
  181: 
  182: The base message.  In addtion to the format string, the message
  183: will be appended to a string containing the name of our remote
  184: host and the time will be formatted into the message.
  185: 
  186: =cut
  187: 
  188: sub Log {
  189: 
  190:     my ($severity, $message) = @_;
  191: 
  192:     if(!$LogFormats{$severity}) {
  193: 	$severity = "DEFAULT";
  194:     }
  195: 
  196:     my $format = $LogFormats{$severity};
  197:     
  198:     #  Put the window dressing in in front of the message format:
  199: 
  200:     my $now   = time;
  201:     my $local = localtime($now);
  202:     my $finalformat = "$local ($$) [$RemoteHost] [$Status] ";
  203:     $finalformat = $finalformat.$format."\n";
  204: 
  205:     # open the file and put the result.
  206: 
  207:     my $execdir = $perlvar{'lonDaemons'};
  208:     my $fh      = IO::File->new(">>$execdir/logs/lonc.log");
  209:     my $msg = sprintf($finalformat, $message);
  210:     $RecentLogEntry = $msg;
  211:     print $fh $msg;
  212:     
  213:     
  214: }
  215: 
  216: 
  217: =pod
  218: 
  219: =head2 GetPeerName
  220: 
  221: Returns the name of the host that a socket object is connected to.
  222: 
  223: =cut
  224: 
  225: sub GetPeername {
  226: 
  227: 
  228:     my ($connection, $AdrFamily) = @_;
  229: 
  230:     my $peer       = $connection->peername();
  231:     my $peerport;
  232:     my $peerip;
  233:     if($AdrFamily == AF_INET) {
  234: 	($peerport, $peerip) = sockaddr_in($peer);
  235: 	my $peername    = gethostbyaddr($peerip, $AdrFamily);
  236: 	return $peername;
  237:     } elsif ($AdrFamily == AF_UNIX) {
  238: 	my $peerfile;
  239: 	($peerfile) = sockaddr_un($peer);
  240: 	return $peerfile;
  241:     }
  242: }
  243: =pod
  244: 
  245: =head2 Debug
  246: 
  247: Invoked to issue a debug message.
  248: 
  249: =cut
  250: 
  251: sub Debug {
  252: 
  253:     my ($level, $message) = @_;
  254: 
  255:     if ($level <= $DebugLevel) {
  256: 	Log("INFO", "-Debug- $message host = $RemoteHost");
  257:     }
  258: }
  259: 
  260: sub SocketDump {
  261: 
  262:     my ($level, $socket) = @_;
  263: 
  264:     if($level <= $DebugLevel) {
  265: 	$socket->Dump(-1);	# Ensure it will get dumped.
  266:     }
  267: }
  268: 
  269: =pod
  270: 
  271: =head2 ShowStatus
  272: 
  273:  Place some text as our pid status.
  274:  and as what we return in a SIGUSR1
  275: 
  276: =cut
  277: 
  278: sub ShowStatus {
  279:     my $state = shift;
  280:     my $now = time;
  281:     my $local = localtime($now);
  282:     $Status   = $local.": ".$state;
  283:     $0='lonc: '.$state.' '.$local;
  284: }
  285: 
  286: =pod
  287: 
  288: =head2 SocketTimeout
  289: 
  290:     Called when an action on the socket times out.  The socket is 
  291:    destroyed and any active transaction is failed.
  292: 
  293: 
  294: =cut
  295: 
  296: sub SocketTimeout {
  297:     my $Socket = shift;
  298:     Log("WARNING", "A socket timeout was detected");
  299:     Debug(5, " SocketTimeout called: ");
  300:     $Socket->Dump(0);
  301:     if(exists($ActiveTransactions{$Socket})) {
  302: 	FailTransaction($ActiveTransactions{$Socket});
  303:     }
  304:     KillSocket($Socket);	# A transaction timeout also counts as
  305:                                 # a connection failure:
  306:     $ConnectionRetriesLeft--;
  307:     if($ConnectionRetriesLeft <= 0) {
  308: 	Log("CRITICAL", "Host marked DEAD: ".GetServerHost());
  309: 	$LondConnecting = 0;
  310:     }
  311: 
  312: }
  313: 
  314: #
  315: #   This function should be called by the child in all cases where it must
  316: #   exit.  The child process must create a lock file for the AF_UNIX socket
  317: #   in order to prevent connection requests from lonnet in the time between
  318: #   process exit and the parent picking up the listen again.
  319: #
  320: # Parameters:
  321: #     exit_code           - Exit status value, however see the next parameter.
  322: #     message             - If this optional parameter is supplied, the exit
  323: #                           is via a die with this message.
  324: #
  325: sub child_exit {
  326:     my ($exit_code, $message) = @_;
  327: 
  328:     # Regardless of how we exit, we may need to do the lock thing:
  329: 
  330:     #
  331:     #  Create a lock file since there will be a time window
  332:     #  between our exit and the parent's picking up the listen
  333:     #  during which no listens will be done on the
  334:     #  lonnet client socket.
  335:     #
  336:     my $lock_file = &GetLoncSocketPath().".lock";
  337:     open(LOCK,">$lock_file");
  338:     print LOCK "Contents not important";
  339:     close(LOCK);
  340:     unlink(&GetLoncSocketPath());
  341: 
  342:     if ($message) {
  343: 	die($message);
  344:     } else {
  345: 	exit($exit_code);
  346:     }
  347: }
  348: #----------------------------- Timer management ------------------------
  349: 
  350: =pod
  351: 
  352: =head2 Tick
  353: 
  354: Invoked each timer tick.
  355: 
  356: =cut
  357: 
  358: 
  359: sub Tick {
  360:     my ($Event)       = @_;
  361:     my $clock_watcher = $Event->w;
  362: 
  363:     my $client;
  364:     UpdateStatus();
  365: 
  366:     # Is it time to prune connection count:
  367: 
  368: 
  369:     if($IdleConnections->Count()  && 
  370:        ($WorkQueue->Count() == 0)) { # Idle connections and nothing to do?
  371: 	$IdleSeconds++;
  372: 	if($IdleSeconds > $IdleTimeout) { # Prune a connection...
  373: 	    my $Socket = $IdleConnections->pop();
  374: 	    KillSocket($Socket);
  375: 	    $IdleSeconds = 0;	# Otherwise all connections get trimmed to fast.
  376: 	    UpdateStatus();
  377: 	    if(($ConnectionCount == 0)) {
  378: 		&child_exit(0);
  379: 
  380: 	    }
  381: 	}
  382:     } else {
  383: 	$IdleSeconds = 0;	# Reset idle count if not idle.
  384:     }
  385:     #
  386:     #  For each inflight transaction, tick down its timeout counter.
  387:     #
  388: 
  389:     foreach my $item (keys %ActiveConnections) {
  390: 	my $State = $ActiveConnections{$item}->data->GetState();
  391: 	if ($State ne 'Idle') {
  392: 	    Debug(5,"Ticking Socket $State $item");
  393: 	    $ActiveConnections{$item}->data->Tick();
  394: 	}
  395:     }
  396:     # Do we have work in the queue, but no connections to service them?
  397:     # If so, try to make some new connections to get things going again.
  398:     #
  399:     #   Note this code is dead now...
  400:     #
  401:     my $Requests = $WorkQueue->Count();
  402:     if (($ConnectionCount == 0)  && ($Requests > 0) && (!$LondConnecting)) { 
  403: 	if ($ConnectionRetriesLeft > 0) {
  404: 	    Debug(5,"Work but no connections, Make a new one");
  405: 	    my $success;
  406: 	    $success    = &MakeLondConnection;
  407: 	    if($success == 0) { # All connections failed:
  408: 		Debug(5,"Work in queue failed to make any connectiouns\n");
  409: 		EmptyQueue();	# Fail pending transactions with con_lost.
  410: 		CloseAllLondConnections(); # Should all be closed but....
  411: 	    }
  412: 	} else {
  413: 	    $LondConnecting = 0;
  414: 	    ShowStatus(GetServerHost()." >>> DEAD!!! <<<");
  415: 	    Debug(5,"Work in queue, but gave up on connections..flushing\n");
  416: 	    EmptyQueue();	# Connections can't be established.
  417: 	    CloseAllLondConnections(); # Should all already be closed but...
  418: 	}
  419:        
  420:     }
  421:     if ($ConnectionCount == 0) {
  422: 	$KeyMode = ""; 
  423: 	$clock_watcher->cancel();
  424:     }
  425:     &UpdateStatus();
  426: }
  427: 
  428: =pod
  429: 
  430: =head2 SetupTimer
  431: 
  432: Sets up a 1 per sec recurring timer event.  The event handler is used to:
  433: 
  434: =item
  435: 
  436: Trigger timeouts on communications along active sockets.
  437: 
  438: =item
  439: 
  440: Trigger disconnections of idle sockets.
  441: 
  442: =cut
  443: 
  444: sub SetupTimer {
  445:     Debug(6, "SetupTimer");
  446:     Event->timer(interval => 1, cb => \&Tick,
  447: 	hard => 1);
  448: }
  449: 
  450: =pod
  451: 
  452: =head2 ServerToIdle
  453: 
  454: This function is called when a connection to the server is
  455: ready for more work.
  456: 
  457: If there is work in the Work queue the top element is dequeued
  458: and the connection will start to work on it.  If the work queue is
  459: empty, the connection is pushed on the idle connection stack where
  460: it will either get another work unit, or alternatively, if it sits there
  461: long enough, it will be shut down and released.
  462: 
  463: =cut
  464: 
  465: sub ServerToIdle {
  466:     my $Socket   = shift;	# Get the socket.
  467:     $KeyMode = $Socket->{AuthenticationMode};
  468:     delete($ActiveTransactions{$Socket}); # Server has no transaction
  469: 
  470:     &Debug(5, "Server to idle");
  471: 
  472:     #  If there's work to do, start the transaction:
  473: 
  474:     my $reqdata = $WorkQueue->dequeue(); # This is a LondTransaction
  475:     if ($reqdata ne undef)  {
  476: 	Debug(5, "Queue gave request data: ".$reqdata->getRequest());
  477: 	&StartRequest($Socket,  $reqdata);
  478: 
  479:     } else {
  480: 	
  481:     #  There's no work waiting, so push the server to idle list.
  482: 	&Debug(5, "No new work requests, server connection going idle");
  483: 	$IdleConnections->push($Socket);
  484:     }
  485: }
  486: 
  487: =pod
  488: 
  489: =head2 ClientWritable
  490: 
  491: Event callback for when a client socket is writable.
  492: 
  493: This callback is established when a transaction response is
  494: available from lond.  The response is forwarded to the unix socket
  495: as it becomes writable in this sub.
  496: 
  497: Parameters:
  498: 
  499: =item Event
  500: 
  501: The event that has been triggered. Event->w->data is
  502: the data and Event->w->fd is the socket to write.
  503: 
  504: =cut
  505: 
  506: sub ClientWritable {
  507:     my $Event    = shift;
  508:     my $Watcher  = $Event->w;
  509:     if (!defined($Watcher)) {
  510: 	&child_exit(-1,'No watcher for event in ClientWritable');
  511:     }
  512:     my $Data     = $Watcher->data;
  513:     my $Socket   = $Watcher->fd;
  514: 
  515:     # Try to send the data:
  516: 
  517:     &Debug(6, "ClientWritable writing".$Data);
  518:     &Debug(9, "Socket is: ".$Socket);
  519: 
  520:     if($Socket->connected) {
  521: 	my $result = $Socket->send($Data, 0);
  522: 	
  523: 	# $result undefined: the write failed.
  524: 	# otherwise $result is the number of bytes written.
  525: 	# Remove that preceding string from the data.
  526: 	# If the resulting data is empty, destroy the watcher
  527: 	# and set up a read event handler to accept the next
  528: 	# request.
  529: 	
  530: 	&Debug(9,"Send result is ".$result." Defined: ".defined($result));
  531: 	if($result ne undef) {
  532: 	    &Debug(9, "send result was defined");
  533: 	    if($result == length($Data)) { # Entire string sent.
  534: 		&Debug(9, "ClientWritable data all written");
  535: 		$Watcher->cancel();
  536: 		#
  537: 		#  Set up to read next request from socket:
  538: 		
  539: 		my $descr     = sprintf("Connection to lonc client %d",
  540: 					$ActiveClients{$Socket});
  541: 		Event->io(cb    => \&ClientRequest,
  542: 			  poll  => 'r',
  543: 			  desc  => $descr,
  544: 			  data  => "",
  545: 			  fd    => $Socket);
  546: 		
  547: 	    } else {		# Partial string sent.
  548: 		$Watcher->data(substr($Data, $result));
  549: 		if($result == 0) {    # client hung up on us!!
  550: 		    # Log("INFO", "lonc pipe client hung up on us!");
  551: 		    $Watcher->cancel;
  552: 		    $Socket->shutdown(2);
  553: 		    $Socket->close();
  554: 		}
  555: 	    }
  556: 	    
  557: 	} else {			# Error of some sort...
  558: 	    
  559: 	    # Some errnos are possible:
  560: 	    my $errno = $!;
  561: 	    if($errno == POSIX::EWOULDBLOCK   ||
  562: 	       $errno == POSIX::EAGAIN        ||
  563: 	       $errno == POSIX::EINTR) {
  564: 		# No action taken...the socket will be writable firing the event again
  565: 		# which will result in a retry of the write.
  566: 	    } else {		# Unanticipated errno.
  567: 		&Debug(5,"ClientWritable error or peer shutdown".$RemoteHost);
  568: 		$Watcher->cancel;	# Stop the watcher.
  569: 		$Socket->shutdown(2); # Kill connection
  570: 		$Socket->close();	# Close the socket.
  571: 	    }
  572: 	    
  573: 	}
  574:     } else {
  575: 	$Watcher->cancel();	# A delayed request...just cancel.
  576: 	return;
  577:     }
  578: }
  579: 
  580: =pod
  581: 
  582: =head2 CompleteTransaction
  583: 
  584: Called when the reply data has been received for a lond 
  585: transaction.   The reply data must now be sent to the
  586: ultimate client on the other end of the Unix socket.  This is
  587: done by setting up a writable event for the socket with the
  588: data the reply data.
  589: 
  590: Parameters:
  591: 
  592: =item Socket
  593: 
  594: Socket on which the lond transaction occurred.  This is a
  595: LondConnection. The data received are in the TransactionReply member.
  596: 
  597: =item Transaction
  598: 
  599: The transaction that is being completed.
  600: 
  601: =cut
  602: 
  603: sub CompleteTransaction {
  604:     &Debug(5,"Complete transaction");
  605: 
  606:     my ($Socket, $Transaction) = @_;
  607: 
  608:     if (!$Transaction->isDeferred()) { # Normal transaction
  609: 	my $data   = $Socket->GetReply(); # Data to send.
  610: 	if($LogTransactions) {
  611: 	    Log("SUCCESS", "Reply from lond: '$data'");
  612: 	}
  613: 	StartClientReply($Transaction, $data);
  614:     } else {			# Delete deferred transaction file.
  615: 	Log("SUCCESS", "A delayed transaction was completed");
  616: 	LogPerm("S:".$Socket->PeerLoncapaHim().":".$Transaction->getRequest());
  617: 	unlink($Transaction->getFile());
  618:     }
  619: }
  620: 
  621: =pod
  622: 
  623: =head1 StartClientReply
  624: 
  625:    Initiates a reply to a client where the reply data is a parameter.
  626: 
  627: =head2  parameters:
  628: 
  629: =item Transaction
  630: 
  631:     The transaction for which we are responding to the client.
  632: 
  633: =item data
  634: 
  635:     The data to send to apache client.
  636: 
  637: =cut
  638: 
  639: sub StartClientReply {
  640: 
  641:     my ($Transaction, $data) = @_;
  642: 
  643:     my $Client   = $Transaction->getClient();
  644: 
  645:     &Debug(8," Reply was: ".$data);
  646:     my $Serial         = $ActiveClients{$Client};
  647:     my $desc           = sprintf("Connection to lonc client %d",
  648: 				 $Serial);
  649:     Event->io(fd       => $Client,
  650: 	      poll     => "w",
  651: 	      desc     => $desc,
  652: 	      cb       => \&ClientWritable,
  653: 	      data     => $data);
  654: }
  655: 
  656: =pod
  657: 
  658: =head2 FailTransaction
  659: 
  660:   Finishes a transaction with failure because the associated lond socket
  661:   disconnected.  There are two possibilities:
  662:   - The transaction is deferred: in which case we just quietly
  663:     delete the transaction since there is no client connection.
  664:   - The transaction is 'live' in which case we initiate the sending
  665:     of "con_lost" to the client.
  666: 
  667: Deleting the transaction means killing it from the %ActiveTransactions hash.
  668: 
  669: Parameters:
  670: 
  671: =item client  
  672:  
  673:    The LondTransaction we are failing.
  674:  
  675: 
  676: =cut
  677: 
  678: sub FailTransaction {
  679:     my $transaction = shift;
  680:     
  681:     #  If the socket is dead, that's already logged.
  682: 
  683:     if ($ConnectionRetriesLeft > 0) {
  684: 	Log("WARNING", "Failing transaction "
  685: 	    .$transaction->getLoggableRequest());
  686:     }
  687:     Debug(1, "Failing transaction: ".$transaction->getLoggableRequest());
  688:     if (!$transaction->isDeferred()) { # If the transaction is deferred we'll get to it.
  689: 	my $client  = $transaction->getClient();
  690: 	Debug(1," Replying con_lost to ".$transaction->getRequest());
  691: 	StartClientReply($transaction, "con_lost\n");
  692:     }
  693: 
  694: }
  695: 
  696: =pod
  697: 
  698: =head1  EmptyQueue
  699: 
  700:   Fails all items in the work queue with con_lost.
  701:   Note that each item in the work queue is a transaction.
  702: 
  703: =cut
  704: 
  705: sub EmptyQueue {
  706:     $ConnectionRetriesLeft--;	# Counts as connection failure too.
  707:     while($WorkQueue->Count()) {
  708: 	my $request = $WorkQueue->dequeue(); # This is a transaction
  709: 	FailTransaction($request);
  710:     }
  711: }
  712: 
  713: =pod
  714: 
  715: =head2 CloseAllLondConnections
  716: 
  717: Close all connections open on lond prior to exit e.g.
  718: 
  719: =cut
  720: 
  721: sub CloseAllLondConnections {
  722:     foreach my $Socket (keys %ActiveConnections) {
  723:       if(exists($ActiveTransactions{$Socket})) {
  724: 	FailTransaction($ActiveTransactions{$Socket});
  725:       }
  726:       KillSocket($Socket);
  727:     }
  728: }
  729: 
  730: =pod
  731: 
  732: =head2 KillSocket
  733:  
  734: Destroys a socket.  This function can be called either when a socket
  735: has died of 'natural' causes or because a socket needs to be pruned due to
  736: idleness.  If the socket has died naturally, if there are no longer any 
  737: live connections a new connection is created (in case there are transactions
  738: in the queue).  If the socket has been pruned, it is never re-created.
  739: 
  740: Parameters:
  741: 
  742: =item Socket
  743:  
  744:   The socket to kill off.
  745: 
  746: =item restart
  747: 
  748: non-zero if we are allowed to create a new connection.
  749: 
  750: =cut
  751: 
  752: sub KillSocket {
  753:     my $Socket = shift;
  754:     my $restart = shift;
  755: 
  756:     Log("WARNING", "Shutting down a socket");
  757:     $Socket->Shutdown();
  758: 
  759:     #  If the socket came from the active connection set,
  760:     #  delete its transaction... note that FailTransaction should
  761:     #  already have been called!!!
  762:     #  otherwise it came from the idle set.
  763:     #  
  764:     
  765:     if(exists($ActiveTransactions{$Socket})) {
  766: 	delete ($ActiveTransactions{$Socket});
  767:     }
  768:     if(exists($ActiveConnections{$Socket})) {
  769: 	$ActiveConnections{$Socket}->cancel;
  770: 	delete($ActiveConnections{$Socket});
  771:         # Decrement ConnectionCount unless we will immediately
  772:         # re-connect (i.e., $restart is true), because this was
  773:         # a connection where the SSL channel for exchange of the
  774:         # shared key failed, and we may use an insecure channel.
  775:         unless ($restart) {
  776: 	    $ConnectionCount--;
  777:         }
  778: 	if ($ConnectionCount < 0) { $ConnectionCount = 0; }
  779:     }
  780:     #  If the connection count has gone to zero and there is work in the
  781:     #  work queue, the work all gets failed with con_lost.
  782:     #
  783:   
  784:     if($ConnectionCount == 0) {
  785: 	$LondConnecting = 0;	# No connections so also not connecting.
  786: 	EmptyQueue();
  787: 	CloseAllLondConnections(); # Should all already be closed but...
  788:         &clear_childpid($$);
  789:     }
  790:     UpdateStatus();
  791: }
  792: 
  793: =pod
  794: 
  795: =head2 LondReadable
  796: 
  797: This function is called whenever a lond connection
  798: is readable.  The action is state dependent:
  799: 
  800: =head3 State=Initialized
  801: 
  802: We are waiting for the challenge, this is a no-op until the
  803: state changes.
  804: 
  805: =head3 State=Challenged 
  806: 
  807: The challenge has arrived we need to transition to Writable.
  808: The connection must echo the challenge back.
  809: 
  810: =head3 State=ChallengeReplied
  811: 
  812: The challenge has been replied to.  Then we are receiving the 
  813: 'ok' from the partner.
  814: 
  815: =head3  State=ReadingVersionString
  816: 
  817: We have requested the lond version and are reading the
  818: version back.  Upon completion, we'll store the version away
  819: for future use(?).
  820: 
  821: =head3 State=HostSet
  822: 
  823: We have selected the domain name of our peer (multhomed hosts)
  824: and are getting the reply (presumably ok) back.
  825: 
  826: =head3 State=RequestingKey
  827: 
  828: The ok has been received and we need to send the request for
  829: an encryption key.  Transition to writable for that.
  830: 
  831: =head3 State=ReceivingKey
  832: 
  833: The the key has been requested, now we are reading the new key.
  834: 
  835: =head3 State=Idle 
  836: 
  837: The encryption key has been negotiated or we have finished 
  838: reading data from the a transaction.   If the callback data have
  839: a client as well as the socket information, then we are 
  840: doing a transaction and the data received are relayed to the client
  841: before the socket is put on the idle list.
  842: 
  843: =head3 State=SendingRequest
  844: 
  845: I do not think this state can be received here, but if it is,
  846: the appropriate thing to do is to transition to writable, and send
  847: the request.
  848: 
  849: =head3 State=ReceivingReply
  850: 
  851: We finished sending the request to the server and now transition
  852: to readable to receive the reply. 
  853: 
  854: The parameter to this function are:
  855: 
  856: The event. Implicit in this is the watcher and its data.  The data 
  857: contain at least the lond connection object and, if a 
  858: transaction is in progress, the socket attached to the local client.
  859: 
  860: =cut
  861: 
  862: sub LondReadable {
  863: 
  864:     my $Event      = shift;
  865:     my $Watcher    = $Event->w;
  866:     my $Socket     = $Watcher->data;
  867:     my $client     = undef;
  868: 
  869:     &Debug(6,"LondReadable called state = ".$Socket->GetState());
  870: 
  871: 
  872:     my $State = $Socket->GetState(); # All action depends on the state.
  873: 
  874:     SocketDump(6, $Socket);
  875:     my $status = $Socket->Readable();
  876: 
  877:     &Debug(2, "Socket->Readable returned: $status");
  878: 
  879:     if($status != 0) {
  880: 	# bad return from socket read. Currently this means that
  881: 	# The socket has become disconnected. We fail the transaction.
  882: 
  883: 	Log("WARNING",
  884: 	    "Lond connection lost.");
  885:         my $state_on_exit = $Socket->GetState();
  886: 	if(exists($ActiveTransactions{$Socket})) {
  887: 	    FailTransaction($ActiveTransactions{$Socket});
  888: 	} else {
  889: 	    #  Socket is connecting and failed... need to mark
  890: 	    #  no longer connecting.
  891: 	    $LondConnecting = 0;
  892: 	}
  893: 	$Watcher->cancel();
  894:         if ($state_on_exit eq 'ReInitNoSSL') {
  895:             # SSL certificate verification failed, and insecure connection
  896:             # allowed. Send restart arg to KillSocket(), so EmptyQueue() 
  897:             # is not called, as we still hope to process queued request.
  898: 
  899:             KillSocket($Socket,1);
  900: 
  901:             # Re-initiate creation of Lond Connection for use with queued
  902:             # request.
  903: 
  904:             ShowStatus("Connected to ".$RemoteHost);
  905:             Log("WARNING","No SSL channel (verification failed), will try with insecure channel");
  906:             &MakeLondConnection(1);
  907: 
  908:         } else {
  909: 	    KillSocket($Socket);
  910: 	    $ConnectionRetriesLeft--;       # Counts as connection failure         
  911:         }
  912: 	return;
  913:     }
  914:     SocketDump(6,$Socket);
  915: 
  916:     $State = $Socket->GetState(); # Update in case of transition.
  917:     &Debug(6, "After read, state is ".$State);
  918: 
  919:     if($State eq "Initialized") {
  920: 
  921: 
  922:     } elsif ($State eq "ReInitNoSSL") {
  923: 
  924:     } elsif ($State eq "ChallengeReceived") {
  925: 	#  The challenge must be echoed back;  The state machine
  926: 	# in the connection takes care of setting that up.  Just
  927: 	# need to transition to writable:
  928: 	
  929: 	$Watcher->cb(\&LondWritable);
  930: 	$Watcher->poll("w");
  931: 
  932:     } elsif ($State eq "ChallengeReplied") {
  933: 
  934:     } elsif ($State eq "RequestingVersion") {
  935: 	# Need to ask for the version... that is writiability:
  936: 
  937: 	$Watcher->cb(\&LondWritable);
  938: 	$Watcher->poll("w");
  939: 
  940:     } elsif ($State eq "ReadingVersionString") {
  941: 	# Read the rest of the version string... 
  942:     } elsif ($State eq "SetHost") {
  943: 	# Need to request the actual domain get set...
  944: 
  945: 	$Watcher->cb(\&LondWritable);
  946: 	$Watcher->poll("w");
  947:     } elsif ($State eq "HostSet") {
  948: 	# Reading the 'ok' from the peer.
  949: 
  950:     } elsif ($State eq "RequestingKey") {
  951: 	#  The ok was received.  Now we need to request the key
  952: 	#  That requires us to be writable:
  953: 
  954: 	$Watcher->cb(\&LondWritable);
  955: 	$Watcher->poll("w");
  956: 
  957:     } elsif ($State eq "ReceivingKey") {
  958: 
  959:     } elsif ($State eq "Idle") {
  960: 
  961:         if ($ConnectionCount == 1) { 
  962:             # Write child Pid file to keep track of ssl and insecure
  963:             # connections
  964: 
  965:             &record_childpid($Socket);
  966:         }
  967: 
  968: 	# This is as good a spot as any to get the peer version
  969: 	# string:
  970:    
  971: 	if($LondVersion eq "unknown") {
  972: 	    $LondVersion = $Socket->PeerVersion();
  973: 	    Log("INFO", "Connected to lond version: $LondVersion");
  974: 	}
  975: 	# If necessary, complete a transaction and then go into the
  976: 	# idle queue.
  977: 	#  Note that a trasition to idle indicates a live lond
  978: 	# on the other end so reset the connection retries.
  979: 	#
  980: 	$ConnectionRetriesLeft = $ConnectionRetries; # success resets the count
  981: 	$Watcher->cancel();
  982: 	if(exists($ActiveTransactions{$Socket})) {
  983: 	    Debug(5,"Completing transaction!!");
  984: 	    CompleteTransaction($Socket, 
  985: 				$ActiveTransactions{$Socket});
  986: 	} else {
  987: 	    my $count = $Socket->GetClientData();
  988: 	    Log("SUCCESS", "Connection ".$count." to "
  989: 		.$RemoteHost." now ready for action");
  990: 	}
  991: 	ServerToIdle($Socket);	# Next work unit or idle.
  992: 
  993: 	#
  994: 	$LondConnecting = 0;	# Best spot I can think of for this.
  995: 	# 
  996: 	
  997:     } elsif ($State eq "SendingRequest") {
  998: 	#  We need to be writable for this and probably don't belong
  999: 	#  here inthe first place.
 1000: 
 1001: 	Debug(6, "SendingRequest state encountered in readable");
 1002: 	$Watcher->poll("w");
 1003: 	$Watcher->cb(\&LondWritable);
 1004: 
 1005:     } elsif ($State eq "ReceivingReply") {
 1006: 
 1007: 
 1008:     } else {
 1009: 	# Invalid state.
 1010: 	Debug(4, "Invalid state in LondReadable");
 1011:     }
 1012: }
 1013: 
 1014: =pod
 1015: 
 1016: =head2 LondWritable
 1017: 
 1018: This function is called whenever a lond connection
 1019: becomes writable while there is a writeable monitoring
 1020: event.  The action taken is very state dependent:
 1021: 
 1022: =head3 State = Connected 
 1023: 
 1024: The connection is in the process of sending the 'init' hailing to the
 1025: lond on the remote end.  The Writable member of the connection object
 1026: is called.  On error, call ConnectionError to destroy the connection
 1027: and remove it from the ActiveConnections hash.
 1028: 
 1029: =head3 Initialized
 1030: 
 1031: 'init' has been sent, writability monitoring is removed and
 1032: readability monitoring is started with LondReadable as the callback.
 1033: 
 1034: =head3 ChallengeReceived
 1035: 
 1036: The connection has received the who are you challenge from the remote
 1037: system, and is in the process of sending the challenge
 1038: response. Writable is called.
 1039: 
 1040: =head3 ChallengeReplied
 1041: 
 1042: The connection has replied to the initial challenge The we switch to
 1043: monitoring readability looking for the server to reply with 'ok'.
 1044: 
 1045: =head3 RequestingKey
 1046: 
 1047: The connection is in the process of requesting its encryption key.
 1048: Writable is called.
 1049: 
 1050: =head3 ReceivingKey
 1051: 
 1052: The connection has sent the request for a key.  Switch to readability
 1053: monitoring to accept the key
 1054: 
 1055: =head3 SendingRequest
 1056: 
 1057: The connection is in the process of sending a request to the server.
 1058: This request is part of a client transaction.  All the states until
 1059: now represent the client setup protocol. Writable is called.
 1060: 
 1061: =head3 ReceivingReply
 1062: 
 1063: The connection has sent a request.  Now it must receive a reply.
 1064: Readability monitoring is requested.
 1065: 
 1066: This function is an event handler and therefore receives as
 1067: a parameter the event that has fired.  The data for the watcher
 1068: of this event is a reference to a list of one or two elements,
 1069: depending on state. The first (and possibly only) element is the
 1070: socket.  The second (present only if a request is in progress)
 1071: is the socket on which to return a reply to the caller.
 1072: 
 1073: =cut
 1074: 
 1075: sub LondWritable {
 1076:     my $Event   = shift;
 1077:     my $Watcher = $Event->w;
 1078:     my $Socket  = $Watcher->data;
 1079:     my $State   = $Socket->GetState();
 1080: 
 1081:     Debug(6,"LondWritable State = ".$State."\n");
 1082: 
 1083:  
 1084:     #  Figure out what to do depending on the state of the socket:
 1085:     
 1086: 
 1087: 
 1088: 
 1089:     SocketDump(6,$Socket);
 1090: 
 1091:     #  If the socket is writable, we must always write.
 1092:     # Only by writing will we undergo state transitions.
 1093:     # Old logic wrote in state specific code below, however
 1094:     # That forces us at least through another invocation of
 1095:     # this function after writability is possible again.
 1096:     # This logic also factors out common code for handling
 1097:     # write failures... in all cases, write failures 
 1098:     # Kill the socket.
 1099:     #  This logic makes the branches of the >big< if below
 1100:     # so that the writing states are actually NO-OPs.
 1101: 
 1102:     if ($Socket->Writable() != 0) {
 1103: 	#  The write resulted in an error.
 1104: 	# We'll treat this as if the socket got disconnected:
 1105: 	Log("WARNING", "Connection to ".$RemoteHost.
 1106: 	    " has been disconnected");
 1107: 	if(exists($ActiveTransactions{$Socket})) {
 1108: 	    FailTransaction($ActiveTransactions{$Socket});
 1109: 	} else {
 1110: 	    #  In the process of conneting, so need to turn that off.
 1111: 	    
 1112: 	    $LondConnecting = 0;
 1113: 	}
 1114: 	$Watcher->cancel();
 1115: 	KillSocket($Socket);
 1116: 	return;
 1117:     }
 1118: 
 1119: 
 1120: 
 1121:     if      ($State eq "Connected")         {
 1122: 
 1123: 	#  "init" is being sent...
 1124:  
 1125:     } elsif ($State eq "Initialized")       {
 1126: 
 1127: 	# Now that init was sent, we switch 
 1128: 	# to watching for readability:
 1129: 
 1130: 	$Watcher->cb(\&LondReadable);
 1131: 	$Watcher->poll("r");
 1132: 
 1133:     } elsif ($State eq "ReInitNoSSL") {
 1134: 
 1135:     } elsif ($State eq "ChallengeReceived") {
 1136: 	# We received the challenge, now we 
 1137: 	# are echoing it back. This is a no-op,
 1138: 	# we're waiting for the state to change
 1139: 	
 1140:     } elsif ($State eq "ChallengeReplied")  {
 1141: 	# The echo was sent back, so we switch
 1142: 	# to watching readability.
 1143: 
 1144: 	$Watcher->cb(\&LondReadable);
 1145: 	$Watcher->poll("r");
 1146:     } elsif ($State eq "RequestingVersion") {
 1147: 	# Sending the peer a version request...
 1148: 
 1149:     } elsif ($State eq "ReadingVersionString") {
 1150: 	# Transition to read since we have sent the
 1151: 	# version command and now just need to read the
 1152: 	# version string from the peer:
 1153:       
 1154: 	$Watcher->cb(\&LondReadable);
 1155: 	$Watcher->poll("r");
 1156:       
 1157:     } elsif ($State eq "SetHost") {
 1158: 	#  Setting the remote domain...
 1159: 
 1160:     } elsif ($State eq "HostSet") {
 1161: 	# Back to readable to get the ok.
 1162:       
 1163: 	$Watcher->cb(\&LondReadable);
 1164: 	$Watcher->poll("r");
 1165:       
 1166: 
 1167:     } elsif ($State eq "RequestingKey")     {
 1168: 	# At this time we're requesting the key.
 1169: 	# again, this is essentially a no-op.
 1170: 
 1171:     } elsif ($State eq "ReceivingKey")      {
 1172: 	# Now we need to wait for the key
 1173: 	# to come back from the peer:
 1174: 
 1175: 	$Watcher->cb(\&LondReadable);
 1176: 	$Watcher->poll("r");
 1177: 
 1178:     } elsif ($State eq "SendingRequest")    {
 1179:  
 1180: 	# At this time we are sending a request to the
 1181: 	# peer... write the next chunk:
 1182: 
 1183: 
 1184:     } elsif ($State eq "ReceivingReply")    {
 1185: 	# The send has completed.  Wait for the
 1186: 	# data to come in for a reply.
 1187: 	Debug(8,"Writable sent request/receiving reply");
 1188: 	$Watcher->cb(\&LondReadable);
 1189: 	$Watcher->poll("r");
 1190: 
 1191:     } else {
 1192: 	#  Control only passes here on an error: 
 1193: 	#  the socket state does not match any
 1194: 	#  of the known states... so an error
 1195: 	#  must be logged.
 1196: 
 1197: 	&Debug(4, "Invalid socket state ".$State."\n");
 1198:     }
 1199:     
 1200: }
 1201: 
 1202: =pod
 1203:     
 1204: =cut
 1205: 
 1206: 
 1207: sub QueueDelayed {
 1208:     Debug(3,"QueueDelayed called");
 1209: 
 1210:     my $path = "$perlvar{'lonSockDir'}/delayed";
 1211: 
 1212:     Debug(4, "Delayed path: ".$path);
 1213:     opendir(DIRHANDLE, $path);
 1214: 
 1215:     my $host_id_re = '(?:'.join('|',map {quotemeta($_)} (@all_host_ids)).')';
 1216:     my @alldelayed = grep(/\.$host_id_re$/, readdir(DIRHANDLE));
 1217:     closedir(DIRHANDLE);
 1218:     foreach my $dfname (sort(@alldelayed)) {
 1219: 	my $reqfile = "$path/$dfname";
 1220: 	my ($host_id) = ($dfname =~ /\.([^.]*)$/);
 1221: 	Debug(4, "queueing ".$reqfile." for $host_id");
 1222: 	my $Handle = IO::File->new($reqfile);
 1223: 	my $cmd    = <$Handle>;
 1224: 	chomp $cmd;		# There may or may not be a newline...
 1225: 	$cmd = $cmd."\n";	# now for sure there's exactly one newline.
 1226: 	my $Transaction = LondTransaction->new("sethost:$host_id:$cmd");
 1227: 	$Transaction->SetDeferred($reqfile);
 1228: 	QueueTransaction($Transaction);
 1229:     }
 1230:     
 1231: }
 1232: 
 1233: =pod
 1234: 
 1235: =head2 MakeLondConnection
 1236: 
 1237: Create a new lond connection object, and start it towards its initial
 1238: idleness.  Once idle, it becomes eligible to receive transactions
 1239: from the work queue.  If the work queue is not empty when the
 1240: connection is completed and becomes idle, it will dequeue an entry and
 1241: start off on it.
 1242: 
 1243: =cut
 1244: 
 1245: sub MakeLondConnection {
 1246:     my ($restart) = @_;
 1247:     Debug(4,"MakeLondConnection to ".GetServerHost()." on port "
 1248: 	  .GetServerPort());
 1249: 
 1250:     my $Connection = LondConnection->new(&GetServerHost(),
 1251: 					 &GetServerPort(),
 1252: 					 &GetHostId(),
 1253: 					 &GetDefHostId());
 1254: 
 1255:     if($Connection eq undef) {
 1256: 	Log("CRITICAL","Failed to make a connection with lond.");
 1257: 	$ConnectionRetriesLeft--;
 1258: 	return 0;		# Failure.
 1259:     }  else {
 1260: 	$LondConnecting = 1;	# Connection in progress.
 1261: 	# The connection needs to have writability 
 1262: 	# monitored in order to send the init sequence
 1263: 	# that starts the whole authentication/key
 1264: 	# exchange underway.
 1265: 	#
 1266: 	my $Socket = $Connection->GetSocket();
 1267: 	if($Socket eq undef) {
 1268: 	    &child_exit(-1, "did not get a socket from the connection");
 1269: 	} else {
 1270: 	    &Debug(9,"MakeLondConnection got socket: ".$Socket);
 1271: 	}
 1272: 	
 1273: 	$Connection->SetTimeoutCallback(\&SocketTimeout);
 1274: 
 1275: 	my $event = Event->io(fd       => $Socket,
 1276: 			   poll     => 'w',
 1277: 			   cb       => \&LondWritable,
 1278: 			   data     => $Connection,
 1279: 			   desc => 'Connection to lond server');
 1280: 	$ActiveConnections{$Connection} = $event;
 1281: 	if ($ConnectionCount == 0) {
 1282: 	    &SetupTimer;	# Need to handle timeouts with connections...
 1283: 	}
 1284:         unless ($restart) {
 1285: 	    $ConnectionCount++;
 1286:         }
 1287: 	$Connection->SetClientData($ConnectionCount);
 1288: 	Debug(4, "Connection count = ".$ConnectionCount);
 1289: 	if($ConnectionCount == 1) { # First Connection:
 1290: 	    QueueDelayed;
 1291: 	}
 1292: 	Log("SUCCESS", "Created connection ".$ConnectionCount
 1293: 	    ." to host ".GetServerHost());
 1294: 	return 1;		# Return success.
 1295:     }
 1296:     
 1297: }
 1298: 
 1299: =pod
 1300: 
 1301: =head2 StartRequest
 1302: 
 1303: Starts a lond request going on a specified lond connection.
 1304: parameters are:
 1305: 
 1306: =item $Lond
 1307: 
 1308: Connection to the lond that will send the transaction and receive the
 1309: reply.
 1310: 
 1311: =item $Client
 1312: 
 1313: Connection to the client that is making this request. We got the
 1314: request from this socket, and when the request has been relayed to
 1315: lond and we get a reply back from lond it will get sent to this
 1316: socket.
 1317: 
 1318: =item $Request
 1319: 
 1320: The text of the request to send.
 1321: 
 1322: =cut
 1323: 
 1324: sub StartRequest {
 1325: 
 1326:     my ($Lond, $Request) = @_;
 1327:     
 1328:     Debug(6, "StartRequest: ".$Request->getRequest());
 1329: 
 1330:     my $Socket = $Lond->GetSocket();
 1331:     
 1332:     $Request->Activate($Lond);
 1333:     $ActiveTransactions{$Lond} = $Request;
 1334: 
 1335:     $Lond->InitiateTransaction($Request->getRequest());
 1336:     my $event = Event->io(fd      => $Socket,
 1337: 		       poll    => "w",
 1338: 		       cb      => \&LondWritable,
 1339: 		       data    => $Lond,
 1340: 		       desc    => "lond transaction connection");
 1341:     $ActiveConnections{$Lond} = $event;
 1342:     Debug(8," Start Request made watcher data with ".$event->data."\n");
 1343: }
 1344: 
 1345: =pod
 1346: 
 1347: =head2 QueueTransaction
 1348: 
 1349: If there is an idle lond connection, it is put to work doing this
 1350: transaction.  Otherwise, the transaction is placed in the work queue.
 1351: If placed in the work queue and the maximum number of connections has
 1352: not yet been created, a new connection will be started.  Our goal is
 1353: to eventually have a sufficient number of connections that the work
 1354: queue will typically be empty.  parameters are:
 1355: 
 1356: =item Socket
 1357: 
 1358: open on the lonc client.
 1359: 
 1360: =item Request
 1361: 
 1362: data to send to the lond.
 1363: 
 1364: =cut
 1365: 
 1366: sub QueueTransaction {
 1367: 
 1368:     my $requestData   = shift;	# This is a LondTransaction.
 1369:     my $cmd           = $requestData->getRequest();
 1370: 
 1371:     Debug(6,"QueueTransaction: ".$cmd);
 1372: 
 1373:     my $LondSocket    = $IdleConnections->pop();
 1374:     if(!defined $LondSocket) {	# Need to queue request.
 1375: 	Debug(5,"Must queue...");
 1376: 	$WorkQueue->enqueue($requestData);
 1377: 	Debug(5, "Queue Transaction startnew $ConnectionCount $LondConnecting");
 1378: 	if(($ConnectionCount < $MaxConnectionCount)   && (! $LondConnecting)) {
 1379: 
 1380: 	    if($ConnectionRetriesLeft > 0) {
 1381: 		Debug(5,"Starting additional lond connection");
 1382: 		if(&MakeLondConnection() == 0) {
 1383: 		    EmptyQueue();	# Fail transactions, can't make connection.
 1384: 		    CloseAllLondConnections; # Should all be closed but...
 1385: 		}
 1386: 	    } else {
 1387: 		ShowStatus(GetServerHost()." >>> DEAD !!!! <<<");
 1388: 		$LondConnecting = 0;
 1389: 		EmptyQueue();	# It's worse than that ... he's dead Jim.
 1390: 		CloseAllLondConnections; # Should all be closed but..
 1391: 	    }
 1392: 	}
 1393:     } else {			# Can start the request:
 1394: 	Debug(8,"Can start...");
 1395: 	StartRequest($LondSocket,  $requestData);
 1396:     }
 1397: }
 1398: 
 1399: #-------------------------- Lonc UNIX socket handling -------------------
 1400: =pod
 1401: 
 1402: =head2 ClientRequest
 1403: 
 1404: Callback that is called when data can be read from the UNIX domain
 1405: socket connecting us with an apache server process.
 1406: 
 1407: =cut
 1408: 
 1409: sub ClientRequest {
 1410:     Debug(6, "ClientRequest");
 1411:     my $event   = shift;
 1412:     my $watcher = $event->w;
 1413:     my $socket  = $watcher->fd;
 1414:     my $data    = $watcher->data;
 1415:     my $thisread;
 1416: 
 1417:     Debug(9, "  Watcher named: ".$watcher->desc);
 1418: 
 1419:     my $rv = $socket->recv($thisread, POSIX::BUFSIZ, 0);
 1420:     Debug(8, "rcv:  data length = ".length($thisread)
 1421: 	  ." read =".$thisread);
 1422:     unless (defined $rv  && length($thisread)) {
 1423: 	 # Likely eof on socket.
 1424: 	Debug(5,"Client Socket closed on lonc for ".$RemoteHost);
 1425: 	close($socket);
 1426: 	$watcher->cancel();
 1427: 	delete($ActiveClients{$socket});
 1428: 	return;
 1429:     }
 1430:     Debug(8,"Data: ".$data." this read: ".$thisread);
 1431:     $data = $data.$thisread;	# Append new data.
 1432:     $watcher->data($data);
 1433:     if($data =~ /\n$/) {	# Request entirely read.
 1434: 	if ($data eq "close_connection_exit\n") {
 1435: 	    Log("CRITICAL",
 1436: 		"Request Close Connection ... exiting");
 1437: 	    CloseAllLondConnections();
 1438: 	    exit;
 1439: 	} elsif ($data eq "reset_retries\n") {
 1440: 	    Log("INFO", "Resetting Connection Retries.");
 1441: 	    $ConnectionRetriesLeft = $ConnectionRetries;
 1442: 	    &UpdateStatus();
 1443: 	    my $Transaction = LondTransaction->new($data);
 1444: 	    $Transaction->SetClient($socket);
 1445: 	    StartClientReply($Transaction, "ok\n");
 1446: 	    $watcher->cancel();
 1447: 	    return;
 1448: 	}
 1449: 	Debug(8, "Complete transaction received: ".$data);
 1450: 	if ($LogTransactions) {
 1451: 	    Log("SUCCESS", "Transaction: '$data'"); # Transaction has \n.
 1452: 	}
 1453: 	my $Transaction = LondTransaction->new($data);
 1454: 	$Transaction->SetClient($socket);
 1455: 	QueueTransaction($Transaction);
 1456: 	$watcher->cancel();	# Done looking for input data.
 1457:     }
 1458: 
 1459: }
 1460: 
 1461: #
 1462: #     Accept a connection request for a client (lonc child) and
 1463: #    start up an event watcher to keep an eye on input from that 
 1464: #    Event.  This can be called both from NewClient and from
 1465: #    ChildProcess.
 1466: # Parameters:
 1467: #    $socket       - The listener socket.
 1468: # Returns:
 1469: #   NONE
 1470: # Side Effects:
 1471: #    An event is made to watch the accepted connection.
 1472: #    Active clients hash is updated to reflect the new connection.
 1473: #    The client connection count is incremented.
 1474: #
 1475: sub accept_client {
 1476:     my ($socket) = @_;
 1477: 
 1478:     Debug(8, "Entering accept for lonc UNIX socket\n");
 1479:     my $connection = $socket->accept();	# Accept the client connection.
 1480:     Debug(8,"Connection request accepted from "
 1481: 	  .GetPeername($connection, AF_UNIX));
 1482: 
 1483: 
 1484:     my $description = sprintf("Connection to lonc client %d",
 1485: 			      $ClientConnection);
 1486:     Debug(9, "Creating event named: ".$description);
 1487:     Event->io(cb      => \&ClientRequest,
 1488: 	      poll    => 'r',
 1489: 	      desc    => $description,
 1490: 	      data    => "",
 1491: 	      fd      => $connection);
 1492:     $ActiveClients{$connection} = $ClientConnection;
 1493:     $ClientConnection++;
 1494: }
 1495: 
 1496: =pod
 1497: 
 1498: =head2  NewClient
 1499: 
 1500: Callback that is called when a connection is received on the unix
 1501: socket for a new client of lonc.  The callback is parameterized by the
 1502: event.. which is a-priori assumed to be an io event, and therefore has
 1503: an fd member that is the Listener socket.  We accept the connection
 1504: and register a new event on the readability of that socket:
 1505: 
 1506: =cut
 1507: 
 1508: sub NewClient {
 1509:     Debug(6, "NewClient");
 1510:     my $event      = shift;		# Get the event parameters.
 1511:     my $watcher    = $event->w; 
 1512:     my $socket     = $watcher->fd;	# Get the event' socket.
 1513: 
 1514:     &accept_client($socket);
 1515: }
 1516: 
 1517: =pod
 1518: 
 1519: =head2 GetLoncSocketPath
 1520: 
 1521: Returns the name of the UNIX socket on which to listen for client
 1522: connections.
 1523: 
 1524: =head2 Parameters:
 1525: 
 1526:     host (optional)  - Name of the host socket to return.. defaults to
 1527:                        the return from GetServerHost().
 1528: 
 1529: =cut
 1530: 
 1531: sub GetLoncSocketPath {
 1532: 
 1533:     my $host = GetServerHost();	# Default host.
 1534:     if (@_) {
 1535: 	($host)  = @_;		# Override if supplied.
 1536:     }
 1537:     return $UnixSocketDir."/".$host;
 1538: }
 1539: 
 1540: =pod
 1541: 
 1542: =head2 GetServerHost
 1543: 
 1544: Returns the host whose lond we talk with.
 1545: 
 1546: =cut
 1547: 
 1548: sub GetServerHost {
 1549:     return $RemoteHost;		# Setup by the fork.
 1550: }
 1551: 
 1552: =pod
 1553: 
 1554: =head2 GetHostId
 1555: 
 1556: Returns the hostid whose lond we talk with.
 1557: 
 1558: =cut
 1559: 
 1560: sub GetHostId {
 1561:     return $RemoteHostId;		# Setup by the fork.
 1562: }
 1563: 
 1564: =pod
 1565: 
 1566: =head2 GetDefHostId
 1567: 
 1568: Returns the default hostid for the node whose lond we talk with.
 1569: 
 1570: =cut
 1571: 
 1572: sub GetDefHostId {                      # Setup by the fork.
 1573:     return $RemoteDefHostId;
 1574: }
 1575: 
 1576: =pod
 1577: 
 1578: =head2 GetServerPort
 1579: 
 1580: Returns the lond port number.
 1581: 
 1582: =cut
 1583: 
 1584: sub GetServerPort {
 1585:     return $perlvar{londPort};
 1586: }
 1587: 
 1588: =pod
 1589: 
 1590: =head2 SetupLoncListener
 1591: 
 1592: Setup a lonc listener event.  The event is called when the socket
 1593: becomes readable.. that corresponds to the receipt of a new
 1594: connection.  The event handler established will accept the connection
 1595: (creating a communications channel), that in turn will establish
 1596: another event handler to subess requests.
 1597: 
 1598: =head2  Parameters:
 1599: 
 1600:    host (optional)   Name of the host to set up a unix socket to.
 1601: 
 1602: =cut
 1603: 
 1604: sub SetupLoncListener {
 1605:     my ($host,$SocketName) = @_;
 1606:     if (!$host) { $host = &GetServerHost(); }
 1607:     if (!$SocketName) { $SocketName = &GetLoncSocketPath($host); }
 1608: 
 1609: 
 1610:     unlink($SocketName);
 1611: 
 1612:     my $socket;
 1613:     unless ($socket =IO::Socket::UNIX->new(Local  => $SocketName,
 1614: 					    Listen => 250, 
 1615: 					    Type   => SOCK_STREAM)) {
 1616: 	if($I_am_child) {
 1617: 	    &child_exit(-1, "Failed to create a lonc listener socket");
 1618: 	} else {
 1619: 	    die "Failed to create a lonc listner socket";
 1620: 	}
 1621:     }
 1622:     return $socket;
 1623: }
 1624: 
 1625: #
 1626: #   Toggle transaction logging.
 1627: #  Implicit inputs:  
 1628: #     LogTransactions
 1629: #  Implicit Outputs:
 1630: #     LogTransactions
 1631: sub ToggleTransactionLogging {
 1632:     print STDERR "Toggle transaction logging...\n";
 1633:     if(!$LogTransactions) {
 1634: 	$LogTransactions = 1;
 1635:     } else {
 1636: 	$LogTransactions = 0;
 1637:     }
 1638: 
 1639: 
 1640:     Log("SUCCESS", "Toggled transaction logging: $LogTransactions \n");
 1641: }
 1642: 
 1643: =pod 
 1644: 
 1645: =head2 ChildStatus
 1646:  
 1647: Child USR1 signal handler to report the most recent status
 1648: into the status file.
 1649: 
 1650: We also use this to reset the retries count in order to allow the
 1651: client to retry connections with a previously dead server.
 1652: 
 1653: =cut
 1654: 
 1655: sub ChildStatus {
 1656:     my $event = shift;
 1657:     my $watcher = $event->w;
 1658: 
 1659:     Debug(2, "Reporting child status because : ".$watcher->data);
 1660:     my $docdir = $perlvar{'lonDocRoot'};
 1661:     
 1662:     open(LOG,">>$docdir/lon-status/loncstatus.txt");
 1663:     flock(LOG,LOCK_EX);
 1664:     print LOG $$."\t".$RemoteHost."\t".$Status."\t".
 1665: 	$RecentLogEntry."\n";
 1666:     #
 1667:     #  Write out information about each of the connections:
 1668:     #
 1669:     if ($DebugLevel > 2) {
 1670: 	print LOG "Active connection statuses: \n";
 1671: 	my $i = 1;
 1672: 	print STDERR  "================================= Socket Status Dump:\n";
 1673: 	foreach my $item (keys %ActiveConnections) {
 1674: 	    my $Socket = $ActiveConnections{$item}->data;
 1675: 	    my $state  = $Socket->GetState();
 1676: 	    print LOG "Connection $i State: $state\n";
 1677: 	    print STDERR "---------------------- Connection $i \n";
 1678: 	    $Socket->Dump(-1);	# Ensure it gets dumped..
 1679: 	    $i++;	
 1680: 	}
 1681:     }
 1682:     flock(LOG,LOCK_UN);
 1683:     close(LOG);
 1684:     $ConnectionRetriesLeft = $ConnectionRetries;
 1685:     UpdateStatus();
 1686: }
 1687: 
 1688: =pod
 1689: 
 1690: =head2 SignalledToDeath
 1691: 
 1692: Called in response to a signal that causes a chid process to die.
 1693: 
 1694: =cut
 1695: 
 1696: 
 1697: sub SignalledToDeath {
 1698:     my $event  = shift;
 1699:     my $watcher= $event->w;
 1700: 
 1701:     Debug(2,"Signalled to death! via ".$watcher->data);
 1702:     my ($signal) = $watcher->data;
 1703:     chomp($signal);
 1704:     Log("CRITICAL", "Abnormal exit.  Child $$ for $RemoteHost "
 1705: 	."died through "."\"$signal\"");
 1706:     #LogPerm("F:lonc: $$ on $RemoteHost signalled to death: "
 1707: #	    ."\"$signal\"");
 1708:     &clear_childpid($$);
 1709:     exit 0;
 1710: 
 1711: }
 1712: 
 1713: =pod
 1714: 
 1715: =head2 ToggleDebug
 1716: 
 1717: This sub toggles trace debugging on and off.
 1718: 
 1719: =cut
 1720: 
 1721: sub ToggleDebug {
 1722:     my $Current    = $DebugLevel;
 1723:        $DebugLevel = $NextDebugLevel;
 1724:        $NextDebugLevel = $Current;
 1725: 
 1726:     Log("SUCCESS", "New debugging level for $RemoteHost now $DebugLevel");
 1727: 
 1728: }
 1729: 
 1730: =pod
 1731: 
 1732: =head2 ChildProcess
 1733: 
 1734: This sub implements a child process for a single lonc daemon.
 1735: Optional parameter:
 1736:    $socket  - if provided, this is a socket already open for listening
 1737:               on the client socket. Otherwise, a new listener is set up.
 1738: 
 1739: =cut
 1740: 
 1741: sub ChildProcess {
 1742:     #  We've inherited all the
 1743:     #  events of our parent and those have to be cancelled or else
 1744:     #  all holy bloody chaos will result.. trust me, I already made
 1745:     #  >that< mistake.
 1746: 
 1747:     my $host = GetServerHost();
 1748:     foreach my $listener (keys %parent_dispatchers) {
 1749: 	my $watcher = $parent_dispatchers{$listener};
 1750: 	my $s       = $watcher->fd;
 1751: 	if ($listener ne $host) { # Close everyone but me.
 1752: 	    Debug(5, "Closing listen socket for $listener");
 1753: 	    $s->close();
 1754: 	}
 1755: 	Debug(5, "Killing watcher for $listener");
 1756: 
 1757: 	$watcher->cancel();
 1758: 	delete($parent_dispatchers{$listener});
 1759: 
 1760:     }
 1761: 
 1762:     #  kill off the parent's signal handlers too!  
 1763:     #
 1764: 
 1765:     for my $handler (keys %parent_handlers) {
 1766: 	my $watcher = $parent_handlers{$handler};
 1767: 	$watcher->cancel();
 1768: 	delete($parent_handlers{$handler});
 1769:     }
 1770: 
 1771:     $I_am_child    = 1;		# Seems like in spite of it all I may still getting
 1772:                                 # parent event dispatches.. flag I'm a child.
 1773: 
 1774: 
 1775:     #
 1776:     #  Signals must be handled by the Event framework...
 1777:     #
 1778: 
 1779:     Event->signal(signal   => "QUIT",
 1780: 		  cb       => \&SignalledToDeath,
 1781: 		  data     => "QUIT");
 1782:     Event->signal(signal   => "HUP",
 1783: 		  cb       => \&ChildStatus,
 1784: 		  data     => "HUP");
 1785:     Event->signal(signal   => "USR1",
 1786: 		  cb       => \&ChildStatus,
 1787: 		  data     => "USR1");
 1788:     Event->signal(signal   => "USR2",
 1789: 		  cb       => \&ToggleTransactionLogging);
 1790:     Event->signal(signal   => "INT",
 1791: 		  cb       => \&ToggleDebug,
 1792: 		  data     => "INT");
 1793: 
 1794:     # Block the pipe signal we'll get when the socket disconnects.  We detect 
 1795:     # socket disconnection via send/receive failures. On disconnect, the
 1796:     # socket becomes readable .. which will force the disconnect detection.
 1797: 
 1798:     my $set = POSIX::SigSet->new(SIGPIPE);
 1799:     sigprocmask(SIG_BLOCK, $set);
 1800: 
 1801:     #  Figure out if we got passed a socket or need to open one to listen for
 1802:     #  client requests.
 1803: 
 1804:     my ($socket) = @_;
 1805:     if (!$socket) {
 1806: 
 1807: 	$socket =  SetupLoncListener();
 1808:     }
 1809:     #  Establish an event to listen for client connection requests.
 1810: 
 1811: 
 1812:     Event->io(cb   => \&NewClient,
 1813: 	      poll => 'r',
 1814: 	      desc => 'Lonc Listener Unix Socket',
 1815: 	      fd   => $socket);
 1816:     
 1817:     $Event::DebugLevel = $DebugLevel;
 1818:     
 1819:     Debug(9, "Making initial lond connection for ".$RemoteHost);
 1820: 
 1821: # Setup the initial server connection:
 1822:     
 1823:      # &MakeLondConnection(); // let first work request do it.
 1824: 
 1825:     #  need to accept the connection since the event may  not fire.
 1826: 
 1827:     &accept_client($socket);
 1828: 
 1829:     Debug(9,"Entering event loop");
 1830:     my $ret = Event::loop();		#  Start the main event loop.
 1831:     
 1832:     
 1833:     &child_exit (-1,"Main event loop exited!!!");
 1834: }
 1835: 
 1836: #  Create a new child for host passed in:
 1837: 
 1838: sub CreateChild {
 1839:     my ($host, $hostid, $defhostid) = @_;
 1840: 
 1841:     my $sigset = POSIX::SigSet->new(SIGINT);
 1842:     sigprocmask(SIG_BLOCK, $sigset);
 1843:     $RemoteHost = $host;
 1844:     ShowStatus('Parent keeping the flock'); # Update time in status message.
 1845:     Log("CRITICAL", "Forking server for ".$host);
 1846:     my $pid          = fork;
 1847:     if($pid) {			# Parent
 1848: 	$RemoteHost = "Parent";
 1849: 	$ChildPid{$pid} = $host;
 1850: 	sigprocmask(SIG_UNBLOCK, $sigset);
 1851: 	undef(@all_host_ids);
 1852:     } else {			# child.
 1853: 	$RemoteHostId = $hostid;
 1854: 	$RemoteDefHostId = $defhostid;
 1855: 	ShowStatus("Connected to ".$RemoteHost);
 1856: 	$SIG{INT} = 'DEFAULT';
 1857: 	sigprocmask(SIG_UNBLOCK, $sigset);
 1858: 	&ChildProcess();		# Does not return.
 1859:     }
 1860: }
 1861: 
 1862: # parent_client_connection:
 1863: #    Event handler that processes client connections for the parent process.
 1864: #    This sub is called when the parent is listening on a socket and
 1865: #    a connection request arrives.  We must:
 1866: #     Start a child process to accept the connection request.
 1867: #     Kill our listen on the socket.
 1868: # Parameter:
 1869: #    event       - The event object that was created to monitor this socket.
 1870: #                  event->w->fd is the socket.
 1871: # Returns:
 1872: #    NONE
 1873: #
 1874: sub parent_client_connection {
 1875:     if ($I_am_child) {
 1876: 	#  Should not get here, but seem to anyway:
 1877: 	&Debug(5," Child caught parent client connection event!!");
 1878: 	my ($event) = @_;
 1879: 	my $watcher = $event->w;
 1880: 	$watcher->cancel();	# Try to kill it off again!!
 1881:     } else {
 1882: 	&Debug(9, "parent_client_connection");
 1883: 	my ($event)   = @_;
 1884: 	my $watcher   = $event->w;
 1885: 	my $socket    = $watcher->fd;
 1886: 	my $connection = $socket->accept();	# Accept the client connection.
 1887: 	Event->io(cb      => \&get_remote_hostname,
 1888: 		  poll    => 'r',
 1889: 		  data    => "",
 1890: 		  fd      => $connection);
 1891:     }
 1892: }
 1893: 
 1894: sub get_remote_hostname {
 1895:     my ($event)   = @_;
 1896:     my $watcher   = $event->w;
 1897:     my $socket    = $watcher->fd;
 1898: 
 1899:     my $thisread;
 1900:     my $rv = $socket->recv($thisread, POSIX::BUFSIZ, 0);
 1901:     Debug(8, "rcv:  data length = ".length($thisread)." read =".$thisread);
 1902:     if (!defined($rv) || length($thisread) == 0) {
 1903: 	# Likely eof on socket.
 1904: 	Debug(5,"Client Socket closed on lonc for p_c_c");
 1905: 	close($socket);
 1906: 	$watcher->cancel();
 1907: 	return;
 1908:     }
 1909: 
 1910:     my $data    = $watcher->data().$thisread;
 1911:     $watcher->data($data);
 1912:     if($data =~ /\n$/) {	# Request entirely read.
 1913: 	chomp($data);
 1914:     } else {
 1915: 	return;
 1916:     }
 1917: 
 1918:     &Debug(5,"Creating child for $data (parent_client_connection)");
 1919:     (my $hostname,my $lonid,@all_host_ids) = split(':',$data);
 1920:     $ChildHost{$hostname}++;
 1921:     if ($ChildHost{$hostname} == 1) {
 1922: 	&CreateChild($hostname,$lonid,$all_host_ids[-1]);
 1923:     } else {
 1924: 	&Log('WARNING',"Request for a second child on $hostname");
 1925:     }
 1926:     # Clean up the listen since now the child takes over until it exits.
 1927:     $watcher->cancel();		# Nolonger listening to this event
 1928:     $socket->send("done\n");
 1929:     $socket->close();
 1930: }
 1931: 
 1932: # parent_listen:
 1933: #    Opens a socket and starts a listen for the parent process on a client UNIX
 1934: #    domain socket.
 1935: #
 1936: #    This involves:
 1937: #       Creating a socket for listen.
 1938: #       Removing any socket lock file
 1939: #       Adding an event handler for this socket becoming readable
 1940: #         To the parent's event dispatcher.
 1941: # Parameters:
 1942: #    loncapa_host    - LonCAPA cluster name of the host represented by the client
 1943: #                      socket.
 1944: # Returns:
 1945: #    NONE
 1946: #
 1947: sub parent_listen {
 1948:     my ($loncapa_host) = @_;
 1949:     Debug(5, "parent_listen: $loncapa_host");
 1950: 
 1951:     my ($socket,$file);
 1952:     if (!$loncapa_host) {
 1953: 	$loncapa_host = 'common_parent';
 1954: 	$file         = $perlvar{'lonSockCreate'};
 1955:     } else {
 1956: 	$file         = &GetLoncSocketPath($loncapa_host);
 1957:     }
 1958:     $socket = &SetupLoncListener($loncapa_host,$file);
 1959: 
 1960:     $listening_to{$socket} = $loncapa_host;
 1961:     if (!$socket) {
 1962: 	die "Unable to create a listen socket for $loncapa_host";
 1963:     }
 1964:     
 1965:     my $lock_file = $file.".lock";
 1966:     unlink($lock_file);		# No problem if it doesn't exist yet [startup e.g.]
 1967: 
 1968:     my $watcher = 
 1969: 	Event->io(cb    => \&parent_client_connection,
 1970: 		  poll  => 'r',
 1971: 		  desc  => "Parent listener unix socket ($loncapa_host)",
 1972: 		  data => "",
 1973: 		  fd    => $socket);
 1974:     $parent_dispatchers{$loncapa_host} = $watcher;
 1975: 
 1976: }
 1977: 
 1978: sub parent_clean_up {
 1979:     my ($loncapa_host) = @_;
 1980:     Debug(1, "parent_clean_up: $loncapa_host");
 1981: 
 1982:     my $socket_file = &GetLoncSocketPath($loncapa_host);
 1983:     unlink($socket_file);	# No problem if it doesn't exist yet [startup e.g.]
 1984:     my $lock_file   = $socket_file.".lock";
 1985:     unlink($lock_file);		# No problem if it doesn't exist yet [startup e.g.]
 1986: }
 1987: 
 1988: 
 1989: 
 1990: #    This sub initiates a listen on the common unix domain lonc client socket.
 1991: #    loncnew starts up with no children, and only spawns off children when a
 1992: #    connection request occurs on the common client unix socket.  The spawned
 1993: #    child continues to run until it has been idle a while at which point it
 1994: #    eventually exits and once more the parent picks up the listen.
 1995: #
 1996: #  Parameters:
 1997: #      NONE
 1998: #  Implicit Inputs:
 1999: #    The configuration file that has been read in by LondConnection.
 2000: #  Returns:
 2001: #     NONE
 2002: #
 2003: sub listen_on_common_socket {
 2004:     Debug(5, "listen_on_common_socket");
 2005:     &parent_listen();
 2006: }
 2007: 
 2008: #   server_died is called whenever a child process exits.
 2009: #   Since this is dispatched via a signal, we must process all
 2010: #   dead children until there are no more left.  The action
 2011: #   is to:
 2012: #      - Remove the child from the bookeeping hashes
 2013: #      - Re-establish a listen on the unix domain socket associated
 2014: #        with that host.
 2015: # Parameters:
 2016: #    The event, but we don't actually care about it.
 2017: sub server_died {
 2018:     &Debug(9, "server_died called...");
 2019:     
 2020:     while(1) {			# Loop until waitpid nowait fails.
 2021: 	my $pid = waitpid(-1, WNOHANG);
 2022: 	if($pid <= 0) {
 2023: 	    return;		# Nothing left to wait for.
 2024: 	}
 2025: 	# need the host to restart:
 2026: 
 2027: 	my $host = $ChildPid{$pid};
 2028: 	if($host) {		# It's for real...
 2029: 	    &Debug(9, "Caught sigchild for $host");
 2030:             &clear_childpid($pid);
 2031: 	    delete($ChildPid{$pid});
 2032: 	    delete($ChildHost{$host});
 2033: 	    &parent_clean_up($host);
 2034: 
 2035: 	} else {
 2036: 	    &Debug(5, "Caught sigchild for pid not in hosts hash: $pid");
 2037: 	}
 2038:     }
 2039: 
 2040: }
 2041: 
 2042: #
 2043: #  Parent process logic pass 1:
 2044: #   For each entry in the hosts table, we will
 2045: #  fork off an instance of ChildProcess to service the transactions
 2046: #  to that host.  Each pid will be entered in a global hash
 2047: #  with the value of the key, the host.
 2048: #  The parent will then enter a loop to wait for process exits.
 2049: #  Each exit gets logged and the child gets restarted.
 2050: #
 2051: 
 2052: #
 2053: #   Fork and start in new session so hang-up isn't going to 
 2054: #   happen without intent.
 2055: #
 2056: 
 2057: 
 2058: 
 2059: 
 2060: 
 2061: 
 2062: ShowStatus("Forming new session");
 2063: my $childpid = fork;
 2064: if ($childpid != 0) {
 2065:     sleep 4;			# Give child a chacne to break to
 2066:     exit 0;			# a new sesion.
 2067: }
 2068: #
 2069: #   Write my pid into the pid file so I can be located
 2070: #
 2071: 
 2072: ShowStatus("Parent writing pid file:");
 2073: my $execdir = $perlvar{'lonDaemons'};
 2074: open (PIDSAVE, ">$execdir/logs/lonc.pid");
 2075: print PIDSAVE "$$\n";
 2076: close(PIDSAVE);
 2077: 
 2078: 
 2079: 
 2080: if (POSIX::setsid() < 0) {
 2081:     print "Could not create new session\n";
 2082:     exit -1;
 2083: }
 2084: 
 2085: ShowStatus("Forking node servers");
 2086: 
 2087: Log("CRITICAL", "--------------- Starting children ---------------");
 2088: 
 2089: LondConnection::ReadConfig;               # Read standard config files.
 2090: 
 2091: $RemoteHost = "[parent]";
 2092: &listen_on_common_socket();
 2093: 
 2094: $RemoteHost = "Parent Server";
 2095: 
 2096: # Maintain the population:
 2097: 
 2098: ShowStatus("Parent keeping the flock");
 2099: 
 2100: 
 2101: # We need to setup a SIGChild event to handle the exit (natural or otherwise)
 2102: # of the children.
 2103: 
 2104: Event->signal(cb       => \&server_died,
 2105: 	      desc     => "Child exit handler",
 2106: 	      signal   => "CHLD");
 2107: 
 2108: 
 2109: # Set up all the other signals we set up.
 2110: 
 2111: $parent_handlers{INT} = Event->signal(cb       => \&Terminate,
 2112: 				      desc     => "Parent INT handler",
 2113: 				      signal   => "INT");
 2114: $parent_handlers{TERM} = Event->signal(cb       => \&Terminate,
 2115: 				       desc     => "Parent TERM handler",
 2116: 				       signal   => "TERM");
 2117: $parent_handlers{HUP}  = Event->signal(cb       => \&KillThemAll,
 2118: 				       desc     => "Parent HUP handler.",
 2119: 				       signal   => "HUP");
 2120: $parent_handlers{USR1} = Event->signal(cb       => \&CheckKids,
 2121: 				       desc     => "Parent USR1 handler",
 2122: 				       signal   => "USR1");
 2123: $parent_handlers{USR2} = Event->signal(cb       => \&UpdateKids,
 2124: 				       desc     => "Parent USR2 handler.",
 2125: 				       signal   => "USR2");
 2126: 
 2127: #  Start procdesing events.
 2128: 
 2129: $Event::DebugLevel = $DebugLevel;
 2130: Debug(9, "Parent entering event loop");
 2131: my $ret = Event::loop();
 2132: die "Main Event loop exited: $ret";
 2133: 
 2134: =pod
 2135: 
 2136: =head1 CheckKids
 2137: 
 2138:   Since kids do not die as easily in this implementation
 2139: as the previous one, there is no need to restart the
 2140: dead ones (all dead kids get restarted when they die!!)
 2141: The only thing this function does is to pass USR1 to the
 2142: kids so that they report their status.
 2143: 
 2144: =cut
 2145: 
 2146: sub CheckKids {
 2147:     Debug(2, "Checking status of children");
 2148:     my $docdir = $perlvar{'lonDocRoot'};
 2149:     my $fh = IO::File->new(">$docdir/lon-status/loncstatus.txt");
 2150:     my $now=time;
 2151:     my $local=localtime($now);
 2152:     print $fh "LONC status $local - parent $$ \n\n";
 2153:     foreach my $host (keys %parent_dispatchers) {
 2154: 	print $fh "LONC Parent process listening for $host\n";
 2155:     }
 2156:     foreach my $pid (keys %ChildPid) {
 2157: 	Debug(2, "Sending USR1 -> $pid");
 2158: 	kill 'USR1' => $pid;	# Tell Child to report status.
 2159:     }
 2160: 
 2161: }
 2162: 
 2163: =pod
 2164: 
 2165: =head1  UpdateKids
 2166: 
 2167: parent's SIGUSR2 handler.  This handler:
 2168: 
 2169: =item
 2170: 
 2171: Rereads the hosts file.
 2172: 
 2173: =item
 2174:  
 2175: Kills off (via sigint) children for hosts that have disappeared.
 2176: 
 2177: =item
 2178: 
 2179: QUITs  children for hosts that already exist (this just forces a status display
 2180: and resets the connection retry count for that host.
 2181: 
 2182: =item
 2183: 
 2184: Starts new children for hosts that have been added to the hosts.tab file since
 2185: the start of the master program and maintains them.
 2186: 
 2187: =cut
 2188: 
 2189: sub UpdateKids {
 2190: 
 2191:     Log("INFO", "Updating connections via SIGUSR2");
 2192: 
 2193:     #  I'm not sure what I was thinking in the first implementation.
 2194:     # someone will have to work hard to convince me the effect is any
 2195:     # different than Restart, especially now that we don't start up 
 2196:     # per host servers automatically, may as well just restart.
 2197:     # The down side is transactions that are in flight will get timed out
 2198:     # (lost unless they are critical).
 2199: 
 2200:     &KillThemAll();
 2201:     LondConnection->ResetReadConfig();
 2202:     ShowStatus('Parent keeping the flock');
 2203: }
 2204: 
 2205: 
 2206: =pod
 2207: 
 2208: =head1 Restart
 2209: 
 2210: Signal handler for HUP... all children are killed and
 2211: we self restart.  This is an el-cheapo way to re-read
 2212: the config file.
 2213: 
 2214: =cut
 2215: 
 2216: sub Restart {
 2217:     &KillThemAll;		# First kill all the children.
 2218:     LondConnection->ResetReadConfig();
 2219:     Log("CRITICAL", "Restarting");
 2220:     my $execdir = $perlvar{'lonDaemons'};
 2221:     unlink("$execdir/logs/lonc.pid");
 2222:     exec("$executable");
 2223: }
 2224: 
 2225: =pod
 2226: 
 2227: =head1 KillThemAll
 2228: 
 2229: Signal handler that kills all children by sending them a 
 2230: SIGHUP.  Responds to sigint and sigterm.
 2231: 
 2232: =cut
 2233: 
 2234: sub KillThemAll {
 2235:     Debug(2, "Kill them all!!");
 2236:     
 2237:     #local($SIG{CHLD}) = 'IGNORE';
 2238:     # Our children >will< die.
 2239:     # but we need to catch their death and cleanup after them in case this is 
 2240:     # a restart set of kills
 2241:     my @allpids = keys(%ChildPid);
 2242:     foreach my $pid (@allpids) {
 2243: 	my $serving = $ChildPid{$pid};
 2244: 	ShowStatus("Nicely Killing lonc for $serving pid = $pid");
 2245: 	Log("CRITICAL", "Nicely Killing lonc for $serving pid = $pid");
 2246: 	kill 'QUIT' => $pid;
 2247:         &clear_childpid($pid);
 2248:     }
 2249:     ShowStatus("Finished killing child processes off.");
 2250: }
 2251: 
 2252: 
 2253: #
 2254: #  Kill all children via KILL.  Just in case the
 2255: #  first shot didn't get them.
 2256: 
 2257: sub really_kill_them_all_dammit
 2258: {
 2259:     Debug(2, "Kill them all Dammit");
 2260:     local($SIG{CHLD} = 'IGNORE'); # In case some purist reenabled them.
 2261:     foreach my $pid (keys %ChildPid) {
 2262: 	my $serving = $ChildPid{$pid};
 2263: 	&ShowStatus("Nastily killing lonc for $serving pid = $pid");
 2264: 	Log("CRITICAL", "Nastily killing lonc for $serving pid = $pid");
 2265: 	kill 'KILL' => $pid;
 2266: 	delete($ChildPid{$pid});
 2267:         delete($ChildKeyMode{$pid});
 2268: 	my $execdir = $perlvar{'lonDaemons'};
 2269: 	unlink("$execdir/logs/lonc.pid");
 2270:     }
 2271: }
 2272: 
 2273: =pod
 2274: 
 2275: =head1 Terminate
 2276:  
 2277: Terminate the system.
 2278: 
 2279: =cut
 2280: 
 2281: sub Terminate {
 2282:     &Log("CRITICAL", "Asked to kill children.. first be nice...");
 2283:     &KillThemAll;
 2284:     #
 2285:     #  By now they really should all be dead.. but just in case 
 2286:     #  send them all SIGKILL's after a bit of waiting:
 2287: 
 2288:     sleep(4);
 2289:     &Log("CRITICAL", "Now kill children nasty");
 2290:     &really_kill_them_all_dammit;
 2291:     Log("CRITICAL","Master process exiting");
 2292:     exit 0;
 2293: 
 2294: }
 2295: 
 2296: =pod
 2297: 
 2298: =cut
 2299: 
 2300: sub my_hostname {
 2301:     use Sys::Hostname::FQDN();
 2302:     my $name = Sys::Hostname::FQDN::fqdn();
 2303:     &Debug(9,"Name is $name");
 2304:     return $name;
 2305: }
 2306: 
 2307: sub record_childpid {
 2308:     my ($Socket) = @_;
 2309:     my $docdir = $perlvar{'lonDocRoot'};
 2310:     my $authmode = $Socket->GetKeyMode();
 2311:     my $peer = $Socket->PeerLoncapaHim();
 2312:     if (($authmode eq 'ssl') || ($authmode eq 'insecure')) {
 2313:         my $childpid = $$;
 2314:         if ($childpid) {
 2315:             unless (exists($ChildKeyMode{$childpid})) {
 2316:                 $ChildKeyMode{$childpid} = $authmode;
 2317:             }
 2318:             if (-d "$docdir/lon-status/loncchld") {
 2319:                 unless (-e "$docdir/lon-status/loncchld/$childpid") {
 2320:                     if (open (my $pidfh,'>',"$docdir/lon-status/loncchld/$childpid")) {
 2321:                         print $pidfh "$peer:$authmode\n";
 2322:                         close($pidfh);
 2323:                     }
 2324:                 }
 2325:             }
 2326:         }
 2327:     }
 2328:     return;
 2329: }
 2330: 
 2331: sub clear_childpid {
 2332:     my ($childpid) = @_; 
 2333:     my $docdir = $perlvar{'lonDocRoot'};
 2334:     if (-d "$docdir/lon-status/loncchld") {
 2335:         if ($childpid =~ /^\d+$/) {
 2336:             if (($ChildKeyMode{$childpid} eq 'insecure') ||
 2337:                 ($ChildKeyMode{$childpid} eq 'ssl')) {
 2338:                 if (-e "$docdir/lon-status/loncchld/$childpid") {
 2339:                     unlink("$docdir/lon-status/loncchld/$childpid");
 2340:                 }
 2341:             }
 2342:         }
 2343:     }
 2344:     if (exists($ChildKeyMode{$childpid})) {
 2345:         delete($ChildKeyMode{$childpid});
 2346:     }
 2347:     return;
 2348: }
 2349: 
 2350: =pod
 2351: 
 2352: =head1 Theory
 2353: 
 2354: The event class is used to build this as a single process with an
 2355: event driven model.  The following events are handled:
 2356: 
 2357: =item UNIX Socket connection Received
 2358: 
 2359: =item Request data arrives on UNIX data transfer socket.
 2360: 
 2361: =item lond connection becomes writable.
 2362: 
 2363: =item timer fires at 1 second intervals.
 2364: 
 2365: All sockets are run in non-blocking mode.  Timeouts managed by the timer
 2366: handler prevents hung connections.
 2367: 
 2368: Key data structures:
 2369: 
 2370: =item RequestQueue
 2371: 
 2372: A queue of requests received from UNIX sockets that are
 2373: waiting for a chance to be forwarded on a lond connection socket.
 2374: 
 2375: =item ActiveConnections
 2376: 
 2377: A hash of lond connections that have transactions in process that are
 2378: available to be timed out.
 2379: 
 2380: =item ActiveTransactions
 2381: 
 2382: A hash indexed by lond connections that contain the client reply
 2383: socket for each connection that has an active transaction on it.
 2384: 
 2385: =item IdleConnections
 2386: 
 2387: A hash of lond connections that have no work to do.  These connections
 2388: can be closed if they are idle for a long enough time.
 2389: 
 2390: =cut
 2391: 
 2392: =pod
 2393: 
 2394: =head1 Log messages
 2395: 
 2396: The following is a list of log messages that can appear in the 
 2397: lonc.log file.  Each log file has a severity and a message.
 2398: 
 2399: =over 2
 2400: 
 2401: =item Warning  A socket timeout was detected
 2402: 
 2403: If there are pending transactions in the socket's queue,
 2404: they are failed (saved if critical).  If the connection
 2405: retry count gets exceeded by this, the
 2406: remote host is marked as dead.
 2407: Called when timeouts occurred during the connection and
 2408: connection dialog with a remote host.
 2409: 
 2410: =item Critical Host makred DEAD <hostname>   
 2411: 
 2412: The numer of retry counts for contacting a host was
 2413: exceeded. The host is marked dead an no 
 2414: further attempts will be made by that child.
 2415: 
 2416: =item Info lonc pipe client hung up on us     
 2417: 
 2418: Write to the client pipe indicated no data transferred
 2419: Socket to remote host is shut down.  Reply to the client 
 2420: is discarded.  Note: This is commented out in &ClientWriteable
 2421: 
 2422: =item Success  Reply from lond: <data>   
 2423: 
 2424: Can be enabled for debugging by setting LogTransactions to nonzero.
 2425: Indicates a successful transaction with lond, <data> is the data received
 2426: from the remote lond.
 2427: 
 2428: =item Success A delayed transaction was completed  
 2429: 
 2430: A transaction that must be reliable was executed and completed
 2431: as lonc restarted.  This is followed by a mesage of the form
 2432: 
 2433:   S: client-name : request
 2434: 
 2435: =item WARNING  Failing transaction <cmd>:<subcmd>  
 2436: 
 2437: Transaction failed on a socket, but the failure retry count for the remote
 2438: node has not yet been exhausted (the node is not yet marked dead).
 2439: cmd is the command, subcmd is the subcommand.  This results from a con_lost
 2440: when communicating with lond.
 2441: 
 2442: =item WARNING Shutting down a socket     
 2443: 
 2444: Called when a socket is being closed to lond.  This is emitted both when 
 2445: idle pruning is being done and when the socket has been disconnected by the remote.
 2446: 
 2447: =item WARNING Lond connection lost.
 2448: 
 2449: Called when a read from lond's socket failed indicating lond has closed the 
 2450: connection or died.  This should be followed by one or more
 2451: 
 2452:  "WARNING Failing transaction..." msgs for each in-flight or queued transaction.
 2453: 
 2454: =item WARNING No SSL channel (verification failed), will try with insecure channel.
 2455: 
 2456: Called when promotion of a socket to SSL failed because SSL certificate verification failed.
 2457: Domain configuration must also permit insecure channel use for key exchange. Connection
 2458: negotiation will start again from the beginning, but with Authentication Mode not set to ssl.
 2459: 
 2460: =item INFO Connected to lond version:  <version> 
 2461: 
 2462: When connection negotiation is complete, the lond version is requested and logged here.
 2463: 
 2464: =item SUCCESS Connection n to host now ready for action
 2465: 
 2466: Emitted when connection has been completed with lond. n is then number of 
 2467: concurrent connections and host, the host to which the connection has just
 2468: been established.
 2469: 
 2470: =item WARNING Connection to host has been disconnected
 2471: 
 2472: Write to a lond resulted in failure status.  Connection to lond is dropped.
 2473: 
 2474: =item SUCCESS Created connection n to host host 
 2475: 
 2476: Initial connection request to host..(before negotiation).
 2477: 
 2478: =item CRITICAL Request Close Connection ... exiting
 2479: 
 2480: Client has sent "close_connection_exit"   The loncnew server is exiting.
 2481: 
 2482: =item INFO Resetting Connection Retries 
 2483: 
 2484: Client has sent "reset_retries" The lond connection retries are reset to zero for the
 2485: corresponding lond.
 2486: 
 2487: =item SUCCESS Transaction <data>
 2488: 
 2489: Only emitted if the global variable $LogTransactions was set to true.
 2490: A client has requested a lond transaction <data> is the contents of the request.
 2491: 
 2492: =item SUCCESS Toggled transaction logging <LogTransactions>
 2493:                                     
 2494: The state of the $LogTransactions global has been toggled, and its current value
 2495: (after being toggled) is displayed.  When non zero additional logging of transactions
 2496: is enabled for debugging purposes.  Transaction logging is toggled on receipt of a USR2
 2497: signal.
 2498: 
 2499: =item CRITICAL Abnormal exit. Child <pid> for <host> died thorugh signal.
 2500: 
 2501: QUIT signal received.  lonc child process is exiting.
 2502: 
 2503: =item SUCCESS New debugging level for <RemoteHost> now <DebugLevel>
 2504:                                     
 2505: Debugging toggled for the host loncnew is talking with.
 2506: Currently debugging is a level based scheme with higher number 
 2507: conveying more information.  The daemon starts out at
 2508: DebugLevel 0 and can toggle back and forth between that and
 2509: DebugLevel 2  These are controlled by
 2510: the global variables $DebugLevel and $NextDebugLevel
 2511: The debug level can go up to 9.
 2512: SIGINT toggles the debug level.  The higher the debug level the 
 2513: more debugging information is spewed.  See the Debug
 2514: sub in loncnew.
 2515: 
 2516: =item CRITICAL Forking server for host  
 2517: 
 2518: A child is being created to service requests for the specified host.
 2519: 
 2520: 
 2521: =item WARNING Request for a second child on hostname
 2522:                                     
 2523: Somehow loncnew was asked to start a second child on a host that already had a child
 2524: servicing it.  This request is not honored, but themessage is emitted.  This could happen
 2525: due to a race condition.  When a client attempts to contact loncnew for a new host, a child
 2526: is forked off to handle the requests for that server.  The parent then backs off the Unix
 2527: domain socket leaving it for the child to service all requests.  If in the time between
 2528: creating the child, and backing off, a new connection request comes in to the unix domain
 2529: socket, this could trigger (unlikely but remotely possible),.
 2530: 
 2531: =item CRITICAL ------ Starting Children ----
 2532: 
 2533: This message should probably be changed to "Entering event loop"  as the loncnew only starts
 2534: children as needed.  This message is emitted as new events are established and
 2535: the event processing loop is entered.
 2536: 
 2537: =item INFO Updating connections via SIGUSR2
 2538:                                     
 2539: SIGUSR2 received. The original code would kill all clients, re-read the host file,
 2540: then restart children for each host.  Now that children are started on demand, this
 2541: just kills all child processes and lets requests start them as needed again.
 2542: 
 2543: 
 2544: =item CRITICAL Restarting
 2545: 
 2546: SigHUP received.  all the children are killed and the script exec's itself to start again.
 2547: 
 2548: =item CRITICAL Nicely killing lonc for host pid = <pid>
 2549: 
 2550: Attempting to kill the child that is serving the specified host (pid given) cleanly via
 2551: SIGQUIT.  The child should handle that, clean up nicely and exit.
 2552: 
 2553: =item CRITICAL Nastily killing lonc for host pid = <pid>
 2554: 
 2555: The child specified did not die when requested via SIGQUIT.  Therefore it is killed
 2556: via SIGKILL.
 2557: 
 2558: =item CRITICAL Asked to kill children.. first be nice..
 2559: 
 2560: In the parent's INT handler.  INT kills the child processes.  This inidicate loncnew
 2561: is about to attempt to kill all known children via SIGQUIT.  This message should be followed 
 2562: by one "Nicely killing" message for each extant child.
 2563: 
 2564: =item CRITICAL Now kill children nasty 
 2565: 
 2566: In the parent's INT handler. remaining children are about to be killed via
 2567: SIGKILL. Should be followed by a Nastily killing... for each lonc child that 
 2568: refused to die.
 2569: 
 2570: =item CRITICAL Master process exiting
 2571: 
 2572: In the parent's INT handler. just prior to the exit 0 call.
 2573: 
 2574: =back
 2575: 
 2576: =cut

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