r24649 - in /scripts/KGB: debian/changelog server/KGB

tincho at users.alioth.debian.org tincho at users.alioth.debian.org
Wed Aug 27 09:42:28 UTC 2008


Author: tincho
Date: Wed Aug 27 09:42:26 2008
New Revision: 24649

URL: http://svn.debian.org/wsvn/?sc=1&rev=24649
Log:
server/KGB: daemonize, handle parent/child communication during setup, and
forcefully die when POE gets silly.

Modified:
    scripts/KGB/debian/changelog
    scripts/KGB/server/KGB

Modified: scripts/KGB/debian/changelog
URL: http://svn.debian.org/wsvn/scripts/KGB/debian/changelog?rev=24649&op=diff
==============================================================================
--- scripts/KGB/debian/changelog (original)
+++ scripts/KGB/debian/changelog Wed Aug 27 09:42:26 2008
@@ -17,5 +17,7 @@
   * client/*: remove hardcodings.
   * debian/init: make use of the SIG(HUP|QUIT) handlers.
   * client/KGB_sendcommit: use v1 protocol.
+  * server/KGB: daemonize, handle parent/child communication during setup, and
+    forcefully die when POE gets silly.
 
  -- Damyan Ivanov <dmn at debian.org>  Mon, 28 Jul 2008 14:44:04 +0300

Modified: scripts/KGB/server/KGB
URL: http://svn.debian.org/wsvn/scripts/KGB/server/KGB?rev=24649&op=diff
==============================================================================
--- scripts/KGB/server/KGB (original)
+++ scripts/KGB/server/KGB Wed Aug 27 09:42:26 2008
@@ -44,6 +44,8 @@
 use strict;
 use warnings;
 
+use Cwd;
+
 our $config;
 our $config_file;
 our %const = (
@@ -56,11 +58,12 @@
     "0" => 1,
     "1" => 1,
 );
-our @argv;
+our $progname;
 our $restart = 0;
-
-sub save_argv () {
-    @argv = ($0, @ARGV);
+our $shuttingdown = 0;
+
+sub save_progname () {
+    $progname = Cwd::realpath($0);
 }
 sub read_conf ($) {
     my $file = shift;
@@ -122,7 +125,7 @@
     my $conf = read_conf($file);
 
     # Save globals
-    $config_file = $file;
+    $config_file = Cwd::realpath($file);
     $config = $conf;
     return $conf;
 }
@@ -180,19 +183,27 @@
 }
 sub sighandler {
     my($kernel, $sig) = ($_[KERNEL], $_[ARG0]);
+    if($KGB::shuttingdown) {
+        die "Dying forcefully...\n";
+    }
     warn "Deadly signal $sig received, exiting...\n";
     $kernel->sig_handled();
     $kernel->signal($kernel => 'POCOIRC_SHUTDOWN', "KGB going to drink vodka");
     $kernel->post(SOAPServer => 'STOPLISTEN');
     my $heap = $_[HEAP];
     delete $heap->{$_} foreach(keys %$heap);
+    $KGB::shuttingdown = 1;
     undef;
 }
 sub restarthandler {
     my($kernel, $sig) = ($_[KERNEL], $_[ARG0]);
+    if($KGB::shuttingdown) {
+        die "Dying forcefully...\n";
+    }
     warn "Signal $sig received, restarting...\n";
     $kernel->sig_handled();
     $KGB::restart = 1;
+    $KGB::shuttingdown = 1;
     $kernel->signal($kernel => 'POCOIRC_SHUTDOWN', "KGB restartink");
     $kernel->post(SOAPServer => 'STOPLISTEN');
     my $heap = $_[HEAP];
@@ -209,6 +220,7 @@
     } elsif($ret == -2) { # needs reload
         warn "Forcing restart\n";
         $KGB::restart = 1;
+        $KGB::shuttingdown = 1;
         $kernel->signal($kernel => 'POCOIRC_SHUTDOWN', "KGB restartink");
         $kernel->post(SOAPServer => 'STOPLISTEN');
         my $heap = $_[HEAP];
@@ -501,22 +513,57 @@
 use YAML ();
 use Proc::PID::File;
 
-KGB::save_argv();
+KGB::save_progname();
 $KGB::out = \*STDERR;
 
 my $conf_file = '/etc/kgb-bot/kgb.conf';
+my $foreground = 0;
 GetOptions(
-    'config=s'  => \$conf_file,
+    'config=s'      => \$conf_file,
+    'foreground'    => \$foreground,
 ) or die 'Invalid parameters';
 
 @ARGV and die "No command line arguments supported\n";
 
 KGB::load_conf($conf_file);
 
-die "Already running\n" if(Proc::PID::File->running(
-        verify	=> 1,
-        dir	=> $KGB::config->{pid_dir},
-    ));
+unless($foreground) {
+    pipe IN, OUT or die "pipe: $!\n";
+    my $pid = fork();
+    die "Can't fork: $!" unless(defined $pid);
+    if($pid) {
+        close OUT;
+        my $r = join("", <IN>);
+        close IN or die $!;
+        if($r =~ /^OK$/) {
+            exit 0;
+        } else {
+            die $r;
+        }
+    }
+    close IN;
+    eval {
+        die "Already running\n" if(Proc::PID::File->running(
+                verify	=> 1,
+                dir	=> $KGB::config->{pid_dir},
+            ));
+        POSIX::setsid() or die "setsid: $!\n";
+        umask(0022);
+        chdir("/") or die "chdir: $!\n";
+        open(STDIN, "<", "/dev/null") or die "Error closing stdin: $!\n";
+        open(STDOUT, ">", "/dev/null") or die "Error closing stdout: $!\n";
+        open(STDERR, ">", "/dev/null") or die "Error closing stderr: $!\n";
+        #open(STDOUT, ">>", $config{LogFile}) or die "Error opening log: $!\n";
+        #open(STDERR, ">>", $config{LogFile}) or die "Error opening log: $!\n";
+    };
+    if($@) {
+        print OUT $@;
+        exit 1;
+    } else {
+        print OUT "OK\n";
+        close OUT;
+    }
+}
 
 POE::Component::Server::SOAP->new(
     ALIAS   => $KGB::const{SOAPsvc},
@@ -532,11 +579,12 @@
         irc_public _default) ],
         "KGB::SOAP" => [ qw(commit) ],
     ],
-#    options => {trace => 1, debug => 0}
+#    options => {trace => 1, debug => 1}
 );
 
 $poe_kernel->run;
 if($KGB::restart) {
-    exec @KGB::argv or die "couldn't re-exec: $!\ņ";
+    exec($KGB::progname, '--foreground', '--config', $KGB::config_file)
+        or die "couldn't re-exec: $!\ņ";
 }
 exit 0;




More information about the Pkg-perl-cvs-commits mailing list