Annotation of loncom/lonsql, revision 1.56
1.1 harris41 1: #!/usr/bin/perl
1.39 harris41 2:
3: # The LearningOnline Network
1.40 harris41 4: # lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
1.39 harris41 5: #
1.56 ! bowersj2 6: # $Id: lonsql,v 1.55 2003/02/03 13:43:38 albertel Exp $
1.41 harris41 7: #
8: # Copyright Michigan State University Board of Trustees
9: #
10: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
11: #
12: # LON-CAPA is free software; you can redistribute it and/or modify
13: # it under the terms of the GNU General Public License as published by
14: # the Free Software Foundation; either version 2 of the License, or
15: # (at your option) any later version.
16: #
17: # LON-CAPA is distributed in the hope that it will be useful,
18: # but WITHOUT ANY WARRANTY; without even the implied warranty of
19: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20: # GNU General Public License for more details.
21: #
22: # You should have received a copy of the GNU General Public License
23: # along with LON-CAPA; if not, write to the Free Software
24: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25: #
26: # /home/httpd/html/adm/gpl.txt
27: #
28: # http://www.lon-capa.org/
29: #
1.51 matthew 30:
31: =pod
32:
33: =head1 NAME
34:
35: lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
36:
37: =head1 SYNOPSIS
38:
39: This script should be run as user=www.
40: Note that a lonsql.pid file contains the pid of the parent process.
41:
1.56 ! bowersj2 42: =head1 OVERVIEW
1.51 matthew 43:
1.56 ! bowersj2 44: The SQL database in LON-CAPA is used for catalog searches against
! 45: resource metadata only. The authoritative version of the resource
! 46: metadata is an XML-file on the normal file system (same file name as
! 47: resource plus ".meta"). The SQL-database is a cache of these files,
! 48: and can be reconstructed from the XML files at any time.
! 49:
! 50: The current database is implemented assuming a non-adjustable
! 51: architecture involving these data fields (specific to each version of
! 52: a resource).
! 53:
! 54: =over 4
! 55:
! 56: =item * title
! 57:
! 58: =item * author
! 59:
! 60: =item * subject
! 61:
! 62: =item * notes
! 63:
! 64: =item * abstract
! 65:
! 66: =item * mime
! 67:
! 68: =item * language
! 69:
! 70: =item * creationdate
! 71:
! 72: =item * lastrevisiondate
! 73:
! 74: =item * owner
! 75:
! 76: =item * copyright
! 77:
! 78: =back
! 79:
! 80: =head2 Purpose within LON-CAPA
! 81:
! 82: LON-CAPA is meant to distribute A LOT of educational content to A LOT
! 83: of people. It is ineffective to directly rely on contents within the
! 84: ext2 filesystem to be speedily scanned for on-the-fly searches of
! 85: content descriptions. (Simply put, it takes a cumbersome amount of
! 86: time to open, read, analyze, and close thousands of files.)
! 87:
! 88: The solution is to index various data fields that are descriptive of
! 89: the educational resources on a LON-CAPA server machine in a
! 90: database. Descriptive data fields are referred to as "metadata". The
! 91: question then arises as to how this metadata is handled in terms of
! 92: the rest of the LON-CAPA network without burdening client and daemon
! 93: processes.
! 94:
! 95: The obvious solution, using lonc to send a query to a lond process,
! 96: doesn't work so well in general as you can see in the following
! 97: example:
! 98:
! 99: lonc= loncapa client process A-lonc= a lonc process on Server A
! 100: lond= loncapa daemon process
! 101:
! 102: database command
! 103: A-lonc --------TCP/IP----------------> B-lond
! 104:
! 105: The problem emerges that A-lonc and B-lond are kept waiting for the
! 106: MySQL server to "do its stuff", or in other words, perform the
! 107: conceivably sophisticated, data-intensive, time-sucking database
! 108: transaction. By tying up a lonc and lond process, this significantly
! 109: cripples the capabilities of LON-CAPA servers.
! 110:
! 111: The solution is to offload the work onto another process, and use
! 112: lonc and lond just for requests and notifications of completed
! 113: processing:
! 114:
! 115: database command
! 116:
! 117: A-lonc ---------TCP/IP-----------------> B-lond =====> B-lonsql
! 118: <---------------------------------/ |
! 119: "ok, I'll get back to you..." |
! 120: |
! 121: /
! 122: A-lond <------------------------------- B-lonc <======
! 123: "Guess what? I have the result!"
! 124:
! 125: Of course, depending on success or failure, the messages may vary, but
! 126: the principle remains the same where a separate pool of children
! 127: processes (lonsql's) handle the MySQL database manipulations.
! 128:
! 129: Thus, lonc and lond spend effectively no time waiting on results from
! 130: the database.
1.51 matthew 131:
132: =head1 Internals
133:
134: =over 4
135:
136: =cut
137:
138: use strict;
1.36 www 139:
1.42 harris41 140: use lib '/home/httpd/lib/perl/';
141: use LONCAPA::Configuration;
142:
1.2 harris41 143: use IO::Socket;
144: use Symbol;
1.1 harris41 145: use POSIX;
146: use IO::Select;
147: use IO::File;
148: use Socket;
149: use Fcntl;
150: use Tie::RefHash;
151: use DBI;
1.51 matthew 152: use File::Find;
153:
154: ########################################################
155: ########################################################
156:
157: =pod
158:
159: =item Global Variables
160:
161: =over 4
162:
163: =item dbh
164:
165: =back
166:
167: =cut
168:
169: ########################################################
170: ########################################################
171: my $dbh;
172:
173: ########################################################
174: ########################################################
175:
176: =pod
177:
178: =item Variables required for forking
1.1 harris41 179:
1.51 matthew 180: =over 4
181:
182: =item $MAX_CLIENTS_PER_CHILD
183:
184: The number of clients each child should process.
185:
186: =item %children
187:
188: The keys to %children are the current child process IDs
189:
190: =item $children
191:
192: The current number of children
193:
194: =back
195:
196: =cut
1.9 harris41 197:
1.51 matthew 198: ########################################################
199: ########################################################
200: my $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child should process
201: my %children = (); # keys are current child process IDs
202: my $children = 0; # current number of children
203:
204: ###################################################################
205: ###################################################################
206:
207: =pod
208:
209: =item Main body of code.
210:
211: =over 4
1.45 www 212:
1.51 matthew 213: =item Read data from loncapa_apache.conf and loncapa.conf.
214:
215: =item Ensure we can access the database.
216:
217: =item Determine if there are other instances of lonsql running.
218:
219: =item Read the hosts file.
220:
221: =item Create a socket for lonsql.
222:
223: =item Fork once and dissociate from parent.
224:
225: =item Write PID to disk.
226:
227: =item Prefork children and maintain the population of children.
228:
229: =back
230:
231: =cut
232:
233: ###################################################################
234: ###################################################################
235: my $childmaxattempts=10;
236: my $run =0; # running counter to generate the query-id
237: #
238: # Read loncapa_apache.conf and loncapa.conf
239: #
1.53 harris41 240: my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
1.51 matthew 241: my %perlvar=%{$perlvarref};
242: #
243: # Make sure that database can be accessed
244: #
245: my $dbh;
246: unless ($dbh = DBI->connect("DBI:mysql:loncapa","www",
247: $perlvar{'lonSqlAccess'},
248: { RaiseError =>0,PrintError=>0})) {
249: print "Cannot connect to database!\n";
250: my $emailto="$perlvar{'lonAdmEMail'},$perlvar{'lonSysEMail'}";
251: my $subj="LON: $perlvar{'lonHostID'} Cannot connect to database!";
252: system("echo 'Cannot connect to MySQL database!' |".
253: " mailto $emailto -s '$subj' > /dev/null");
254: exit 1;
255: } else {
256: $dbh->disconnect;
257: }
1.52 matthew 258:
1.51 matthew 259: #
260: # Check if other instance running
261: #
262: my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid";
263: if (-e $pidfile) {
264: my $lfh=IO::File->new("$pidfile");
265: my $pide=<$lfh>;
266: chomp($pide);
267: if (kill 0 => $pide) { die "already running"; }
268: }
1.52 matthew 269:
1.49 www 270: #
1.51 matthew 271: # Read hosts file
1.49 www 272: #
1.51 matthew 273: my %hostip;
274: my $thisserver;
275: my $PREFORK=4; # number of children to maintain, at least four spare
276: open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file";
277: while (my $configline=<CONFIG>) {
278: my ($id,$domain,$role,$name,$ip)=split(/:/,$configline);
279: chomp($ip);
280: $hostip{$ip}=$id;
281: $thisserver=$name if ($id eq $perlvar{'lonHostID'});
282: $PREFORK++;
1.45 www 283: }
1.51 matthew 284: close(CONFIG);
285: #
286: $PREFORK=int($PREFORK/4);
1.52 matthew 287:
1.51 matthew 288: #
289: # Create a socket to talk to lond
290: #
291: my $unixsock = "mysqlsock";
292: my $localfile="$perlvar{'lonSockDir'}/$unixsock";
293: my $server;
294: unlink ($localfile);
295: unless ($server=IO::Socket::UNIX->new(Local =>"$localfile",
296: Type => SOCK_STREAM,
297: Listen => 10)) {
298: print "in socket error:$@\n";
1.45 www 299: }
1.52 matthew 300:
1.51 matthew 301: #
302: # Fork once and dissociate
1.52 matthew 303: #
1.51 matthew 304: my $fpid=fork;
1.1 harris41 305: exit if $fpid;
306: die "Couldn't fork: $!" unless defined ($fpid);
307: POSIX::setsid() or die "Can't start new session: $!";
1.52 matthew 308:
1.51 matthew 309: #
310: # Write our PID on disk
311: my $execdir=$perlvar{'lonDaemons'};
1.1 harris41 312: open (PIDSAVE,">$execdir/logs/lonsql.pid");
313: print PIDSAVE "$$\n";
314: close(PIDSAVE);
315: &logthis("<font color=red>CRITICAL: ---------- Starting ----------</font>");
1.52 matthew 316:
1.51 matthew 317: #
318: # Ignore signals generated during initial startup
1.1 harris41 319: $SIG{HUP}=$SIG{USR1}='IGNORE';
1.51 matthew 320: # Now we are on our own
321: # Fork off our children.
1.2 harris41 322: for (1 .. $PREFORK) {
323: make_new_child();
1.1 harris41 324: }
1.52 matthew 325:
1.51 matthew 326: #
1.2 harris41 327: # Install signal handlers.
1.1 harris41 328: $SIG{CHLD} = \&REAPER;
329: $SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
330: $SIG{HUP} = \&HUPSMAN;
1.52 matthew 331:
1.51 matthew 332: #
1.1 harris41 333: # And maintain the population.
334: while (1) {
335: sleep; # wait for a signal (i.e., child's death)
1.51 matthew 336: for (my $i = $children; $i < $PREFORK; $i++) {
1.2 harris41 337: make_new_child(); # top up the child pool
1.1 harris41 338: }
339: }
340:
1.51 matthew 341: ########################################################
342: ########################################################
343:
344: =pod
345:
346: =item &make_new_child
347:
348: Inputs: None
349:
350: Returns: None
351:
352: =cut
1.2 harris41 353:
1.51 matthew 354: ########################################################
355: ########################################################
1.1 harris41 356: sub make_new_child {
357: my $pid;
358: my $sigset;
1.51 matthew 359: #
1.1 harris41 360: # block signal for fork
361: $sigset = POSIX::SigSet->new(SIGINT);
362: sigprocmask(SIG_BLOCK, $sigset)
363: or die "Can't block SIGINT for fork: $!\n";
1.51 matthew 364: #
1.2 harris41 365: die "fork: $!" unless defined ($pid = fork);
1.51 matthew 366: #
1.1 harris41 367: if ($pid) {
368: # Parent records the child's birth and returns.
369: sigprocmask(SIG_UNBLOCK, $sigset)
370: or die "Can't unblock SIGINT for fork: $!\n";
371: $children{$pid} = 1;
372: $children++;
373: return;
374: } else {
1.2 harris41 375: # Child can *not* return from this subroutine.
1.1 harris41 376: $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did before
377: # unblock signals
378: sigprocmask(SIG_UNBLOCK, $sigset)
379: or die "Can't unblock SIGINT for fork: $!\n";
1.2 harris41 380: #open database handle
381: # making dbh global to avoid garbage collector
1.51 matthew 382: unless ($dbh = DBI->connect("DBI:mysql:loncapa","www",
383: $perlvar{'lonSqlAccess'},
384: { RaiseError =>0,PrintError=>0})) {
385: sleep(10+int(rand(20)));
386: &logthis("<font color=blue>WARNING: Couldn't connect to database".
387: ": $@</font>");
388: # "($st secs): $@</font>");
389: print "database handle error\n";
390: exit;
391: }
392: # make sure that a database disconnection occurs with
393: # ending kill signals
1.2 harris41 394: $SIG{TERM}=$SIG{INT}=$SIG{QUIT}=$SIG{__DIE__}=\&DISCONNECT;
1.1 harris41 395: # handle connections until we've reached $MAX_CLIENTS_PER_CHILD
1.51 matthew 396: for (my $i=0; $i < $MAX_CLIENTS_PER_CHILD; $i++) {
397: my $client = $server->accept() or last;
1.2 harris41 398: # do something with the connection
1.1 harris41 399: $run = $run+1;
1.2 harris41 400: my $userinput = <$client>;
401: chomp($userinput);
1.51 matthew 402: #
1.45 www 403: my ($conserver,$query,
404: $arg1,$arg2,$arg3)=split(/&/,$userinput);
405: my $query=unescape($query);
1.51 matthew 406: #
1.2 harris41 407: #send query id which is pid_unixdatetime_runningcounter
1.51 matthew 408: my $queryid = $thisserver;
1.2 harris41 409: $queryid .="_".($$)."_";
410: $queryid .= time."_";
411: $queryid .= $run;
412: print $client "$queryid\n";
1.51 matthew 413: #
1.47 www 414: &logthis("QUERY: $query - $arg1 - $arg2 - $arg3");
1.25 harris41 415: sleep 1;
1.51 matthew 416: #
1.45 www 417: my $result='';
1.51 matthew 418: #
419: # At this point, query is received, query-ID assigned and sent
420: # back, $query eq 'logquery' will mean that this is a query
421: # against log-files
422: if (($query eq 'userlog') || ($query eq 'courselog')) {
423: # beginning of log query
424: my $udom = &unescape($arg1);
425: my $uname = &unescape($arg2);
426: my $command = &unescape($arg3);
427: my $path = &propath($udom,$uname);
428: if (-e "$path/activity.log") {
429: if ($query eq 'userlog') {
430: $result=&userlog($path,$command);
431: } else {
432: $result=&courselog($path,$command);
433: }
434: } else {
435: &logthis('Unable to do log query: '.$uname.'@'.$udom);
436: $result='no_such_file';
437: }
438: # end of log query
439: } else {
440: # Do an sql query
441: $result = &do_sql_query($query,$arg1,$arg2);
442: }
1.50 matthew 443: # result does not need to be escaped because it has already been
444: # escaped.
445: #$result=&escape($result);
1.51 matthew 446: # reply with result, append \n unless already there
1.44 www 447: $result.="\n" unless ($result=~/\n$/);
1.17 harris41 448: &reply("queryreply:$queryid:$result",$conserver);
1.1 harris41 449: }
450: # tidy up gracefully and finish
1.51 matthew 451: #
452: # close the database handle
1.2 harris41 453: $dbh->disconnect
1.51 matthew 454: or &logthis("<font color=blue>WARNING: Couldn't disconnect".
455: " from database $DBI::errstr : $@</font>");
1.1 harris41 456: # this exit is VERY important, otherwise the child will become
457: # a producer of more and more children, forking yourself into
458: # process death.
459: exit;
460: }
1.2 harris41 461: }
1.1 harris41 462:
1.51 matthew 463: ########################################################
464: ########################################################
465:
466: =pod
467:
468: =item &do_sql_query
469:
470: Runs an sql metadata table query.
471:
472: Inputs: $query, $custom, $customshow
473:
474: Returns: A string containing escaped results.
475:
476: =cut
477:
478: ########################################################
479: ########################################################
480: {
481: my @metalist;
482:
483: sub process_file {
484: if ( -e $_ && # file exists
485: -f $_ && # and is a normal file
486: /\.meta$/ && # ends in meta
487: ! /^.+\.\d+\.[^\.]+\.meta$/ # is not a previous version
488: ) {
489: push(@metalist,$File::Find::name);
490: }
491: }
492:
493: sub do_sql_query {
494: my ($query,$custom,$customshow) = @_;
495: $custom = &unescape($custom);
496: $customshow = &unescape($customshow);
497: #
498: @metalist = ();
499: #
500: my $result = '';
501: my @results = ();
502: my @files;
503: my $subsetflag=0;
504: #
505: if ($query) {
506: #prepare and execute the query
507: my $sth = $dbh->prepare($query);
508: unless ($sth->execute()) {
509: &logthis("<font color=blue>WARNING: ".
510: "Could not retrieve from database: $@</font>");
511: } else {
512: my $aref=$sth->fetchall_arrayref;
513: foreach my $row (@$aref) {
514: push @files,@{$row}[3] if ($custom or $customshow);
515: my @b=map { &escape($_); } @$row;
516: push @results,join(",", @b);
517: # Build up the @files array with the LON-CAPA urls
518: # of the resources.
519: }
520: }
521: }
522: # do custom metadata searching here and build into result
523: return join("&",@results) if (! ($custom or $customshow));
524: # Only get here if there is a custom query or custom show request
525: &logthis("Doing custom query for $custom");
526: if ($query) {
527: @metalist=map {
528: $perlvar{'lonDocRoot'}.$_.'.meta';
529: } @files;
530: } else {
531: my $dir = "$perlvar{'lonDocRoot'}/res/$perlvar{'lonDefDomain'}";
532: @metalist=();
533: opendir(RESOURCES,$dir);
534: my @homeusers=grep {
535: &ishome($dir.'/'.$_);
536: } grep {!/^\.\.?$/} readdir(RESOURCES);
537: closedir RESOURCES;
538: # Define the
539: foreach my $user (@homeusers) {
540: find (\&process_file,$dir.'/'.$user);
541: }
542: }
543: # if file is indicated in sql database and
544: # not part of sql-relevant query, do not pattern match.
545: #
546: # if file is not in sql database, output error.
547: #
548: # if file is indicated in sql database and is
549: # part of query result list, then do the pattern match.
550: my $customresult='';
551: my @results;
552: foreach my $metafile (@metalist) {
553: my $fh=IO::File->new($metafile);
554: my @lines=<$fh>;
555: my $stuff=join('',@lines);
556: if ($stuff=~/$custom/s) {
557: foreach my $f ('abstract','author','copyright',
558: 'creationdate','keywords','language',
559: 'lastrevisiondate','mime','notes',
560: 'owner','subject','title') {
561: $stuff=~s/\n?\<$f[^\>]*\>.*?<\/$f[^\>]*\>\n?//s;
562: }
563: my $mfile=$metafile;
564: my $docroot=$perlvar{'lonDocRoot'};
565: $mfile=~s/^$docroot//;
566: $mfile=~s/\.meta$//;
567: unless ($query) {
568: my $q2="SELECT * FROM metadata WHERE url ".
569: " LIKE BINARY '?'";
570: my $sth = $dbh->prepare($q2);
571: $sth->execute($mfile);
572: my $aref=$sth->fetchall_arrayref;
573: foreach my $a (@$aref) {
574: my @b=map { &escape($_)} @$a;
575: push @results,join(",", @b);
576: }
577: }
578: # &logthis("found: $stuff");
579: $customresult.='&custom='.&escape($mfile).','.
580: escape($stuff);
581: }
582: }
583: $result=join("&",@results) unless $query;
584: $result.=$customresult;
585: #
586: return $result;
587: } # End of &do_sql_query
588:
589: } # End of scoping curly braces for &process_file and &do_sql_query
590: ########################################################
591: ########################################################
592:
593: =pod
594:
595: =item &logthis
596:
597: Inputs: $message, the message to log
598:
599: Returns: nothing
600:
601: Writes $message to the logfile.
602:
603: =cut
604:
605: ########################################################
606: ########################################################
607: sub logthis {
608: my $message=shift;
609: my $execdir=$perlvar{'lonDaemons'};
1.52 matthew 610: my $fh=IO::File->new(">>$execdir/logs/lonsql.log");
1.51 matthew 611: my $now=time;
612: my $local=localtime($now);
613: print $fh "$local ($$): $message\n";
1.2 harris41 614: }
1.1 harris41 615:
1.2 harris41 616: # -------------------------------------------------- Non-critical communication
1.1 harris41 617:
1.51 matthew 618: ########################################################
619: ########################################################
620:
621: =pod
622:
623: =item &subreply
624:
625: Sends a command to a server. Called only by &reply.
626:
627: Inputs: $cmd,$server
628:
629: Returns: The results of the message or 'con_lost' on error.
630:
631: =cut
632:
633: ########################################################
634: ########################################################
1.2 harris41 635: sub subreply {
636: my ($cmd,$server)=@_;
637: my $peerfile="$perlvar{'lonSockDir'}/$server";
638: my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile",
639: Type => SOCK_STREAM,
640: Timeout => 10)
641: or return "con_lost";
642: print $sclient "$cmd\n";
643: my $answer=<$sclient>;
644: chomp($answer);
1.51 matthew 645: $answer="con_lost" if (!$answer);
1.2 harris41 646: return $answer;
647: }
1.1 harris41 648:
1.51 matthew 649: ########################################################
650: ########################################################
651:
652: =pod
653:
654: =item &reply
655:
656: Sends a command to a server.
657:
658: Inputs: $cmd,$server
659:
660: Returns: The results of the message or 'con_lost' on error.
661:
662: =cut
663:
664: ########################################################
665: ########################################################
1.2 harris41 666: sub reply {
667: my ($cmd,$server)=@_;
668: my $answer;
669: if ($server ne $perlvar{'lonHostID'}) {
670: $answer=subreply($cmd,$server);
671: if ($answer eq 'con_lost') {
672: $answer=subreply("ping",$server);
673: $answer=subreply($cmd,$server);
674: }
675: } else {
676: $answer='self_reply';
1.33 harris41 677: $answer=subreply($cmd,$server);
1.2 harris41 678: }
679: return $answer;
680: }
1.1 harris41 681:
1.51 matthew 682: ########################################################
683: ########################################################
684:
685: =pod
686:
687: =item &escape
688:
689: Escape special characters in a string.
1.3 harris41 690:
1.51 matthew 691: Inputs: string to escape
692:
693: Returns: The input string with special characters escaped.
694:
695: =cut
696:
697: ########################################################
698: ########################################################
1.3 harris41 699: sub escape {
700: my $str=shift;
701: $str =~ s/(\W)/"%".unpack('H2',$1)/eg;
702: return $str;
703: }
704:
1.51 matthew 705: ########################################################
706: ########################################################
707:
708: =pod
709:
710: =item &unescape
711:
712: Unescape special characters in a string.
1.3 harris41 713:
1.51 matthew 714: Inputs: string to unescape
715:
716: Returns: The input string with special characters unescaped.
717:
718: =cut
719:
720: ########################################################
721: ########################################################
1.3 harris41 722: sub unescape {
723: my $str=shift;
724: $str =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
725: return $str;
726: }
1.34 harris41 727:
1.51 matthew 728: ########################################################
729: ########################################################
730:
731: =pod
732:
733: =item &ishome
734:
735: Determine if the current machine is the home server for a user.
736: The determination is made by checking the filesystem for the users information.
737:
738: Inputs: $author
739:
740: Returns: 0 - this is not the authors home server, 1 - this is.
741:
742: =cut
743:
744: ########################################################
745: ########################################################
1.34 harris41 746: sub ishome {
747: my $author=shift;
748: $author=~s/\/home\/httpd\/html\/res\/([^\/]*)\/([^\/]*).*/$1\/$2/;
749: my ($udom,$uname)=split(/\//,$author);
750: my $proname=propath($udom,$uname);
751: if (-e $proname) {
752: return 1;
753: } else {
754: return 0;
755: }
756: }
757:
1.51 matthew 758: ########################################################
759: ########################################################
760:
761: =pod
762:
763: =item &propath
764:
765: Inputs: user name, user domain
766:
767: Returns: The full path to the users directory.
768:
769: =cut
770:
771: ########################################################
772: ########################################################
1.34 harris41 773: sub propath {
774: my ($udom,$uname)=@_;
775: $udom=~s/\W//g;
776: $uname=~s/\W//g;
777: my $subdir=$uname.'__';
778: $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/;
779: my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname";
780: return $proname;
781: }
1.40 harris41 782:
1.51 matthew 783: ########################################################
784: ########################################################
785:
786: =pod
787:
788: =item &courselog
789:
790: Inputs: $path, $command
791:
792: Returns: unescaped string of values.
793:
794: =cut
795:
796: ########################################################
797: ########################################################
798: sub courselog {
799: my ($path,$command)=@_;
800: my %filters=();
801: foreach (split(/\:/,&unescape($command))) {
802: my ($name,$value)=split(/\=/,$_);
803: $filters{$name}=$value;
804: }
805: my @results=();
806: open(IN,$path.'/activity.log') or return ('file_error');
807: while (my $line=<IN>) {
808: chomp($line);
809: my ($timestamp,$host,$log)=split(/\:/,$line);
810: #
811: # $log has the actual log entries; currently still escaped, and
812: # %26(timestamp)%3a(url)%3a(user)%3a(domain)
813: # then additionally
814: # %3aPOST%3a(name)%3d(value)%3a(name)%3d(value)
815: # or
816: # %3aCSTORE%3a(name)%3d(value)%26(name)%3d(value)
817: #
818: # get delimiter between timestamped entries to be &&&
819: $log=~s/\%26(\d+)\%3a/\&\&\&$1\%3a/g;
820: # now go over all log entries
821: foreach (split(/\&\&\&/,&unescape($log))) {
822: my ($time,$res,$uname,$udom,$action,@values)=split(/\:/,$_);
823: my $values=&unescape(join(':',@values));
824: $values=~s/\&/\:/g;
825: $res=&unescape($res);
826: my $include=1;
827: if (($filters{'username'}) && ($uname ne $filters{'username'}))
828: { $include=0; }
829: if (($filters{'domain'}) && ($udom ne $filters{'domain'}))
830: { $include=0; }
831: if (($filters{'url'}) && ($res!~/$filters{'url'}/))
832: { $include=0; }
833: if (($filters{'start'}) && ($time<$filters{'start'}))
834: { $include=0; }
835: if (($filters{'end'}) && ($time>$filters{'end'}))
836: { $include=0; }
837: if (($filters{'action'} eq 'view') && ($action))
838: { $include=0; }
839: if (($filters{'action'} eq 'submit') && ($action ne 'POST'))
840: { $include=0; }
841: if (($filters{'action'} eq 'grade') && ($action ne 'CSTORE'))
842: { $include=0; }
843: if ($include) {
844: push(@results,($time<1000000000?'0':'').$time.':'.$res.':'.
845: $uname.':'.$udom.':'.
846: $action.':'.$values);
847: }
848: }
849: }
850: close IN;
851: return join('&',sort(@results));
852: }
853:
854: ########################################################
855: ########################################################
856:
857: =pod
858:
859: =item &userlog
860:
861: Inputs: $path, $command
862:
863: Returns: unescaped string of values.
1.40 harris41 864:
1.51 matthew 865: =cut
1.40 harris41 866:
1.51 matthew 867: ########################################################
868: ########################################################
869: sub userlog {
870: my ($path,$command)=@_;
871: my %filters=();
872: foreach (split(/\:/,&unescape($command))) {
873: my ($name,$value)=split(/\=/,$_);
874: $filters{$name}=$value;
875: }
876: my @results=();
877: open(IN,$path.'/activity.log') or return ('file_error');
878: while (my $line=<IN>) {
879: chomp($line);
880: my ($timestamp,$host,$log)=split(/\:/,$line);
881: $log=&unescape($log);
882: my $include=1;
883: if (($filters{'start'}) && ($timestamp<$filters{'start'}))
884: { $include=0; }
885: if (($filters{'end'}) && ($timestamp>$filters{'end'}))
886: { $include=0; }
887: if (($filters{'action'} eq 'log') && ($log!~/^Log/)) { $include=0; }
888: if (($filters{'action'} eq 'check') && ($log!~/^Check/))
889: { $include=0; }
890: if ($include) {
891: push(@results,$timestamp.':'.$log);
892: }
893: }
894: close IN;
895: return join('&',sort(@results));
1.52 matthew 896: }
897:
898: ########################################################
899: ########################################################
900:
901: =pod
902:
903: =item Functions required for forking
904:
905: =over 4
906:
907: =item REAPER
908:
909: REAPER takes care of dead children.
910:
911: =item HUNTSMAN
912:
913: Signal handler for SIGINT.
914:
915: =item HUPSMAN
916:
917: Signal handler for SIGHUP
918:
919: =item DISCONNECT
920:
921: Disconnects from database.
922:
923: =back
924:
925: =cut
926:
927: ########################################################
928: ########################################################
929: sub REAPER { # takes care of dead children
930: $SIG{CHLD} = \&REAPER;
931: my $pid = wait;
932: $children --;
933: &logthis("Child $pid died");
934: delete $children{$pid};
935: }
936:
937: sub HUNTSMAN { # signal handler for SIGINT
938: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
939: kill 'INT' => keys %children;
940: my $execdir=$perlvar{'lonDaemons'};
941: unlink("$execdir/logs/lonsql.pid");
942: &logthis("<font color=red>CRITICAL: Shutting down</font>");
943: $unixsock = "mysqlsock";
944: my $port="$perlvar{'lonSockDir'}/$unixsock";
945: unlink($port);
946: exit; # clean up with dignity
947: }
948:
949: sub HUPSMAN { # signal handler for SIGHUP
950: local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
951: kill 'INT' => keys %children;
952: close($server); # free up socket
953: &logthis("<font color=red>CRITICAL: Restarting</font>");
954: my $execdir=$perlvar{'lonDaemons'};
955: $unixsock = "mysqlsock";
956: my $port="$perlvar{'lonSockDir'}/$unixsock";
957: unlink($port);
958: exec("$execdir/lonsql"); # here we go again
959: }
960:
961: sub DISCONNECT {
962: $dbh->disconnect or
963: &logthis("<font color=blue>WARNING: Couldn't disconnect from database ".
964: " $DBI::errstr : $@</font>");
965: exit;
1.51 matthew 966: }
1.40 harris41 967:
968:
969:
970:
971:
972:
973:
974:
975:
976:
977:
1.51 matthew 978: # ----------------------------------- POD (plain old documentation, CPAN style)
1.40 harris41 979:
1.51 matthew 980: =pod
1.40 harris41 981:
1.51 matthew 982: =back
1.40 harris41 983:
984: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>