File:  [LON-CAPA] / loncom / lonmaxima
Revision 1.35: download - view: text, annotated - select for diffs
Fri Nov 9 18:51:10 2007 UTC (16 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#5517 when a child is exiting let the parent now so it can fork off a new child right away without having to wait for the child to fully pass on

    1: #!/usr/bin/perl
    2: #
    3: # The LearningOnline Network with CAPA
    4: # Connect to MAXIMA CAS
    5: #
    6: # $Id: lonmaxima,v 1.35 2007/11/09 18:51:10 albertel Exp $
    7: #
    8: # Copyright Michigan State University Board of Trustees
    9: #
   10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
   11: #
   12: # LON-CAPA is free software; you can redistribute it and/or modify
   13: # it under the terms of the GNU General Public License as published by
   14: # the Free Software Foundation; either version 2 of the License, or
   15: # (at your option) any later version.
   16: #
   17: # LON-CAPA is distributed in the hope that it will be useful,
   18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   20: # GNU General Public License for more details.
   21: #
   22: # You should have received a copy of the GNU General Public License
   23: # along with LON-CAPA; if not, write to the Free Software
   24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   25: #
   26: # /home/httpd/html/adm/gpl.txt
   27: #
   28: 
   29: # 
   30: # http://www.lon-capa.org/
   31: #
   32: 
   33: use Expect; 
   34: use IO::Select;
   35: use IO::Socket;
   36: use IO::File;
   37: use Symbol;
   38: use POSIX;
   39: use lib '/home/httpd/lib/perl/';
   40: use LONCAPA::Configuration;
   41:  
   42: use strict;
   43: 
   44: # global variables
   45: my $PREFORK                = 5;        # number of children to maintain
   46: my $MAX_CLIENTS_PER_CHILD  = 50;       # number of clients each child should process
   47: my $extra_children         = 0;
   48: my %children               = ();       # keys are current child process IDs
   49: my $children               = 0;        # current number of children
   50: my $status;                            # string for current status
   51: my $pidfile;                           # file containg parent process pid
   52: my $port;                              # path to UNIX socket file
   53: my %perlvar;                           # configuration file info
   54: my $lastlog;                           # last string that was logged
   55: 
   56: use vars qw($PREFORK $MAX_CLIENTS_PER_CHILD %children $children $status
   57: 	    $pidfile $port %perlvar $lastlog);
   58:  
   59: # ------------------------------------------------------------ Service routines 
   60: sub REAPER {                        # takes care of dead children 
   61:                                     # and MAXIMA processes
   62:     $SIG{CHLD} = \&REAPER;
   63:     my $pid = wait;
   64:     if (exists($children{$pid})) {
   65: 	$children--;
   66: 	delete($children{$pid});
   67: 	if ($extra_children) {
   68: 	    $extra_children--;
   69: 	}
   70:     }    
   71: }
   72:  
   73: sub HUNTSMAN {                      # signal handler for SIGINT
   74:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
   75:     kill('INT' => keys(%children));
   76:     unlink($pidfile);
   77:     unlink($port);
   78:     &logthis('---- Shutdown ----');
   79:     exit;                           # clean up with dignity
   80: }
   81: 
   82: 
   83:  
   84: # --------------------------------------------------------------------- Logging
   85:  
   86: sub logthis {
   87:     my ($message)=@_;
   88:     my $execdir=$perlvar{'lonDaemons'};
   89:     my $fh=IO::File->new(">>$execdir/logs/lonmaxima.log");
   90:     my $now=time;
   91:     my $local=localtime($now);
   92:     $lastlog=$local.': '.$message;
   93:     print $fh "$local ($$): $message\n";
   94: }
   95:  
   96: # -------------------------------------------------------------- Status setting
   97:  
   98: sub status {
   99:     my ($what)=@_;
  100:     my $now=time;
  101:     my $local=localtime($now);
  102:     $status=$local.': '.$what;
  103:     $0='lonmaxima: '.$what.' '.$local;
  104: }
  105:  
  106: # -------------------------------------------------------- Escape Special Chars
  107:  
  108: sub escape {
  109:     my ($str)=@_;
  110:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  111:     return $str;
  112: }
  113:  
  114: # ----------------------------------------------------- Un-Escape Special Chars
  115:  
  116: sub unescape {
  117:     my ($str)=@_;
  118:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  119:     return $str;
  120: }
  121:  
  122: # ------------------------ grabs exception and records it to log before exiting
  123: sub catchexception {
  124:     my ($signal)=@_;
  125:     $SIG{QUIT}='DEFAULT';
  126:     $SIG{__DIE__}='DEFAULT';
  127:     chomp($signal);
  128:     &logthis("<font color=\"red\">CRITICAL: "
  129: 	     ."ABNORMAL EXIT. Child $$ died through "
  130: 	     ."\"$signal\"</font>");
  131:     die("Signal abend");
  132: }
  133: 
  134: 
  135: sub child_announce_death {
  136:     $SIG{USR1} = \&child_announce_death;
  137:     $extra_children++;
  138: }
  139: 
  140: # ---------------------------------------------------------------- Main program
  141: # -------------------------------- Set signal handlers to record abnormal exits
  142:  
  143:  
  144: $SIG{'QUIT'}=\&catchexception;
  145: $SIG{__DIE__}=\&catchexception;
  146: $SIG{USR1} = \&child_announce_death;
  147:  
  148: # ---------------------------------- Read loncapa_apache.conf and loncapa.conf
  149: &status("Read loncapa.conf and loncapa_apache.conf");
  150: %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')};
  151:  
  152: # ----------------------------- Make sure this process is running from user=www
  153: my $wwwid=getpwnam('www');
  154: if ($wwwid!=$<) {
  155:     my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
  156:     my $subj="LON: User ID mismatch";
  157:     system("echo 'User ID mismatch.  lonmaxima must be run as user www.' |\
  158:  mailto $emailto -s '$subj' > /dev/null");
  159:     exit 1;
  160: }
  161:  
  162: # --------------------------------------------- Check if other instance running
  163:  
  164: $pidfile="$perlvar{'lonDaemons'}/logs/lonmaxima.pid";
  165:  
  166: if (-e $pidfile) {
  167:     my $lfh=IO::File->new("$pidfile");
  168:     my $pide=<$lfh>;
  169:     chomp($pide);
  170:     if (kill(0 => $pide)) { die "already running"; }
  171: }
  172: 
  173: # ------------------------------------------------------- Listen to UNIX socket
  174: &status("Opening socket");
  175:  
  176: $port = "$perlvar{'lonSockDir'}/maximasock";
  177:  
  178: unlink($port);
  179:  
  180: 
  181: my $server = IO::Socket::UNIX->new(Local  => $port,
  182: 				   Type   => SOCK_STREAM,
  183: 				   Listen => 10 );
  184: if (!$server) {
  185:     my $st=120+int(rand(240));
  186: 
  187:     &logthis("<font color=blue>WARNING: ".
  188: 	     "Can't make server socket ($st secs):  .. exiting</font>");
  189: 
  190:     sleep($st);
  191:     exit;
  192: }
  193:     
  194:  
  195: # ---------------------------------------------------- Fork once and dissociate
  196:  
  197: my $fpid=fork;
  198: exit if $fpid;
  199: die("Couldn't fork: $!") unless defined($fpid);
  200:  
  201: POSIX::setsid() or die "Can't start new session: $!";
  202:  
  203: # ------------------------------------------------------- Write our PID on disk
  204:  
  205: my $execdir=$perlvar{'lonDaemons'};
  206: open(PIDSAVE,">$execdir/logs/lonmaxima.pid");
  207: print PIDSAVE "$$\n";
  208: close(PIDSAVE);
  209: &logthis("<font color='red'>CRITICAL: ---------- Starting ----------</font>");
  210: &status('Starting');
  211:      
  212: 
  213: # Install signal handlers.
  214: $SIG{CHLD} = \&REAPER;
  215: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  216:  
  217: # Fork off our children.
  218: for (1 .. $PREFORK) {
  219:     &make_new_child($server);
  220: }
  221:  
  222: # And maintain the population.
  223: while (1) {
  224:     &status('Parent process, sleeping');
  225:     sleep;                          # wait for a signal (i.e., child's death)
  226:     for (my $i = $children; $i < $PREFORK+$extra_children; $i++) {
  227:         &status('Parent process, starting child');
  228:         &make_new_child($server);           # top up the child pool
  229:     }
  230: }
  231:                                                                                 
  232: sub make_new_child {
  233:     my ($server) = @_;
  234: 
  235:     # block signal for fork
  236:     my $sigset = POSIX::SigSet->new(SIGINT);
  237:     sigprocmask(SIG_BLOCK, $sigset)
  238:         or die("Can't block SIGINT for fork: $!\n");
  239:      
  240:     die("fork: $!") unless defined(my $pid = fork);
  241:      
  242:     if ($pid) {
  243:         # Parent records the child's birth and returns.
  244:         sigprocmask(SIG_UNBLOCK, $sigset)
  245:             or die("Can't unblock SIGINT for fork: $!\n");
  246:         $children{$pid} = 1;
  247:         $children++;
  248:         return;
  249:     } else {
  250:         # Child can *not* return from this subroutine.
  251:         
  252: 	my $ppid = getppid();
  253:      
  254:         # unblock signals
  255:         sigprocmask(SIG_UNBLOCK, $sigset)
  256:             or die("Can't unblock SIGINT for fork: $!\n");
  257: 
  258:         &logthis('New process started');
  259: 
  260:         my $command=Expect->spawn('maxima');
  261: 	# soft/hard_close can take awhile and we really
  262:         # don't care we just want it gone
  263: 	$SIG{INT} = sub {
  264: 	    my $pid = $command->pid();
  265: 	    kill('KILL'=>$pid);
  266: 	    exit; 
  267: 	};
  268: 
  269: 	$command->log_stdout(0);
  270: 	#$command->log_file("$execdir/logs/lonmaxima.session.log");
  271:         &sync($command);
  272: 
  273:         for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  274:             &status('Accepting connections');
  275:             my $client = $server->accept()     or last;
  276:             print $command ("display2d:false;kill(all);\n");
  277: 	    &getmaximaoutput($command,2);
  278:             &sync($command);
  279:             my $syntaxerr = 0;
  280:             while (my $cmd=<$client>) {
  281:                 &status('Processing command');
  282:                 print $command &unescape($cmd);
  283:                 my ($reply,$syntaxerr) = &getmaximaoutput($command,1);
  284:                 print $client &escape($reply)."\n";
  285:                 if ($syntaxerr) {
  286:                     last;
  287:                 } elsif ($reply=~/^Error\:/) {
  288:                     &logthis('Died through '.$reply);
  289: 		    kill('USR1' => $ppid);
  290:                     $client->close();
  291:                     $command->hard_close();     
  292:                     exit;
  293:                 }
  294: 	        &sync($command);
  295:                 &status('Waiting for commands');
  296:             }
  297:         }
  298: 
  299: 	kill('USR1' => $ppid);
  300: 	print $command ("quit();\n");
  301:         # tidy up gracefully and finish
  302: 
  303:         $command->soft_close();
  304: 
  305:         # this exit is VERY important, otherwise the child will become
  306:         # a producer of more and more children, forking yourself into
  307:         # process death.
  308:         exit;
  309:     }
  310: }
  311: 
  312: {
  313:     my $counter;
  314:     sub sync {
  315: 	my ($command)=@_;
  316: 	$counter++;
  317: 	my $expect=$counter.time;
  318: 	print $command "$expect;\n";
  319: 	while (1) {
  320: 	    my $output=&getmaximaoutput($command,1);
  321: 	    if (($output=~/\Q$expect\E/) || ($output=~/^Error\:/)) {
  322: 		return;
  323: 	    }
  324: 	}
  325:     }
  326: }
  327: 
  328: sub getmaximaoutput {
  329:     my ($command,$numcheck)=@_;
  330:     my $regexp = '\(\%i\d+\)';
  331:     my $syntaxerr=0;
  332:     if ($numcheck) {
  333:        	if ($numcheck eq 2) {
  334: 	    # command was the killall so should get a full reset on
  335: 	    # command numbers
  336: 	    $regexp = '(\(\%i(1)\)|Incorrect syntax\:)';
  337: 	} elsif ($command->match() =~ /\(\%i(\d+)\)/) {
  338:             my $nextmatch = $1+1;
  339:             $regexp = '(\(\%i'.$nextmatch.'\)|Incorrect syntax\:)';
  340:         }
  341:     }
  342:     my $timeout = 20;
  343:     my (undef,$error,$matched,$output) =
  344: 	$command->expect($timeout, -re => $regexp);
  345: 
  346:     if ($numcheck && $matched eq 'Incorrect syntax:') {
  347: 	$syntaxerr = 1;
  348: 	if (wantarray) {
  349: 	    return ($matched,$syntaxerr);
  350: 	} else {
  351: 	    return $matched;
  352: 	}
  353:     }
  354:     if ($error) {
  355: 	return 'Error: '.$error;
  356:     }
  357:     $output =~ s/\r+//g; # Remove Windows-style linebreaks
  358:     my $foundoutput=0;
  359:     my $found_label=0;
  360:     my $realoutput='';
  361:     foreach my $line (split(/\n/,$output)) {
  362:        if ($line=~/\;/) { $foundoutput=1; next; }
  363:        if (!$foundoutput) { next; }
  364:        if ($line=~/^Incorrect syntax:/) { $syntaxerr = 1; next; }
  365:        if ($line=~ /^(\(\%o\d+\))(.+)$/){
  366:            my $label = $1;
  367:            $line = $2;
  368:            $label =~s/\S/ /g;
  369:            $line=$label.$line;
  370: 	   $found_label=1;
  371:        }
  372:        if ($found_label) {
  373: 	   $realoutput.=$line."\n";
  374:        }
  375:     }
  376:     if (wantarray) {
  377:         return ($realoutput,$syntaxerr);
  378:     } else {
  379:         return $realoutput;
  380:     }
  381: }

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