File:  [LON-CAPA] / loncom / lonsql
Revision 1.34: download - view: text, annotated - select for diffs
Mon Apr 16 18:51:22 2001 UTC (23 years ago) by harris41
Branches: MAIN
CVS tags: HEAD
only looking at current versions for home user resources .. adding
in this filter -Scott

    1: #!/usr/bin/perl
    2: # lonsql-based on the preforker:harsha jagasia:date:5/10/00
    3: # 7/25 Gerd Kortemeyer
    4: # many different dates Scott Harrison
    5: # 03/22/2001 Scott Harrison
    6: use IO::Socket;
    7: use Symbol;
    8: use POSIX;
    9: use IO::Select;
   10: use IO::File;
   11: use Socket;
   12: use Fcntl;
   13: use Tie::RefHash;
   14: use DBI;
   15: 
   16: my @metalist;
   17: # ----------------- Code to enable 'find' subroutine listing of the .meta files
   18: require "find.pl";
   19: sub wanted {
   20:     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
   21:     -f _ &&
   22:     /^.*\.meta$/ && !/^.+\.\d+\.[^\.]+\.meta$/ &&
   23:     push(@metalist,"$dir/$_");
   24: }
   25: 
   26: $childmaxattempts=10;
   27: $run =0;#running counter to generate the query-id
   28: 
   29: # ------------------------------------ Read httpd access.conf and get variables
   30: open (CONFIG,"/etc/httpd/conf/access.conf") || die "Can't read access.conf";
   31: 
   32: while ($configline=<CONFIG>) {
   33:     if ($configline =~ /PerlSetVar/) {
   34: 	my ($dummy,$varname,$varvalue)=split(/\s+/,$configline);
   35:         chomp($varvalue);
   36:         $perlvar{$varname}=$varvalue;
   37:     }
   38: }
   39: close(CONFIG);
   40: 
   41: # ------------------------------------- Make sure that database can be accessed
   42: {
   43:     my $dbh;
   44:     unless (
   45: 	    $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
   46: 	    ) { 
   47: 	print "Cannot connect to database!\n";
   48: 	exit;
   49:     }
   50:     else {
   51: 	$dbh->disconnect;
   52:     }
   53: }
   54: 
   55: # --------------------------------------------- Check if other instance running
   56: 
   57: my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid";
   58: 
   59: if (-e $pidfile) {
   60:    my $lfh=IO::File->new("$pidfile");
   61:    my $pide=<$lfh>;
   62:    chomp($pide);
   63:    if (kill 0 => $pide) { die "already running"; }
   64: }
   65: 
   66: # ------------------------------------------------------------- Read hosts file
   67: $PREFORK=4; # number of children to maintain, at least four spare
   68: 
   69: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
   70: 
   71: while ($configline=<CONFIG>) {
   72:     my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
   73:     chomp($ip);
   74: 
   75:     $hostip{$ip}=$id;
   76:     if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
   77: 
   78:     $PREFORK++;
   79: }
   80: close(CONFIG);
   81: 
   82: $unixsock = "mysqlsock";
   83: my $localfile="$perlvar{'lonSockDir'}/$unixsock";
   84: my $server;
   85: unlink ($localfile);
   86: unless ($server=IO::Socket::UNIX->new(Local    =>"$localfile",
   87: 				  Type    => SOCK_STREAM,
   88: 				  Listen => 10))
   89: {
   90:     print "in socket error:$@\n";
   91: }
   92: 
   93: # -------------------------------------------------------- Routines for forking
   94: # global variables
   95: $MAX_CLIENTS_PER_CHILD  = 5;        # number of clients each child should process
   96: %children               = ();       # keys are current child process IDs
   97: $children               = 0;        # current number of children
   98: 
   99: sub REAPER {                        # takes care of dead children
  100:     $SIG{CHLD} = \&REAPER;
  101:     my $pid = wait;
  102:     $children --;
  103:     &logthis("Child $pid died");
  104:     delete $children{$pid};
  105: }
  106: 
  107: sub HUNTSMAN {                      # signal handler for SIGINT
  108:     local($SIG{CHLD}) = 'IGNORE';   # we're going to kill our children
  109:     kill 'INT' => keys %children;
  110:     my $execdir=$perlvar{'lonDaemons'};
  111:     unlink("$execdir/logs/lonsql.pid");
  112:     &logthis("<font color=red>CRITICAL: Shutting down</font>");
  113:     $unixsock = "mysqlsock";
  114:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  115:     unlink(port);
  116:     exit;                           # clean up with dignity
  117: }
  118: 
  119: sub HUPSMAN {                      # signal handler for SIGHUP
  120:     local($SIG{CHLD}) = 'IGNORE';  # we're going to kill our children
  121:     kill 'INT' => keys %children;
  122:     close($server);                # free up socket
  123:     &logthis("<font color=red>CRITICAL: Restarting</font>");
  124:     my $execdir=$perlvar{'lonDaemons'};
  125:     $unixsock = "mysqlsock";
  126:     my $port="$perlvar{'lonSockDir'}/$unixsock";
  127:     unlink(port);
  128:     exec("$execdir/lonsql");         # here we go again
  129: }
  130: 
  131: sub logthis {
  132:     my $message=shift;
  133:     my $execdir=$perlvar{'lonDaemons'};
  134:     my $fh=IO::File->new(">>$execdir/logs/lonsqlfinal.log");
  135:     my $now=time;
  136:     my $local=localtime($now);
  137:     print $fh "$local ($$): $message\n";
  138: }
  139: # ---------------------------------------------------- Fork once and dissociate
  140: $fpid=fork;
  141: exit if $fpid;
  142: die "Couldn't fork: $!" unless defined ($fpid);
  143: 
  144: POSIX::setsid() or die "Can't start new session: $!";
  145: 
  146: # ------------------------------------------------------- Write our PID on disk
  147: 
  148: $execdir=$perlvar{'lonDaemons'};
  149: open (PIDSAVE,">$execdir/logs/lonsql.pid");
  150: print PIDSAVE "$$\n";
  151: close(PIDSAVE);
  152: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
  153: 
  154: # ----------------------------- Ignore signals generated during initial startup
  155: $SIG{HUP}=$SIG{USR1}='IGNORE';
  156: # ------------------------------------------------------- Now we are on our own    
  157: # Fork off our children.
  158: for (1 .. $PREFORK) {
  159:     make_new_child();
  160: }
  161: 
  162: # Install signal handlers.
  163: $SIG{CHLD} = \&REAPER;
  164: $SIG{INT}  = $SIG{TERM} = \&HUNTSMAN;
  165: $SIG{HUP}  = \&HUPSMAN;
  166: 
  167: # And maintain the population.
  168: while (1) {
  169:     sleep;                          # wait for a signal (i.e., child's death)
  170:     for ($i = $children; $i < $PREFORK; $i++) {
  171:         make_new_child();           # top up the child pool
  172:     }
  173: }
  174: 
  175: 
  176: sub make_new_child {
  177:     my $pid;
  178:     my $sigset;
  179:     
  180:     # block signal for fork
  181:     $sigset = POSIX::SigSet->new(SIGINT);
  182:     sigprocmask(SIG_BLOCK, $sigset)
  183:         or die "Can't block SIGINT for fork: $!\n";
  184:     
  185:     die "fork: $!" unless defined ($pid = fork);
  186:     
  187:     if ($pid) {
  188:         # Parent records the child's birth and returns.
  189:         sigprocmask(SIG_UNBLOCK, $sigset)
  190:             or die "Can't unblock SIGINT for fork: $!\n";
  191:         $children{$pid} = 1;
  192:         $children++;
  193:         return;
  194:     } else {
  195:         # Child can *not* return from this subroutine.
  196:         $SIG{INT} = 'DEFAULT';      # make SIGINT kill us as it did before
  197:     
  198:         # unblock signals
  199:         sigprocmask(SIG_UNBLOCK, $sigset)
  200:             or die "Can't unblock SIGINT for fork: $!\n";
  201: 	
  202: 	
  203:         #open database handle
  204: 	# making dbh global to avoid garbage collector
  205: 	unless (
  206: 		$dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
  207: 		) { 
  208:   	            sleep(10+int(rand(20)));
  209: 		    &logthis("<font color=blue>WARNING: Couldn't connect to database  ($st secs): $@</font>");
  210: 		    print "database handle error\n";
  211: 		    exit;
  212: 
  213: 	  };
  214: 	# make sure that a database disconnection occurs with ending kill signals
  215: 	$SIG{TERM}=$SIG{INT}=$SIG{QUIT}=$SIG{__DIE__}=\&DISCONNECT;
  216: 
  217:         # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
  218:         for ($i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
  219:             $client = $server->accept()     or last;
  220:             
  221:             # do something with the connection
  222: 	    $run = $run+1;
  223: 	    my $userinput = <$client>;
  224: 	    chomp($userinput);
  225: 	    	    
  226: 	    my ($conserver,$querytmp,
  227: 		$customtmp,$customshowtmp)=split(/&/,$userinput);
  228: 	    my $query=unescape($querytmp);
  229: 	    my $custom=unescape($customtmp);
  230: 	    my $customshow=unescape($customshowtmp);
  231: 
  232:             #send query id which is pid_unixdatetime_runningcounter
  233: 	    $queryid = $thisserver;
  234: 	    $queryid .="_".($$)."_";
  235: 	    $queryid .= time."_";
  236: 	    $queryid .= $run;
  237: 	    print $client "$queryid\n";
  238: 	    
  239: 	    &logthis("QUERY: $query");
  240: 	    &logthis("QUERY: $query");
  241: 	    sleep 1;
  242:             #prepare and execute the query
  243: 	    my $sth = $dbh->prepare($query);
  244: 	    my $result;
  245: 	    my @files;
  246: 	    my $subsetflag=0;
  247: 	    if ($query) {
  248: 		unless ($sth->execute())
  249: 		{
  250: 		    &logthis("<font color=blue>WARNING: Could not retrieve from database: $@</font>");
  251: 		    $result="";
  252: 		}
  253: 		else {
  254: 		    my $r1=$sth->fetchall_arrayref;
  255: 		    my @r2;
  256: 		    map {my $a=$_; 
  257: 			 my @b=map {escape($_)} @$a;
  258: 			 push @files,@{$a}[3];
  259: 			 push @r2,join(",", @b)
  260: 			 } (@$r1);
  261: 		    $result=join("&",@r2);
  262: 		}
  263: 	    }
  264: 	    # do custom metadata searching here and build into result
  265: 	    if ($custom or $customshow) {
  266: 		&logthis("am going to do custom query for $custom");
  267: 		if ($query) {
  268: 		    @metalist=map {$perlvar{'lonDocRoot'}.$_.'.meta'} @files;
  269: 		}
  270: 		else {
  271: 		    @metalist=(); pop @metalist;
  272: 		    opendir(RESOURCES,"$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}");
  273: 		    my @homeusers=grep
  274: 		          {&ishome("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$_")}
  275: 		          grep {!/^\.\.?$/} readdir(RESOURCES);
  276: 		    closedir RESOURCES;
  277: 		    foreach my $user (@homeusers) {
  278: 			&find("$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}/$user");
  279: 		    }
  280: 		}
  281: #		&logthis("FILELIST:" . join(":::",@metalist));
  282: 		# if file is indicated in sql database and
  283: 		# not part of sql-relevant query, do not pattern match.
  284: 		# if file is not in sql database, output error.
  285: 		# if file is indicated in sql database and is
  286: 		# part of query result list, then do the pattern match.
  287: 		my $customresult='';
  288: 		my @r2;
  289: 		foreach my $m (@metalist) {
  290: 		    my $fh=IO::File->new($m);
  291: 		    my @lines=<$fh>;
  292: 		    my $stuff=join('',@lines);
  293: 		    if ($stuff=~/$custom/s) {
  294: 			foreach my $f ('abstract','author','copyright',
  295: 				       'creationdate','keywords','language',
  296: 				       'lastrevisiondate','mime','notes',
  297: 				       'owner','subject','title') {
  298: 			    $stuff=~s/\n?\<$f[^\>]*\>.*?<\/$f[^\>]*\>\n?//;
  299: 			}
  300: 			my $m2=$m; my $docroot=$perlvar{'lonDocRoot'};
  301: 			$m2=~s/^$docroot//;
  302: 			$m2=~s/\.meta$//;
  303: 			unless ($query) {
  304: 			    my $q2="select * from metadata where url like '$m2'";
  305: 			    my $sth = $dbh->prepare($q2);
  306: 			    $sth->execute();
  307: 			    my $r1=$sth->fetchall_arrayref;
  308: 			    map {my $a=$_; 
  309: 				 my @b=map {escape($_)} @$a;
  310: 				 push @files,@{$a}[3];
  311: 				 push @r2,join(",", @b)
  312: 				 } (@$r1);
  313: 			}
  314: #			&logthis("found: $stuff");
  315: 			$customresult.='&custom='.escape($m2).','.escape($stuff);
  316: 		    }
  317: 		}
  318: 		$result=join("&",@r2) unless $query;
  319: 		$result.=$customresult;
  320: 	    }
  321: 	    # reply with result
  322: 	    $result.="\n" if $result;
  323:             &reply("queryreply:$queryid:$result",$conserver);
  324: 
  325:         }
  326:     
  327:         # tidy up gracefully and finish
  328: 	
  329:         #close the database handle
  330: 	$dbh->disconnect
  331: 	   or &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  332:     
  333:         # this exit is VERY important, otherwise the child will become
  334:         # a producer of more and more children, forking yourself into
  335:         # process death.
  336:         exit;
  337:     }
  338: }
  339: 
  340: sub DISCONNECT {
  341:     $dbh->disconnect or 
  342:     &logthis("<font color=blue>WARNING: Couldn't disconnect from database  $DBI::errstr ($st secs): $@</font>");
  343:     exit;
  344: }
  345: 
  346: # -------------------------------------------------- Non-critical communication
  347: 
  348: sub subreply {
  349:     my ($cmd,$server)=@_;
  350:     my $peerfile="$perlvar{'lonSockDir'}/$server";
  351:     my $sclient=IO::Socket::UNIX->new(Peer    =>"$peerfile",
  352:                                       Type    => SOCK_STREAM,
  353:                                       Timeout => 10)
  354:        or return "con_lost";
  355:     print $sclient "$cmd\n";
  356:     my $answer=<$sclient>;
  357:     chomp($answer);
  358:     if (!$answer) { $answer="con_lost"; }
  359:     return $answer;
  360: }
  361: 
  362: sub reply {
  363:   my ($cmd,$server)=@_;
  364:   my $answer;
  365:   if ($server ne $perlvar{'lonHostID'}) { 
  366:     $answer=subreply($cmd,$server);
  367:     if ($answer eq 'con_lost') {
  368: 	$answer=subreply("ping",$server);
  369:         $answer=subreply($cmd,$server);
  370:     }
  371:   } else {
  372:     $answer='self_reply';
  373:     $answer=subreply($cmd,$server);
  374:   } 
  375:   return $answer;
  376: }
  377: 
  378: # -------------------------------------------------------- Escape Special Chars
  379: 
  380: sub escape {
  381:     my $str=shift;
  382:     $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
  383:     return $str;
  384: }
  385: 
  386: # ----------------------------------------------------- Un-Escape Special Chars
  387: 
  388: sub unescape {
  389:     my $str=shift;
  390:     $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
  391:     return $str;
  392: }
  393: 
  394: # --------------------------------------- Is this the home server of an author?
  395: # (copied from lond, modification of the return value)
  396: sub ishome {
  397:     my $author=shift;
  398:     $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
  399:     my ($udom,$uname)=split(/\//,$author);
  400:     my $proname=propath($udom,$uname);
  401:     if (-e $proname) {
  402: 	return 1;
  403:     } else {
  404:         return 0;
  405:     }
  406: }
  407: 
  408: # -------------------------------------------- Return path to profile directory
  409: # (copied from lond)
  410: sub propath {
  411:     my ($udom,$uname)=@_;
  412:     $udom=~s/\W//g;
  413:     $uname=~s/\W//g;
  414:     my $subdir=$uname.'__';
  415:     $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
  416:     my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
  417:     return $proname;
  418: } 

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