File:  [LON-CAPA] / loncom / Attic / lonc
Revision 1.39: download - view: text, annotated - select for diffs
Wed Apr 10 04:35:31 2002 UTC (22 years, 1 month ago) by foxr
Branches: MAIN
CVS tags: stable_2002_april, HEAD
Fixed USR1 handler to:
1. Reset retry counters to zero rather than clearing counter hash.
2. On all counters which were >= maxchildretries, restart the lonc
   server for the associated host.

    1: #!/usr/bin/perl
    2: 
    3: # The LearningOnline Network
    4: # lonc - LON TCP-Client Domain-Socket-Server
    5: # provides persistent TCP connections to the other servers in the network
    6: # through multiplexed domain sockets
    7: #
    8: # $Id: lonc,v 1.39 2002/04/10 04:35:31 foxr Exp $
    9: #
   10: # Copyright Michigan State University Board of Trustees
   11: #
   12: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   13: #
   14: # LON-CAPA is free software; you can redistribute it and/or modify
   15: # it under the terms of the GNU General Public License as published by
   16: # the Free Software Foundation; either version 2 of the License, or
   17: # (at your option) any later version.
   18: #
   19: # LON-CAPA is distributed in the hope that it will be useful,
   20: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   21: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   22: # GNU General Public License for more details.
   23: #
   24: # You should have received a copy of the GNU General Public License
   25: # along with LON-CAPA; if not, write to the Free Software
   26: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   27: #
   28: # /home/httpd/html/adm/gpl.txt
   29: #
   30: # http://www.lon-capa.org/
   31: #
   32: # PID in subdir logs/lonc.pid
   33: # kill kills
   34: # HUP restarts
   35: # USR1 tries to open connections again
   36: 
   37: # 6/4/99,6/5,6/7,6/8,6/9,6/10,6/11,6/12,7/14,7/19,
   38: # 10/8,10/9,10/15,11/18,12/22,
   39: # 2/8,7/25 Gerd Kortemeyer
   40: # 12/05 Scott Harrison
   41: # 12/05 Gerd Kortemeyer
   42: # YEAR=2001
   43: # 01/10/01 Scott Harrison
   44: # 03/14/01,03/15,06/12,11/26,11/27,11/28 Gerd Kortemeyer
   45: # 12/20 Scott Harrison
   46: # YEAR=2002
   47: # 2/19/02,02/22/02,02/25/02 Gerd Kortemeyer
   48: # 3/07/02 Ron Fox 
   49: # based on nonforker from Perl Cookbook
   50: # - server who multiplexes without forking
   51: 
   52: use POSIX;
   53: use IO::Socket;
   54: use IO::Select;
   55: use IO::File;
   56: use Socket;
   57: use Fcntl;
   58: use Tie::RefHash;
   59: use Crypt::IDEA;
   60: #use Net::Ping;
   61: use LWP::UserAgent();
   62: 
   63: $status='';
   64: $lastlog='';
   65: $conserver='SHELL';
   66: $DEBUG = 0;			# Set to 1 for annoyingly complete logs.
   67: 
   68: # -------------------------------- Set signal handlers to record abnormal exits
   69: 
   70: &status("Init exception handlers");
   71: $SIG{QUIT}=\&catchexception;
   72: $SIG{__DIE__}=\&catchexception;
   73: 
   74: # ------------------------------------ Read httpd access.conf and get variables
   75: &status("Read access.conf");
   76: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
   77: 
   78: while ($configline=<CONFIG>) {
   79:     if ($configline =~ /PerlSetVar/) {
   80: 	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
   81:         chomp($varvalue);
   82:         $perlvar{$varname}=$varvalue;
   83:     }
   84: }
   85: close(CONFIG);
   86: 
   87: # ----------------------------- Make sure this process is running from user=www
   88: &status("Check user ID");
   89: my $wwwid=getpwnam('www');
   90: if ($wwwid!=$<) {
   91:    $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
   92:    $subj="LON: $perlvar{'lonHostID'} User ID mismatch";
   93:    system("echo 'User ID mismatch.  lonc must be run as user www.' |\
   94:  mailto $emailto -s '$subj' > /dev/null");
   95:    exit 1;
   96: }
   97: 
   98: # --------------------------------------------- Check if other instance running
   99: 
  100: my $pidfile="$perlvar{'lonDaemons'}/logs/lonc.pid";
  101: 
  102: if (-e $pidfile) {
  103:    my $lfh=IO::File->new("$pidfile");
  104:    my $pide=<$lfh>;
  105:    chomp($pide);
  106:    if (kill 0 => $pide) { die "already running"; }
  107: }
  108: 
  109: # ------------------------------------------------------------- Read hosts file
  110: 
  111: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
  112: 
  113: while ($configline=<CONFIG>) {
  114:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
  115:     chomp($ip);
  116:     if ($ip) {
  117:      $hostip{$id}=$ip;
  118:      $hostname{$id}=$name;
  119:     }
  120: }
  121: 
  122: close(CONFIG);
  123: 
  124: # -------------------------------------------------------- Routines for forking
  125: 
  126: %children               = ();       # keys are current child process IDs,
  127:                                     # values are hosts
  128: %childpid               = ();       # the other way around
  129: 
  130: %childatt               = ();       # number of attempts to start server
  131:                                     # for ID
  132: 
  133: $childmaxattempts=5;
  134: 
  135: # ---------------------------------------------------- Fork once and dissociate
  136: &status("Fork and dissociate");
  137: $fpid=fork;
  138: exit if $fpid;
  139: die "Couldn't fork: $!" unless defined ($fpid);
  140: 
  141: POSIX::setsid() or die "Can't start new session: $!";
  142: 
  143: $conserver='PARENT';
  144: 
  145: # ------------------------------------------------------- Write our PID on disk
  146: &status("Write PID");
  147: $execdir=$perlvar{'lonDaemons'};
  148: open (PIDSAVE,">$execdir/logs/lonc.pid");
  149: print PIDSAVE "$$\n";
  150: close(PIDSAVE);
  151: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  152: 
  153: # ----------------------------- Ignore signals generated during initial startup
  154: $SIG{HUP}=$SIG{USR1}='IGNORE';
  155: # ------------------------------------------------------- Now we are on our own
  156:     
  157: # Fork off our children, one for every server
  158: 
  159: &status("Forking ...");
  160: 
  161: foreach $thisserver (keys %hostip) {
  162:     #if (&online($hostname{$thisserver})) {
  163:        make_new_child($thisserver);
  164:     #}
  165: }
  166: 
  167: &logthis("Done starting initial servers");
  168: # ----------------------------------------------------- Install signal handlers
  169: 
  170: 
  171: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  172: $SIG{HUP}  = \&HUPSMAN;
  173: $SIG{USR1} = \&USRMAN;
  174: 
  175: # And maintain the population.
  176: while (1) {
  177:     my $deadpid = wait;		# Wait for the next child to die.
  178:                                 # See who died and start new one
  179:                                 # or a signal (e.g. USR1 for restart).
  180:                                 # if a signal, the wait will fail
  181:                                 # This is ordinarily detected by
  182:                                 # checking for the existence of the
  183:                                 # pid index inthe children hash since
  184:                                 # the return value from a failed wait is -1
  185:                                 # which is an impossible PID.
  186:     &status("Woke up");
  187:     my $skipping='';
  188: 
  189:     if(exists($children{$deadpid})) {
  190: 
  191: 	$thisserver = $children{$deadpid}; # Look name of dead guy's peer.
  192: 
  193: 	delete($children{$deadpid}); # Get rid of dead hash entry.
  194: 
  195: 	if($childatt{$thisserver} < $childmaxattempts) {
  196: 	    $childatt{$thisserver}++;
  197: 	    &logthis(
  198: 	       "<font color=yellow>INFO: Trying to reconnect for $thisserver "
  199:             ."($childatt{$thisserver} of $childmaxattempts attempts)</font>"); 
  200: 	    make_new_child($thisserver);
  201: 	
  202: 	}
  203: 	else {
  204: 	    $skipping .= $thisserver.' ';
  205: 	}
  206: 	if($skipping) {
  207: 	    &logthis("<font color=blue>WARNING: Skipped $skipping</font>");
  208:   
  209: 	}
  210:     }
  211: 
  212: }
  213: 
  214: 
  215: 
  216: sub make_new_child {
  217:    
  218:     $newserver=shift;
  219:     my $pid;
  220:     my $sigset;
  221:     &logthis("Attempting to start child for server $newserver");
  222:     # block signal for fork
  223:     $sigset = POSIX::SigSet->new(SIGINT);
  224:     sigprocmask(SIG_BLOCK, $sigset)
  225:         or die "Can't block SIGINT for fork: $!\n";
  226:     
  227:     die "fork: $!" unless defined ($pid = fork);
  228:     
  229:     if ($pid) {
  230:         # Parent records the child's birth and returns.
  231:         sigprocmask(SIG_UNBLOCK, $sigset)
  232:             or die "Can't unblock SIGINT for fork: $!\n";
  233:         $children{$pid} = $newserver;
  234:         $childpid{$newserver} = $pid;
  235:         return;
  236:     } else {
  237:         $conserver=$newserver;
  238:         # Child can *not* return from this subroutine.
  239:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  240:         $SIG{USR1}= \&logstatus;
  241:    
  242:         # unblock signals
  243:         sigprocmask(SIG_UNBLOCK, $sigset)
  244:             or die "Can't unblock SIGINT for fork: $!\n";
  245: 
  246: # ----------------------------- This is the modified main program of non-forker
  247: 
  248: $port = "$perlvar{'lonSockDir'}/$conserver";
  249: 
  250: unlink($port);
  251: 
  252: # -------------------------------------------------------------- Open other end
  253: 
  254: &openremote($conserver);
  255: 	&logthis("<font color=green> Connection to $conserver open </font>");
  256: # ----------------------------------------- We're online, send delayed messages
  257:     &status("Checking for delayed messages");
  258: 
  259:     my @allbuffered;
  260:     my $path="$perlvar{'lonSockDir'}/delayed";
  261:     opendir(DIRHANDLE,$path);
  262:     @allbuffered=grep /\.$conserver$/, readdir DIRHANDLE;
  263:     closedir(DIRHANDLE);
  264:     my $dfname;
  265:     foreach (@allbuffered) {
  266:         &status("Sending delayed: $_");
  267:         $dfname="$path/$_";
  268:         if($DEBUG) { &logthis('Sending '.$dfname); }
  269:         my $wcmd;
  270:         {
  271:          my $dfh=IO::File->new($dfname);
  272:          $cmd=<$dfh>;
  273:         }
  274:         chomp($cmd);
  275:         my $bcmd=$cmd;
  276:         if ($cmd =~ /^encrypt\:/) {
  277: 	    my $rcmd=$cmd;
  278:             $rcmd =~ s/^encrypt\://;
  279:             chomp($rcmd);
  280:             my $cmdlength=length($rcmd);
  281:             $rcmd.="         ";
  282:             my $encrequest='';
  283:             for (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
  284:                 $encrequest.=
  285:                     unpack("H16",$cipher->encrypt(substr($rcmd,$encidx,8)));
  286:             }
  287:             $cmd="enc:$cmdlength:$encrequest\n";
  288:         }
  289: 	$answer = londtransaction($remotesock, $cmd, 60);
  290: 	chomp($answer);
  291: 
  292:         if (($answer ne '') && ($@!~/timeout/)) {
  293: 	    unlink("$dfname");
  294:             &logthis("Delayed $cmd: >$answer<");
  295:             &logperm("S:$conserver:$bcmd");
  296:         }        
  297:     }
  298: 	if($DEBUG) { &logthis("<font color=green> Delayed transactions sent"); }
  299: 
  300: # ------------------------------------------------------- Listen to UNIX socket
  301: &status("Opening socket");
  302: unless (
  303:   $server = IO::Socket::UNIX->new(Local  => $port,
  304:                                   Type   => SOCK_STREAM,
  305:                                   Listen => 10 )
  306:    ) { 
  307:        my $st=120+int(rand(240));
  308:        &logthis(
  309:          "<font color=blue>WARNING: ".
  310:          "Can't make server socket ($st secs):  .. exiting</font>");
  311:        sleep($st);
  312:        exit; 
  313:      };
  314:    
  315: # -----------------------------------------------------------------------------
  316: 
  317: &logthis("<font color=green>$conserver online</font>");
  318: 
  319: # -----------------------------------------------------------------------------
  320: # begin with empty buffers
  321: %inbuffer  = ();
  322: %outbuffer = ();
  323: %ready     = ();
  324: %servers   = ();	# To be compatible with make filevector.  indexed by
  325: 			# File ids, values are sockets.
  326: 			# note that the accept socket is omitted.
  327: 
  328: tie %ready, 'Tie::RefHash';
  329: 
  330: # nonblock($server);
  331: # $select = IO::Select->new($server);
  332: 
  333: # Main loop: check reads/accepts, check writes, check ready to process
  334: 
  335: status("Main loop");
  336: while (1) {
  337:     my $client;
  338:     my $rv;
  339:     my $data;
  340: 
  341:     my $infdset;		# bit vec of fd's to select on input.
  342: 
  343:     my $outfdset;		# Bit vec of fd's to select on output.
  344: 
  345: 
  346:     $infdset = MakeFileVector(\%servers);
  347:     $outfdset= MakeFileVector(\%outbuffer);
  348:     vec($infdset, $server->fileno, 1) = 1;
  349:     if($DEBUG) {
  350: 	&logthis("Adding ".$server->fileno.
  351: 		 " to input select vector (listner)".
  352: 		 unpack("b*",$infdset)."\n");
  353:     }
  354:     DoSelect(\$infdset, \$outfdset); # Wait for input.
  355:     if($DEBUG) {
  356: 	&logthis("Doselect completed!");
  357: 	&logthis("ins = ".unpack("b*",$infdset)."\n");
  358: 	&logthis("outs= ".unpack("b*",$outfdset)."\n");
  359: 		 
  360:     }
  361: 
  362:     # Checkfor new connections:
  363:     if (vec($infdset, $server->fileno, 1)) {
  364: 	if($DEBUG) {
  365: 	    &logthis("New connection established");
  366: 	}
  367: 	# accept a new connection
  368: 	&status("Accept new connection: $conserver");
  369: 	$client = $server->accept();
  370: 	if($DEBUG) {
  371: 	    &logthis("New client fd = ".$client->fileno."\n");
  372: 	}
  373: 	$servers{$client->fileno} = $client;
  374: 	nonblock($client);
  375:     }
  376:     HandleInput($infdset, \%servers, \%inbuffer, \%outbuffer, \%ready);
  377:     HandleOutput($outfdset, \%servers, \%outbuffer, \%inbuffer,
  378: 		 \%ready);
  379: # -------------------------------------------------------- Wow, connection lost
  380: 
  381: }
  382:    
  383:     }
  384: }
  385: 
  386: # ------------------------------------------------------- End of make_new_child
  387: 
  388: 
  389: #
  390: #  Make a vector of file descriptors to wait for in a select.
  391: #  parameters:
  392: #     \%fdhash  -reference to a hash which has IO::Socket's as indices.  
  393: #                We only care about the indices, not the values.
  394: #  A select vector is created from all indices of the hash.
  395: 
  396: sub MakeFileVector
  397: {
  398:     my $fdhash = shift;
  399:     my $selvar = "";
  400: 
  401:     foreach $socket (keys %$fdhash) {
  402: 	if($DEBUG) {
  403: 	    &logthis("Adding  ".$socket.
  404: 		     "to select vector. (client)\n");
  405: 	}
  406: 	vec($selvar, $socket, 1) = 1;
  407:     }
  408:     return $selvar;
  409: }
  410: 
  411: 
  412: #
  413: #  HandleOutput:
  414: #    Processes output on a buffered set of file descriptors which are
  415: #    ready to be read.
  416: #  Parameters:
  417: #    $selvector - Vector of file descriptors which are writable.
  418: #    \%sockets  - Vector of socket references indexed by socket.
  419: #    \%buffers  - Reference to a hash containing output buffers.
  420: #                 Hashes are indexed by sockets.  The file descriptors of some
  421: #                 of those sockets will be present in $selvector.
  422: #                 For each one of those, we will attempt to write the output
  423: #                 buffer to the socket.  Note that we will assume that
  424: #                 the sockets are being run in non blocking mode.
  425: #   \%inbufs    - Reference to hash containing input buffers.
  426: #   \%readys    - Reference to hash containing flags for items with complete
  427: #                 requests.
  428: #
  429: sub HandleOutput
  430: {
  431:     my $selvector = shift;
  432:     my $sockets   = shift;
  433:     my $buffers   = shift;
  434:     my $inbufs    = shift;
  435:     my $readys    = shift;
  436:     my $sock;
  437: 
  438:     if($DEBUG) {
  439: 	&logthis("HandleOutput entered\n");
  440:     }
  441: 
  442:     foreach $sock (keys %$sockets) {
  443: 	my $socket = $sockets->{$sock};
  444: 	if(vec($selvector, $sock, 1)) { # $socket is writable.
  445: 	    if($DEBUG) {
  446: 		&logthis("Sending $buffers->{$sock} \n");
  447: 	    }
  448: 	    my $rv = $socket->send($buffers->{$sock}, 0);
  449: 	    $errno = $!;
  450: 	    unless ($buffers->{$sock} eq "con_lost\n") {
  451: 		unless (defined $rv) { # Write failed... could be EINTR
  452: 		    unless ($errno == POSIX::EINTR) {
  453: 			&logthis("Write failed on writable socket");
  454: 		    }		# EINTR is not an error .. just retry.
  455: 		    next;
  456: 		}
  457: 		if( ($rv == length $buffers->{$sock})    ||
  458: 		    ($errno == POSIX::EWOULDBLOCK)       ||
  459: 		    ($errno == POSIX::EAGAIN)            || # same as above.
  460: 		    ($errno == POSIX::EINTR)             || # signal during IO
  461: 		    ($errno == 0)) {
  462: 		    substr($buffers->{$sock}, 0, $rv)=""; # delete written part
  463: 		    delete $buffers->{$sock} unless length $buffers->{$sock};
  464: 		} else {
  465: 		    # For some reason the write failed with an error code
  466: 		    # we didn't look for.  Shutdown the socket.
  467: 		    &logthis("Unable to write data with ".$errno.": ".
  468: 			     "Dropping data: ".length($buffers->{$sock}).
  469: 			     ", $rv");
  470: 		    #
  471: 		    # kill off the buffers in the hash:
  472: 
  473: 		    delete $buffers->{$sock};
  474: 		    delete $inbufs->{$sock};
  475: 		    delete $readys->{$sock};
  476: 
  477: 		    close($socket); # Close the client socket.
  478: 		    next;
  479: 		}
  480: 	    } else {		# Kludgy way to mark lond connection lost.
  481: 		&logthis(
  482: 		 "<font color=red>CRITICAL lond connection lost</font>");
  483: 		status("Connection lost");
  484: 		$remotesock->shutdown(2);
  485: 		&logthis("Attempting to open a new connection");
  486: 		&openremote($conserver);
  487: 	    }
  488: 		   
  489: 	}
  490:     }
  491: 
  492: }
  493: #
  494: #   HandleInput - Deals with input on client sockets.
  495: #                 Each socket has an associated input buffer.
  496: #                 For each readable socket, the currently available
  497: #                 data is appended to this buffer.
  498: #                 If necessary, the buffer is created.
  499: #                 On various failures, we may shutdown the client.
  500: #  Parameters:
  501: #     $selvec   - Vector of readable sockets.
  502: #     \%sockets - Refers to the  Hash of sockets indexed by sockets.  
  503: #                 Each of these may or may not have it's fd bit set 
  504: #                 in the $selvec.
  505: #     \%ibufs   - Refers to the hash of input buffers indexed by socket.
  506: #     \%obufs   - Hash of output buffers indexed by socket. 
  507: #     \%ready   - Hash of ready flags indicating the existence of a completed
  508: #                 Request.
  509: sub HandleInput 
  510: {
  511: 
  512:     # Marshall the parameters.   Note that the hashes are actually
  513:     # references not values.
  514: 
  515:     my $selvec  = shift;
  516:     my $sockets = shift;
  517:     my $ibufs   = shift;
  518:     my $obufs   = shift;
  519:     my $ready   = shift;
  520:     my $sock;
  521: 
  522:     if($DEBUG) {
  523: 	&logthis("Entered HandleInput\n");
  524:     }
  525:     foreach $sock (keys %$sockets) {
  526: 	my $socket = $sockets->{$sock};
  527: 	if(vec($selvec, $sock, 1)) { # Socket which is readable.
  528: 
  529: 	    #  Attempt to read the data and do error management.
  530: 	    my $data = '';
  531: 	    my $rv = $socket->recv($data, POSIX::BUFSIZ, 0);
  532: 	    if($DEBUG) {
  533: 		&logthis("Received $data from socket");
  534: 	    }
  535: 	    unless (defined($rv) && length $data) {
  536: 
  537: 		# Read an end of file.. this is a disconnect from the peer.
  538: 
  539: 		delete $sockets->{$sock};
  540: 		delete $ibufs->{$sock};
  541: 		delete $obufs->{$sock};
  542: 		delete $ready->{$sock};
  543: 
  544: 		status("Idle");
  545: 		close $socket;
  546: 		next;
  547: 	    }
  548: 	    #  Append the read data to the input buffer. If the buffer
  549: 	    # now contains a \n the request is complete and we can 
  550: 	    # mark this in the $ready hash (one request for each \n.)
  551: 
  552: 	    $ibufs->{$sock} .= $data;
  553: 	    while($ibufs->{$sock} =~ s/(.*\n)//) {
  554: 		push(@{$ready->{$sock}}, $1);
  555: 	    }
  556: 	    
  557: 	}
  558:     }
  559:     #  Now handle any requests which are ready:
  560: 
  561:     foreach $client (keys %ready) {
  562: 	handle($client);
  563:     }
  564: }
  565: 
  566: # DoSelect:  does a select with no timeout.  On signal (errno == EINTR), 
  567: #            the select is retried until there are items in the returned
  568: #            vectors.  
  569: #
  570: # Parameters:
  571: #   \$readvec   - Reference to a vector of file descriptors to 
  572: #                 check for readability.
  573: #   \$writevec  - Reference to a vector of file descriptors to check for
  574: #                 writability.
  575: #  On exit, the referents are modified with vectors indicating which 
  576: #  file handles are readable/writable.
  577: #
  578: sub DoSelect {
  579:     my $readvec = shift;
  580:     my $writevec= shift;
  581:     my $outs;
  582:     my $ins;
  583: 
  584:     while (1) {
  585: 	my $nfds = select( $ins = $$readvec, $outs = $$writevec, undef, undef);
  586: 	if($nfds) {
  587: 	    if($DEBUG) {
  588: 		&logthis("select exited with ".$nfds." fds\n");
  589: 		&logthis("ins = ".unpack("b*",$ins).
  590: 			 " readvec = ".unpack("b*",$$readvec)."\n");
  591: 		&logthis("outs = ".unpack("b*",$outs).
  592: 			 " writevec = ".unpack("b*",$$writevec)."\n");
  593: 	    }
  594: 	    $$readvec  = $ins;
  595: 	    $$writevec = $outs;
  596: 	    return;
  597: 	} else {
  598: 	    if($DEBUG) {
  599: 		&logthis("Select exited with no bits set in mask\n");
  600: 	    }
  601: 	    die "Select failed" unless $! == EINTR;
  602: 	}
  603:     }
  604: }
  605: 
  606: # handle($socket) deals with all pending requests for $client
  607: #
  608: sub handle {
  609:     # requests are in $ready{$client}
  610:     # send output to $outbuffer{$client}
  611:     my $client = shift;
  612:     my $request;
  613:     foreach $request (@{$ready{$client}}) {
  614: # ============================================================= Process request
  615:         # $request is the text of the request
  616:         # put text of reply into $outbuffer{$client}
  617: # ------------------------------------------------------------ Is this the end?
  618: 	chomp($request);
  619: 	if($DEBUG) {
  620:      &logthis("<font color=green> Request $request processing starts</font>");
  621:         }
  622:         if ($request eq "close_connection_exit\n") {
  623: 	    &status("Request close connection");
  624:            &logthis(
  625:      "<font color=red>CRITICAL: Request Close Connection ... exiting</font>");
  626:            $remotesock->shutdown(2);
  627:            $server->close();
  628:            exit;
  629:         }
  630: # -----------------------------------------------------------------------------
  631:         if ($request =~ /^encrypt\:/) {
  632: 	    my $cmd=$request;
  633:             $cmd =~ s/^encrypt\://;
  634:             chomp($cmd);
  635:             my $cmdlength=length($cmd);
  636:             $cmd.="         ";
  637:             my $encrequest='';
  638:             for (my $encidx=0;$encidx<=$cmdlength;$encidx+=8) {
  639:                 $encrequest.=
  640:                     unpack("H16",$cipher->encrypt(substr($cmd,$encidx,8)));
  641:             }
  642:             $request="enc:$cmdlength:$encrequest";
  643:         }
  644: # --------------------------------------------------------------- Main exchange
  645: 	$answer = londtransaction($remotesock, $request, 300);
  646: 
  647: 	if($DEBUG) { 
  648: 	    &logthis("<font color=green> Request data exchange complete");
  649: 	}
  650: 	if ($@=~/timeout/) { 
  651: 	    $answer='';
  652: 	    &logthis(
  653: 		     "<font color=red>CRITICAL: Timeout: $request</font>");
  654: 	}  
  655: 
  656: 
  657:         if ($answer) {
  658: 	   if ($answer =~ /^enc/) {
  659:                my ($cmd,$cmdlength,$encinput)=split(/:/,$answer);
  660:                chomp($encinput);
  661: 	       $answer='';
  662:                for (my $encidx=0;$encidx<length($encinput);$encidx+=16) {
  663:                   $answer.=$cipher->decrypt(
  664:                    pack("H16",substr($encinput,$encidx,16))
  665:                   );
  666: 	       }
  667: 	      $answer=substr($answer,0,$cmdlength);
  668: 	      $answer.="\n";
  669: 	   }
  670: 	   if($DEBUG) {
  671: 	       &logthis("sending $answer to client\n");
  672: 	   }
  673:            $outbuffer{$client} .= $answer;
  674:         } else {
  675:            $outbuffer{$client} .= "con_lost\n";
  676:         }
  677: 
  678:      &status("Completed: $request");
  679: 	if($DEBUG) {
  680: 	    &logthis("<font color=green> Request processing complete</font>");
  681: 	}
  682: # ===================================================== Done processing request
  683:     }
  684:     delete $ready{$client};
  685: # -------------------------------------------------------------- End non-forker
  686:     if($DEBUG) {
  687: 	&logthis("<font color=green> requests for child handled</font>");
  688:     }
  689: }
  690: # ---------------------------------------------------------- End make_new_child
  691: 
  692: # nonblock($socket) puts socket into nonblocking mode
  693: sub nonblock {
  694:     my $socket = shift;
  695:     my $flags;
  696: 
  697:     
  698:     $flags = fcntl($socket, F_GETFL, 0)
  699:             or die "Can't get flags for socket: $!\n";
  700:     fcntl($socket, F_SETFL, $flags | O_NONBLOCK)
  701:             or die "Can't make socket nonblocking: $!\n";
  702: }
  703: 
  704: 
  705: sub openremote {
  706: # ---------------------------------------------------- Client to network server
  707: 
  708:     my $conserver=shift;
  709: 
  710: &status("Opening TCP");
  711:     my $st=120+int(rand(240)); # Sleep before opening:
  712: 
  713: unless (
  714:   $remotesock = IO::Socket::INET->new(PeerAddr => $hostip{$conserver},
  715:                                       PeerPort => $perlvar{'londPort'},
  716:                                       Proto    => "tcp",
  717:                                       Type     => SOCK_STREAM)
  718:    ) { 
  719: 
  720:        &logthis(
  721: "<font color=blue>WARNING: Couldn't connect to $conserver ($st secs): </font>");
  722:        sleep($st);
  723:        exit; 
  724:      };
  725: # ----------------------------------------------------------------- Init dialog
  726: 
  727: &logthis("<font color=green>INFO Connected to $conserver, initing </font>");
  728: &status("Init dialogue: $conserver");
  729: 
  730:     $answer = londtransaction($remotesock, "init", 60);
  731:     chomp($answer);
  732:     $answer = londtransaction($remotesock, $answer, 60);
  733:     chomp($answer);
  734:  
  735:      if ($@=~/timeout/) {
  736: 	 &logthis("Timed out during init.. exiting");
  737:          exit;
  738:      }
  739: 
  740: if ($answer ne 'ok') {
  741:        &logthis("Init reply: >$answer<");
  742:        my $st=120+int(rand(240));
  743:        &logthis(
  744: "<font color=blue>WARNING: Init failed ($st secs)</font>");
  745:        sleep($st);
  746:        exit; 
  747: }
  748: 
  749: sleep 5;
  750: &status("Ponging");
  751: print $remotesock "pong\n";
  752: $answer=<$remotesock>;
  753: chomp($answer);
  754: if ($answer!~/^$conserver/) {
  755:    &logthis("Pong reply: >$answer<");
  756: }
  757: # ----------------------------------------------------------- Initialize cipher
  758: 
  759: &status("Initialize cipher");
  760: print $remotesock "ekey\n";
  761: my $buildkey=<$remotesock>;
  762: my $key=$conserver.$perlvar{'lonHostID'};
  763: $key=~tr/a-z/A-Z/;
  764: $key=~tr/G-P/0-9/;
  765: $key=~tr/Q-Z/0-9/;
  766: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
  767: $key=substr($key,0,32);
  768: my $cipherkey=pack("H32",$key);
  769: if ($cipher=new IDEA $cipherkey) {
  770:    &logthis("Secure connection initialized");
  771: } else {
  772:    my $st=120+int(rand(240));
  773:    &logthis(
  774:      "<font color=blue>WARNING: ".
  775:      "Could not establish secure connection ($st secs)!</font>");
  776:    sleep($st);
  777:    exit;
  778: }
  779:     &logthis("<font color=green> Remote open success </font>");
  780: }
  781: 
  782: 
  783: 
  784: # grabs exception and records it to log before exiting
  785: sub catchexception {
  786:     my ($signal)=@_;
  787:     $SIG{QUIT}='DEFAULT';
  788:     $SIG{__DIE__}='DEFAULT';
  789:     chomp($signal);
  790:     &logthis("<font color=red>CRITICAL: "
  791:      ."ABNORMAL EXIT. Child $$ for server [$wasserver] died through "
  792:      ."\"$signal\" with parameter </font>");
  793:     die("Signal abend");
  794: }
  795: 
  796: # -------------------------------------- Routines to see if other box available
  797: 
  798: #sub online {
  799: #    my $host=shift;
  800: #    &status("Pinging ".$host);
  801: #    my $p=Net::Ping->new("tcp",20);
  802: #    my $online=$p->ping("$host");
  803: #    $p->close();
  804: #    undef ($p);
  805: #    return $online;
  806: #}
  807: 
  808: sub connected {
  809:     my ($local,$remote)=@_;
  810:     &status("Checking connection $local to $remote");
  811:     $local=~s/\W//g;
  812:     $remote=~s/\W//g;
  813: 
  814:     unless ($hostname{$local}) { return 'local_unknown'; }
  815:     unless ($hostname{$remote}) { return 'remote_unknown'; }
  816: 
  817:     #unless (&online($hostname{$local})) { return 'local_offline'; }
  818: 
  819:     my $ua=new LWP::UserAgent;
  820:     
  821:     my $request=new HTTP::Request('GET',
  822:       "http://".$hostname{$local}.'/cgi-bin/ping.pl?'.$remote);
  823: 
  824:     my $response=$ua->request($request);
  825: 
  826:     unless ($response->is_success) { return 'local_error'; }
  827: 
  828:     my $reply=$response->content;
  829:     $reply=(split("\n",$reply))[0];
  830:     $reply=~s/\W//g;
  831:     if ($reply ne $remote) { return $reply; }
  832:     return 'ok';
  833: }
  834: 
  835: 
  836: 
  837: sub hangup {
  838:     foreach (keys %children) {
  839:         $wasserver=$children{$_};
  840:         &status("Closing $wasserver");
  841:         &logthis('Closing '.$wasserver.': '.&subreply('exit',$wasserver));
  842:         &status("Kill PID $_ for $wasserver");
  843: 	kill ('INT',$_);
  844:     }
  845: }
  846: 
  847: sub HUNTSMAN {                      # signal handler for SIGINT
  848:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  849:     &hangup();
  850:     my $execdir=$perlvar{'lonDaemons'};
  851:     unlink("$execdir/logs/lonc.pid");
  852:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  853:     exit;                           # clean up with dignity
  854: }
  855: 
  856: sub HUPSMAN {                      # signal handler for SIGHUP
  857:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  858:     &hangup();
  859:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  860:     unlink("$execdir/logs/lonc.pid");
  861:     my $execdir=$perlvar{'lonDaemons'};
  862:     exec("$execdir/lonc");         # here we go again
  863: }
  864: 
  865: sub checkchildren {
  866:     &initnewstatus();
  867:     &logstatus();
  868:     &logthis('Going to check on the children');
  869:     foreach (sort keys %children) {
  870: 	sleep 1;
  871:         unless (kill 'USR1' => $_) {
  872: 	    &logthis ('<font color=red>CRITICAL: Child '.$_.' is dead</font>');
  873:             &logstatus($$.' is dead');
  874:         } 
  875:     }
  876: }
  877: 
  878: sub USRMAN {
  879:     &logthis("USR1: Trying to establish connections again");
  880:     #
  881:     #  It is really important not to just clear the childatt hash or we will
  882:     #  lose all memory of the children.  What we really want to do is this:
  883:     #  For each index where childatt is >= $childmaxattempts
  884:     #  Zero the associated counter and do a make_child for the host.
  885:     #  Regardles, the childatt entry is zeroed:
  886:     my $host;
  887:     foreach $host (keys %childatt) {
  888: 	if ($childatt{$host} >= $childmaxattempts) {
  889: 	    $childatt{$host} = 0;
  890: 	    &logthis("<font color=green>INFO: Restarting child for server: "
  891: 		     .$host."</font>\n");
  892: 	    make_new_child($host);
  893: 	}
  894: 	else {
  895: 	    $childatt{$host} = 0;
  896: 	}
  897:     }
  898:     &checkchildren();		# See if any children are still dead...
  899: }
  900: 
  901: # -------------------------------------------------- Non-critical communication
  902: sub subreply { 
  903:  my ($cmd,$server)=@_;
  904:  my $answer='';
  905:  if ($server ne $perlvar{'lonHostID'}) { 
  906:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  907:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  908:                                       Type    => SOCK_STREAM,
  909:                                       Timeout => 10)
  910:        or return "con_lost";
  911: 
  912: 
  913:     $answer = londtransaction($sclient, $cmd, 10);
  914: 
  915:     if ((!$answer) || ($@=~/timeout/)) { $answer="con_lost"; }
  916:     $SIG{ALRM}='DEFAULT';
  917:     $SIG{__DIE__}=\&catchexception;
  918:  } else { $answer='self_reply'; }
  919:  return $answer;
  920: }
  921: 
  922: # --------------------------------------------------------------------- Logging
  923: 
  924: sub logthis {
  925:     my $message=shift;
  926:     my $execdir=$perlvar{'lonDaemons'};
  927:     my $fh=IO::File->new(">>$execdir/logs/lonc.log");
  928:     my $now=time;
  929:     my $local=localtime($now);
  930:     $lastlog=$local.': '.$message;
  931:     print $fh "$local ($$) [$conserver] [$status]: $message\n";
  932: }
  933: 
  934: #--------------------------------------  londtransaction:
  935: #  
  936: #  Performs a transaction with lond with timeout support.
  937: #    result = londtransaction(socket,request,timeout)
  938: #
  939: sub londtransaction {
  940:     my ($socket, $request, $tmo) = @_;
  941: 
  942:     if($DEBUG) {
  943: 	&logthis("londtransaction request: $request");
  944:     }
  945: 
  946:     # Set the signal handlers: ALRM for timeout and disble the others.
  947: 
  948:     $SIG{ALRM} = sub { die "timeout" };
  949:     $SIG{__DIE__} = 'DEFAULT';
  950:     
  951:     # Disable all but alarm so that only that can interupt the
  952:     # send /receive.
  953:     #
  954:     my $sigset = POSIX::SigSet->new(QUIT, USR1, HUP, INT, TERM);
  955:     my $priorsigs = POSIX::SigSet->new;
  956:     unless (defined sigprocmask(SIG_BLOCK, $sigset, $priorsigs)) {
  957: 	&logthis("<font color=red> CRITICAL -- londtransaction ".
  958: 		"failed to block signals </font>");
  959: 	die "could not block signals in londtransaction";
  960:     }
  961:     $answer = '';
  962:     #
  963:     #  Send request to lond.
  964:     #
  965:     eval { 
  966: 	alarm($tmo);
  967: 	print $socket "$request\n";
  968: 	alarm(0);
  969:     };
  970:     #  If request didn't timeout, try for the response.
  971:     #
  972: 
  973:     if ($@!~/timeout/) {
  974: 	eval {
  975: 	    alarm($tmo);
  976: 	    $answer = <$socket>;
  977: 	    if($DEBUG) {
  978: 		&logthis("Received $answer in londtransaction");
  979: 	    }
  980: 	    alarm(0);
  981: 	};
  982:     } else {
  983: 	if($DEBUG) {
  984: 	    &logthis("Timeout on send in londtransaction");
  985: 	}
  986:     }
  987:     if( ($@ =~ /timeout/)  && ($DEBUG)) {
  988: 	&logthis("Timeout on receive in londtransaction");
  989:     }
  990:     #
  991:     # Restore the initial sigmask set.
  992:     #
  993:     unless (defined sigprocmask(SIG_UNBLOCK, $priorsigs)) {
  994: 	&logthis("<font color=red> CRITICAL -- londtransaction ".
  995: 		"failed to re-enable signal processing. </font>");
  996: 	die "londtransaction failed to re-enable signals";
  997:     }
  998:     #
  999:     # go back to the prior handler set.
 1000:     #
 1001:     $SIG{ALRM} = 'DEFAULT';
 1002:     $SIG{__DIE__} = \&cathcexception;
 1003: 
 1004:     #    chomp $answer;
 1005:     if ($DEBUG) {
 1006: 	&logthis("Returning $answer in londtransaction");
 1007:     }
 1008:     return $answer;
 1009: 
 1010: }
 1011: 
 1012: sub logperm {
 1013:     my $message=shift;
 1014:     my $execdir=$perlvar{'lonDaemons'};
 1015:     my $now=time;
 1016:     my $local=localtime($now);
 1017:     my $fh=IO::File->new(">>$execdir/logs/lonnet.perm.log");
 1018:     print $fh "$now:$message:$local\n";
 1019: }
 1020: # ------------------------------------------------------------------ Log status
 1021: 
 1022: sub logstatus {
 1023:     my $docdir=$perlvar{'lonDocRoot'};
 1024:     my $fh=IO::File->new(">>$docdir/lon-status/loncstatus.txt");
 1025:     print $fh $$."\t".$conserver."\t".$status."\t".$lastlog."\n";
 1026: }
 1027: 
 1028: sub initnewstatus {
 1029:     my $docdir=$perlvar{'lonDocRoot'};
 1030:     my $fh=IO::File->new(">$docdir/lon-status/loncstatus.txt");
 1031:     my $now=time;
 1032:     my $local=localtime($now);
 1033:     print $fh "LONC status $local - parent $$\n\n";
 1034: }
 1035: 
 1036: # -------------------------------------------------------------- Status setting
 1037: 
 1038: sub status {
 1039:     my $what=shift;
 1040:     my $now=time;
 1041:     my $local=localtime($now);
 1042:     $status=$local.': '.$what;
 1043: }
 1044: 
 1045: 
 1046: 
 1047: # ----------------------------------- POD (plain old documentation, CPAN style)
 1048: 
 1049: =head1 NAME
 1050: 
 1051: lonc - LON TCP-MySQL-Server Daemon for handling database requests.
 1052: 
 1053: =head1 SYNOPSIS
 1054: 
 1055: Usage: B<lonc>
 1056: 
 1057: Should only be run as user=www.  This is a command-line script which
 1058: is invoked by B<loncron>.  There is no expectation that a typical user
 1059: will manually start B<lonc> from the command-line.  (In other words,
 1060: DO NOT START B<lonc> YOURSELF.)
 1061: 
 1062: =head1 DESCRIPTION
 1063: 
 1064: Provides persistent TCP connections to the other servers in the network
 1065: through multiplexed domain sockets
 1066: 
 1067: B<lonc> forks off children processes that correspond to the other servers
 1068: in the network.  Management of these processes can be done at the
 1069: parent process level or the child process level.
 1070: 
 1071:   After forking off the children, B<lonc> the B<parent> 
 1072: executes a main loop which simply waits for processes to exit.
 1073: As a process exits, a new process managing a link to the same
 1074: peer as the exiting process is created.  
 1075: 
 1076: B<logs/lonc.log> is the location of log messages.
 1077: 
 1078: The process management is now explained in terms of linux shell commands,
 1079: subroutines internal to this code, and signal assignments:
 1080: 
 1081: =over 4
 1082: 
 1083: =item *
 1084: 
 1085: PID is stored in B<logs/lonc.pid>
 1086: 
 1087: This is the process id number of the parent B<lonc> process.
 1088: 
 1089: =item *
 1090: 
 1091: SIGTERM and SIGINT
 1092: 
 1093: Parent signal assignment:
 1094:  $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
 1095: 
 1096: Child signal assignment:
 1097:  $SIG{INT}  = 'DEFAULT'; (and SIGTERM is DEFAULT also)
 1098: (The child dies and a SIGALRM is sent to parent, awaking parent from slumber
 1099:  to restart a new child.)
 1100: 
 1101: Command-line invocations:
 1102:  B<kill> B<-s> SIGTERM I<PID>
 1103:  B<kill> B<-s> SIGINT I<PID>
 1104: 
 1105: Subroutine B<HUNTSMAN>:
 1106:  This is only invoked for the B<lonc> parent I<PID>.
 1107: This kills all the children, and then the parent.
 1108: The B<lonc.pid> file is cleared.
 1109: 
 1110: =item *
 1111: 
 1112: SIGHUP
 1113: 
 1114: Current bug:
 1115:  This signal can only be processed the first time
 1116: on the parent process.  Subsequent SIGHUP signals
 1117: have no effect.
 1118: 
 1119: Parent signal assignment:
 1120:  $SIG{HUP}  = \&HUPSMAN;
 1121: 
 1122: Child signal assignment:
 1123:  none (nothing happens)
 1124: 
 1125: Command-line invocations:
 1126:  B<kill> B<-s> SIGHUP I<PID>
 1127: 
 1128: Subroutine B<HUPSMAN>:
 1129:  This is only invoked for the B<lonc> parent I<PID>,
 1130: This kills all the children, and then the parent.
 1131: The B<lonc.pid> file is cleared.
 1132: 
 1133: =item *
 1134: 
 1135: SIGUSR1
 1136: 
 1137: Parent signal assignment:
 1138:  $SIG{USR1} = \&USRMAN;
 1139: 
 1140: Child signal assignment:
 1141:  $SIG{USR1}= \&logstatus;
 1142: 
 1143: Command-line invocations:
 1144:  B<kill> B<-s> SIGUSR1 I<PID>
 1145: 
 1146: Subroutine B<USRMAN>:
 1147:  When invoked for the B<lonc> parent I<PID>,
 1148: SIGUSR1 is sent to all the children, and the status of
 1149: each connection is logged.
 1150: 
 1151: 
 1152: =back
 1153: 
 1154: =head1 PREREQUISITES
 1155: 
 1156: POSIX
 1157: IO::Socket
 1158: IO::Select
 1159: IO::File
 1160: Socket
 1161: Fcntl
 1162: Tie::RefHash
 1163: Crypt::IDEA
 1164: 
 1165: =head1 COREQUISITES
 1166: 
 1167: =head1 OSNAMES
 1168: 
 1169: linux
 1170: 
 1171: =head1 SCRIPT CATEGORIES
 1172: 
 1173: Server/Process
 1174: 
 1175: =cut

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