From 7a17f11379bd9d0e42f340a5f3d45117e17f9670 Mon Sep 17 00:00:00 2001
From: Ramon Novoa <rnovoa@artica.es>
Date: Wed, 28 May 2014 09:37:01 +0000
Subject: [PATCH] 2014-05-28  Ramon Novoa  <rnovoa@artica.es>

	* bin/pandora_server,
	  lib/PandoraFMS/Config.pm: Removed getopts. Fixes a problem found by Junichi.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@10027 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_server/ChangeLog                |  5 ++
 pandora_server/bin/pandora_server       | 75 ++++++++++---------------
 pandora_server/lib/PandoraFMS/Config.pm |  4 ++
 3 files changed, 40 insertions(+), 44 deletions(-)

diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog
index 8594258e53..62c4413a30 100644
--- a/pandora_server/ChangeLog
+++ b/pandora_server/ChangeLog
@@ -1,3 +1,8 @@
+2014-05-28  Ramon Novoa  <rnovoa@artica.es>
+
+	* bin/pandora_server,
+	  lib/PandoraFMS/Config.pm: Removed getopts. Fixes a problem found by Junichi.
+
 2014-05-27  Ramon Novoa  <rnovoa@artica.es>
 
 	* lib/PandoraFMS/Tools.pm: If no logfile is defined print the message to
diff --git a/pandora_server/bin/pandora_server b/pandora_server/bin/pandora_server
index 3570c965bf..ee63b73f8c 100755
--- a/pandora_server/bin/pandora_server
+++ b/pandora_server/bin/pandora_server
@@ -19,7 +19,6 @@
 
 use strict;
 use warnings;
-use Getopt::Std;
 use POSIX qw(strftime);
 use threads;
 
@@ -148,7 +147,7 @@ sub pandora_crash () {
 	# worried about that. If perl has a more "clean" way to avoid this messages
 	# will be nice to replace this code, but at this time it's the only way I know
 	
-	callback_stop() if (defined($Config{'__win32_service__'}));
+	callback_stop() if ($^O eq 'MSWin32' && defined($Config{'win32_service'}));
 
 	foreach my $error_line (@_) {
 		# Trap the XML error and exit without nasty messages
@@ -448,36 +447,24 @@ sub win32_service_run() {
 ################################################################################
 ## Parse command line options.
 ################################################################################
-sub parse_options {
-	my %opts;
-	my $tmp;
-	my @t_addresses_tmp;
+sub parse_service_options ($) {
+	my $config = shift;
 
-	# Get options
-	if (getopts('S:', \%opts) == 0 || defined ($opts{'h'})) {
-		print_help ();
-		exit 1;
-	}
+	# Sanity checks.
+	return unless defined($config->{'win32_service'});
+	die ("[ERROR] Windows services are only available on Win32.\n\n") if ($^O ne 'MSWin32');
 
-	# Win32 service management
-	if (defined ($opts{'S'})) {
-		my $service_action = $opts{'S'};
-		if ($^O ne 'MSWin32') {
-			error ("Windows services are only available on Win32.");
-		} else {
-			eval "use Win32::Daemon";
-			die($@) if ($@);
+	# Win32 service management.
+	eval "use Win32::Daemon";
+	die($@) if ($@);
 
-			if ($service_action eq 'install') {
-				win32_install_service();
-			} elsif ($service_action eq 'uninstall') {
-				win32_uninstall_service();
-			} elsif ($service_action eq 'run') {
-				$Config{'__win32_service__'} == 1;
-			} else {
-				error("Unknown action: $service_action");
-			}
-		}
+	if ($config->{'win32_service'} eq 'install') {
+		win32_install_service();
+	} elsif ($config->{'win32_service'} eq 'uninstall') {
+		win32_uninstall_service();
+	} elsif ($config->{'win32_service'} eq 'run') {
+	} else {
+		die("[ERROR] Unknown action: " . $config->{'win32_service'});
 	}
 }
 
@@ -488,19 +475,6 @@ sub parse_options {
 ################################################################
 sub main() {
 
-	$SIG{'TERM'} = 'pandora_shutdown';
-	$SIG{'INT'} = 'pandora_shutdown';
-	
-	# Error handler needs to be reviewed, Enterprise not found errors are too nasty :(
-	$SIG{__DIE__} = 'pandora_crash';
-	
-	# Prevent alarm from bombing the main thread when called within a thread
-	$SIG{'ALRM'} = 'IGNORE';
-	
-	# Initialize
-	pandora_init(\%Config, 'Pandora FMS Server');
-	pandora_load_config (\%Config);
-	
 	# Daemonize and put in background
 	if ($Config{'daemon'} == 1) {
 		print_message (\%Config, " [*] Backgrounding Pandora FMS Server process.\n", 1);
@@ -623,11 +597,24 @@ sub main() {
 	pandora_shutdown();
 }
 
+$SIG{'TERM'} = 'pandora_shutdown';
+$SIG{'INT'} = 'pandora_shutdown';
+	
+# Error handler needs to be reviewed, Enterprise not found errors are too nasty :(
+$SIG{__DIE__} = 'pandora_crash';
+	
+# Prevent alarm from bombing the main thread when called within a thread
+$SIG{'ALRM'} = 'IGNORE';
+	
+# Initialize
+pandora_init(\%Config, 'Pandora FMS Server');
+pandora_load_config (\%Config);
+	
 # Parse command line options.
-parse_options();
+parse_service_options(\%Config);
 
 # Run as a regular process.
-if (!defined($Config{'__win32_service__'})) {
+if (!defined($Config{'win32_service'})) {
 	main();
 }
 # Run as a Windows service.
diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm
index f816f65d1b..416c7f6a29 100644
--- a/pandora_server/lib/PandoraFMS/Config.pm
+++ b/pandora_server/lib/PandoraFMS/Config.pm
@@ -63,6 +63,7 @@ sub help_screen {
 	print "	-D        :  Daemon mode (runs in background)\n";
 	print "	-P <file> :  Store PID to file.\n";
 	print "	-q        :  Quiet startup \n";
+	print "	-S <install|uninstall|run>:  Manage the win32 service.\n";
 	print "	-h        :  This screen. Shows a little help screen \n";
 	print " \n";
 	exit;
@@ -115,6 +116,9 @@ sub pandora_init {
 		elsif ($parametro =~ m/-D\z/) {
 			$pa_config->{"daemon"}=1;
 		}
+		elsif ($parametro =~ m/^-S\z/i) {
+			$pa_config->{'win32_service'}= clean_blank($ARGV[$ax+1]);
+		}
 		else {
 			($pa_config->{"pandora_path"} = $parametro);
 		}