From: Stephen L Johnson Date: Mon, 24 Jun 2002 19:01:07 +0000 (+0000) Subject: made the already_running() function smarter in checking for a running program instance. X-Git-Tag: spong-2_8_0-beta1~70 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4881720218645d2f5a67e406df2ff7aafa5f19dc;p=spong.git made the already_running() function smarter in checking for a running program instance. pushed daemonizing to later in the setup code. this is to allow for startup errors to be logged the console. --- diff --git a/src/spong-network.pl b/src/spong-network.pl index 90afa19..dac66fa 100755 --- a/src/spong-network.pl +++ b/src/spong-network.pl @@ -17,7 +17,7 @@ # (2) Converted checks to new plugin mechanism (Stephen Johnson May 28, 1999) # Added user-configurable escalation mechanism # -# $Id: spong-network.pl,v 1.43 2002/06/10 20:34:58 sljohnson Exp $ +# $Id: spong-network.pl,v 1.44 2002/06/24 19:01:07 sljohnson Exp $ use Carp; use lib "@@LIBDIR@@"; @@ -60,9 +60,29 @@ $HOST =~ tr/A-Z/a-z/; &load_config_files(); # Loads the user specified configuration information &init_logging(); # Initialize logging contexts -Spong::Daemon::Daemonize() # Daemonize if not signalling or a one-shot - unless ($nosleep || $restart || $kill || $debug || $nodaemonize ); -&handle_signals(); # Set up handlers, and signal the current server if asked + + + + + + + +# signal the current server is asked +if ( $restart || $kill ) { + handle_signals(); + exit 0; +} + +if ( already_running() && ! ( $debug || $nosleep ) ) { + error( "spong-network is already running" ); + exit 1; +} + +# Deamonize if not running from command line +daemonize() if ! ( $debug || $nodaemonize || $nosleep ); + +# Write our pid to the spong tmp directory. +system( "echo $$ >$SPONGTMP/spong-network.pid" ) unless $nosleep; # Find our SPONGSLEEP value @@ -480,11 +500,14 @@ sub load_config_files { &debug( "host file loaded" ); } -# This is part of the set up code, this sets up the signal handlers, and -# handles any command line arguments that are given to us that tell us to -# signal the current running spong-server program. -sub handle_signals { +# This is part of the set up code. This daemonizes the program and +# this sets up the signal handlers + +sub daemonize { + + # Daemonize + Spong::Daemon::Daemonize # Clear out signal mask in case we inherit any blocked sigs @@ -495,14 +518,24 @@ sub handle_signals { # listen for the HUP signal, and if we se that, we re-exec ourself. $SIG{'QUIT'} = \&exit_handler; + $SIG{'TERM'} = \&exit_handler; $SIG{'HUP'} = \&hup_handler; + $SIG{'USR1'} = \&hup_handler; $SIG{'PIPE'} = \&pipe_handler; +} + + +# Handles any command line arguments that are given to us that tell us to +# signal the current running spong-network program. + +sub handle_signals { + # If the user gives us the --restart or --kill flags, then we signal the # currently running spong-network process, and tell it to either die, or # re-exec itself (which causes it to re-read it's configuration files. - if( $restart || $kill ) { + if( already_running() ) { open( PID, "$SPONGTMP/spong-network.pid") || die "Can't find pid: $!"; my $pid = ; chomp $pid; close PID; @@ -512,22 +545,19 @@ sub handle_signals { if( $kill ) { &debug( "telling pid $pid to die" ); kill( 'QUIT', $pid );} - exit(0); + } else { + debug("Can't find instance of spong-network running"); } - # Check to see if we are already running - &already_running() unless $nosleep; - - # Write our pid to the spong tmp directory. - - system( "echo $$ >$SPONGTMP/spong-network.pid" ) unless $nosleep; } - -# This routine check to see if another instance of spong-server is already -# running. If there is another instance, this instance will complain and die +# This routine check to see if another instance of spong-network is already +# running. sub already_running { + + my $retcd = 0; + # if there is a PID file if ( -f "$SPONGTMP/spong-network.pid" ) { # Read the pid @@ -536,13 +566,17 @@ sub already_running { close PID; if ( kill 0,$pid ) { - `ps -f -p $pid | grep spong-network >/dev/null`; + my $myps; + # If $PS has a sort pipe, skip it + if ( $PS =~ m/^(.*)\|.*sort/ ) { $myps = $1 } else { $myps = $PS } + `$myps | grep -v grep | grep $pid | grep spong-network >/dev/null`; if ( ! $? ) { - &error("Spong-network is already running as pid $pid"); - exit 1; + $retcd = 1; } } } + + return $retcd; }