File:  [LON-CAPA] / loncom / Attic / lonc
Revision 1.41: download - view: text, annotated - select for diffs
Fri May 17 14:03:04 2002 UTC (22 years ago) by matthew
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, version_0_4, stable_2002_july, STABLE, HEAD
Replaced access.conf with loncapa_apache.conf.

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

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