File:  [LON-CAPA] / loncom / lonsql
Revision 1.47: download - view: text, annotated - select for diffs
Tue Jun 18 19:39:13 2002 UTC (21 years, 10 months ago) by www
Branches: MAIN
CVS tags: HEAD
Toward bug 121 - filters start to work now.

lonsearchcat display definitely broken due to "escape" bugfix in lonsql.

    1: #!/usr/bin/perl
    2: 
    3: # The LearningOnline Network
    4: # lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
    5: #
    6: # $Id: lonsql,v 1.47 2002/06/18 19:39:13 www 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: # http://www.lon-capa.org/
   29: #
   30: # YEAR=2000
   31: # lonsql-based on the preforker:harsha jagasia:date:5/10/00
   32: # 7/25 Gerd Kortemeyer
   33: # many different dates Scott Harrison
   34: # YEAR=2001
   35: # many different dates Scott Harrison
   36: # 03/22/2001 Scott Harrison
   37: # 8/30 Gerd Kortemeyer
   38: # 10/17,11/28,11/29,12/20 Scott Harrison
   39: # YEAR=2001
   40: # 5/11 Scott Harrison
   41: #
   42: ###
   43: 
   44: ###############################################################################
   45: ##                                                                           ##
   46: ## ORGANIZATION OF THIS PERL SCRIPT                                          ##
   47: ## 1. Modules used                                                           ##
   48: ## 2. Enable find subroutine                                                 ##
   49: ## 3. Read httpd config files and get variables                              ##
   50: ## 4. Make sure that database can be accessed                                ##
   51: ## 5. Make sure this process is running from user=www                        ##
   52: ## 6. Check if other instance is running                                     ##
   53: ## 7. POD (plain old documentation, CPAN style)                              ##
   54: ##                                                                           ##
   55: ###############################################################################
   56: 
   57: use lib '/home/httpd/lib/perl/';
   58: use LONCAPA::Configuration;
   59: 
   60: use IO::Socket;
   61: use Symbol;
   62: use POSIX;
   63: use IO::Select;
   64: use IO::File;
   65: use Socket;
   66: use Fcntl;
   67: use Tie::RefHash;
   68: use DBI;
   69: 
   70: my @metalist;
   71: # ----------------- Code to enable 'find' subroutine listing of the .meta files
   72: require "find.pl";
   73: sub wanted {
   74:     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
   75:     -f _ &&
   76:     /^.*\.meta$/ && !/^.+\.\d+\.[^\.]+\.meta$/ &&
   77:     push(@metalist,"$dir/$_");
   78: }
   79: 
   80: $childmaxattempts=10;
   81: $run =0;#running counter to generate the query-id
   82: 
   83: # -------------------------------- Read loncapa_apache.conf and loncapa.conf
   84: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa_apache.conf',
   85:                                                  'loncapa.conf');
   86: my %perlvar=%{$perlvarref};
   87: 
   88: # ------------------------------------- Make sure that database can be accessed
   89: {
   90:     my $dbh;
   91:     unless (
   92: 	    $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
   93: 	    ) { 
   94: 	print "Cannot connect to database!\n";
   95: 	$emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
   96: 	$subj="LON: $perlvar{'lonHostID'} Cannot connect to database!";
   97: 	system("echo 'Cannot connect to MySQL database!' |\
   98:  mailto $emailto -s '$subj' > /dev/null");
   99: 	exit 1;
  100:     }
  101:     else {
  102: 	$dbh->disconnect;
  103:     }
  104: }
  105: 
  106: # --------------------------------------------- Check if other instance running
  107: 
  108: my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid";
  109: 
  110: if (-e $pidfile) {
  111:    my $lfh=IO::File->new("$pidfile");
  112:    my $pide=<$lfh>;
  113:    chomp($pide);
  114:    if (kill 0 => $pide) { die "already running"; }
  115: }
  116: 
  117: # ------------------------------------------------------------- Read hosts file
  118: $PREFORK=4; # number of children to maintain, at least four spare
  119: 
  120: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
  121: 
  122: while ($configline=<CONFIG>) {
  123:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
  124:     chomp($ip);
  125: 
  126:     $hostip{$ip}=$id;
  127:     if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
  128: 
  129:     $PREFORK++;
  130: }
  131: close(CONFIG);
  132: 
  133: $PREFORK=int($PREFORK/4);
  134: 
  135: $unixsock = "mysqlsock";
  136: my $localfile="$perlvar{'lonSockDir'}/$unixsock";
  137: my $server;
  138: unlink ($localfile);
  139: unless ($server=IO::Socket::UNIX->new(Local    =>"$localfile",
  140: 				  Type    => SOCK_STREAM,
  141: 				  Listen => 10))
  142: {
  143:     print "in socket error:$@\n";
  144: }
  145: 
  146: # -------------------------------------------------------- Routines for forking
  147: # global variables
  148: $MAX_CLIENTS_PER_CHILD  = 5;        # number of clients each child should process
  149: %children               = ();       # keys are current child process IDs
  150: $children               = 0;        # current number of children
  151: 
  152: sub REAPER {                        # takes care of dead children
  153:     $SIG{CHLD} = \&REAPER;
  154:     my $pid = wait;
  155:     $children --;
  156:     &logthis("Child $pid died");
  157:     delete $children{$pid};
  158: }
  159: 
  160: sub HUNTSMAN {                      # signal handler for SIGINT
  161:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  162:     kill 'INT' => keys %children;
  163:     my $execdir=$perlvar{'lonDaemons'};
  164:     unlink("$execdir/logs/lonsql.pid");
  165:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  166:     $unixsock = "mysqlsock";
  167:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  168:     unlink(port);
  169:     exit;                           # clean up with dignity
  170: }
  171: 
  172: sub HUPSMAN {                      # signal handler for SIGHUP
  173:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  174:     kill 'INT' => keys %children;
  175:     close($server);                # free up socket
  176:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  177:     my $execdir=$perlvar{'lonDaemons'};
  178:     $unixsock = "mysqlsock";
  179:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  180:     unlink(port);
  181:     exec("$execdir/lonsql");         # here we go again
  182: }
  183: 
  184: sub logthis {
  185:     my $message=shift;
  186:     my $execdir=$perlvar{'lonDaemons'};
  187:     my $fh=IO::File->new(">>$execdir/logs/lonsqlfinal.log");
  188:     my $now=time;
  189:     my $local=localtime($now);
  190:     print $fh "$local ($$): $message\n";
  191: }
  192: 
  193: # ------------------------------------------------------------------ Course log
  194: 
  195: sub courselog {
  196:     my ($path,$command)=@_;
  197:     my %filters=();
  198:     foreach (split(/\:/,&unescape($command))) {
  199: 	my ($name,$value)=split(/\=/,$_);
  200:         $filters{$name}=$value;
  201:     }
  202:     my @results=();
  203:     open(IN,$path.'/activity.log') or return ('file_error');
  204:     while ($line=<IN>) {
  205:         chomp($line);
  206:         my ($timestamp,$host,$log)=split(/\:/,$line);
  207:         foreach (split(/\&/,&unescape($log))) {
  208: 	    my ($time,$res,$uname,$udom,$action,$values)=split(/\:/,$_);
  209:             my $include=1;
  210:             if (($filters{'username'}) && ($uname ne $filters{'username'})) 
  211:                                                                { $include=0; }
  212:             if (($filters{'domain'}) && ($udom ne $filters{'domain'})) 
  213:                                                                { $include=0; }
  214:             if (($filters{'url'}) && ($res!~/$filters{'url'}/)) 
  215:                                                                { $include=0; }
  216:             if (($filters{'start'}) && ($time<$filters{'start'})) 
  217:                                                                { $include=0; }
  218:             if (($filters{'end'}) && ($time>$filters{'end'})) 
  219:                                                                { $include=0; }
  220:             if (($filters{'action'} eq 'view') && ($action)) 
  221:                                                                { $include=0; }
  222:             if (($filters{'action'} eq 'submit') && ($action ne 'POST')) 
  223:                                                                { $include=0; }
  224:             if (($filters{'action'} eq 'grade') && ($action ne 'CSTORE')) 
  225:                                                                { $include=0; }
  226:             if ($include) {
  227: 	       push(@results,$time.':'.$res.':'.$uname.':'.$udom.':'.
  228:                                             $action.':'.$values);
  229:             }
  230:        }
  231:     }
  232:     close IN;
  233:     return join('&',sort(@results));
  234: }
  235: 
  236: # -------------------------------------------------------------------- User log
  237: 
  238: sub userlog {
  239:     my ($path,$command)=@_;
  240:     my %filters=();
  241:     foreach (split(/\:/,&unescape($command))) {
  242: 	my ($name,$value)=split(/\=/,$_);
  243:         $filters{$name}=$value;
  244:     }
  245:     my @results=();
  246:     open(IN,$path.'/activity.log') or return ('file_error');
  247:     while ($line=<IN>) {
  248:         chomp($line);
  249:         my ($timestamp,$host,$log)=split(/\:/,$line);
  250:         $log=&unescape($log);
  251:         my $include=1;
  252:         if (($filters{'action'} eq 'log') && ($log!~/^Log/)) { $include=0; }
  253:         if ($include) {
  254: 	   push(@results,$timestamp.':'.$log);
  255:         }
  256:     }
  257:     close IN;
  258:     return join('&',sort(@results));
  259: }
  260: 
  261: 
  262: # ---------------------------------------------------- Fork once and dissociate
  263: $fpid=fork;
  264: exit if $fpid;
  265: die "Couldn't fork: $!" unless defined ($fpid);
  266: 
  267: POSIX::setsid() or die "Can't start new session: $!";
  268: 
  269: # ------------------------------------------------------- Write our PID on disk
  270: 
  271: $execdir=$perlvar{'lonDaemons'};
  272: open (PIDSAVE,">$execdir/logs/lonsql.pid");
  273: print PIDSAVE "$$\n";
  274: close(PIDSAVE);
  275: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  276: 
  277: # ----------------------------- Ignore signals generated during initial startup
  278: $SIG{HUP}=$SIG{USR1}='IGNORE';
  279: # ------------------------------------------------------- Now we are on our own    
  280: # Fork off our children.
  281: for (1 .. $PREFORK) {
  282:     make_new_child();
  283: }
  284: 
  285: # Install signal handlers.
  286: $SIG{CHLD} = \&REAPER;
  287: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  288: $SIG{HUP}  = \&HUPSMAN;
  289: 
  290: # And maintain the population.
  291: while (1) {
  292:     sleep;                          # wait for a signal (i.e., child's death)
  293:     for ($i = $children; $i < $PREFORK; $i++) {
  294:         make_new_child();           # top up the child pool
  295:     }
  296: }
  297: 
  298: 
  299: sub make_new_child {
  300:     my $pid;
  301:     my $sigset;
  302:     
  303:     # block signal for fork
  304:     $sigset = POSIX::SigSet->new(SIGINT);
  305:     sigprocmask(SIG_BLOCK, $sigset)
  306:         or die "Can't block SIGINT for fork: $!\n";
  307:     
  308:     die "fork: $!" unless defined ($pid = fork);
  309:     
  310:     if ($pid) {
  311:         # Parent records the child's birth and returns.
  312:         sigprocmask(SIG_UNBLOCK, $sigset)
  313:             or die "Can't unblock SIGINT for fork: $!\n";
  314:         $children{$pid} = 1;
  315:         $children++;
  316:         return;
  317:     } else {
  318:         # Child can *not* return from this subroutine.
  319:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  320:     
  321:         # unblock signals
  322:         sigprocmask(SIG_UNBLOCK, $sigset)
  323:             or die "Can't unblock SIGINT for fork: $!\n";
  324: 	
  325: 	
  326:         #open database handle
  327: 	# making dbh global to avoid garbage collector
  328: 	unless (
  329: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  330: 		) { 
  331:   	            sleep(10+int(rand(20)));
  332: 		    &logthis("<font color=blue>WARNING: Couldn't connect to database  ($st secs): $@</font>");
  333: 		    print "database handle error\n";
  334: 		    exit;
  335: 
  336: 	  };
  337: 	# make sure that a database disconnection occurs with ending kill signals
  338: 	$SIG{TERM}=$SIG{INT}=$SIG{QUIT}=$SIG{__DIE__}=\&DISCONNECT;
  339: 
  340:         # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
  341:         for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  342:             $client = $server->accept()     or last;
  343:             
  344:             # do something with the connection
  345: 	    $run = $run+1;
  346: 	    my $userinput = <$client>;
  347: 	    chomp($userinput);
  348: 	    	    
  349: 	    my ($conserver,$query,
  350: 		$arg1,$arg2,$arg3)=split(/&/,$userinput);
  351: 	    my $query=unescape($query);
  352: 
  353:             #send query id which is pid_unixdatetime_runningcounter
  354: 	    $queryid = $thisserver;
  355: 	    $queryid .="_".($$)."_";
  356: 	    $queryid .= time."_";
  357: 	    $queryid .= $run;
  358: 	    print $client "$queryid\n";
  359: 	    
  360: 	    &logthis("QUERY: $query - $arg1 - $arg2 - $arg3");
  361: 	    sleep 1;
  362: 
  363:             my $result='';
  364: 
  365: # ---------- At this point, query is received, query-ID assigned and sent back 
  366: # $query eq 'logquery' will mean that this is a query against log-files
  367: 
  368: 
  369: 	   if (($query eq 'userlog') || ($query eq 'courselog')) {
  370: # ----------------------------------------------------- beginning of log query
  371: #
  372: # this goes against a user's log file
  373: #
  374: 	       my $udom=&unescape($arg1);
  375: 	       my $uname=&unescape($arg2);
  376:                my $command=&unescape($arg3);
  377:                my $path=&propath($udom,$uname);
  378:                if (-e "$path/activity.log") {
  379: 		   if ($query eq 'userlog') {
  380:                        $result=&userlog($path,$command);
  381:                    } else {
  382:                        $result=&courselog($path,$command);
  383:                    }
  384:                } else {
  385: 		   &logthis('Unable to do log query: '.$uname.'@'.$udom);
  386: 	           $result='no_such_file';
  387: 	       }
  388: # ------------------------------------------------------------ end of log query
  389:           } else {
  390: # -------------------------------------------------------- This is an sql query
  391: 	    my $custom=unescape($arg1);
  392: 	    my $customshow=unescape($arg2);
  393:             #prepare and execute the query
  394: 	    my $sth = $dbh->prepare($query);
  395: 
  396: 	    my @files;
  397: 	    my $subsetflag=0;
  398: 	    if ($query) {
  399: 		unless ($sth->execute())
  400: 		{
  401: 		    &logthis("<font color=blue>WARNING: Could not retrieve from database: $@</font>");
  402: 		    $result="";
  403: 		}
  404: 		else {
  405: 		    my $r1=$sth->fetchall_arrayref;
  406: 		    my @r2;
  407: 		    foreach (@$r1) {my $a=$_; 
  408: 			 my @b=map {escape($_)} @$a;
  409: 			 push @files,@{$a}[3];
  410: 			 push @r2,join(",", @b)
  411: 			 }
  412: 		    $result=join("&",@r2);
  413: 		}
  414: 	    }
  415: 	    # do custom metadata searching here and build into result
  416: 	    if ($custom or $customshow) {
  417: 		&logthis("am going to do custom query for $custom");
  418: 		if ($query) {
  419: 		    @metalist=map {$perlvar{'lonDocRoot'}.$_.'.meta'} @files;
  420: 		}
  421: 		else {
  422: 		    @metalist=(); pop @metalist;
  423: 		    opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}");
  424: 		    my @homeusers=grep
  425: 		          {&ishome("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$_")}
  426: 		          grep {!/^\.\.?$/} readdir(RESOURCES);
  427: 		    closedir RESOURCES;
  428: 		    foreach my $user (@homeusers) {
  429: 			&find("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$user");
  430: 		    }
  431: 		}
  432: #		&logthis("FILELIST:" . join(":::",@metalist));
  433: 		# if file is indicated in sql database and
  434: 		# not part of sql-relevant query, do not pattern match.
  435: 		# if file is not in sql database, output error.
  436: 		# if file is indicated in sql database and is
  437: 		# part of query result list, then do the pattern match.
  438: 		my $customresult='';
  439: 		my @r2;
  440: 		foreach my $m (@metalist) {
  441: 		    my $fh=IO::File->new($m);
  442: 		    my @lines=<$fh>;
  443: 		    my $stuff=join('',@lines);
  444: 		    if ($stuff=~/$custom/s) {
  445: 			foreach my $f ('abstract','author','copyright',
  446: 				       'creationdate','keywords','language',
  447: 				       'lastrevisiondate','mime','notes',
  448: 				       'owner','subject','title') {
  449: 			    $stuff=~s/\n?\<$f[^\>]*\>.*?<\/$f[^\>]*\>\n?//s;
  450: 			}
  451: 			my $m2=$m; my $docroot=$perlvar{'lonDocRoot'};
  452: 			$m2=~s/^$docroot//;
  453: 			$m2=~s/\.meta$//;
  454: 			unless ($query) {
  455: 			    my $q2="select * from metadata where url like binary '$m2'";
  456: 			    my $sth = $dbh->prepare($q2);
  457: 			    $sth->execute();
  458: 			    my $r1=$sth->fetchall_arrayref;
  459: 			    foreach (@$r1) {my $a=$_; 
  460: 				 my @b=map {escape($_)} @$a;
  461: 				 push @files,@{$a}[3];
  462: 				 push @r2,join(",", @b)
  463: 				 }
  464: 			}
  465: #			&logthis("found: $stuff");
  466: 			$customresult.='&custom='.escape($m2).','.escape($stuff);
  467: 		    }
  468: 		}
  469: 		$result=join("&",@r2) unless $query;
  470: 		$result.=$customresult;
  471: 	    }
  472: # ------------------------------------------------------------ end of sql query
  473: 	   }
  474: 
  475:             # result does need to be escaped
  476: 
  477:             $result=&escape($result);
  478: 
  479: 	    # reply with result, append \n unless already there
  480: 
  481: 	    $result.="\n" unless ($result=~/\n$/);
  482:             &reply("queryreply:$queryid:$result",$conserver);
  483: 
  484:         }
  485:     
  486:         # tidy up gracefully and finish
  487: 	
  488:         #close the database handle
  489: 	$dbh->disconnect
  490: 	   or &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  491:     
  492:         # this exit is VERY important, otherwise the child will become
  493:         # a producer of more and more children, forking yourself into
  494:         # process death.
  495:         exit;
  496:     }
  497: }
  498: 
  499: sub DISCONNECT {
  500:     $dbh->disconnect or 
  501:     &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  502:     exit;
  503: }
  504: 
  505: # -------------------------------------------------- Non-critical communication
  506: 
  507: sub subreply {
  508:     my ($cmd,$server)=@_;
  509:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  510:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  511:                                       Type    => SOCK_STREAM,
  512:                                       Timeout => 10)
  513:        or return "con_lost";
  514:     print $sclient "$cmd\n";
  515:     my $answer=<$sclient>;
  516:     chomp($answer);
  517:     if (!$answer) { $answer="con_lost"; }
  518:     return $answer;
  519: }
  520: 
  521: sub reply {
  522:   my ($cmd,$server)=@_;
  523:   my $answer;
  524:   if ($server ne $perlvar{'lonHostID'}) { 
  525:     $answer=subreply($cmd,$server);
  526:     if ($answer eq 'con_lost') {
  527: 	$answer=subreply("ping",$server);
  528:         $answer=subreply($cmd,$server);
  529:     }
  530:   } else {
  531:     $answer='self_reply';
  532:     $answer=subreply($cmd,$server);
  533:   } 
  534:   return $answer;
  535: }
  536: 
  537: # -------------------------------------------------------- Escape Special Chars
  538: 
  539: sub escape {
  540:     my $str=shift;
  541:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  542:     return $str;
  543: }
  544: 
  545: # ----------------------------------------------------- Un-Escape Special Chars
  546: 
  547: sub unescape {
  548:     my $str=shift;
  549:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  550:     return $str;
  551: }
  552: 
  553: # --------------------------------------- Is this the home server of an author?
  554: # (copied from lond, modification of the return value)
  555: sub ishome {
  556:     my $author=shift;
  557:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  558:     my ($udom,$uname)=split(/\//,$author);
  559:     my $proname=propath($udom,$uname);
  560:     if (-e $proname) {
  561: 	return 1;
  562:     } else {
  563:         return 0;
  564:     }
  565: }
  566: 
  567: # -------------------------------------------- Return path to profile directory
  568: # (copied from lond)
  569: sub propath {
  570:     my ($udom,$uname)=@_;
  571:     $udom=~s/\W//g;
  572:     $uname=~s/\W//g;
  573:     my $subdir=$uname.'__';
  574:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  575:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
  576:     return $proname;
  577: } 
  578: 
  579: # ----------------------------------- POD (plain old documentation, CPAN style)
  580: 
  581: =head1 NAME
  582: 
  583: lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
  584: 
  585: =head1 SYNOPSIS
  586: 
  587: This script should be run as user=www.  The following is an example invocation
  588: from the loncron script.  Note that a lonsql.pid file contains the pid of
  589: the parent process.
  590: 
  591:     if (-e $lonsqlfile) {
  592: 	my $lfh=IO::File->new("$lonsqlfile");
  593: 	my $lonsqlpid=<$lfh>;
  594: 	chomp($lonsqlpid);
  595: 	if (kill 0 => $lonsqlpid) {
  596: 	    print $fh "<h3>lonsql at pid $lonsqlpid responding</h3>";
  597: 	    $restartflag=0;
  598: 	} else {
  599: 	    $errors++; $errors++;
  600: 	    print $fh "<h3>lonsql at pid $lonsqlpid not responding</h3>";
  601: 		$restartflag=1;
  602: 	print $fh 
  603: 	    "<h3>Decided to clean up stale .pid file and restart lonsql</h3>";
  604: 	}
  605:     }
  606:     if ($restartflag==1) {
  607: 	$errors++;
  608: 	         print $fh '<br><font color="red">Killall lonsql: '.
  609:                     system('killall lonsql').' - ';
  610:                     sleep 60;
  611:                     print $fh unlink($lonsqlfile).' - '.
  612:                               system('killall -9 lonsql').
  613:                     '</font><br>';
  614: 	print $fh "<h3>lonsql not running, trying to start</h3>";
  615: 	system(
  616:  "$perlvar{'lonDaemons'}/lonsql 2>>$perlvar{'lonDaemons'}/logs/lonsql_errors");
  617: 	sleep 10;
  618: 
  619: =head1 DESCRIPTION
  620: 
  621: Not yet written.
  622: 
  623: =head1 README
  624: 
  625: Not yet written.
  626: 
  627: =head1 PREREQUISITES
  628: 
  629: IO::Socket
  630: Symbol
  631: POSIX
  632: IO::Select
  633: IO::File
  634: Socket
  635: Fcntl
  636: Tie::RefHash
  637: DBI
  638: 
  639: =head1 COREQUISITES
  640: 
  641: =head1 OSNAMES
  642: 
  643: linux
  644: 
  645: =head1 SCRIPT CATEGORIES
  646: 
  647: Server/Process
  648: 
  649: =cut

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