--- loncom/lonsql 2007/04/03 17:51:45 1.80 +++ loncom/lonsql 2007/04/12 00:00:55 1.81 @@ -3,7 +3,7 @@ # The LearningOnline Network # lonsql - LON TCP-MySQL-Server Daemon for handling database requests. # -# $Id: lonsql,v 1.80 2007/04/03 17:51:45 raeburn Exp $ +# $Id: lonsql,v 1.81 2007/04/12 00:00:55 albertel Exp $ # # Copyright Michigan State University Board of Trustees # @@ -105,20 +105,16 @@ use lib '/home/httpd/lib/perl/'; use LONCAPA; use LONCAPA::Configuration; use LONCAPA::lonmetadata(); +use Apache::lonnet; use IO::Socket; use Symbol; use POSIX; use IO::Select; -use IO::File; -use Socket; -use Fcntl; -use Tie::RefHash; use DBI; use File::Find; use localenroll; use GDBM_File; -use Storable qw(thaw); ######################################################## ######################################################## @@ -206,8 +202,7 @@ my $run =0; # running count # # Read loncapa_apache.conf and loncapa.conf # -my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf'); -my %perlvar=%{$perlvarref}; +my %perlvar=%{&LONCAPA::Configuration::read_conf('loncapa.conf')}; # # Write the /home/www/.my.cnf file my $conf_file = '/home/www/.my.cnf'; @@ -253,27 +248,13 @@ unless ($dbh = DBI->connect("DBI:mysql:l # my $pidfile="$perlvar{'lonDaemons'}/logs/lonsql.pid"; if (-e $pidfile) { - my $lfh=IO::File->new("$pidfile"); + open(my $lfh,"$pidfile"); my $pide=<$lfh>; chomp($pide); if (kill 0 => $pide) { die "already running"; } } -# -# Read hosts file -# -my $thisserver; -my %hostname; my $PREFORK=4; # number of children to maintain, at least four spare -open (CONFIG,"$perlvar{'lonTabDir'}/hosts.tab") || die "Can't read host file"; -while (my $configline=) { - my ($id,$domain,$role,$name)=split(/:/,$configline); - $name=~s/\s//g; - $thisserver=$name if ($id eq $perlvar{'lonHostID'}); - $hostname{$id}=$name; - #$PREFORK++; -} -close(CONFIG); # #$PREFORK=int($PREFORK/4); @@ -399,13 +380,13 @@ sub make_new_child { my $query=unescape($query); # #send query id which is pid_unixdatetime_runningcounter - my $queryid = $thisserver; + my $queryid = &Apache::lonnet::hostname($perlvar{'lonHostID'}); $queryid .="_".($$)."_"; $queryid .= time."_"; $queryid .= $run; print $client "$queryid\n"; # - # &logthis("QUERY: $query - $arg1 - $arg2 - $arg3"); + # &logthis("QUERY: $query - $arg1 - $arg2 - $arg3 - $queryid"); sleep 1; # my $result=''; @@ -485,7 +466,7 @@ sub make_new_child { # result does not need to be escaped because it has already been # escaped. #$result=&escape($result); - &reply("queryreply:$queryid:$result",$conserver); + &Apache::lonnet::reply("queryreply:$queryid:$result",$conserver); } # tidy up gracefully and finish # @@ -602,7 +583,7 @@ sub do_sql_query { my $customresult=''; my @results; foreach my $metafile (@metalist) { - my $fh=IO::File->new($metafile); + open(my $fh,$metafile); my @lines=<$fh>; my $stuff=join('',@lines); if ($stuff=~/$custom/s) { @@ -737,7 +718,7 @@ sub get_access_hash { while (my ($key,$value) = each(%$hashref)) { $key = &unescape($key); next if ($key =~ /^error: 2 /); - $curr_perms{$key}=&thaw_unescape($value); + $curr_perms{$key}=&Apache::lonnet::thaw_unescape($value); } if (!&untie_user_hash($hashref)) { &logthis("error: ".($!+0)." untie (GDBM) Failed"); @@ -755,16 +736,6 @@ sub get_access_hash { return %access; } -sub thaw_unescape { - my ($value)=@_; - if ($value =~ /^__FROZEN__/) { - substr($value,0,10,undef); - $value=&unescape($value); - return &thaw($value); - } - return &unescape($value); -} - ########################################### sub check_table { my ($table_id) = @_; @@ -821,78 +792,12 @@ Writes $message to the logfile. sub logthis { my $message=shift; my $execdir=$perlvar{'lonDaemons'}; - my $fh=IO::File->new(">>$execdir/logs/lonsql.log"); + open(my $fh,">>$execdir/logs/lonsql.log"); my $now=time; my $local=localtime($now); print $fh "$local ($$): $message\n"; } -# -------------------------------------------------- Non-critical communication - -######################################################## -######################################################## - -=pod - -=item &subreply - -Sends a command to a server. Called only by &reply. - -Inputs: $cmd,$server - -Returns: The results of the message or 'con_lost' on error. - -=cut - -######################################################## -######################################################## -sub subreply { - my ($cmd,$server)=@_; - my $peerfile="$perlvar{'lonSockDir'}/".$hostname{$server}; - my $sclient=IO::Socket::UNIX->new(Peer =>"$peerfile", - Type => SOCK_STREAM, - Timeout => 10) - or return "con_lost"; - print $sclient "sethost:$server:$cmd\n"; - my $answer=<$sclient>; - chomp($answer); - $answer="con_lost" if (!$answer); - return $answer; -} - -######################################################## -######################################################## - -=pod - -=item &reply - -Sends a command to a server. - -Inputs: $cmd,$server - -Returns: The results of the message or 'con_lost' on error. - -=cut - -######################################################## -######################################################## -sub reply { - my ($cmd,$server)=@_; - my $answer; - if ($server ne $perlvar{'lonHostID'}) { - $answer=subreply($cmd,$server); - if ($answer eq 'con_lost') { - $answer=subreply("ping",$server); - $answer=subreply($cmd,$server); - } - } else { - $answer='self_reply'; - $answer=subreply($cmd,$server); - } - return $answer; -} - ######################################################## ######################################################## @@ -925,31 +830,6 @@ sub ishome { ######################################################## ######################################################## - -=pod - -=item &propath - -Inputs: user name, user domain - -Returns: The full path to the users directory. - -=cut - -######################################################## -######################################################## -sub propath { - my ($udom,$uname)=@_; - $udom=~s/\W//g; - $uname=~s/\W//g; - my $subdir=$uname.'__'; - $subdir =~ s/(.)(.)(.).*/$1\/$2\/$3/; - my $proname="$perlvar{'lonUsersDir'}/$udom/$subdir/$uname"; - return $proname; -} - -######################################################## -######################################################## =pod