]> git.ipfire.org Git - people/stevee/guardian.git/blob - guardian
Add "Daemon" module.
[people/stevee/guardian.git] / guardian
1 #!/usr/bin/perl
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2015 IPFire Development Team #
6 # #
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. #
11 # #
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. #
16 # #
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/>. #
19 # #
20 ###############################################################################
21
22 use strict;
23 use threads;
24 use threads::shared;
25 use Getopt::Long;
26 use Thread::Queue;
27 use Linux::Inotify2;
28 use Time::HiRes qw[ time sleep ];
29
30 require Guardian::Base;
31 require Guardian::Config;
32 require Guardian::Logger;
33 require Guardian::Parser;
34 require Guardian::Socket;
35
36 use warnings;
37
38 # Disable warnings of unjoinded threads when stopping guardian.
39 no warnings 'threads';
40
41 # Define version.
42 my $version ="2.0";
43
44 # Get and store the given command line arguments in a hash.
45 my %cmdargs = ();
46
47 &GetOptions (\%cmdargs,
48 'foreground|f',
49 'config|c=s',
50 'help|h',
51 'version|v',
52 );
53
54 # Show help / version information.
55 if (defined($cmdargs{"help"})) {
56 print "Guardian $version \n";
57 print "Usage: guardian <optional arguments>\n";
58 print " -c, --config\t\tspecifiy a configuration file other than the default (/etc/guardian/guardian.conf)\n";
59 print " -f, --foreground\trun in the foreground (doesn't fork, any output goes to STDOUT)\n";
60 print " -h, --help\t\tshows this help\n";
61 print " -v, --version\t\tdisplay programm version and exit.\n";
62 exit;
63 } elsif (defined($cmdargs{"version"})) {
64 print "Guardian $version \n";
65 exit;
66 }
67
68 # Read-in the configuration file and store the settings.
69 # Push the may be given config file argument.
70 my %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
71
72 # Initialize Logger.
73 my $logger = Guardian::Logger->Init(%mainsettings);
74 $logger->Log("debug", "Logger successfully initialized...");
75
76 # Add the logger object to the mainsettings for passing
77 # it to the modules.
78 $mainsettings{Logger} = $logger;
79
80 # Redirect perls "die" messages to the logger before exiting.
81 $SIG{__DIE__} = sub { $logger->Log("err", "@_"); };
82
83 # Shared hash between the main process and all threads. It will store all
84 # monitored files and their current file position.
85 my %monitored_files :shared = ();
86
87 # Create the main queue. It is used to store and process all events which are
88 # reported and enqueued by the worker threads.
89 my $queue :shared = new Thread::Queue or die "Could not create new, empty queue. $!";;
90
91 # Array to store all currently running worker objects.
92 # (Does not include the socket thread)
93 my @running_workers;
94
95 # Call Init function to initzialize guardian.
96 &Init();
97
98 # Infinite main loop, which processes all queued events.
99 while(1) {
100 # Get the amount of elements in our queue.
101 # "undef" will be returned if it is empty.
102 my $current_events = $queue->pending();
103
104 # If there is at least one element enqued
105 if($current_events > 0) {
106 # Grab the data of the top enqueued event.
107 my $event = $queue->peek();
108
109 # Log processed event.
110 $logger->Log("debug", "QUEUE - Processed event: $event");
111
112 # Drop processed event from queue.
113 $queue->dequeue();
114 }
115
116 # Sleep 10ms to reduce the load of the main process.
117 sleep(0.01);
118 }
119
120 #
121 ## Init function.
122 #
123 ## This function contains code which has to be executed while guardian
124 ## is starting.
125 #
126 sub Init () {
127 # Setup signal handler.
128 &SignalHandler();
129
130 # Setup IPC mechanism via Socket in an own thread.
131 threads->create(\&Socket);
132
133 # Generate hash of monitored files.
134 %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
135
136 # Start worker threads.
137 &StartWorkers();
138 }
139
140 #
141 ## Worker function.
142 #
143 ## This function is responsible for monitoring modifications of the given logfile,
144 ## read them and pass them to the message parser.
145 #
146 ## To get file modifications the inotify subsystem of the linux kernel is used.
147 #
148 ## In order to prevent from permanently read and keep files opened, or dealing
149 ## with huge logfiles, at initialization time of the worker process, the file will
150 ## be opened once and the cursor position of the end of file (EOF) get stored. When
151 ## reading any newly added lines from the file, we directly can jump to the last
152 ## known position and get these lines. Afterwards, we store the current curser position
153 ## again, so we can do it in this way over and over again.
154 #
155 ## All read lines get stored in an array, which will be passed to the Parser.
156 #
157 ## If any response (action) from the parser get recieved, it will be put into the
158 ## shared event queue.
159 #
160 sub Worker ($) {
161 my $file = $_[0];
162
163 # Signal handler to kill worker.
164 $SIG{'KILL'} = sub { threads->exit(); };
165
166 # Create inotify watcher.
167 my $watcher = new Linux::Inotify2 or die "Could not use inotify. $!";
168
169 # Monitor the specified file.
170 $watcher->watch("$file", IN_MODIFY) or die "Could not monitor $file. $!";
171
172 # Switch watcher into non-blocking mode.
173 $watcher->blocking(0);
174
175 # Log successfully spawned worker.
176 $logger->Log("debug", "Spawned worker thread for: $file");
177
178 # Infinite loop.
179 while(1) {
180 # Check for any events and perform them, if there
181 # is a least one.
182 if ($watcher->read) {
183 my @message = ();
184
185 # Obtain fileposition from hash.
186 my $fileposition = $monitored_files{$file};
187
188 # Open the file.
189 open (FILE, $file) or die "Could not open $file. $!";
190
191 # Seek to the last known position.
192 seek (FILE, $fileposition, 0);
193
194 # Get the log message.
195 while (my $line = <FILE>) {
196 # Remove any newlines.
197 chomp $line;
198
199 # Add all lines to the message array.
200 push (@message, $line);
201 }
202
203 {
204 # Lock shared hash.
205 lock(%monitored_files);
206
207 # Update fileposition.
208 $monitored_files{$file} = tell(FILE);
209 }
210
211 # Close file.
212 close(FILE);
213
214 # Send filename and message to the parser,
215 # which will return if an action has to be performed.
216 my @action = &Guardian::Parser::Parser("$file", @message);
217
218 # Send the action to the main process and put it into
219 # the queue.
220 if (@action) {
221 # Lock the queue.
222 lock($queue);
223
224 # Put the required action into the queue.
225 $queue->enqueue(@action);
226 }
227 } else {
228 # Sleep for 10ms until the next round of the loop will start.
229 sleep(0.01);
230 }
231 }
232 }
233
234 #
235 ## Socket function.
236 #
237 ## This function uses the Socket module to create and listen to an UNIX socket.
238 ## It automatically accepts all incomming connections and pass the recieved
239 ## data messages to the the Message_Parser function which is also part of the
240 ## socket module.
241 #
242 ## If a valid command has been sent through the socket, the corresponding event
243 ## will be enqueued into the shared event queue.
244 #
245 sub Socket () {
246 # Create the Server socket by calling the responsible function.
247 my $server = &Guardian::Socket::Server();
248
249 # Log successfull creation of socket.
250 $logger->Log("debug", "Listening to Socket...");
251
252 # Accept incomming connections from the socket.
253 while (my $connection = $server->accept()) {
254 # Autoflush the socket after the data
255 # has been recieved.
256 $connection->autoflush(1);
257
258 # Gather all data from the connection.
259 while (my $message = <$connection>) {
260 # Remove any newlines.
261 chomp($message);
262
263 # Log recieved socket command.
264 $logger->Log("debug", "Socket - Recieved message: $message");
265
266 # Send the recieved data message to the
267 # socket parser.
268 my $action = &Guardian::Socket::Message_Parser($message);
269
270 # If the parser returns to perform an action,
271 # add it to the main event queue.
272 if ($action) {
273 # Lock the queue.
274 lock($queue);
275
276 # Enqueue the returned action.
277 $queue->enqueue($action);
278 }
279 }
280 }
281 }
282
283 #
284 ## Function for capturing process signals.
285 #
286 ## This function captures any sent process signals and will call various
287 ## actions, basend on the captured signal.
288 #
289 sub SignalHandler {
290 $SIG{INT} = \&Shutdown;
291 $SIG{TERM} = \&Shutdown;
292 $SIG{QUIT} = \&Shutdown;
293 $SIG{HUP} = \&Reload;
294 }
295
296 #
297 ## Function to start the workers (threads) for all monitored files.
298 #
299 ## This function will loop through the hash of monitored files and will
300 ## spawn an own thread based worker for each file. Every created worker will
301 ## be added to the array of running workers.
302 #
303 sub StartWorkers () {
304 # Loop through the hash which contains the monitored files and start
305 # a worker thread for each single one.
306 foreach my $file (keys %monitored_files) {
307 $logger->Log("debug", "Starting worker thread for $file");
308 # Create worker thread for the file.
309 push @running_workers, threads->create(\&Worker,$file);
310 }
311 }
312
313 #
314 ## Function to stop all running workers.
315 #
316 ## This function is used to stop all currently running workers and will be
317 ## called when reloading or shutting down guardian.
318 #
319 sub StopWorkers () {
320 # Loop through all running workers.
321 foreach my $worker (@running_workers) {
322 # Send the worker the "KILL" signal and detach the
323 # thread so perl can do an automatically clean-up.
324 $worker->kill('KILL');
325 }
326 $logger->Log("debug", "All workers are stopped now...");
327 }
328
329 #
330 ## Reload function.
331 #
332 ## This function will get called if the signal handler recieves a "SIGHUP" signal,
333 ## or the reload command will be sent via socket connection. It is responsible for
334 ## reloading all configure options and stopping/starting the worker threads.
335 #
336 sub Reload () {
337 # Log reload.
338 $logger->Log("info", "Reload configuration...");
339
340 # Stop all running workers.
341 &StopWorkers();
342
343 # Re-read configuration file.
344 %mainsettings = &Guardian::Config::UseConfig($cmdargs{"config"});
345
346 # Update Logger settings.
347 $logger = Guardian::Logger->Init(%mainsettings);
348
349 # Update logger object in mainsettings hash.
350 $mainsettings{Logger} = $logger;
351
352 # Re-generate hash of monitored files.
353 %monitored_files = &Guardian::Base::GenerateMonitoredFiles(\%mainsettings, \%monitored_files);
354
355 # Restart the worker threads.
356 &StartWorkers();
357 }
358
359 #
360 ## Shutdown function.
361 #
362 ## This function is used to do a clean shutdown of guardian. It will be called
363 ## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
364 #
365 sub Shutdown () {
366 # Log shutdown.
367 $logger->Log("info", "Shutting down...");
368
369 # Stop all workers.
370 &StopWorkers();
371
372 # Remove socket file on exit.
373 &Guardian::Socket::RemoveSocketFile();
374
375 # Sleep for one second to give perl some
376 # time to proper clean up everything before
377 # exiting.
378 sleep(1);
379
380 # Log good bye message.
381 $logger->Log("debug", "Good Bye!");
382
383 # Exit guardian.
384 exit;
385 }