--- loncom/lonsql 2002/08/06 13:48:47 1.51
+++ loncom/lonsql 2002/09/09 14:04:02 1.53
@@ -3,7 +3,7 @@
# The LearningOnline Network
# lonsql - LON TCP-MySQL-Server Daemon for handling database requests.
#
-# $Id: lonsql,v 1.51 2002/08/06 13:48:47 matthew Exp $
+# $Id: lonsql,v 1.53 2002/09/09 14:04:02 harris41 Exp $
#
# Copyright Michigan State University Board of Trustees
#
@@ -116,76 +116,6 @@ my $MAX_CLIENTS_PER_CHILD = 5; # numb
my %children = (); # keys are current child process IDs
my $children = 0; # current number of children
-########################################################
-########################################################
-
-=pod
-
-=item Functions required for forking
-
-=over 4
-
-=item REAPER
-
-REAPER takes care of dead children.
-
-=item HUNTSMAN
-
-Signal handler for SIGINT.
-
-=item HUPSMAN
-
-Signal handler for SIGHUP
-
-=item DISCONNECT
-
-Disconnects from database.
-
-=back
-
-=cut
-
-########################################################
-########################################################
-sub REAPER { # takes care of dead children
- $SIG{CHLD} = \&REAPER;
- my $pid = wait;
- $children --;
- &logthis("Child $pid died");
- delete $children{$pid};
-}
-
-sub HUNTSMAN { # signal handler for SIGINT
- local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
- kill 'INT' => keys %children;
- my $execdir=$perlvar{'lonDaemons'};
- unlink("$execdir/logs/lonsql.pid");
- &logthis("CRITICAL: Shutting down");
- $unixsock = "mysqlsock";
- my $port="$perlvar{'lonSockDir'}/$unixsock";
- unlink($port);
- exit; # clean up with dignity
-}
-
-sub HUPSMAN { # signal handler for SIGHUP
- local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
- kill 'INT' => keys %children;
- close($server); # free up socket
- &logthis("CRITICAL: Restarting");
- my $execdir=$perlvar{'lonDaemons'};
- $unixsock = "mysqlsock";
- my $port="$perlvar{'lonSockDir'}/$unixsock";
- unlink($port);
- exec("$execdir/lonsql"); # here we go again
-}
-
-sub DISCONNECT {
- $dbh->disconnect or
- &logthis("WARNING: Couldn't disconnect from database ".
- " $DBI::errstr : $@");
- exit;
-}
-
###################################################################
###################################################################
@@ -222,8 +152,7 @@ my $run =0; # running count
#
# Read loncapa_apache.conf and loncapa.conf
#
-my $perlvarref=LONCAPA::Configuration::read_conf('loncapa_apache.conf',
- 'loncapa.conf');
+my $perlvarref=LONCAPA::Configuration::read_conf('loncapa.conf');
my %perlvar=%{$perlvarref};
#
# Make sure that database can be accessed
@@ -241,6 +170,7 @@ unless ($dbh = DBI->connect("DBI:mysql:l
} else {
$dbh->disconnect;
}
+
#
# Check if other instance running
#
@@ -251,6 +181,7 @@ if (-e $pidfile) {
chomp($pide);
if (kill 0 => $pide) { die "already running"; }
}
+
#
# Read hosts file
#
@@ -268,6 +199,7 @@ while (my $configline=) {
close(CONFIG);
#
$PREFORK=int($PREFORK/4);
+
#
# Create a socket to talk to lond
#
@@ -280,14 +212,15 @@ unless ($server=IO::Socket::UNIX->new(Lo
Listen => 10)) {
print "in socket error:$@\n";
}
-########################################################
-########################################################
+
#
# Fork once and dissociate
+#
my $fpid=fork;
exit if $fpid;
die "Couldn't fork: $!" unless defined ($fpid);
POSIX::setsid() or die "Can't start new session: $!";
+
#
# Write our PID on disk
my $execdir=$perlvar{'lonDaemons'};
@@ -295,6 +228,7 @@ open (PIDSAVE,">$execdir/logs/lonsql.pid
print PIDSAVE "$$\n";
close(PIDSAVE);
&logthis("CRITICAL: ---------- Starting ----------");
+
#
# Ignore signals generated during initial startup
$SIG{HUP}=$SIG{USR1}='IGNORE';
@@ -303,11 +237,13 @@ $SIG{HUP}=$SIG{USR1}='IGNORE';
for (1 .. $PREFORK) {
make_new_child();
}
+
#
# Install signal handlers.
$SIG{CHLD} = \&REAPER;
$SIG{INT} = $SIG{TERM} = \&HUNTSMAN;
$SIG{HUP} = \&HUPSMAN;
+
#
# And maintain the population.
while (1) {
@@ -586,7 +522,7 @@ Writes $message to the logfile.
sub logthis {
my $message=shift;
my $execdir=$perlvar{'lonDaemons'};
- my $fh=IO::File->new(">>$execdir/logs/lonsqlfinal.log");
+ my $fh=IO::File->new(">>$execdir/logs/lonsql.log");
my $now=time;
my $local=localtime($now);
print $fh "$local ($$): $message\n";
@@ -874,6 +810,76 @@ sub userlog {
return join('&',sort(@results));
}
+########################################################
+########################################################
+
+=pod
+
+=item Functions required for forking
+
+=over 4
+
+=item REAPER
+
+REAPER takes care of dead children.
+
+=item HUNTSMAN
+
+Signal handler for SIGINT.
+
+=item HUPSMAN
+
+Signal handler for SIGHUP
+
+=item DISCONNECT
+
+Disconnects from database.
+
+=back
+
+=cut
+
+########################################################
+########################################################
+sub REAPER { # takes care of dead children
+ $SIG{CHLD} = \&REAPER;
+ my $pid = wait;
+ $children --;
+ &logthis("Child $pid died");
+ delete $children{$pid};
+}
+
+sub HUNTSMAN { # signal handler for SIGINT
+ local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
+ kill 'INT' => keys %children;
+ my $execdir=$perlvar{'lonDaemons'};
+ unlink("$execdir/logs/lonsql.pid");
+ &logthis("CRITICAL: Shutting down");
+ $unixsock = "mysqlsock";
+ my $port="$perlvar{'lonSockDir'}/$unixsock";
+ unlink($port);
+ exit; # clean up with dignity
+}
+
+sub HUPSMAN { # signal handler for SIGHUP
+ local($SIG{CHLD}) = 'IGNORE'; # we're going to kill our children
+ kill 'INT' => keys %children;
+ close($server); # free up socket
+ &logthis("CRITICAL: Restarting");
+ my $execdir=$perlvar{'lonDaemons'};
+ $unixsock = "mysqlsock";
+ my $port="$perlvar{'lonSockDir'}/$unixsock";
+ unlink($port);
+ exec("$execdir/lonsql"); # here we go again
+}
+
+sub DISCONNECT {
+ $dbh->disconnect or
+ &logthis("WARNING: Couldn't disconnect from database ".
+ " $DBI::errstr : $@");
+ exit;
+}
+