From 9010817f78918e744a843556da7e344f3c29a3d9 Mon Sep 17 00:00:00 2001 From: Stephen L Johnson Date: Tue, 9 Jul 2002 17:58:55 +0000 Subject: [PATCH] make the already_running() function smarter in checking for a running program instance pushed daemonizing to laster in the setup code. This is to allow startup errors to be logged to the console --- src/spong-server.pl | 103 ++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 37 deletions(-) diff --git a/src/spong-server.pl b/src/spong-server.pl index f83102e..d931cd3 100755 --- a/src/spong-server.pl +++ b/src/spong-server.pl @@ -6,7 +6,7 @@ # There are one or more update processes that listen for status updates # from client programs. -# $Id: spong-server.pl,v 1.50 2002/05/29 02:01:27 sljohnson Exp $ +# $Id: spong-server.pl,v 1.51 2002/07/09 17:58:55 sljohnson Exp $ use lib "@@LIBDIR@@"; @@ -51,10 +51,6 @@ if (! GetOptions("debug:i" => \$debuglevel, "restart" => \$restart, Spong::Log::set_debug_context( 'debuglevel' => $debuglevel ); -#if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; } -#if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; } -#if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; } - $ETCDIR = '@@ETCDIR@@'; $BINDIR = '@@BINDIR@@'; @@ -82,8 +78,23 @@ $shutdown = 0; &load_config_files(); # Loads the user specified configuration information &init_logging(); # Initialize logging contexts -Spong::Daemon::Daemonize() unless ($debug || $restart || $kill || $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() ) { + error( "spong-server 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-server.pid" ) unless $nosleep; # Find our SPONGSLEEP value @@ -1347,11 +1358,14 @@ sub load_config_files { } -# 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 @@ -1359,19 +1373,28 @@ sub handle_signals { sigprocmask(SIG_SETMASK, $sigset ); # Set up some signal handlers to handle our death gracefully, and also - # listen for the HUP signal, and if we see that, we re-exec ourself. + # 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 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 + # currently running spong-server process, and tell it to either die, or # re-exec itself (which causes it to re-read it's configuration files. - if( $restart || $kill ) { - open( PID, "$SPONGTMP/spong-server.pid" ) || die "Can't find pid: $!"; + if( already_running() ) { + open( PID, "$SPONGTMP/spong-network.pid") || die "Can't find pid: $!"; my $pid = ; chomp $pid; close PID; @@ -1380,15 +1403,38 @@ 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(); +} + +# This routine check to see if another instance of spong-server is already +# running. + +sub already_running { - # Write out our pid files, so that others can signal us. + my $retcd = 0; - system( "echo $$ >$SPONGTMP/spong-server.pid" ); + # if there is a PID file + if ( -f "$SPONGTMP/spong-server.pid" ) { + # Read the pid + open( PID, "$SPONGTMP/spong-server.pid" ) || die "Can't open pid: $!"; + my $pid = ; chomp $pid; + close PID; + + if ( kill 0,$pid ) { + 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-server >/dev/null`; + if ( ! $? ) { + $retcd = 1; + } + } + } + + return $retcd; } @@ -1438,23 +1484,6 @@ sub save_data { } } -# 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 -sub already_running { - # if there is a PID file - if ( -f "$SPONGTMP/spong-server.pid" ) { - # Read the pid - open( PID, "$SPONGTMP/spong-server.pid" ) || die "Can't open pid: $!"; - my $pid = ; chomp $pid; - close PID; - - if ( kill 0,$pid ) { - &error("Spong-server is already running as pid $pid"); - exit 1; - } - } -} - # Output functions, one for debugging information, the other for errors. sub debug { Spong::Log::debug($_[0],$_[1]); } -- 2.30.2