From 2d83c50d039733a2b6681a9f387d906c059d8997 Mon Sep 17 00:00:00 2001 From: Stephen L Johnson Date: Tue, 14 Mar 2000 21:25:59 +0000 Subject: [PATCH] convert parameter handling to use Getopt::Long and converted debug() and error() to use Spong::Log --- src/spong-client.pl | 105 ++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 37 deletions(-) diff --git a/src/spong-client.pl b/src/spong-client.pl index f41c3c3..9d3b0b9 100755 --- a/src/spong-client.pl +++ b/src/spong-client.pl @@ -17,17 +17,31 @@ use lib '@@LIBDIR@@'; use Sys::Hostname; use Socket; use POSIX; +use Getopt::Long; use Spong::Daemon; use Spong::Status qw(status); +use Spong::Log; srand( time() ^ ($$ + ($$ << 15 )) ); -if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; } -if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; } -if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; } -if( $ARGV[0] eq "--nosleep" ) { $nosleep = 1; shift; } -if( $ARGV[0] eq "--refresh" ) { $nosleep = 1; shift; } +$debug = $restart = $kill = $nosleep = 0; + +Getopt::Long::Configure('pass_through'); +if ( ! GetOptions("debug:i" => \$debuglevel, "restart" => \$restart, + "kill" => \$kill, "nosleep|refresh" => \$nosleep ) ) { + &usage(); + exit 1; +} + +#if( $ARGV[0] eq "--debug" ) { $debug = 1; shift; } +#if( $ARGV[0] eq "--restart" ) { $restart = 1; shift; } +#if( $ARGV[0] eq "--kill" ) { $kill = 1; shift; } +#if( $ARGV[0] eq "--nosleep" ) { $nosleep = 1; shift; } +#if( $ARGV[0] eq "--refresh" ) { $nosleep = 1; shift; } + +# Initial debugging for preconfiguration debugging +Spong::Log::set_debug_context( 'debuglevel' => $debuglevel ); $me = "@@BINDIR@@/spong-client"; $conf_file = $ARGV[0] || "@@ETCDIR@@/spong.conf"; @@ -37,6 +51,7 @@ $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); &handle_signals(); # Set up handlers, and signal the current server if asked @@ -73,11 +88,56 @@ unlink( "$SPONGTMP/spong-client.pid" ) unless $nosleep; exit(0); - # =========================================================================== # Utility functions, and signal handlers... # =========================================================================== +sub usage { + + print qq +(Usage: + $0 [--debug n] [--nosleep|--refresh] [config_file] + $0 --kill | --restart + + --debug n + Run in the foreround and print debugging output + --nosleep + --refresh + Run one cycle of checks in the foreground and exit + --restart + Signal a running spong-client to restart. + --kill + Signal a running spong-client to terminate. + config_file + Use the named file as the configuration file +); + +} + + +# This function initializes the debug and error logging contexts in the +# Log module. + +sub init_logging { + if (defined $debuglevel) { + $debug = ($debuglevel == 0) ? 1 : $debuglevel + } + + Spong::Log::set_debug_context( 'debuglevel' => $debug ); + + my $filename = ($SPONG_LOG_FILE) ? "$SPONGTMP/spong-client.log" : ""; + my $syslog = ($SPONG_LOG_SYSLOG) ? 1 : 0; + + Spong::Log::set_error_context( syslog => $syslog, + ident => 'spong-client', + logopt => 'pid cons', + priority => 'ERR', + filename => $filename, + ); + +} + + # Load our configuration variables, including anything specific to the host # that we are running on. @@ -138,37 +198,8 @@ sub handle_signals { # Output functions, one for debugging information, the other for errors. -sub debug { print scalar localtime, " ", $_[0], "\n" if $main::debug; } -sub error { warn scalar localtime(), " Error: ", $_[0], "\n"; } - - -# Fork from the parent and become a daemon -sub daemonize { - - my( $pid ); - - # Try to fork - $pid = fork(); - if (! defined $pid) { - die "ERROR: Could not fork: $!"; - } elsif ($pid) { - # I'm the parent, so just exit gracefully - exit(0); - } else { - # I'm the child - - # If we are not debuging, disconnect from the console - if ( ! $debug ) { - open(STDIN,"/dev/null"); - open(STDERR,">/dev/null"); - } - - # Become session group leader - POSIX::setsid(); - } -} - +sub debug { Spong::Log::debug($_[0],$_[1]); } +sub error { Spong::Log::error($_[0]); } # Signal handlers... -- 2.30.2