2002-12-02 07:36:54 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
2002-12-10 21:17:16 +08:00
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Zone Minder Daemon Control Script, $Date$, $Revision$
|
|
|
|
# Copyright (C) 2002 Philip Coombes
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
2002-12-02 07:36:54 +08:00
|
|
|
# This script is the gateway for controlling the various ZoneMinder
|
|
|
|
# daemons. All starting, stopping and restarting goes through here.
|
|
|
|
# On the first invocation it starts up a server which subsequently
|
|
|
|
# records what's running and what's not. Other invocations just
|
|
|
|
# connect to the server and pass instructions to it.
|
|
|
|
#
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# User config
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2002-12-10 19:02:11 +08:00
|
|
|
use constant DAEMON_PATH => '@prefix@/bin/';
|
2002-12-10 18:59:11 +08:00
|
|
|
use constant DC_SOCK_FILE => '/tmp/zmdc.sock';
|
2002-12-15 22:08:51 +08:00
|
|
|
use constant DC_LOG_FILE => '/tmp/zmdc.log';
|
2003-01-07 20:26:34 +08:00
|
|
|
use constant VERBOSE => 0; # Whether to output more verbose debug
|
2002-12-02 07:36:54 +08:00
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything from here on down
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use POSIX;
|
|
|
|
use Socket;
|
|
|
|
use IO::Handle;
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2003-01-07 19:32:37 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2003-01-07 18:45:40 +08:00
|
|
|
my @daemons = ( 'zmc', 'zma', 'zmfilter.pl', 'zmaudit.pl', 'zmx10.pl', 'zmwatch.pl' );
|
2002-12-02 07:36:54 +08:00
|
|
|
|
|
|
|
my $command = shift @ARGV;
|
|
|
|
die( "No command given" ) unless( $command );
|
2002-12-24 19:51:53 +08:00
|
|
|
my $needs_daemon = $command !~ /(?:shutdown|status|check)/;
|
2002-12-02 07:36:54 +08:00
|
|
|
my $daemon = shift( @ARGV );
|
|
|
|
die( "No daemon given" ) unless( !$needs_daemon || $daemon );
|
|
|
|
my @args;
|
|
|
|
|
|
|
|
my $daemon_patt = '('.join( '|', @daemons ).')';
|
|
|
|
if ( $needs_daemon )
|
|
|
|
{
|
|
|
|
if ( $daemon =~ /^${daemon_patt}$/ )
|
|
|
|
{
|
|
|
|
$daemon = $1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Invalid daemon '$daemon' specified" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $arg ( @ARGV )
|
|
|
|
{
|
|
|
|
# Detaint arguments, if they look ok
|
2002-12-15 22:08:51 +08:00
|
|
|
if ( $arg =~ /^(-{0,2}[\w\d]+)/ )
|
2002-12-02 07:36:54 +08:00
|
|
|
{
|
|
|
|
push( @args, $1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Bogus argument '$arg' found" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or die( "Can't open socket: $!" );
|
|
|
|
|
2002-12-10 18:59:11 +08:00
|
|
|
my $saddr = sockaddr_un( DC_SOCK_FILE );
|
2002-12-02 07:36:54 +08:00
|
|
|
|
|
|
|
if ( !connect( CLIENT, $saddr ) )
|
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
if ( $command eq "check" )
|
|
|
|
{
|
|
|
|
print( "stopped\n" );
|
|
|
|
exit();
|
|
|
|
}
|
2002-12-02 07:36:54 +08:00
|
|
|
# The server isn't there
|
|
|
|
print( "Unable to connect, starting server\n" );
|
|
|
|
close( CLIENT );
|
|
|
|
|
|
|
|
if ( my $cpid = fork() )
|
|
|
|
{
|
|
|
|
# Parent process just sleep and fall through
|
|
|
|
sleep( 2 );
|
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or die( "Can't open socket: $!" );
|
|
|
|
connect( CLIENT, $saddr ) or die( "Can't connect: $!" );
|
|
|
|
}
|
|
|
|
elsif ( defined($cpid) )
|
|
|
|
{
|
|
|
|
setpgrp();
|
|
|
|
|
2002-12-15 22:08:51 +08:00
|
|
|
open( LOG, ">>".DC_LOG_FILE ) or die( "Can't open log file: $!" );
|
|
|
|
open(STDOUT, ">&LOG") || die( "Can't dup stdout: $!" );
|
|
|
|
select( STDOUT ); $| = 1;
|
|
|
|
open(STDERR, ">&LOG") || die( "Can't dup stderr: $!" );
|
|
|
|
select( STDERR ); $| = 1;
|
|
|
|
select( LOG ); $| = 1;
|
|
|
|
|
2002-12-02 07:36:54 +08:00
|
|
|
dprint( "Server starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
|
|
|
|
socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or die( "Can't open socket: $!" );
|
2002-12-10 18:59:11 +08:00
|
|
|
unlink( DC_SOCK_FILE );
|
2002-12-02 07:36:54 +08:00
|
|
|
bind( SERVER, $saddr ) or die( "Can't bind: $!" );
|
|
|
|
listen( SERVER, SOMAXCONN ) or die( "Can't listen: $!" );
|
|
|
|
|
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
$SIG{INT} = \&shutdown_all;
|
|
|
|
$SIG{TERM} = \&shutdown_all;
|
|
|
|
$SIG{ABRT} = \&shutdown_all;
|
2002-12-15 22:08:51 +08:00
|
|
|
$SIG{HUP} = \&status;
|
2002-12-02 07:36:54 +08:00
|
|
|
|
|
|
|
my %cmd_hash;
|
|
|
|
my %pid_hash;
|
|
|
|
|
|
|
|
sub dprint
|
|
|
|
{
|
|
|
|
if ( fileno(CLIENT) )
|
|
|
|
{
|
|
|
|
print CLIENT @_
|
|
|
|
}
|
2002-12-15 22:08:51 +08:00
|
|
|
print @_;
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
sub start
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
2003-01-07 21:08:06 +08:00
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
2002-12-02 07:36:54 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
|
|
|
|
if ( !$process )
|
|
|
|
{
|
2002-12-15 22:08:51 +08:00
|
|
|
# It's not running, or at least it's not been started by us
|
2002-12-02 07:36:54 +08:00
|
|
|
$process = { daemon=>$daemon, args=>\@args, command=>$command, keepalive=>!undef };
|
2002-12-15 22:08:51 +08:00
|
|
|
# So check whether someone else has started it
|
|
|
|
#$ps_command = "ps -wh --user $> | grep '$command' | grep -v grep";
|
|
|
|
#$ps_array = preg_split( "/\s+/", exec( $ps_command ) );
|
|
|
|
#$pid = $ps_array[3];
|
|
|
|
#if ( $pid )
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
elsif ( $process->{pid} && $pid_hash{$process->{pid}} )
|
|
|
|
{
|
|
|
|
dprint( "'$process->{command}' already running at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( my $cpid = fork() )
|
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
my $sigset = POSIX::SigSet->new;
|
|
|
|
my $blockset = POSIX::SigSet->new( SIGCHLD );
|
|
|
|
sigprocmask( SIG_BLOCK, $blockset, $sigset ) or die( "Can't block SIGCHLD: $!" );
|
2002-12-02 07:36:54 +08:00
|
|
|
$process->{pid} = $cpid;
|
|
|
|
$process->{started} = time();
|
|
|
|
delete( $process->{pending} );
|
|
|
|
|
|
|
|
dprint( "'$command' starting at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}\n" );
|
|
|
|
|
|
|
|
$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
|
2002-12-24 19:51:53 +08:00
|
|
|
sigprocmask( SIG_SETMASK, $sigset ) or die( "Can't restore SIGCHLD: $!" );
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
elsif ( defined($cpid ) )
|
|
|
|
{
|
|
|
|
# Child process
|
|
|
|
$SIG{CHLD} = 'DEFAULT';
|
|
|
|
$SIG{INT} = 'DEFAULT';
|
|
|
|
$SIG{TERM} = 'DEFAULT';
|
|
|
|
$SIG{ABRT} = 'DEFAULT';
|
|
|
|
$SIG{HUP} = 'DEFAULT';
|
|
|
|
dprint( "'".join( ' ', ( $daemon, @args ) )."' started at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
|
|
|
|
if ( $daemon =~ /^${daemon_patt}$/ )
|
|
|
|
{
|
|
|
|
$daemon = DAEMON_PATH.$1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Invalid daemon '$daemon' specified" );
|
|
|
|
}
|
|
|
|
|
|
|
|
exec( $daemon, @args ) or die( "Can't exec: $!" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Can't fork: $!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub _stop
|
|
|
|
{
|
|
|
|
my $final = shift;
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
2003-01-07 21:08:06 +08:00
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
2002-12-02 07:36:54 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( !$process )
|
|
|
|
{
|
|
|
|
dprint( "Can't find process with command of '$command'\n" );
|
|
|
|
return();
|
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
elsif ( $process->{pending} )
|
|
|
|
{
|
|
|
|
delete( $cmd_hash{$command} );
|
|
|
|
dprint( "Command '$command' removed from pending list at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
|
|
|
my $cpid = $process->{pid};
|
2002-12-11 08:20:51 +08:00
|
|
|
if ( !$pid_hash{$cpid} )
|
2002-12-02 07:36:54 +08:00
|
|
|
{
|
|
|
|
dprint( "No process with command of '$command' is running\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
|
|
|
print( "'$daemon ".join( ' ', @args )."' stopping at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
|
|
|
$process->{keepalive} = !$final;
|
2002-12-11 08:20:51 +08:00
|
|
|
kill( 'TERM', $cpid );
|
2002-12-02 07:36:54 +08:00
|
|
|
delete( $cmd_hash{$command} );
|
2002-12-10 20:35:59 +08:00
|
|
|
|
|
|
|
# Now check it has actually gone away, if not kill -9 it
|
|
|
|
my $count = 0;
|
2002-12-24 19:51:53 +08:00
|
|
|
while( $cpid && kill( 0, $cpid ) )
|
2002-12-10 20:35:59 +08:00
|
|
|
{
|
|
|
|
if ( $count++ > 5 )
|
|
|
|
{
|
2002-12-11 08:20:51 +08:00
|
|
|
kill( 'KILL', $cpid );
|
2002-12-10 20:35:59 +08:00
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
sleep( 1 );
|
2002-12-10 20:35:59 +08:00
|
|
|
}
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
sub stop
|
|
|
|
{
|
|
|
|
_stop( 1, @_ );
|
|
|
|
}
|
|
|
|
sub restart
|
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
2003-01-07 21:08:06 +08:00
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
2002-12-24 19:51:53 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( $process )
|
|
|
|
{
|
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( $pid_hash{$cpid} )
|
|
|
|
{
|
|
|
|
_stop( 0, $daemon, @args );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start( $daemon, @args );
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
sub reaper
|
|
|
|
{
|
|
|
|
my $cpid = wait;
|
|
|
|
|
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
|
|
|
|
my $process = $pid_hash{$cpid};
|
|
|
|
delete( $pid_hash{$cpid} );
|
|
|
|
|
|
|
|
die( "Can't find child with pid of '$cpid'\n" ) unless( $process );
|
|
|
|
|
|
|
|
$process->{stopped} = time();
|
|
|
|
$process->{runtime} = ($process->{stopped}-$process->{started});
|
|
|
|
delete( $process->{pid} );
|
|
|
|
|
|
|
|
my $exit_status = $?>>8;
|
|
|
|
my $exit_signal = $?&0xfe;
|
|
|
|
my $core_dumped = $?&0x01;
|
|
|
|
|
|
|
|
if ( $exit_status == 0 )
|
|
|
|
{
|
|
|
|
print( "'$process->{daemon} ".join( ' ', @{$process->{args}} )."' died at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{stopped} ) ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print( "'$process->{daemon} ".join( ' ', @{$process->{args}} )."' crashed at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{stopped} ) ) );
|
|
|
|
}
|
|
|
|
print( ", exit status $exit_status" ) if ( $exit_status );
|
|
|
|
print( ", signal $exit_signal" ) if ( $exit_signal );
|
2002-12-24 19:51:53 +08:00
|
|
|
#print( ", core dumped" ) if ( $core_dumped );
|
2002-12-02 07:36:54 +08:00
|
|
|
print( "\n" );
|
|
|
|
|
|
|
|
if ( $process->{keepalive} )
|
|
|
|
{
|
|
|
|
if ( !$process->{delay} || $process->{runtime} > $process->{delay} )
|
|
|
|
{
|
|
|
|
start( $process->{daemon}, @{$process->{args}} );
|
|
|
|
$process->{delay} = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$process->{pending} = $process->{stopped}+$process->{delay};
|
|
|
|
$process->{delay} *= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub shutdown_all()
|
|
|
|
{
|
|
|
|
foreach my $process ( values( %pid_hash ) )
|
|
|
|
{
|
|
|
|
stop( $process->{daemon}, @{$process->{args}} );
|
|
|
|
}
|
|
|
|
dprint( "Server shutdown at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
2002-12-10 18:59:11 +08:00
|
|
|
unlink( DC_SOCK_FILE );
|
2002-12-02 07:36:54 +08:00
|
|
|
close( CLIENT );
|
|
|
|
close( SERVER );
|
|
|
|
exit();
|
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
sub check
|
2002-12-02 07:36:54 +08:00
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
2003-01-07 21:08:06 +08:00
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
2002-12-24 19:51:53 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( !$process )
|
2002-12-02 07:36:54 +08:00
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
dprint( "unknown\n" );
|
|
|
|
}
|
|
|
|
elsif ( $process->{pending} )
|
|
|
|
{
|
|
|
|
dprint( "pending\n" );
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
else
|
2002-12-15 22:08:51 +08:00
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( !$pid_hash{$cpid} )
|
|
|
|
{
|
|
|
|
dprint( "stopped\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dprint( "running\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub status
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
if ( defined($daemon) )
|
|
|
|
{
|
2003-01-07 21:08:06 +08:00
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
2002-12-24 19:51:53 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( !$process )
|
|
|
|
{
|
|
|
|
dprint( "'$command' not running\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
2002-12-15 22:08:51 +08:00
|
|
|
if ( $process->{pending} )
|
|
|
|
{
|
|
|
|
dprint( "'$process->{command}' pending at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )."\n" );
|
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
else
|
|
|
|
{
|
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( !$pid_hash{$cpid} )
|
|
|
|
{
|
|
|
|
dprint( "'$command' not running\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dprint( "'$process->{command}' running at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach my $process ( values(%pid_hash) )
|
|
|
|
{
|
|
|
|
dprint( "'$process->{command}' running at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) ).", pid = $process->{pid}" );
|
|
|
|
dprint( ", valid" ) if ( kill( 0, $process->{pid} ) );
|
|
|
|
dprint( "\n" );
|
|
|
|
}
|
|
|
|
foreach my $process ( values( %cmd_hash ) )
|
|
|
|
{
|
|
|
|
if ( $process->{pending} )
|
|
|
|
{
|
|
|
|
dprint( "'$process->{command}' pending at ".strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )."\n" );
|
|
|
|
}
|
|
|
|
}
|
2002-12-15 22:08:51 +08:00
|
|
|
}
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec( $rin, fileno(SERVER),1) = 1;
|
|
|
|
my $win = $rin;
|
|
|
|
my $ein = $win;
|
|
|
|
my $timeout = 1;
|
|
|
|
#my ( $nfound, $timeleft) = select( $rin, $win, $ein, $timeout );
|
|
|
|
#print( "F:".fileno(SERVER)."\n" );
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
my $nfound = select( my $rout = $rin, undef, my $eout = $ein, $timeout );
|
|
|
|
#print( "Off select, NF:$nfound, ER:$!\n" );
|
|
|
|
#print( vec( $rout, fileno(SERVER),1)."\n" );
|
|
|
|
#print( vec( $eout, fileno(SERVER),1)."\n" );
|
|
|
|
#print( "C:".fileno(CLIENT)."S:".fileno(SERVER)."\n" );
|
|
|
|
if ( $nfound > 0 )
|
|
|
|
{
|
|
|
|
if ( vec( $rout, fileno(SERVER),1) )
|
|
|
|
{
|
|
|
|
my $paddr = accept( CLIENT, SERVER );
|
|
|
|
my $message = <CLIENT>;
|
|
|
|
|
2002-12-24 19:51:53 +08:00
|
|
|
next if ( !$message );
|
|
|
|
|
2002-12-02 07:36:54 +08:00
|
|
|
my ( $command, $daemon, @args ) = split( ';', $message );
|
|
|
|
|
|
|
|
if ( $command eq 'start' )
|
|
|
|
{
|
|
|
|
start( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'stop' )
|
|
|
|
{
|
|
|
|
stop( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'restart' )
|
|
|
|
{
|
|
|
|
restart( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'shutdown' )
|
|
|
|
{
|
|
|
|
shutdown_all();
|
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
elsif ( $command eq 'check' )
|
|
|
|
{
|
|
|
|
check( $daemon, @args );
|
|
|
|
}
|
2002-12-15 22:08:51 +08:00
|
|
|
elsif ( $command eq 'status' )
|
2002-12-02 07:36:54 +08:00
|
|
|
{
|
2002-12-24 19:51:53 +08:00
|
|
|
if ( $daemon )
|
|
|
|
{
|
|
|
|
status( $daemon, @args );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status();
|
|
|
|
}
|
2002-12-02 07:36:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
dprint( "Invalid command '$command'\n" );
|
|
|
|
}
|
|
|
|
close( CLIENT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Bogus descriptor" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $nfound < 0 )
|
|
|
|
{
|
|
|
|
if ( $! == EINTR )
|
|
|
|
{
|
|
|
|
# Dead child, will be reaped
|
|
|
|
#print( "Probable dead child\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Can't select: $!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#print( "Select timed out\n" );
|
|
|
|
foreach my $process ( values( %cmd_hash ) )
|
|
|
|
{
|
|
|
|
if ( $process->{pending} && $process->{pending} <= time() )
|
|
|
|
{
|
|
|
|
dprint( "Starting pending process, $process->{command}\n" );
|
|
|
|
start( $process->{daemon}, @{$process->{args}} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dprint( "Server exiting at ".strftime( '%y/%m/%d %H:%M:%S', localtime() )."\n" );
|
2002-12-15 22:08:51 +08:00
|
|
|
close( LOG );
|
2002-12-02 07:36:54 +08:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
die( "Can't fork: $!" );
|
|
|
|
}
|
|
|
|
}
|
2002-12-24 19:51:53 +08:00
|
|
|
if ( $command eq "check" && !$daemon )
|
|
|
|
{
|
|
|
|
print( "running\n" );
|
|
|
|
exit();
|
|
|
|
}
|
2002-12-02 07:36:54 +08:00
|
|
|
# The server is there, connect to it
|
|
|
|
#print( "Writing commands\n" );
|
|
|
|
CLIENT->autoflush();
|
|
|
|
my $message = "$command";
|
|
|
|
$message .= ";$daemon" if ( $daemon );
|
|
|
|
$message .= ";".join( ';', @args ) if ( @args );
|
|
|
|
print( CLIENT $message );
|
|
|
|
shutdown( CLIENT, 1 );
|
|
|
|
while ( my $line = <CLIENT> )
|
|
|
|
{
|
|
|
|
chomp( $line );
|
|
|
|
print( "$line\n" );
|
|
|
|
}
|
|
|
|
close( CLIENT );
|
|
|
|
#print( "Finished writing, bye\n" );
|