]>
Commit | Line | Data |
---|---|---|
0b0ec9b4 SS |
1 | package Guardian::Socket; |
2 | use strict; | |
3 | use warnings; | |
4 | ||
5 | use Exporter qw(import); | |
6 | ||
27dc0dbf | 7 | our @EXPORT_OK = qw(Server Client Message_Parser RemoveSocketFile); |
0b0ec9b4 SS |
8 | |
9 | use IO::Socket::UNIX qw( SOCK_STREAM SOMAXCONN ); | |
10 | ||
11 | # The path and filename to the UNIX socket. | |
12 | my $socketpath = "/run/guardian"; | |
13 | my $socketfile = "/run/guardian/guardian.sock"; | |
14 | ||
15 | # Hash with all supported socket commands and events which should | |
16 | # be returned by the parser. | |
17 | my %supported_commands = ( | |
18 | 'block' => 'block', | |
19 | 'unblock' => 'unblock', | |
20 | 'flush' => 'flush', | |
21 | 'reload' => 'reload', | |
22 | 'logrotate' => 'logrotate', | |
23 | ); | |
24 | ||
25 | # | |
26 | ## Socket server function. | |
27 | # | |
28 | ## It is used to create the server component to provide an IPC | |
29 | ## mechanism for guardian. The server function creates an UNIX | |
30 | ## socket. | |
31 | # | |
32 | sub Server () { | |
33 | # If the path for the socketfile does not exist, try to | |
34 | # create it. | |
35 | unless (-d "$socketpath") { | |
c0a59a63 | 36 | mkdir("$socketpath") or die "Could not create $socketpath: $!"; |
0b0ec9b4 SS |
37 | } |
38 | ||
39 | # Delete an existing socket file. | |
3ae3ca9b SS |
40 | if (-e "$socketfile") { |
41 | unlink $socketfile | |
c0a59a63 | 42 | or die "Could not release existing socket file: $!"; |
0b0ec9b4 SS |
43 | } |
44 | ||
45 | # Create a new UNIX socket. | |
46 | my $server = IO::Socket::UNIX->new( | |
47 | Local => $socketfile, | |
48 | Listen => SOMAXCONN, | |
49 | Type => SOCK_STREAM, | |
c0a59a63 | 50 | ) or die "Could not create socket: $!"; |
0b0ec9b4 SS |
51 | |
52 | # Return the server object. | |
53 | return $server; | |
54 | } | |
55 | ||
56 | # | |
57 | ## A client for the socket server. | |
58 | # | |
59 | ## This function provides a simple client to connect to the server socket | |
60 | ## and send messages through it to a running guardian process. | |
61 | # | |
62 | sub Client($) { | |
63 | my ($message) = @_; | |
64 | ||
65 | # Create the client and connect to the server socket. | |
66 | my $client = IO::Socket::UNIX->new( | |
67 | Type => SOCK_STREAM, | |
68 | Peer => $socketfile, | |
69 | ) or die "Could not connect to socketfile: $!\n"; | |
70 | ||
71 | # Remove any newline. | |
72 | chomp($message); | |
73 | ||
74 | # Send the message through the socket. | |
75 | print $client "$message\n"; | |
76 | } | |
77 | ||
78 | # | |
79 | ## The Socket message parser. | |
80 | # | |
81 | ## This subfunction is responsible for parsing any data or messages | |
82 | ## which have been recieved through the server socket. | |
83 | # | |
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; | |
90 | ||
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. | |
95 | if ($unsupported) { | |
96 | return undef; | |
97 | } | |
98 | ||
99 | # Check if we got a supported command. | |
100 | if (exists $supported_commands{$command}) { | |
101 | # Check an optional argument has been given. | |
102 | if ($optarg) { | |
103 | # The message is valid, return the event and | |
104 | # the optional argument. | |
ebd440a9 | 105 | return "$supported_commands{$command} $optarg Socket User-requested action."; |
0b0ec9b4 SS |
106 | } else { |
107 | # Only return the event for the recieved command. | |
108 | return "$supported_commands{$command}"; | |
109 | } | |
110 | } | |
111 | ||
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). | |
114 | return undef; | |
115 | } | |
116 | ||
27dc0dbf SS |
117 | # |
118 | ## RemoveSocketFile function. | |
119 | # | |
120 | ## A tiny function which just removes an existing Socket file. | |
121 | # | |
122 | sub RemoveSocketFile () { | |
123 | # Check if a socketfile exists. | |
124 | if (-e $socketfile) { | |
125 | # Delete the socket file. | |
126 | unlink($socketfile); | |
127 | } | |
128 | } | |
129 | ||
0b0ec9b4 | 130 | 1; |