RC-Script ferdsch. bind8
authorheiko
Mon, 27 Jan 2003 15:42:03 +0000
changeset 8 098af5defd01
parent 7 836e273e9992
child 9 6f5ef6fb479d
RC-Script ferdsch. Go daemon per default.
master_watcher
rc.master_watcher
--- a/master_watcher	Fri Jan 24 07:04:42 2003 +0000
+++ b/master_watcher	Mon Jan 27 15:42:03 2003 +0000
@@ -1,4 +1,5 @@
 #! /usr/bin/perl -w
+# $Id$
 my $USAGE = <<'#';
 Usage: $ME [options]
        -l --logfile=s   Name of the logfile we've to read [$opt_logfile]
@@ -9,6 +10,7 @@
        -h --help        This text [$opt_help]
           --daemon      go into background [$opt_daemon]
        -p --pidfile=s   file to store the pid [$opt_pidfile]
+       -v --version     print version [$opt_version]
 #
 # Es wird ein Verzeichnis geben, in diesem Verzeichnis liegt für 
 # *jede* Zone eine eigene Konfigurations-Datei.
@@ -62,6 +64,7 @@
 
 $SIG{__DIE__} = sub { syslog(LOG_ERR, $_[0]); exit -1; };
 $SIG{__WARN__} = sub { syslog(LOG_WARNING, $_[0]); };
+$SIG{TERM} = $SIG{INT} = sub { exit 0; };
 
 my %seen;
 
@@ -72,7 +75,8 @@
 my $opt_follow = 0;
 my $opt_update = 0;
 my $opt_debug = 0;
-my $opt_daemon = 0;
+my $opt_daemon = 1;
+my $opt_version = 0;
 
 my $naptime = 60;
 
@@ -80,6 +84,13 @@
 sub updateFile($$$);
 sub debug($;@) { syslog(LOG_DEBUG, "DEBUG " . shift @_, @_) if $opt_debug; }
 
+END {
+    open(PID, $opt_pidfile);
+    my $pid = <PID>;
+    close(PID);
+    unlink $opt_pidfile if $$ == $pid;
+}
+
 MAIN: {
 
 
@@ -94,6 +105,7 @@
 	"debug!" => \$opt_debug,
 	"daemon!" => \$opt_daemon,
 	"pidfile=s" => \$opt_pidfile,
+	"version!" => \$opt_version,
 	"zonesdir=s" => \$opt_zonesdir)
     or die "$ME: Bad Usage\n";
 
@@ -102,13 +114,11 @@
 	exit 0;
     }
 
-    open (LOGFILE, $_ = "<$opt_logfile") or die "Can't open $_: $!\n";
+    if ($opt_version) {
+	print "$ME Version: ", '$Id$', "\n";
+	exit 0;
+    }
 
-    # Go Daemon
-    #if ($opt_daemon) {
-#	my $pid = fork();
-#	if 
-#    }
 
     # Create the PID-File
     {
@@ -116,12 +126,33 @@
 	print PID "$$\n";
 	close(PID);
 
-	if (!rename($_ = "$opt_pidfile.$$", $opt_pidfile)) {
+	if (!link($_ = "$opt_pidfile.$$", $opt_pidfile)) {
 	    unlink "$opt_pidfile.$$";
 	    die "There's another $ME running.  Bad. Stop.";
 	}
+	unlink "$opt_pidfile.$$";
     }
 
+    if ($opt_daemon) {
+	my $pid = fork();
+
+	if ($pid < 0) {
+	    die "Can't fork: $!\n";
+	} 
+
+	if ($pid) {
+	    open(PID, $_ = ">$opt_pidfile") or die "Can't open $_: $!\n";
+	    print PID "$pid\n";
+	    close(PID);
+	    exit 0;
+	}
+
+	close(STDIN); close(STDOUT); close(STDERR);
+    }
+
+
+    open (LOGFILE, $_ = "<$opt_logfile") or die "Can't open $_: $!\n";
+
     for (;;) {
 	my (%masters, %missing, %nomasters);
 	while (<LOGFILE>) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rc.master_watcher	Mon Jan 27 15:42:03 2003 +0000
@@ -0,0 +1,33 @@
+#! /bin/bash
+
+DAEMON=/usr/local/sbin/master_watcher
+PIDFILE=/var/run/master_watcher.pid
+
+test -x $MASTER_WATCHER || exit 0
+
+case $1 in
+	
+	start)	
+		echo -n "Starting $DAEMON..."
+		start-stop-daemon --start --pid $PIDFILE --exec $DAEMON -- -f
+		echo " OK";
+		;;
+		
+	stop)	
+		echo -n "Stopping $DAEMON..."
+		start-stop-daemon --stop --pid $PIDFILE
+		echo " OK";
+		;;
+		
+	restart) echo -n "Restarting $DAEMON..."
+		$0 stop
+		$0 start
+		;;
+
+	*)
+		echo "Usage: /etc/init.d/$DAEMON {start|stop|restart}" >&2
+		exit 1			
+		;;
+esac
+
+exit 0