]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Prevent from running guardian twice.
[people/stevee/guardian.git] / guardian
index f480c11b9d7437e29e1f0864ebb806638a0dba1d..2a5b460da4d37137e43211570007da01d28b155d 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;
@@ -65,14 +66,23 @@ if (defined($cmdargs{"help"})) {
        exit;
 }
 
+# Check if another instance of guardian is allready running.
+if (&Guardian::Daemon::IsRunning()) {
+       die "Another instance of Guardian is allready running...\n";
+}
+
 # Read-in the configuration file and store the settings.
 # Push the may be given config file argument.
 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", "@_"); };
 
@@ -88,6 +98,15 @@ my $queue :shared = new Thread::Queue or die "Could not create new, empty queue.
 # (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();
 
@@ -339,6 +358,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);
 
@@ -362,6 +387,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.