From: Stephen L Johnson Date: Mon, 24 Jun 2002 16:59:22 +0000 (+0000) Subject: made the already_running() function smarter in checking for a running spong-client... X-Git-Tag: spong-2_8_0-beta1~73 X-Git-Url: http://git.etc.gen.nz/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=71961e49be5f22765481505cd89dd1bdbf7ec69c;p=spong.git made the already_running() function smarter in checking for a running spong-client 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-client.pl b/src/spong-client.pl index 990a9c9..a194221 100755 --- a/src/spong-client.pl +++ b/src/spong-client.pl @@ -12,7 +12,7 @@ # History: # (1) Ported bb-local.sh script to perl. (Ed Hill Feb 26, 1997) # -# $Id: spong-client.pl,v 1.21 2002/06/10 20:31:20 sljohnson Exp $ +# $Id: spong-client.pl,v 1.22 2002/06/24 16:59:22 sljohnson Exp $ use lib '@@LIBDIR@@'; @@ -48,11 +48,25 @@ $HOST =~ tr/A-Z/a-z/; $CHECKS = ""; -&load_config_files(); # Loads the user specified configuration information -&init_logging(); # Initialize logging contexts -Spong::Daemon::Daemonize # Daemonize if not signalling or debugging - unless ($restart || $kill || $nosleep || $debug || $nodaemonize); -&handle_signals(); # Set up handlers, and signal the current server if asked +load_config_files(); # Loads the user specified configuration information +init_logging(); # Initialize logging contexts + +# signal the current server is asked +if ( $restart || $kill ) { + handle_signals(); + exit 0; +} + +if ( already_running() && ! ( $debug || $nosleep ) ) { + error( "spong-client 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-client.pid" ) unless $nosleep; # Find our SPONGSLEEP value @@ -163,11 +177,13 @@ sub load_config_files { &debug( "configuration file(s) 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. +# This is part of the set up code. This daemonized the program and +# this sets up the signal handlers -sub handle_signals { +sub daemonize { + + # Daemonize + Spong::Daemon::Daemonize # Clear out signal mask in case we inherit any blocked sigs @@ -178,15 +194,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-server program. + +sub handle_signals { + # If the user gives us the --restart or --kill flags, then we signal the # currently running spong-client 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-client.pid") || die "Can't find pid: $!"; my $pid = ; chomp $pid; close PID; @@ -196,21 +221,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-client 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-client.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 +# running. sub already_running { + + my $retcd = 0; + # if there is a PID file if ( -f "$SPONGTMP/spong-client.pid" ) { # Read the pid @@ -219,13 +242,17 @@ sub already_running { close PID; if ( kill 0,$pid ) { - `ps -fp $pid | grep spong-client >/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-client >/dev/null`; if ( ! $? ) { - &error("Spong-client is already running as pid $pid"); - exit 1; + $retcd = 1; } } } + + return $retcd; } @@ -238,7 +265,7 @@ sub error { Spong::Log::error($_[0]); } # Signal handlers... sub exit_handler { - &debug( "caught QUIT signal, exiting..." ); + debug( "caught QUIT signal, exiting..." ); unlink "$SPONGTMP/spong-client.pid" if -f "$SPONGTMP/spong-client.pid"; exit(0); }