]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Drop PID file when exiting guardian.
[people/stevee/guardian.git] / guardian
index ba8e99cf52400c8c92cebdbdc2a1441cf53cd95c..58a08195038eaf771b747b12b8e9cd8b4f603d60 100644 (file)
--- a/guardian
+++ b/guardian
@@ -29,6 +29,7 @@ use Time::HiRes qw[ time sleep ];
 
 require Guardian::Base;
 require Guardian::Config;
+require Guardian::Daemon;
 require Guardian::Logger;
 require Guardian::Parser;
 require Guardian::Socket;
@@ -70,21 +71,37 @@ if (defined($cmdargs{"help"})) {
 my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
 
 # Initialize Logger.
-my $logger = Guardian::Logger->New(%mainsettings);
+my $logger = Guardian::Logger->Init(%mainsettings);
 $logger->Log("debug", "Logger successfully initialized...");
 
+# Add the logger object to the mainsettings for passing
+# it to the modules.
+$mainsettings{Logger} = $logger;
+
+# Redirect perls "die" messages to the logger before exiting.
+$SIG{__DIE__} = sub { $logger->Log("err", "@_"); };
+
 # Shared hash between the main process and all threads. It will store all
 # monitored files and their current file position.
 my %monitored_files :shared = ();
 
 # Create the main queue. It is used to store and process all events which are
 # reported and enqueued by the worker threads.
-my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!\n";;
+my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!";;
 
 # Array to store all currently running worker objects.
 # (Does not include the socket thread)
 my @running_workers;
 
+# Check if guardian should be daemonized or keep in the foreground.
+unless (defined($cmdargs{"foreground"})) {
+       # Fork into background.
+       &Guardian::Daemon::Daemonize();
+} else {
+       # Write PID (process-id).
+       &Guardian::Daemon::WritePID();
+}
+
 # Call Init function to initzialize guardian.
 &Init();
 
@@ -157,10 +174,10 @@ sub Worker ($) {
        $SIG{'KILL'} = sub { threads->exit(); };
 
        # Create inotify watcher.
-       my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!\n";
+       my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!";
 
        # Monitor the specified file.
-       $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!\n";
+       $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!";
 
        # Switch watcher into non-blocking mode.
        $watcher->blocking(0);
@@ -179,7 +196,7 @@ sub Worker ($) {
                        my $fileposition = $monitored_files{$file};
 
                        # Open the file.
-                       open (FILE, $file) or die "Could not open $file. $!\n";
+                       open (FILE, $file) or die "Could not open $file. $!";
 
                        # Seek to the last known position.
                        seek (FILE, $fileposition, 0);
@@ -336,6 +353,12 @@ sub Reload () {
        # Re-read configuration file.
        %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
 
+       # Update Logger settings.
+       $logger = Guardian::Logger->Init(%mainsettings);
+
+       # Update logger object in mainsettings hash.
+       $mainsettings{Logger} = $logger;
+
        # Re-generate hash of monitored files.
        %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
 
@@ -359,6 +382,9 @@ sub Shutdown () {
        # Remove socket file on exit.
        &Guardian::Socket::RemoveSocketFile();
 
+       # Remove pid file on exit.
+       &Guardian::Daemon::RemovePIDFile();
+
        # Sleep for one second to give perl some
        # time to proper clean up everything before
        # exiting.