]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Use TimeHires core module for main loop interupting.
[people/stevee/guardian.git] / guardian
index c0773eef175bd1b4e137898b6f56704dfd3fd22d..af5caebf152c89b473a0b42fc476fc53b951a63e 100644 (file)
--- a/guardian
+++ b/guardian
 use strict;
 use threads;
 use threads::shared;
+use Getopt::Long;
 use Thread::Queue;
 use Linux::Inotify2;
+use Time::HiRes qw[ time sleep ];
 
+require Guardian::Config;
 require Guardian::Parser;
 require Guardian::Socket;
 
+# Define version.
+my $version ="2.0";
+
 # Array to store the monitored logfiles.
 my @monitored_files = (
        "/var/log/snort/alert",
 );
 
+# Get and store the given command line arguments in a hash.
+my %cmdargs = ();
+
+&GetOptions (\%cmdargs,
+       'foreground|f',
+       'config|c=s',
+       'help|h',
+       'version|v',
+);
+
+# Show help / version information.
+if (defined($cmdargs{"help"})) {
+       print "Guardian $version \n";
+       print "Usage: guardian <optional arguments>\n";
+       print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
+       print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n";
+       print " -h, --help\t\tshows this help\n";
+       print " -v, --version\t\tdisplay programm version and exit.\n";
+       exit;
+} elsif (defined($cmdargs{"version"})) {
+       print "Guardian $version \n";
+       exit;
+}
+
+# Read-in the configuration file and store the settings.
+# Push the may be given config file argument.
+my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
+
 # 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";;
@@ -57,9 +91,8 @@ while(1) {
                $queue->dequeue();
        }
 
-       # XXX
-       # Temporary workaround to reduce the load of the main process.
-       sleep(1);
+       # Sleep 10ms to reduce the load of the main process.
+       sleep(0.01);
 }
 
 #
@@ -69,6 +102,9 @@ while(1) {
 ## is starting.
 #
 sub Init () {
+       # Setup signal handler.
+       &SignalHandler();
+
        # Setup IPC mechanism via Socket in an own thread.
        threads->create(\&Socket);
 
@@ -227,3 +263,29 @@ sub Init_fileposition ($) {
        # Return the position.
        return $position;
 }
+
+#
+## Function for capturing process signals.
+#
+## This function captures any sent process signals and will call various
+## actions, basend on the captured signal.
+#
+sub SignalHandler {
+       $SIG{INT} = \&Shutdown;
+       $SIG{TERM} = \&Shutdown;
+       $SIG{QUIT} = \&Shutdown;
+}
+
+#
+## Shutdown function.
+#
+## This function is used to do a clean shutdown of guardian. It will be called
+## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
+#
+sub Shutdown () {
+       # Remove socket file on exit.
+       &Guardian::Socket::RemoveSocketFile();
+
+       # Exit guardian.
+       exit;
+}