]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Socket.pm
5e5a9982e86cfff87603117591588713e7e12dd1
1 package Guardian
::Socket
;
5 use Exporter
qw(import);
7 our @EXPORT_OK = qw(Server Client Message_Parser RemoveSocketFile);
9 use IO
::Socket
::UNIX
qw( SOCK_STREAM SOMAXCONN );
11 # The path and filename to the UNIX socket.
12 my $socketpath = "/run/guardian";
13 my $socketfile = "/run/guardian/guardian.sock";
15 # Hash with all supported socket commands and events which should
16 # be returned by the parser.
17 my %supported_commands = (
19 'unblock' => 'unblock',
22 'logrotate' => 'logrotate',
26 ## Socket server function.
28 ## It is used to create the server component to provide an IPC
29 ## mechanism for guardian. The server function creates an UNIX
33 # If the path for the socketfile does not exist, try to
35 unless (-d
"$socketpath") {
36 mkdir("$socketpath") or die "Could not create $socketpath: $!\n";
39 # Delete an existing socket file.
40 if (-e
"$socketfile") {
42 or die "Could not release existing socket file: $!\n";
45 # Create a new UNIX socket.
46 my $server = IO
::Socket
::UNIX
->new(
50 ) or die "Could not create socket: $!\n";
52 # Return the server object.
57 ## A client for the socket server.
59 ## This function provides a simple client to connect to the server socket
60 ## and send messages through it to a running guardian process.
65 # Create the client and connect to the server socket.
66 my $client = IO
::Socket
::UNIX
->new(
69 ) or die "Could not connect to socketfile: $!\n";
74 # Send the message through the socket.
75 print $client "$message\n";
79 ## The Socket message parser.
81 ## This subfunction is responsible for parsing any data or messages
82 ## which have been recieved through the server socket.
84 sub Message_Parser
($) {
85 # Splitt the message into single parts.
86 # The first part contains the command, the second
87 # one an optional argument.
88 my @message = split(/ /, $_[0]);
89 my ($command, $optarg, $unsupported) = @message;
91 # Currently only socket messages with two arguments (command and
92 # the additional argument which is required for some commands)
93 # are supported. If a third argument is passed, the message
94 # is invalid and the parser returns false.
99 # Check if we got a supported command.
100 if (exists $supported_commands{$command}) {
101 # Check an optional argument has been given.
103 # The message is valid, return the event and
104 # the optional argument.
105 return "$supported_commands{$command} $optarg";
107 # Only return the event for the recieved command.
108 return "$supported_commands{$command}";
112 # If we got here, the command was not supported or something different
113 # went wrong. In such cases the function will return undef (False).
118 ## RemoveSocketFile function.
120 ## A tiny function which just removes an existing Socket file.
122 sub RemoveSocketFile
() {
123 # Check if a socketfile exists.
124 if (-e
$socketfile) {
125 # Delete the socket file.