From f9dfd4ce4b7f037267648fbd7778ad939c41a2d6 Mon Sep 17 00:00:00 2001 From: opensides Date: Tue, 18 May 2010 15:48:52 +0000 Subject: [PATCH] - Put everything in main in sub fucntions - Put a new parameter in gosa-si-server.default to allow not starting the server (not active yet) git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@18535 594d385d-05f5-0310-b6e9-bd551577e9d8 --- gosa-si/debian/gosa-si-server.default | 4 +- gosa-si/gosa-si-client | 197 +++++++++----------------- gosa-si/gosa-si-server | 143 ++++++++++--------- 3 files changed, 147 insertions(+), 197 deletions(-) diff --git a/gosa-si/debian/gosa-si-server.default b/gosa-si/debian/gosa-si-server.default index 6929d6d9e..595c21dec 100644 --- a/gosa-si/debian/gosa-si-server.default +++ b/gosa-si/debian/gosa-si-server.default @@ -1,4 +1,6 @@ +# Should we start the server +START_SERVER=1 # Configure Debuging Level DEBUG=-vvvvvvv # Debug parts of GOsa-si server see man gosa-si-server -#DEBUGPARTS= \ No newline at end of file +#DEBUGPARTS= diff --git a/gosa-si/gosa-si-client b/gosa-si/gosa-si-client index 9253fd7b4..6675b6ae3 100755 --- a/gosa-si/gosa-si-client +++ b/gosa-si/gosa-si-client @@ -110,7 +110,8 @@ my @servers; my $gotoHardwareChecksum; my $system_com; my $servers_string; -$verbose= 1; + +my $client_status_hash = { 'developmental'=>'revision', 'stable'=>'release'}; # globalise variables which are used in imported events our $global_kernel; @@ -125,6 +126,7 @@ our $client_force_hostname; our $server_key; our $terminal_server_hash; our $opts_dnslookup; +our $verbose= 0; # where is the config stored by default and his name our $config = '/etc/gosa-si/client.conf'; @@ -294,81 +296,6 @@ sub dump_configuration { } } - -#=== FUNCTION ================================================================ -# NAME: check_cmdline_param -# PARAMETERS: -# RETURNS: -# DESCRIPTION: -#=============================================================================== -sub check_cmdline_param () { - my $err_config; - my $err_counter = 0; - if(not defined($cfg_file)) { - $cfg_file = "/etc/gosa-si/client.conf"; - if(! -r $cfg_file) { - $err_config = "please specify a config file"; - $err_counter += 1; - } - } - if( $err_counter > 0 ) { - &usage( "", 1 ); - if( defined( $err_config)) { print STDERR "$err_config\n"} - print STDERR "\n"; - exit( -1 ); - } -} - - -#=== FUNCTION ================================================================ -# NAME: check_pid -# PARAMETERS: -# RETURNS: -# DESCRIPTION: -#=============================================================================== -sub check_pid { - $pid = -1; - # Check, if we are already running - if( open(my $LOCK_FILE, "<","$pid_file") ) { - $pid = <$LOCK_FILE>; - if( defined $pid ) { - chomp( $pid ); - if( -f "/proc/$pid/stat" ) { - my($stat) = `cat /proc/$pid/stat` =~ m/$pid \((.+)\).*/; - if( $0 eq $stat ) { - close($LOCK_FILE); - exit -1; - } - } - } - close( $LOCK_FILE ); - unlink( $pid_file ); - } - - my $LOCK_FILE2; - # create a syslog msg if it is not to possible to open PID file - if (not sysopen(LOCK_FILE, $pid_file, O_WRONLY|O_CREAT|O_EXCL, 0644)) { - my($msg) = "Couldn't obtain lockfile '$pid_file' "; - if (open($LOCK_FILE2, '<', "$pid_file") - && ($pid = <$LOCK_FILE2>)) - { - chomp($pid); - $msg .= "(PID $pid)\n"; - } else { - $msg .= "(unable to read PID)\n"; - } - if( ! ($foreground) ) { - openlog( $0, "cons,pid", "daemon" ); - syslog( "warning", $msg ); - closelog(); - } - else { - print( STDERR " $msg " ); - } - exit( -1 ); - } -} - ############################# # # @brief When caching an interrupt remove file pid and stopping gosa-si @@ -1381,6 +1308,56 @@ sub find_servers { } } +sub start_daemon { + + if( ! $foreground ) { + chdir '/' or die "Can't chdir to /: $!"; + umask 0; + open STDIN, '+>/dev/null' or die "Can't read /dev/null: $!"; + open STDOUT, '+>&STDIN' or die "Can't write to /dev/null: $!"; + open STDERR, '+>&STDIN' or die "Can't write to /dev/null: $!"; + defined(my $child_pid = fork) or die "Can't fork: $!"; + exit if $child_pid; + setsid or die "Can't start a new session: $!"; + } + return; +} + +sub put_version { + + # parse head url and revision from svn + $client_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/; + $client_headURL = defined $1 ? $1 : 'unknown' ; + $client_revision = defined $2 ? $2 : 'unknown' ; + if ($client_headURL =~ /\/tag\// || + $client_headURL =~ /\/branches\// ) { + $client_status = "stable"; + } else { + $client_status = "developmental" ; + } + return; +} + +sub get_perms_admin { + # Determine root uid and adm gid, used for creating log files + $root_uid = getpwnam('root'); + $adm_gid = getgrnam('adm'); + if(not defined $adm_gid){ + $adm_gid = getgrnam('root'); + } + return; +} + +sub create_pid { + # Create the PID object + $pid = File::Pid->new({ file => $pid_file }); + + # Write the PID file + $pid->write; + + return; +} + # #==== MAIN = main ============================================================== # @@ -1405,78 +1382,34 @@ if( defined $dump_config ) { # read and set config parameters &read_configfile($config, %cfg_defaults); -#&check_pid; - -# Create the PID object -# Ensure you put a name that won't clobber -# another program's PID file -$pid = File::Pid->new({ - file => $pid_file, -}); - -# Write the PID file -$pid->write; - -# forward error messages to logfile -if ( ! $foreground ) { - open( STDIN, '+>/dev/null' ); - open( STDOUT, '+>&STDIN' ); - open( STDERR, '+>&STDIN' ); -} -# Just fork, if we are not in foreground mode -if( ! $foreground ) { - chdir '/' or die "Can't chdir to /: $!"; - $pid = fork; - setsid or die "Can't start a new session: $!"; - umask 0; -#} else { -# $pid = $$; -} +# create pid file +&create_pid($pid, $pid_file); -# Do something useful - put our PID into the pid_file -#if( 0 != $pid ) { -# open( my $LOCK_FILE, ">", "$pid_file" ); -# print $LOCK_FILE "$pid\n"; -# close( $LOCK_FILE ); -# if( !$foreground ) { -# exit( 0 ) -# }; -#} - -# parse head url and revision from svn -my $client_status_hash = { 'developmental'=>'revision', 'stable'=>'release'}; -$client_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/; -$client_headURL = defined $1 ? $1 : 'unknown' ; -$client_revision = defined $2 ? $2 : 'unknown' ; -if ($client_headURL =~ /\/tag\// || - $client_headURL =~ /\/branches\// ) { - $client_status = "stable"; -} else { - $client_status = "developmental" ; -} +# daemonize the program +&start_daemon($foreground); # Determine root uid and adm gid, used for creating log files -$root_uid = getpwnam('root'); -$adm_gid = getgrnam('adm'); -if(not defined $adm_gid){ - $adm_gid = getgrnam('root'); -} +&get_perms_admin($root_uid, $adm_gid); + +# put version +&put_version($client_status_hash, $client_version, $client_version, $client_headURL, $client_status); daemon_log(" ", 1); -daemon_log("gosa-si-client started!", 1); +daemon_log("$0 started!", 1); daemon_log("INFO: status: $client_status", 1); daemon_log("INFO: ".$client_status_hash->{$client_status}.": $client_revision", 1); # to be changed maybe not trace of it when running -system('rm -f /tmp/gosa_si_lock*gosa-si-client*'); +#system('rm -f /tmp/gosa_si_lock*gosa-si-client*'); # detect ip and mac address and complete host address $client_address = $client_ip.":".$client_port; my $network_interface= &get_interface_for_ip($client_ip); $client_mac_address= &get_mac($network_interface); + daemon_log("INFO: ip address detected: $client_ip", 1); -daemon_log("INFO: gosa-si-client mac address detected: $client_mac_address", 1); +daemon_log("INFO: $0 mac address detected: $client_mac_address", 1); # import events diff --git a/gosa-si/gosa-si-server b/gosa-si/gosa-si-server index 9765c3ebe..88e13681e 100755 --- a/gosa-si/gosa-si-server +++ b/gosa-si/gosa-si-server @@ -144,6 +144,8 @@ my %repo_files=(); my $repo_path; my %repo_dirs=(); +my $server_status_hash = { 'developmental'=>'revision', 'stable'=>'release'}; + # Variables declared in config file are always set to 'our' our (%cfg_defaults, $log_file, $pid_file, $pid, $server_ip, $server_port, $ClientPackages_key, $dns_lookup, @@ -744,8 +746,7 @@ sub sig_int_handler { daemon_log("shutting down gosa-si-server", 1); -# should be done with the alias TCP_SERVER - $global_kernel->yield('shutdown'); + $global_kernel->yield(TCP_SERVER => 'shutdown'); # to be removed rather crude !! #system("kill `ps -C gosa-si-server -o pid=`"); @@ -3621,6 +3622,66 @@ sub control_server_registration { return; } +sub start_daemon { + + if( ! $foreground ) { + chdir '/' or die "Can't chdir to /: $!"; + umask 0; + open STDIN, '+>/dev/null' or die "Can't read /dev/null: $!"; + open STDOUT, '+>&STDIN' or die "Can't write to /dev/null: $!"; + open STDERR, '+>&STDIN' or die "Can't write to /dev/null: $!"; + defined(my $child_pid = fork) or die "Can't fork: $!"; + exit if $child_pid; + setsid or die "Can't start a new session: $!"; + } + return; +} + +sub put_version { + + # parse head url and revision from svn + $server_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/; + $server_headURL = defined $1 ? $1 : 'unknown' ; + $server_revision = defined $2 ? $2 : 'unknown' ; + if ($server_headURL =~ /\/tag\// || + $server_headURL =~ /\/branches\// ) { + $server_status = "stable"; + } else { + $server_status = "developmental" ; + } + return; +} + +sub get_perms_admin { +# Determine root uid and adm gid, used for creating log files + $root_uid = getpwnam('root'); + $adm_gid = getgrnam('adm'); + if(not defined $adm_gid){ + $adm_gid = getgrnam('root'); + } + return; +} + +sub open_log { + # Prepare log file and set permissions + open(my $log, ">>", "$log_file"); + close($log); + chmod(0440, $log_file); + chown($root_uid, $adm_gid, $log_file); + + return; +} + +sub create_pid { + # Create the PID object + $pid = File::Pid->new({ file => $pid_file }); + + # Write the PID file + $pid->write; + + return; +} + # #==== MAIN = main ============================================================== # @@ -3646,65 +3707,21 @@ if( defined $dump_config ) { # read and set config parameters &read_configfile($config, %cfg_defaults); -#&check_pid; - -# not sure but seems to be for igonring phantom -$SIG{CHLD} = 'IGNORE'; - -# Create the PID object -# -$pid = File::Pid->new({ - file => $pid_file, -}); - -# Write the PID file -$pid->write; - -# forward error messages to logfile -if( ! $foreground ) { - open( STDIN, '+>/dev/null' ); - open( STDOUT, '+>&STDIN' ); - open( STDERR, '+>&STDIN' ); -} - -# Just fork, if we are not in foreground mode -if( ! $foreground ) { - chdir '/' or die "Can't chdir to /: $!"; - $pid = fork; - setsid or die "Can't start a new session: $!"; - umask 0; -#} else { -# $pid = $$; -} - -# Do something useful - put our PID into the pid_file -#if( 0 != $pid ) { -# open( my $LOCK_FILE, ">", "$pid_file" ); -# print $LOCK_FILE "$pid\n"; -# close( $LOCK_FILE ); -# if( !$foreground ) { -# exit( 0 ) -# }; -#} - -# parse head url and revision from svn -my $server_status_hash = { 'developmental'=>'revision', 'stable'=>'release'}; -$server_version =~ /^\$HeadURL: (\S+) \$:\$Rev: (\d+) \$$/; -$server_headURL = defined $1 ? $1 : 'unknown' ; -$server_revision = defined $2 ? $2 : 'unknown' ; -if ($server_headURL =~ /\/tag\// || - $server_headURL =~ /\/branches\// ) { - $server_status = "stable"; -} else { - $server_status = "developmental" ; -} -# Prepare log file and set permissions -$root_uid = getpwnam('root'); -$adm_gid = getgrnam('adm'); -open(my $FH, ">>", "$log_file"); -close($FH); -chmod(0440, $log_file); -chown($root_uid, $adm_gid, $log_file); + +# create pid file +&create_pid($pid, $pid_file); + +# daemonize the program +&start_daemon($foreground); + +# Determine root uid and adm gid, used for creating log files +&get_perms_admin($root_uid, $adm_gid); + +# put version +&put_version($server_status_hash, $server_version, $server_headURL, $server_revision, $server_status); + +#open log file +&open_log($root_uid, $adm_gid, $log_file); # prepare directory for databases mkpath('/var/lib/gosa-si', 0, {owner=>'root', group=>'root'}); @@ -3718,7 +3735,6 @@ unlink '/tmp/gosa_si_tmp_sources_list'; # remove marker that the list creation is in progress unlink '/tmp/packages_list_creation_in_progress'; - daemon_log(" ", 1); daemon_log("$0 started!", 1); daemon_log("status: $server_status", 1); @@ -3878,6 +3894,7 @@ if ($opsi_enabled eq "true") { $opsi_client = new JSON::RPC::Client; } +daemon_log("0 INFO: start socket for incoming xml messages at port '$server_port' ", 1); POE::Component::Server::TCP->new( Alias => "TCP_SERVER", @@ -3913,8 +3930,6 @@ POE::Component::Server::TCP->new( } ); -daemon_log("0 INFO: start socket for incoming xml messages at port '$server_port' ", 1); - # create session for repeatedly checking the job queue for jobs POE::Session->create( inline_states => { -- 2.30.2