Annotation of loncom/lonsql, revision 1.30

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

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