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