]>
git.ipfire.org Git - people/stevee/guardian.git/blob - modules/Socket.pm
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 'reload-ignore-list' => 'reload-ignore-list',
23 'logrotate' => 'logrotate',
27 ## Socket server function.
29 ## It is used to create the server component to provide an IPC
30 ## mechanism for guardian. The server function creates an UNIX
34 my $socket_owner = shift;
36 # If the path for the socketfile does not exist, try to
38 unless (-d
"$socketpath") {
39 mkdir("$socketpath") or die "Could not create $socketpath: $!";
42 # Delete an existing socket file.
43 if (-e
"$socketfile") {
45 or die "Could not release existing socket file: $!";
48 # Create a new UNIX socket.
49 my $server = IO
::Socket
::UNIX
->new(
53 ) or die "Could not create socket: $!";
56 # Translate the given user/group name into ID values.
57 if (defined ($socket_owner)) {
58 # Splitt provided user/group into single arguments.
59 my ($username, $groupname) = split(/:/, $socket_owner);
61 # Get the ID for the given user name.
62 my $uid = getpwnam($username) or die "Could not get an UID for $username: $!";
64 # Get the ID for given group name.
65 my $gid = getgrnam($groupname) or die "Could not get a GID for $groupname: $!";
67 # Set new ownership for the socket file.
68 chown($uid, $gid, "$socketfile") or die "Could not change ownership to ($uid:$gid) for $socketfile: $!";
71 # Return the server object.
76 ## A client for the socket server.
78 ## This function provides a simple client to connect to the server socket
79 ## and send messages through it to a running guardian process.
84 # Create the client and connect to the server socket.
85 my $client = IO
::Socket
::UNIX
->new(
88 ) or die "Could not connect to socketfile: $!\n";
93 # Send the message through the socket.
94 print $client "$message\n";
98 ## The Socket message parser.
100 ## This subfunction is responsible for parsing any data or messages
101 ## which have been recieved through the server socket.
103 sub Message_Parser
($) {
104 # Splitt the message into single parts.
105 # The first part contains the command, the second
106 # one an optional argument.
107 my @message = split(/ /, $_[0]);
108 my ($command, $optarg, $unsupported) = @message;
110 # Currently only socket messages with two arguments (command and
111 # the additional argument which is required for some commands)
112 # are supported. If a third argument is passed, the message
113 # is invalid and the parser returns false.
118 # Check if we got a supported command.
119 if (exists $supported_commands{$command}) {
120 # Check an optional argument has been given.
122 # The message is valid, return the event and
123 # the optional argument.
124 return "$supported_commands{$command} $optarg Socket User-requested action.";
126 # Only return the event for the recieved command.
127 return "$supported_commands{$command}";
131 # If we got here, the command was not supported or something different
132 # went wrong. In such cases the function will return undef (False).
137 ## RemoveSocketFile function.
139 ## A tiny function which just removes an existing Socket file.
141 sub RemoveSocketFile
() {
142 # Check if a socketfile exists.
143 if (-e
$socketfile) {
144 # Delete the socket file.