#!/usr/bin/perl # $HeadURL$ $LastCangedRevision: 1397 $ my($progname) = ($0 =~ /.*\/([^\/]*)$/); use strict; use Socket; use Carp; use File::Basename; use Sys::Syslog; use POSIX qw(setsid); my($mkdi_cmd) = dirname($0) . "/mkdi"; # The port number is also defined in mkdi.sh my($releaseserver_port) = 55000; sub main { my ($target_hostname, $release_cmdline); my ($paddr, $name, $iaddr, $eol, $proto, $output, $pid); $eol = "\015\012"; $proto = getprotobyname('tcp'); # initiate logging openlog($progname, "pid", "local0") || die "openlog: $!\n"; # become a daemon $SIG{INT} = $SIG{TERM} = sub { &log("exiting on SIG$_[0] ..."); exit 0; }; &log("detaching from terminal ..."); open STDIN, '/dev/null' || die "can't read /dev/null: $!"; open STDOUT, '>/dev/null'; open STDERR, '>/dev/null'; POSIX::setsid() || die "can't detach from tty : $!\n"; umask(0); defined($pid=fork) || die "fork failed: $!\n"; return (0) if ($pid); # start listening &log("starting listening ..."); socket(Server, PF_INET, SOCK_STREAM, $proto) || die "socket: $!"; setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!"; bind(Server, sockaddr_in($releaseserver_port, INADDR_ANY)) || die "bind: $!"; listen(Server,SOMAXCONN) || die "listen: $!"; # process all incoming connections while (1) { # Get hostname from socket &log("awaiting connection ..."); $paddr = accept(Client,Server); # First parameter returned by sockaddr_in() is the port number # again and as we know this we discard it into undef. (undef,$iaddr) = sockaddr_in($paddr); $name = gethostbyaddr($iaddr,AF_INET); $name =~ s/\..*$//; $name = "" if (!$name); &log("connection established from $name"); chomp($target_hostname = ); close Client; # Tell mkdi that its being called by the releaser and, as such, it should # not delete VMs and their storage. $ENV{'RELEASER_FLAG'} = 'true'; $release_cmdline = "$mkdi_cmd delete $target_hostname"; &log("release command is [$release_cmdline]"); chomp($output = `$release_cmdline 2>&1`); &log("output was [$output]"); } } sub log { syslog("info", $_[0]); } main(@ARGV);