]>
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 '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 my $socket_owner = shift;
35 # If the path for the socketfile does not exist, try to
37 unless (-d
"$socketpath") {
38 mkdir("$socketpath") or die "Could not create $socketpath: $!";
41 # Delete an existing socket file.
42 if (-e
"$socketfile") {
44 or die "Could not release existing socket file: $!";
47 # Create a new UNIX socket.
48 my $server = IO
::Socket
::UNIX
->new(
52 ) or die "Could not create socket: $!";
55 # Translate the given user/group name into ID values.
56 if (defined ($socket_owner)) {
57 # Splitt provided user/group into single arguments.
58 my ($username, $groupname) = split(/:/, $socket_owner);
60 # Get the ID for the given user name.
61 my $uid = getpwnam($username) or die "Could not get an UID for $username: $!";
63 # Get the ID for given group name.
64 my $gid = getgrnam($groupname) or die "Could not get a GID for $groupname: $!";
66 # Set new ownership for the socket file.
67 chown($uid, $gid, "$socketfile") or die "Could not change ownership to ($uid:$gid) for $socketfile: $!";
70 # Return the server object.
75 ## A client for the socket server.
77 ## This function provides a simple client to connect to the server socket
78 ## and send messages through it to a running guardian process.
83 # Create the client and connect to the server socket.
84 my $client = IO
::Socket
::UNIX
->new(
87 ) or die "Could not connect to socketfile: $!\n";
92 # Send the message through the socket.
93 print $client "$message\n";
97 ## The Socket message parser.
99 ## This subfunction is responsible for parsing any data or messages
100 ## which have been recieved through the server socket.
102 sub Message_Parser
($) {
103 # Splitt the message into single parts.
104 # The first part contains the command, the second
105 # one an optional argument.
106 my @message = split(/ /, $_[0]);
107 my ($command, $optarg, $unsupported) = @message;
109 # Currently only socket messages with two arguments (command and
110 # the additional argument which is required for some commands)
111 # are supported. If a third argument is passed, the message
112 # is invalid and the parser returns false.
117 # Check if we got a supported command.
118 if (exists $supported_commands{$command}) {
119 # Check an optional argument has been given.
121 # The message is valid, return the event and
122 # the optional argument.
123 return "$supported_commands{$command} $optarg Socket User-requested action.";
125 # Only return the event for the recieved command.
126 return "$supported_commands{$command}";
130 # If we got here, the command was not supported or something different
131 # went wrong. In such cases the function will return undef (False).
136 ## RemoveSocketFile function.
138 ## A tiny function which just removes an existing Socket file.
140 sub RemoveSocketFile
() {
141 # Check if a socketfile exists.
142 if (-e
$socketfile) {
143 # Delete the socket file.