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