diff -r 836e273e9992 -r 098af5defd01 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 = ; + 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 () {