Annotation of loncom/lonsql, revision 1.28

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

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