]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Drop PID file when exiting guardian.
[people/stevee/guardian.git] / guardian
index 9245f9004827548fda1f18cecb747c1c11e85bdd..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,7 +71,7 @@ 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
@@ -92,6 +93,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();
 
@@ -344,7 +354,7 @@ sub Reload () {
        %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
 
        # Update Logger settings.
-       $logger = Guardian::Logger->Update(%mainsettings);
+       $logger = Guardian::Logger->Init(%mainsettings);
 
        # Update logger object in mainsettings hash.
        $mainsettings{Logger} = $logger;
@@ -372,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.