]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Use socket module to provide an IPC mechanism.
[people/stevee/guardian.git] / guardian
index 62cb9cf8f99158ef801b0ca2923eea2770bf3ead..c0773eef175bd1b4e137898b6f56704dfd3fd22d 100644 (file)
--- a/guardian
+++ b/guardian
@@ -26,6 +26,7 @@ use Thread::Queue;
 use Linux::Inotify2;
 
 require Guardian::Parser;
+require Guardian::Socket;
 
 # Array to store the monitored logfiles.
 my @monitored_files = (
@@ -68,6 +69,9 @@ while(1) {
 ## is starting.
 #
 sub Init () {
+       # Setup IPC mechanism via Socket in an own thread.
+       threads->create(\&Socket);
+
        # Loop through the array of which files should be monitored and
        # create a worker thread for each single one.
        foreach my $monitored_file (@monitored_files) {
@@ -152,6 +156,49 @@ sub Worker ($) {
        }
 }
 
+#
+## Socket function.
+#
+## This function uses the Socket module to create and listen to an UNIX socket.
+## It automatically accepts all incomming connections and pass the recieved
+## data messages to the the Message_Parser function which is also part of the
+## socket module.
+#
+## If a valid command has been sent through the socket, the corresponding event
+## will be enqueued into the shared event queue.
+#
+sub Socket () {
+       # Create the Server socket by calling the responsible function.
+       my $server = &Guardian::Socket::Server();
+
+        # Accept incomming connections from the socket.
+        while (my $connection = $server->accept()) {
+               # Autoflush the socket after the data
+               # has been recieved.
+               $connection->autoflush(1);
+
+               # Gather all data from the connection.
+               while (my $message = <$connection>) {
+                       # Remove any newlines.
+                       chomp($message);
+
+                       # Send the recieved data message to the
+                       # socket parser.
+                       my $action = &Guardian::Socket::Message_Parser($message);
+
+                       # If the parser returns to perform an action,
+                       # add it to the main event queue.
+                       if ($action) {
+                               # Lock the queue.
+                               lock($queue);
+
+                               # Enqueue the returned action.
+                               $queue->enqueue($action);
+                       }
+               }
+       }
+}
+
 #
 ## Function for fileposition initialization.
 #