]>
git.ipfire.org Git - people/stevee/guardian.git/blob - guardian
691ddc66e1f0013add0fd5245f7864d70931d049
2 ###############################################################################
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2015 IPFire Development Team #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
20 ###############################################################################
28 use Time
::HiRes qw
[ time sleep ];
30 require Guardian
::Base
;
31 require Guardian
::Config
;
32 require Guardian
::Daemon
;
33 require Guardian
::Events
;
34 require Guardian
::Logger
;
35 require Guardian
::Parser
;
36 require Guardian
::Socket
;
40 # Disable warnings of unjoinded threads when stopping guardian.
41 no warnings
'threads';
46 # Get and store the given command line arguments in a hash.
49 &GetOptions
(\
%cmdargs,
56 # Show help / version information.
57 if (defined($cmdargs{"help"})) {
58 print "Guardian $version \n";
59 print "Usage: guardian <optional arguments>\n";
60 print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
61 print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n";
62 print " -h, --help\t\tshows this help\n";
63 print " -v, --version\t\tdisplay programm version and exit.\n";
65 } elsif (defined($cmdargs{"version"})) {
66 print "Guardian $version \n";
70 # Check if another instance of guardian is allready running.
71 if (&Guardian
::Daemon
::IsRunning
()) {
72 die "Another instance of Guardian is allready running...\n";
75 # Read-in the configuration file and store the settings.
76 # Push the may be given config file argument.
77 my %mainsettings = &Guardian
::Config
::UseConfig
($cmdargs{"config"});
80 my $logger = Guardian
::Logger
->Init(%mainsettings);
81 $logger->Log("debug", "Logger successfully initialized...");
83 # Add the logger object to the mainsettings for passing
85 $mainsettings{Logger
} = $logger;
87 # Redirect perls "die" messages to the logger before exiting.
88 $SIG{__DIE__
} = sub { $logger->Log("err", "@_"); };
90 # Initialize the event handler.
91 my $events = Guardian
::Events
->Init(%mainsettings);
93 # Hash to store the currently monitored files and their configured
95 my %monitored_files = ();
97 # Shared hash between the main process and all threads. It will store the
98 # monitored files and their current file position.
99 my %file_positions :shared
= ();
101 # Create the main queue. It is used to store and process all events which are
102 # reported and enqueued by the worker threads.
103 my $queue :shared
= new Thread
::Queue
or die "Could not create new, empty queue. $!";;
105 # Array to store all currently running worker objects.
106 # (Does not include the socket thread)
109 # Check if guardian should be daemonized or keep in the foreground.
110 unless (defined($cmdargs{"foreground"})) {
111 # Fork into background.
112 &Guardian
::Daemon
::Daemonize
();
114 # Write PID (process-id).
115 &Guardian
::Daemon
::WritePID
();
118 # Call Init function to initzialize guardian.
121 # Infinite main loop, which processes all queued events.
123 # Get the amount of elements in our queue.
124 # "undef" will be returned if it is empty.
125 my $current_events = $queue->pending();
127 # If there is at least one element enqued
128 if($current_events > 0) {
129 # Grab the data of the top enqueued event.
130 my $event = $queue->peek();
132 # Log processed event.
133 $logger->Log("debug", "QUEUE - Processed event: $event");
135 # Send event data to the events parser to determine
136 # if any action is required.
137 $events->CheckAction($event);
139 # Drop processed event from queue.
143 # Call RemoveBlocks routine from the Events module to check
144 # if items from the block list can be dropped.
145 $events->RemoveBlocks();
147 # Sleep 10ms to reduce the load of the main process.
154 ## This function contains code which has to be executed while guardian
158 # Setup signal handler.
161 # Setup IPC mechanism via Socket in an own thread.
162 threads
->create(\
&Socket
);
164 # Generate hash of monitored files.
165 %monitored_files = &Guardian
::Base
::GenerateMonitoredFiles
(\
%mainsettings, \
%monitored_files);
167 # Start worker threads.
174 ## This function is responsible for monitoring modifications of the given logfile,
175 ## read them and pass them to the message parser.
177 ## To get file modifications the inotify subsystem of the linux kernel is used.
179 ## In order to prevent from permanently read and keep files opened, or dealing
180 ## with huge logfiles, at initialization time of the worker process, the file will
181 ## be opened once and the cursor position of the end of file (EOF) get stored. When
182 ## reading any newly added lines from the file, we directly can jump to the last
183 ## known position and get these lines. Afterwards, we store the current curser position
184 ## again, so we can do it in this way over and over again.
186 ## All read lines get stored in an array, which will be passed to the Parser.
188 ## If any response (action) from the parser get recieved, it will be put into the
189 ## shared event queue.
194 # Obtain the parser name which should be used to parser any
195 # messages of this file.
196 my $parser = $monitored_files{$file};
198 # Signal handler to kill worker.
199 $SIG{'KILL'} = sub { threads
->exit(); };
201 # Create inotify watcher.
202 my $watcher = new Linux
::Inotify2
or die "Could not use inotify. $!";
204 # Monitor the specified file.
205 $watcher->watch("$file", IN_MODIFY
) or die "Could not monitor $file. $!";
207 # Switch watcher into non-blocking mode.
208 $watcher->blocking(0);
210 # Log successfully spawned worker.
211 $logger->Log("debug", "Spawned worker thread for: $file");
215 # Check for any events and perform them, if there
217 if ($watcher->read) {
220 # Obtain fileposition from hash.
221 my $fileposition = $file_positions{$file};
224 open (FILE
, $file) or die "Could not open $file. $!";
226 # Seek to the last known position.
227 seek (FILE
, $fileposition, 0);
229 # Get the log message.
230 while (my $line = <FILE
>) {
231 # Remove any newlines.
234 # Add all lines to the message array.
235 push (@message, $line);
240 lock(%file_positions);
242 # Update fileposition.
243 $file_positions{$file} = tell(FILE
);
249 # Send filename and message to the parser,
250 # which will return if an action has to be performed.
251 my @action = &Guardian
::Parser
::Parser
("$parser", @message);
253 # Send the action to the main process and put it into
259 # Put the required action into the queue.
260 $queue->enqueue(@action);
262 # If no action is returned by the Parser, the message
263 # could not be parser corretly.
265 # Log failed parse attempt.
266 $logger->Log("err", "Error parsing event: \[$parser - @message\]");
269 # Sleep for 10ms until the next round of the loop will start.
278 ## This function uses the Socket module to create and listen to an UNIX socket.
279 ## It automatically accepts all incomming connections and pass the recieved
280 ## data messages to the the Message_Parser function which is also part of the
283 ## If a valid command has been sent through the socket, the corresponding event
284 ## will be enqueued into the shared event queue.
287 # Create the Server socket by calling the responsible function.
288 my $server = &Guardian
::Socket
::Server
();
290 # Log successfull creation of socket.
291 $logger->Log("debug", "Listening to Socket...");
293 # Accept incomming connections from the socket.
294 while (my $connection = $server->accept()) {
295 # Autoflush the socket after the data
297 $connection->autoflush(1);
299 # Gather all data from the connection.
300 while (my $message = <$connection>) {
301 # Remove any newlines.
304 # Log recieved socket command.
305 $logger->Log("debug", "Socket - Recieved message: $message");
307 # Send the recieved data message to the
309 my $action = &Guardian
::Socket
::Message_Parser
($message);
311 # If the parser returns to perform an action,
312 # add it to the main event queue.
317 # Enqueue the returned action.
318 $queue->enqueue($action);
325 ## Function for capturing process signals.
327 ## This function captures any sent process signals and will call various
328 ## actions, basend on the captured signal.
331 $SIG{INT
} = \
&Shutdown
;
332 $SIG{TERM
} = \
&Shutdown
;
333 $SIG{QUIT
} = \
&Shutdown
;
334 $SIG{HUP
} = \
&Reload
;
338 ## Function to start the workers (threads) for all monitored files.
340 ## This function will loop through the hash of monitored files and will
341 ## spawn an own thread based worker for each file. Every created worker will
342 ## be added to the array of running workers.
344 sub StartWorkers
() {
345 # Init/Update hash which contains the cursor position of EOF.
346 %file_positions = &Guardian
::Base
::FilePositions
(\
%monitored_files, \
%file_positions);
348 # Loop through the hash which contains the monitored files and start
349 # a worker thread for each single one.
350 foreach my $file (keys %monitored_files) {
351 $logger->Log("debug", "Starting worker thread for $file");
352 # Create worker thread for the file.
353 push @running_workers, threads
->create(\
&Worker
,$file);
358 ## Function to stop all running workers.
360 ## This function is used to stop all currently running workers and will be
361 ## called when reloading or shutting down guardian.
364 # Loop through all running workers.
365 foreach my $worker (@running_workers) {
366 # Send the worker the "KILL" signal and detach the
367 # thread so perl can do an automatically clean-up.
368 $worker->kill('KILL');
370 $logger->Log("debug", "All workers are stopped now...");
376 ## This function will get called if the signal handler recieves a "SIGHUP" signal,
377 ## or the reload command will be sent via socket connection. It is responsible for
378 ## reloading all configure options and stopping/starting the worker threads.
382 $logger->Log("info", "Reload configuration...");
384 # Stop all running workers.
387 # Re-read configuration file.
388 %mainsettings = &Guardian
::Config
::UseConfig
($cmdargs{"config"});
390 # Update Logger settings.
391 $logger = Guardian
::Logger
->Init(%mainsettings);
393 # Update logger object in mainsettings hash.
394 $mainsettings{Logger
} = $logger;
396 # Update ignore list.
397 &Guardian
::Events
::GenerateIgnoreList
($mainsettings{IgnoreFile
});
399 # Re-generate hash of monitored files.
400 %monitored_files = &Guardian
::Base
::GenerateMonitoredFiles
(\
%mainsettings, \
%monitored_files);
402 # Restart the worker threads.
407 ## Shutdown function.
409 ## This function is used to do a clean shutdown of guardian. It will be called
410 ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
414 $logger->Log("info", "Shutting down...");
419 # Remove socket file on exit.
420 &Guardian
::Socket
::RemoveSocketFile
();
422 # Remove pid file on exit.
423 &Guardian
::Daemon
::RemovePIDFile
();
425 # Sleep for one second to give perl some
426 # time to proper clean up everything before
430 # Log good bye message.
431 $logger->Log("debug", "Good Bye!");