]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Add ability to reload the ignore list.
[people/stevee/guardian.git] / guardian
index 22856c9395b087be9caffb8072775ce920bed307..d01612525dfe2ec2332b3aefc6afd3b74dd8f111 100644 (file)
--- a/guardian
+++ b/guardian
@@ -2,7 +2,7 @@
 ###############################################################################
 #                                                                             #
 # IPFire.org - A linux based firewall                                         #
-# Copyright (C) 2015  IPFire Development Team                                 #
+# Copyright (C) 2015-2016  IPFire Development Team                            #
 #                                                                             #
 # This program is free software: you can redistribute it and/or modify        #
 # it under the terms of the GNU General Public License as published by        #
@@ -29,6 +29,8 @@ use Time::HiRes qw[ time sleep ];
 
 require Guardian::Base;
 require Guardian::Config;
+require Guardian::Daemon;
+require Guardian::Events;
 require Guardian::Logger;
 require Guardian::Parser;
 require Guardian::Socket;
@@ -65,6 +67,11 @@ 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"});
@@ -80,9 +87,16 @@ $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
+# Initialize the event handler.
+my $events = Guardian::Events->Init(%mainsettings);
+
+# Hash to store the currently monitored files and their configured
+# parsers.
+my %monitored_files = ();
+
+# Shared hash between the main process and all threads. It will store the
 # monitored files and their current file position.
-my %monitored_files :shared = ();
+my %file_positions :shared = ();
 
 # Create the main queue. It is used to store and process all events which are
 # reported and enqueued by the worker threads.
@@ -92,6 +106,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();
 
@@ -109,10 +132,18 @@ while(1) {
                # Log processed event.
                $logger->Log("debug", "QUEUE - Processed event: $event");
 
+               # Send event data to the events parser to determine
+               # if any action is required.
+               $events->CheckAction($event);
+
                # Drop processed event from queue.
                $queue->dequeue();
        }
 
+       # Call RemoveBlocks routine from the Events module to check
+       # if items from the block list can be dropped.
+       $events->RemoveBlocks();
+
        # Sleep 10ms to reduce the load of the main process.
        sleep(0.01);
 }
@@ -160,6 +191,10 @@ sub Init () {
 sub Worker ($) {
        my $file = $_[0];
 
+       # Obtain the parser name which should be used to parser any
+       # messages of this file.
+       my $parser = $monitored_files{$file};
+
        # Signal handler to kill worker.
        $SIG{'KILL'} = sub { threads->exit(); };
 
@@ -183,7 +218,7 @@ sub Worker ($) {
                        my @message = ();
 
                        # Obtain fileposition from hash.
-                       my $fileposition = $monitored_files{$file};
+                       my $fileposition = $file_positions{$file};
 
                        # Open the file.
                        open (FILE, $file) or die "Could not open $file. $!";
@@ -202,27 +237,34 @@ sub Worker ($) {
 
                        {
                                # Lock shared hash.
-                               lock(%monitored_files);
+                               lock(%file_positions);
 
                                # Update fileposition.
-                               $monitored_files{$file} = tell(FILE);
+                               $file_positions{$file} = tell(FILE);
                        }
 
                        # Close file.
                        close(FILE);
 
                        # Send filename and message to the parser,
-                       # which will return if an action has to be performed.
-                       my @action = &Guardian::Parser::Parser("$file", @message);
+                       # which will return if any actions have to be performed.
+                       my @actions = &Guardian::Parser::Parser("$parser", @message);
 
                        # Send the action to the main process and put it into
                        # the queue.
-                       if (@action) {
+                       if (@actions) {
                                # Lock the queue.
                                lock($queue);
 
-                               # Put the required action into the queue.
-                               $queue->enqueue(@action);
+                               # Loop through the actions array, and perform
+                               # every single action.
+                               foreach my $action (@actions) {
+                                       # Prevent from enqueuing empty actions.
+                                       if (defined($action)) {
+                                               # Put the required action into the queue.
+                                               $queue->enqueue($action);
+                                       }
+                               }
                        }
                } else {
                        # Sleep for 10ms until the next round of the loop will start.
@@ -244,7 +286,7 @@ sub Worker ($) {
 #
 sub Socket () {
        # Create the Server socket by calling the responsible function.
-       my $server = &Guardian::Socket::Server();
+       my $server = &Guardian::Socket::Server($mainsettings{SocketOwner});
 
        # Log successfull creation of socket.
        $logger->Log("debug", "Listening to Socket...");
@@ -291,6 +333,7 @@ sub SignalHandler {
        $SIG{TERM} = \&Shutdown;
        $SIG{QUIT} = \&Shutdown;
        $SIG{HUP} = \&Reload;
+       $SIG{USR1} = \&ReloadIgnoreList;
 }
 
 #
@@ -301,6 +344,9 @@ sub SignalHandler {
 ## be added to the array of running workers.
 #
 sub StartWorkers () {
+       # Init/Update hash which contains the cursor position of EOF.
+       %file_positions = &Guardian::Base::FilePositions(\%monitored_files, \%file_positions);
+
        # Loop through the hash which contains the monitored files and start
        # a worker thread for each single one.
        foreach my $file (keys %monitored_files) {
@@ -349,6 +395,9 @@ sub Reload () {
        # Update logger object in mainsettings hash.
        $mainsettings{Logger} = $logger;
 
+       # Update ignore list.
+       &ReloadIgnoreList();
+
        # Re-generate hash of monitored files.
        %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
 
@@ -356,6 +405,25 @@ sub Reload () {
        &StartWorkers();
 }
 
+#
+## ReloadIgnoreList function.
+#
+## This function will be called if the signal handler recieves a "SIGUSR1" signal,
+## or the reload-ignore-list command will be sent via the socket connection. It just
+## performs a simple check if an ignore file has been configured and calls the responsible
+## function on the events module.
+#
+sub ReloadIgnoreList () {
+       # Update ignore list, if an ignorefile has been specified.
+       if (exists($mainsettings{IgnoreFile})) {
+               # Log reload of the ignore list.
+               $logger->Log("info", "Reloading ignore list...");
+
+               # Call responsible function from the events module.
+               &Guardian::Events::GenerateIgnoreList($mainsettings{IgnoreFile});
+       }
+}
+
 #
 ## Shutdown function.
 #
@@ -372,6 +440,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.