Code

Apply patch for #6281 from LHM
[gosa.git] / trunk / gosa-si / gosa-si-client
index 481ea3e1e5b42c4bd5af358fd31e0263afe3dd90..403261ac6300e77c4eefba23731ed706c27b9902 100755 (executable)
@@ -221,15 +221,43 @@ sub check_pid {
     }
 }
 
+sub clean_shutdown
+{
+    unlink($pid_file) if (-w $pid_file);
+    unlink($gosa_si_client_fifo) if (-S $gosa_si_client_fifo);
+}
 
-sub sig_int_handler {
+sub sig_int_or_term_handler
+{
     my ($signal) = @_;
+    daemon_log("Got SIG${signal} - shutting down gosa-si-client", 1);
+    clean_shutdown();
+    POE::Kernel->signal('client_session', 'KILL');
+    POE::Kernel->signal('gosa-si-client', 'KILL');
+    return 1;
+}
+
+sub sig_warn_handler
+{
+    my @loc = caller(0);
+    daemon_log( "SIGWARN line " . $loc[2] . ": " . $_[0], 1 );
+    return 1;
+}
        
-    daemon_log("shutting down gosa-si-client", 1);
-    system("kill `ps -C gosa-si-client -o pid=`");
+sub sig_die_handler
+{
+    my @loc = caller(0);
+    daemon_log( "SIGDIE line " . $loc[2] . ": " . $_[0], 1 );
+    clean_shutdown();
+    return 1;
 }
-$SIG{INT} = \&sig_int_handler;
 
+$SIG{'INT'} = \&sig_int_or_term_handler;
+$SIG{'TERM'} = \&sig_int_or_term_handler;
+$SIG{'__WARN__'} = \&sig_warn_handler;
+$SIG{'__DIE__'} = \&sig_die_handler;
+$SIG{'USR1'} = 'IGNORE';
+$SIG{'USR2'} = 'IGNORE';
 
 #===  FUNCTION  ================================================================
 #         NAME:  logging
@@ -630,10 +658,8 @@ sub register_at_gosa_si_server {
                 last;
 
                        # delivery of registration msg failed   
-                       } else {
-                       # wait 1 sec until trying to register again
-                               sleep(1);
-                               next;
+                       } elsif($REGISTRATION_TRIES >= scalar(@servers)) {
+                               last;
                        }
 
     } # end of while
@@ -970,21 +996,11 @@ sub save_fai_log {
 }
 
 
-sub sig_handler {
-       my ($kernel, $signal) = @_[KERNEL, ARG0] ;
-       daemon_log("0 INFO got signal '$signal'", 1); 
-       $kernel->sig_handled();
-       return;
-}
-
-
 sub _start {
     my ($kernel, $heap) = @_[KERNEL, HEAP];
     $kernel->alias_set('client_session');
     $global_kernel = $kernel;       # this is used to throw events at each point of the skript
  
-    $kernel->sig(USR1 => "sig_handler");
-
     # force a registration at a gosa-si-server
     $kernel->yield('register_at_gosa_si_server');
     
@@ -1008,6 +1024,16 @@ sub _start {
 }
 
 
+sub _stop {
+    my ($kernel, $session, $heap) = @_[KERNEL, SESSION, HEAP];
+    delete $heap->{'services'};
+    delete $heap->{'watchers'};
+    $kernel->alias_remove($heap->{alias});
+    $kernel->alarm_remove_all();
+    $kernel->post($heap->{child_session}, '_stop');
+}
+
+
 sub _default {
     daemon_log("ERROR: can not handle incoming msg with header '$_[ARG0]'", 1);
     return;
@@ -1209,33 +1235,6 @@ GetOptions("h|help" => \&usage,
 &read_configfile($cfg_file, %cfg_defaults);
 &check_pid;
 
-# 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( 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+) \$$/;
@@ -1264,6 +1263,39 @@ daemon_log("INFO: ".$client_status_hash->{$client_status}.": $client_revision",
 # delete old DBsqlite lock files
 system('rm -f /tmp/gosa_si_lock*gosa-si-client*');
 
+# Just fork, if we are not in foreground mode
+if( ! $foreground ) {
+    if (! chdir('/')) {
+        daemon_log("ERROR: Can't chdir to /: $!");
+        exit( 1 );
+    }
+    umask( 0 );
+    $pid = fork;
+} else { 
+    $pid = $$; 
+}
+
+if( 0 != $pid ) {
+    # Parent: put our PID into the $pid_file
+    open( LOCK_FILE, ">$pid_file" );
+    print LOCK_FILE "$pid\n";
+    close( LOCK_FILE );
+    if( !$foreground ) {
+        exit( 0 );
+    }
+}
+else {
+    # Child
+    open( STDIN,  '+>/dev/null' );
+    open( STDOUT, '+>&STDIN'    );
+    open( STDERR, '+>&STDIN'    );
+    if (! POSIX::setsid()) {
+        deamon_log("ERROR: Can't start a new session: $!", 1);
+        exit( 1 );
+    }
+    $poe_kernel->has_forked() if ($poe_kernel->can('has_forked'));
+}
+
 # detect ip and mac address and complete host address
 $client_address = $client_ip.":".$client_port;
 my $network_interface= &get_interface_for_ip($client_ip);
@@ -1315,8 +1347,8 @@ POSIX::mkfifo("$gosa_si_client_fifo", "0600");
 POE::Session->create(
        inline_states => {
                _start => \&_start, 
+        _stop => \&_stop,
         _default => \&_default,
-        sig_handler => \&sig_handler,
         register_at_gosa_si_server => \&register_at_gosa_si_server,
 
         # trigger periodical tasks