--- loncom/lonsql 2002/08/06 13:48:47 1.51 +++ loncom/lonsql 2002/08/13 19:40:57 1.52 @@ -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.52 2002/08/13 19:40:57 matthew 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; -} - ################################################################### ################################################################### @@ -241,6 +171,7 @@ unless ($dbh = DBI->connect("DBI:mysql:l } else { $dbh->disconnect; } + # # Check if other instance running # @@ -251,6 +182,7 @@ if (-e $pidfile) { chomp($pide); if (kill 0 => $pide) { die "already running"; } } + # # Read hosts file # @@ -268,6 +200,7 @@ while (my $configline=) { close(CONFIG); # $PREFORK=int($PREFORK/4); + # # Create a socket to talk to lond # @@ -280,14 +213,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 +229,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 +238,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 +523,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 +811,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; +} +