Annotation of loncom/lonsql, revision 1.38

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

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