File:  [LON-CAPA] / nsdl / bin / gatewayc
Revision 1.1: download - view: text, annotated - select for diffs
Tue May 28 05:12:09 2002 UTC (21 years, 11 months ago) by harris41
Branches: MAIN
CVS tags: HEAD
based on LON-CAPA's lonc/lond functionality; these scripts are not yet
customized for gateway service

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

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