|
|
076e023 |
#!/usr/bin/perl
|
|
|
076e023 |
|
|
|
076e023 |
# Copyright 2014 Jan Pazdziora
|
|
|
076e023 |
#
|
|
|
076e023 |
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
076e023 |
# you may not use this file except in compliance with the License.
|
|
|
076e023 |
# You may obtain a copy of the License at
|
|
|
076e023 |
#
|
|
|
076e023 |
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
076e023 |
#
|
|
|
076e023 |
# Unless required by applicable law or agreed to in writing, software
|
|
|
076e023 |
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
076e023 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
076e023 |
# See the License for the specific language governing permissions and
|
|
|
076e023 |
# limitations under the License.
|
|
|
076e023 |
|
|
|
076e023 |
use strict;
|
|
|
076e023 |
use warnings FATAL => 'all';
|
|
|
076e023 |
|
|
|
076e023 |
use IO::Socket::UNIX ();
|
|
|
076e023 |
use Socket ();
|
|
|
076e023 |
use POSIX ();
|
|
|
076e023 |
|
|
|
076e023 |
sub daemonize {
|
|
|
076e023 |
open(STDERR, '>>', '/var/log/systemctl-socket-daemon.log') || die "can't write log: $!";
|
|
|
076e023 |
open(STDOUT, '>&STDERR') || die "can't write stdout to log: $!";
|
|
|
076e023 |
chdir("/") || die "can't chdir to /: $!";
|
|
|
076e023 |
open(STDIN, '<', '/dev/null') || die "can't read /dev/null: $!";
|
|
|
076e023 |
defined(my $pid = fork()) || die "can't fork: $!";
|
|
|
076e023 |
exit if $pid; # non-zero now means I am the parent
|
|
|
076e023 |
(POSIX::setsid() != -1) || die "Can't start a new session: $!";
|
|
|
076e023 |
}
|
|
|
076e023 |
|
|
|
076e023 |
my ($socket_path, $socket_mode, $service) = @ARGV;
|
|
|
076e023 |
if (not defined $socket_path or not defined $service) {
|
|
|
076e023 |
die "Usage: $0 /path/to/unix/socket mode service-to-run\n";
|
|
|
076e023 |
}
|
|
|
076e023 |
if (-e $socket_path) {
|
|
|
076e023 |
warn "Path [$socket_path] already exists, removing\n";
|
|
|
076e023 |
unlink $socket_path;
|
|
|
076e023 |
}
|
|
|
076e023 |
|
|
|
076e023 |
my $service_data = `/bin/systemctl show $service 2>&1;;
|
|
|
076e023 |
if ($?) {
|
|
|
076e023 |
die "Failed to find service [$service]:\n$service_data";
|
|
|
076e023 |
}
|
|
|
076e023 |
|
|
|
076e023 |
my $socket = new IO::Socket::UNIX(
|
|
|
076e023 |
Type => Socket::SOCK_STREAM,
|
|
|
076e023 |
Local => $socket_path,
|
|
|
076e023 |
Listen => Socket::SOMAXCONN
|
|
|
076e023 |
) or die "socket: $!\n";
|
|
|
076e023 |
chmod oct($socket_mode), $socket_path;
|
|
|
076e023 |
|
|
|
076e023 |
daemonize();
|
|
|
076e023 |
|
|
|
076e023 |
while (1) {
|
|
|
076e023 |
next unless my $connection = $socket->accept;
|
|
|
076e023 |
my $pid = fork();
|
|
|
076e023 |
if ($pid == 0) {
|
|
|
076e023 |
*STDIN = $connection;
|
|
|
076e023 |
*STDOUT = $connection;
|
|
|
076e023 |
exec '/bin/systemctl', 'start', $service;
|
|
|
076e023 |
die "exec should have never reached here\n";
|
|
|
076e023 |
}
|
|
|
076e023 |
}
|
|
|
076e023 |
|