--- loncom/lonmaxima 2006/03/03 22:35:09 1.2 +++ loncom/lonmaxima 2006/03/03 23:31:06 1.4 @@ -3,7 +3,7 @@ # The LearningOnline Network with CAPA # Connect to MAXIMA CAS # -# $Id: lonmaxima,v 1.2 2006/03/03 22:35:09 www Exp $ +# $Id: lonmaxima,v 1.4 2006/03/03 23:31:06 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -30,11 +30,6 @@ # http://www.lon-capa.org/ # -# global variables -$PREFORK = 5; # number of children to maintain -$MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should process -%children = (); # keys are current child process IDs -$children = 0; # current number of children use IPC::Open3; use IO::Select; @@ -45,11 +40,22 @@ use POSIX; use lib '/home/httpd/lib/perl/'; use LONCAPA::Configuration; -# Scary: cannot use strict!!! -##### use strict; +use strict; + +# global variables +my $PREFORK = 5; # number of children to maintain +my $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should process +my %children = (); # keys are current child process IDs +my $children = 0; # current number of children +my $status; # string for current status + +use vars qw($PREFORK $MAX_CLIENTS_PER_CHILD %children $children $status + $cmd_in $cmd_out $cmd_err $pidfile $port %perlvar $lastlog + $currenthostid $client $server $cmd + ); sub maximareply { - my $cmd=shift; + my ($cmd) = @_; my $reply=''; my $error=''; my $exitstatus=''; @@ -109,7 +115,7 @@ sub HUNTSMAN { # si # --------------------------------------------------------------------- Logging sub logthis { - my $message=shift; + my ($message)=@_; my $execdir=$perlvar{'lonDaemons'}; my $fh=IO::File->new(">>$execdir/logs/lonmaxima.log"); my $now=time; @@ -121,7 +127,7 @@ sub logthis { # -------------------------------------------------------------- Status setting sub status { - my $what=shift; + my ($what)=@_; my $now=time; my $local=localtime($now); $status=$local.': '.$what; @@ -131,7 +137,7 @@ sub status { # -------------------------------------------------------- Escape Special Chars sub escape { - my $str=shift; + my ($str)=@_; $str =~ s/(\W)/"%".unpack('H2',$1)/eg; return $str; } @@ -139,7 +145,7 @@ sub escape { # ----------------------------------------------------- Un-Escape Special Chars sub unescape { - my $str=shift; + my ($str)=@_; $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg; return $str; } @@ -167,9 +173,7 @@ $SIG{__DIE__}=\&catchexception; # ---------------------------------- Read loncapa_apache.conf and loncapa.conf &status("Read loncapa.conf and loncapa_apache.conf"); -my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf'); -%perlvar=%{$perlvarref}; -undef $perlvarref; +%perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')}; # ----------------------------- Make sure this process is running from user=www my $wwwid=getpwnam('www'); @@ -236,7 +240,7 @@ close(PIDSAVE); # Fork off our children. for (1 .. $PREFORK) { - make_new_child( ); + &make_new_child(); } # Install signal handlers. @@ -247,22 +251,20 @@ $SIG{INT} = $SIG{TERM} = \&HUNTSMAN; while (1) { &status('Parent process, sleeping'); sleep; # wait for a signal (i.e., child's death) - for ($i = $children; $i < $PREFORK; $i++) { + for (my $i = $children; $i < $PREFORK; $i++) { &status('Parent process, starting child'); - make_new_child( ); # top up the child pool + &make_new_child(); # top up the child pool } } sub make_new_child { - my $pid; - my $sigset; - + # block signal for fork - $sigset = POSIX::SigSet->new(SIGINT); + my $sigset = POSIX::SigSet->new(SIGINT); sigprocmask(SIG_BLOCK, $sigset) or die "Can't block SIGINT for fork: $!\n"; - die "fork: $!" unless defined ($pid = fork); + die "fork: $!" unless defined (my $pid = fork); if ($pid) { # Parent records the child's birth and returns. @@ -278,17 +280,9 @@ sub make_new_child { # unblock signals sigprocmask(SIG_UNBLOCK, $sigset) or die "Can't unblock SIGINT for fork: $!\n"; - - # handle connections until we've reached $MAX_CLIENTS_PER_CHILD - for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { - &status('Accepting connections'); - $client = $server->accept( ) or last; - while ($cmd=<$client>) { - &status('Processing command'); - print $client &escape((&maximareply(&unescape($cmd)))[0])."\n"; - } - } - + + &process_requests(); + # tidy up gracefully and finish # this exit is VERY important, otherwise the child will become @@ -297,3 +291,15 @@ sub make_new_child { exit; } } + +sub process_requests { + # handle connections until we've reached $MAX_CLIENTS_PER_CHILD + for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) { + &status('Accepting connections'); + $client = $server->accept( ) or last; + while ($cmd=<$client>) { + &status('Processing command'); + print $client &escape((&maximareply(&unescape($cmd)))[0])."\n"; + } + } +}