]> git.ipfire.org Git - people/stevee/guardian.git/commitdiff
Use socket module to provide an IPC mechanism.
authorStefan Schantl <stefan.schantl@ipfire.org>
Fri, 20 Nov 2015 21:56:02 +0000 (22:56 +0100)
committerStefan Schantl <stefan.schantl@ipfire.org>
Fri, 20 Nov 2015 21:56:02 +0000 (22:56 +0100)
Guardian now supports inter-process-communication based on
an UNIX socket, which is based on the guardians socket module.

All recieved messages via socket automatically will be accepted
and parsed. If they are valid, the corresponding events will be
enqueued into the main event queue of guardian.

Signed-off-by: Stefan Schantl <stefan.schantl@ipfire.org>
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.
 #