1: #!/usr/bin/perl
2:
3: # The LearningOnline Network
4: # lonsql - LON TCP-MySQL-Server
5: #
6: # YEAR=2000
7: # lonsql-based on the preforker:harsha jagasia:date:5/10/00
8: # 7/25 Gerd Kortemeyer
9: # many different dates Scott Harrison
10: # YEAR=2001
11: # many different dates Scott Harrison
12: # 03/22/2001 Scott Harrison
13: # 8/30 Gerd Kortemeyer
14: # 10/17,11/28,11/29 Scott Harrison
15: #
16: # $Id: lonsql,v 1.39 2001/11/29 13:53:56 harris41 Exp $
17: ###
18:
19:
20: use IO::Socket;
21: use Symbol;
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:
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 _ &&
36: /^.*\.meta$/ && !/^.+\.\d+\.[^\.]+\.meta$/ &&
37: push(@metalist,"$dir/$_");
38: }
39:
40: $childmaxattempts=10;
41: $run =0;#running counter to generate the query-id
42:
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);
54:
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";
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;
67: }
68: else {
69: $dbh->disconnect;
70: }
71: }
72:
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: }
83:
84: # ------------------------------------------------------------- Read hosts file
85: $PREFORK=4; # number of children to maintain, at least four spare
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:
93: $hostip{$ip}=$id;
94: if ($id eq $perlvar{'lonHostID'}) { $thisserver=$name; }
95:
96: $PREFORK++;
97: }
98: close(CONFIG);
99:
100: $PREFORK=int($PREFORK/4);
101:
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: }
112:
113: # -------------------------------------------------------- Routines for forking
114: # global variables
115: $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should process
116: %children = (); # keys are current child process IDs
117: $children = 0; # current number of children
118:
119: sub REAPER { # takes care of dead children
120: $SIG{CHLD} = \&REAPER;
121: my $pid = wait;
122: $children --;
123: &logthis("Child $pid died");
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>");
133: $unixsock = "mysqlsock";
134: my $port="$perlvar{'lonSockDir'}/$unixsock";
135: unlink(port);
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'};
145: $unixsock = "mysqlsock";
146: my $port="$perlvar{'lonSockDir'}/$unixsock";
147: unlink(port);
148: exec("$execdir/lonsql"); # here we go again
149: }
150:
151: sub logthis {
152: my $message=shift;
153: my $execdir=$perlvar{'lonDaemons'};
154: my $fh=IO::File->new(">>$execdir/logs/lonsqlfinal.log");
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';
176: # ------------------------------------------------------- Now we are on our own
177: # Fork off our children.
178: for (1 .. $PREFORK) {
179: make_new_child();
180: }
181:
182: # Install signal handlers.
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)
190: for ($i = $children; $i < $PREFORK; $i++) {
191: make_new_child(); # top up the child pool
192: }
193: }
194:
195:
196: sub make_new_child {
197: my $pid;
198: my $sigset;
199:
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:
205: die "fork: $!" unless defined ($pid = fork);
206:
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 {
215: # Child can *not* return from this subroutine.
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";
221:
222:
223: #open database handle
224: # making dbh global to avoid garbage collector
225: unless (
226: $dbh = DBI->connect("DBI:mysql:loncapa","www",$perlvar{'lonSqlAccess'},{ RaiseError =>0,PrintError=>0})
227: ) {
228: sleep(10+int(rand(20)));
229: &logthis("<font color=blue>WARNING: Couldn't connect to database ($st secs): $@</font>");
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:
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;
240:
241: # do something with the connection
242: $run = $run+1;
243: my $userinput = <$client>;
244: chomp($userinput);
245:
246: my ($conserver,$querytmp,
247: $customtmp,$customshowtmp)=split(/&/,$userinput);
248: my $query=unescape($querytmp);
249: my $custom=unescape($customtmp);
250: my $customshow=unescape($customshowtmp);
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:
259: &logthis("QUERY: $query");
260: &logthis("QUERY: $query");
261: sleep 1;
262: #prepare and execute the query
263: my $sth = $dbh->prepare($query);
264: my $result;
265: my @files;
266: my $subsetflag=0;
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: }
283: }
284: # do custom metadata searching here and build into result
285: if ($custom or $customshow) {
286: &logthis("am going to do custom query for $custom");
287: if ($query) {
288: @metalist=map {$perlvar{'lonDocRoot'}.$_.'.meta'} @files;
289: }
290: else {
291: @metalist=(); pop @metalist;
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: }
300: }
301: # &logthis("FILELIST:" . join(":::",@metalist));
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.
307: my $customresult='';
308: my @r2;
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) {
314: foreach my $f ('abstract','author','copyright',
315: 'creationdate','keywords','language',
316: 'lastrevisiondate','mime','notes',
317: 'owner','subject','title') {
318: $stuff=~s/\n?\<$f[^\>]*\>.*?<\/$f[^\>]*\>\n?//s;
319: }
320: my $m2=$m; my $docroot=$perlvar{'lonDocRoot'};
321: $m2=~s/^$docroot//;
322: $m2=~s/\.meta$//;
323: unless ($query) {
324: my $q2="select * from metadata where url like binary '$m2'";
325: my $sth = $dbh->prepare($q2);
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: }
334: # &logthis("found: $stuff");
335: $customresult.='&custom='.escape($m2).','.escape($stuff);
336: }
337: }
338: $result=join("&",@r2) unless $query;
339: $result.=$customresult;
340: }
341: # reply with result
342: $result.="\n" if $result;
343: &reply("queryreply:$queryid:$result",$conserver);
344:
345: }
346:
347: # tidy up gracefully and finish
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>");
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: }
358: }
359:
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: }
365:
366: # -------------------------------------------------- Non-critical communication
367:
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: }
381:
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';
393: $answer=subreply($cmd,$server);
394: }
395: return $answer;
396: }
397:
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: }
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>