2005-12-16 18:05:29 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Daemon Control Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-09 01:41:20 +08:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmdc.pl - ZoneMinder Daemon Control script
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
zmdc.pl {command} [daemon [options]]
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
{command} - One of 'startup|shutdown|status|check|logrot' or
|
|
|
|
'start|stop|restart|reload|version'.
|
|
|
|
[daemon [options]] - Daemon name and options, required for second group of commands
|
|
|
|
|
|
|
|
=cut
|
2005-12-16 18:05:29 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# User config
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2005-12-16 21:16:37 +08:00
|
|
|
use constant MAX_CONNECT_DELAY => 10;
|
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything from here on down
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:05:29 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use POSIX;
|
|
|
|
use Socket;
|
|
|
|
use IO::Handle;
|
2015-04-09 01:41:20 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2014-11-26 03:44:08 +08:00
|
|
|
#use Data::Dumper;
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2013-12-17 05:32:02 +08:00
|
|
|
use constant SOCK_FILE => $Config{ZM_PATH_SOCKS}.'/zmdc.sock';
|
2005-12-16 18:36:22 +08:00
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2005-12-16 18:05:29 +08:00
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
2016-05-10 00:46:26 +08:00
|
|
|
if ( $Config{ZM_LD_PRELOAD} ) {
|
|
|
|
Debug("Adding ENV{LD_PRELOAD} = $Config{ZM_LD_PRELOAD}");
|
|
|
|
$ENV{LD_PRELOAD} = $Config{ZM_LD_PRELOAD};
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2006-01-12 07:55:22 +08:00
|
|
|
my @daemons = (
|
2011-06-21 17:19:10 +08:00
|
|
|
'zmc',
|
|
|
|
'zma',
|
|
|
|
'zmf',
|
|
|
|
'zmfilter.pl',
|
|
|
|
'zmaudit.pl',
|
|
|
|
'zmtrigger.pl',
|
|
|
|
'zmx10.pl',
|
|
|
|
'zmwatch.pl',
|
|
|
|
'zmupdate.pl',
|
2016-02-07 04:08:28 +08:00
|
|
|
'zmtrack.pl',
|
2016-05-19 05:58:04 +08:00
|
|
|
'zmtelemetry.pl'
|
2006-01-12 07:55:22 +08:00
|
|
|
);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
my $command = shift @ARGV;
|
2006-10-20 17:31:40 +08:00
|
|
|
if( !$command )
|
|
|
|
{
|
|
|
|
print( STDERR "No command given\n" );
|
2015-04-09 01:41:20 +08:00
|
|
|
pod2usage(-exitstatus => -1);
|
2006-10-20 17:31:40 +08:00
|
|
|
}
|
2015-01-07 21:47:32 +08:00
|
|
|
if ( $command eq 'version' ) {
|
2015-04-09 01:29:38 +08:00
|
|
|
print ZoneMinder::Base::ZM_VERSION."\n";
|
|
|
|
exit( 0 );
|
2015-01-07 21:47:32 +08:00
|
|
|
}
|
|
|
|
my $needs_daemon = $command !~ /(?:startup|shutdown|status|check|logrot|version)/;
|
2005-12-16 18:05:29 +08:00
|
|
|
my $daemon = shift( @ARGV );
|
2006-10-20 17:31:40 +08:00
|
|
|
if( $needs_daemon && !$daemon )
|
|
|
|
{
|
|
|
|
print( STDERR "No daemon given\n" );
|
2015-04-09 01:41:20 +08:00
|
|
|
pod2usage(-exitstatus => -1);
|
2006-10-20 17:31:40 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
my @args;
|
|
|
|
|
|
|
|
my $daemon_patt = '('.join( '|', @daemons ).')';
|
|
|
|
if ( $needs_daemon )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
if ( $daemon =~ /^${daemon_patt}$/ )
|
|
|
|
{
|
|
|
|
$daemon = $1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print( STDERR "Invalid daemon '$daemon' specified" );
|
2015-04-09 01:41:20 +08:00
|
|
|
pod2usage(-exitstatus => -1);
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach my $arg ( @ARGV )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
# Detaint arguments, if they look ok
|
|
|
|
#if ( $arg =~ /^(-{0,2}[\w]+)/ )
|
|
|
|
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ )
|
|
|
|
{
|
|
|
|
push( @args, $1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
print( STDERR "Bogus argument '$arg' found" );
|
2006-10-20 17:31:40 +08:00
|
|
|
exit( -1 );
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2006-10-20 17:31:40 +08:00
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2006-01-12 07:55:22 +08:00
|
|
|
my $saddr = sockaddr_un( SOCK_FILE );
|
2005-12-23 18:22:32 +08:00
|
|
|
my $server_up = connect( CLIENT, $saddr );
|
|
|
|
if ( !$server_up )
|
2005-12-16 18:05:29 +08:00
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
if ( $command eq "logrot" )
|
2006-07-04 18:34:21 +08:00
|
|
|
{
|
|
|
|
exit();
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
if ( $command eq "check" )
|
|
|
|
{
|
|
|
|
print( "stopped\n" );
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
elsif ( $command ne "startup" )
|
|
|
|
{
|
|
|
|
print( "Unable to connect to server\n" );
|
|
|
|
exit( -1 );
|
|
|
|
}
|
2015-04-09 01:29:38 +08:00
|
|
|
# The server isn't there
|
2011-06-21 17:19:10 +08:00
|
|
|
print( "Starting server\n" );
|
|
|
|
close( CLIENT );
|
|
|
|
|
|
|
|
if ( my $cpid = fork() )
|
|
|
|
{
|
|
|
|
logInit();
|
|
|
|
|
|
|
|
# Parent process just sleep and fall through
|
|
|
|
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
|
|
|
|
my $attempts = 0;
|
|
|
|
while (!connect( CLIENT, $saddr ))
|
|
|
|
{
|
|
|
|
$attempts++;
|
|
|
|
Fatal( "Can't connect: $!" ) if ($attempts > MAX_CONNECT_DELAY);
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( defined($cpid) )
|
|
|
|
{
|
2011-04-28 22:22:44 +08:00
|
|
|
ZMServer::run();
|
2011-06-21 17:19:10 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Can't fork: $!" );
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
if ( $command eq "check" && !$daemon )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
print( "running\n" );
|
|
|
|
exit();
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
2005-12-23 18:22:32 +08:00
|
|
|
elsif ( $command eq "startup" )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
# Our work here is done
|
|
|
|
exit() if ( !$server_up );
|
2005-12-23 18:22:32 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +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> )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
chomp( $line );
|
|
|
|
print( "$line\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
2011-05-04 22:10:56 +08:00
|
|
|
# And we're done!
|
2005-12-16 18:05:29 +08:00
|
|
|
close( CLIENT );
|
|
|
|
#print( "Finished writing, bye\n" );
|
2005-12-23 18:22:32 +08:00
|
|
|
|
|
|
|
exit;
|
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
package ZMServer;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
2015-06-23 01:56:27 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2011-04-28 22:22:44 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use POSIX;
|
|
|
|
use Socket;
|
|
|
|
use IO::Handle;
|
2014-11-26 03:44:08 +08:00
|
|
|
#use Data::Dumper;
|
2011-04-28 22:22:44 +08:00
|
|
|
|
|
|
|
our %cmd_hash;
|
|
|
|
our %pid_hash;
|
|
|
|
|
|
|
|
sub run
|
|
|
|
{
|
|
|
|
my $fd = 0;
|
|
|
|
while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) )
|
|
|
|
{
|
|
|
|
POSIX::close( $fd++ );
|
|
|
|
}
|
|
|
|
|
|
|
|
setpgrp();
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
logInit();
|
2011-04-28 22:22:44 +08:00
|
|
|
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Server starting at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
|
2015-04-09 01:29:38 +08:00
|
|
|
if ( open( my $PID, '>', ZM_PID ) )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
2015-04-09 01:29:38 +08:00
|
|
|
print( $PID $$ );
|
|
|
|
close( $PID );
|
2016-02-16 23:02:30 +08:00
|
|
|
} else {
|
|
|
|
Error( "Can't open pid file at " . ZM_PID );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
killAll( 1 );
|
|
|
|
|
|
|
|
socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
|
2016-05-25 01:13:17 +08:00
|
|
|
unlink( main::SOCK_FILE ) or Error( "Unable to unlink " . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
|
2016-02-16 23:02:30 +08:00
|
|
|
bind( SERVER, $saddr ) or Fatal( "Can't bind to " . main::SOCK_FILE . ": $!" );
|
2011-04-28 22:22:44 +08:00
|
|
|
listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" );
|
|
|
|
|
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
$SIG{INT} = \&shutdownAll;
|
|
|
|
$SIG{TERM} = \&shutdownAll;
|
|
|
|
$SIG{ABRT} = \&shutdownAll;
|
|
|
|
$SIG{HUP} = \&logrot;
|
|
|
|
|
|
|
|
my $rin = '';
|
|
|
|
vec( $rin, fileno(SERVER), 1 ) = 1;
|
|
|
|
my $win = $rin;
|
|
|
|
my $ein = $win;
|
|
|
|
my $timeout = 0.1;
|
|
|
|
while( 1 )
|
|
|
|
{
|
|
|
|
my $nfound = select( my $rout = $rin, undef, undef, $timeout );
|
|
|
|
if ( $nfound > 0 )
|
|
|
|
{
|
|
|
|
if ( vec( $rout, fileno(SERVER), 1 ) )
|
|
|
|
{
|
|
|
|
my $paddr = accept( CLIENT, SERVER );
|
|
|
|
my $message = <CLIENT>;
|
|
|
|
|
|
|
|
next if ( !$message );
|
|
|
|
|
2011-08-26 15:51:36 +08:00
|
|
|
my ( $command, $daemon, @args ) = split( /;/, $message );
|
2011-04-28 22:22:44 +08:00
|
|
|
|
|
|
|
if ( $command eq 'start' )
|
|
|
|
{
|
|
|
|
start( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'stop' )
|
|
|
|
{
|
|
|
|
stop( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'restart' )
|
|
|
|
{
|
|
|
|
restart( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'reload' )
|
|
|
|
{
|
|
|
|
reload( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'startup' )
|
|
|
|
{
|
|
|
|
# Do nothing, this is all we're here for
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::WARNING, "Already running, ignoring command '$command'\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
elsif ( $command eq 'shutdown' )
|
|
|
|
{
|
|
|
|
shutdownAll();
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'check' )
|
|
|
|
{
|
|
|
|
check( $daemon, @args );
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'status' )
|
|
|
|
{
|
|
|
|
if ( $daemon )
|
|
|
|
{
|
|
|
|
status( $daemon, @args );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
status();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $command eq 'logrot' )
|
|
|
|
{
|
|
|
|
logrot();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::ERROR, "Invalid command '$command'\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
close( CLIENT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Bogus descriptor" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ( $nfound < 0 )
|
|
|
|
{
|
|
|
|
if ( $! == EINTR )
|
|
|
|
{
|
|
|
|
# Dead child, will be reaped
|
|
|
|
#print( "Probable dead child\n" );
|
|
|
|
# See if it needs to start up again
|
|
|
|
restartPending();
|
|
|
|
}
|
|
|
|
elsif ( $! == EPIPE )
|
|
|
|
{
|
|
|
|
Error( "Can't select: $!" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Can't select: $!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#print( "Select timed out\n" );
|
|
|
|
restartPending();
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Server exiting at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2016-05-25 01:13:17 +08:00
|
|
|
unlink( main::SOCK_FILE ) or Error( "Unable to unlink " . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
|
|
|
|
unlink( ZM_PID ) or Error( "Unable to unlink " . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID );
|
2011-04-28 22:22:44 +08:00
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub cPrint
|
|
|
|
{
|
|
|
|
if ( fileno(CLIENT) )
|
|
|
|
{
|
|
|
|
print CLIENT @_
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub dPrint
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
my $logLevel = shift;
|
2011-04-28 22:22:44 +08:00
|
|
|
if ( fileno(CLIENT) )
|
|
|
|
{
|
|
|
|
print CLIENT @_
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
if ( $logLevel == ZoneMinder::Logger::DEBUG )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
Debug( @_ );
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
elsif ( $logLevel == ZoneMinder::Logger::INFO )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
Info( @_ );
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
elsif ( $logLevel == ZoneMinder::Logger::WARNING )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
Warning( @_ );
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
elsif ( $logLevel == ZoneMinder::Logger::ERROR )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
Error( @_ );
|
|
|
|
}
|
2011-06-21 17:19:10 +08:00
|
|
|
elsif ( $logLevel == ZoneMinder::Logger::FATAL )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
Fatal( @_ );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub start
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
my $command = join(' ', $daemon, @args );
|
2011-04-28 22:22:44 +08:00
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
|
|
|
|
if ( !$process )
|
|
|
|
{
|
|
|
|
# It's not running, or at least it's not been started by us
|
|
|
|
$process = { daemon=>$daemon, args=>\@args, command=>$command, keepalive=>!undef };
|
|
|
|
}
|
|
|
|
elsif ( $process->{pid} && $pid_hash{$process->{pid}} )
|
|
|
|
{
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "'$process->{command}' already running at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
|
|
|
|
.", pid = $process->{pid}\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
my $sigset = POSIX::SigSet->new;
|
|
|
|
my $blockset = POSIX::SigSet->new( SIGCHLD );
|
|
|
|
sigprocmask( SIG_BLOCK, $blockset, $sigset ) or Fatal( "Can't block SIGCHLD: $!" );
|
2011-04-28 22:22:44 +08:00
|
|
|
if ( my $cpid = fork() )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
logReinit();
|
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
$process->{pid} = $cpid;
|
|
|
|
$process->{started} = time();
|
|
|
|
delete( $process->{pending} );
|
|
|
|
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "'$command' starting at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
|
|
|
|
.", pid = $process->{pid}\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
|
|
|
|
$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
|
|
|
|
sigprocmask( SIG_SETMASK, $sigset ) or Fatal( "Can't restore SIGCHLD: $!" );
|
|
|
|
}
|
|
|
|
elsif ( defined($cpid ) )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
logReinit();
|
2011-04-28 22:22:44 +08:00
|
|
|
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "'".join( ' ', ( $daemon, @args ) )
|
|
|
|
."' started at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
|
|
|
|
if ( $daemon =~ /^${daemon_patt}$/ )
|
|
|
|
{
|
2013-12-17 05:32:02 +08:00
|
|
|
$daemon = $Config{ZM_PATH_BIN}.'/'.$1;
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Invalid daemon '$daemon' specified" );
|
|
|
|
}
|
|
|
|
|
|
|
|
my @good_args;
|
|
|
|
foreach my $arg ( @args )
|
|
|
|
{
|
|
|
|
# Detaint arguments, if they look ok
|
|
|
|
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ )
|
|
|
|
{
|
|
|
|
push( @good_args, $1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Bogus argument '$arg' found" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
logTerm();
|
|
|
|
|
|
|
|
my $fd = 0;
|
|
|
|
while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) )
|
|
|
|
{
|
|
|
|
POSIX::close( $fd++ );
|
|
|
|
}
|
|
|
|
|
|
|
|
# Child process
|
|
|
|
$SIG{CHLD} = 'DEFAULT';
|
|
|
|
$SIG{INT} = 'DEFAULT';
|
|
|
|
$SIG{TERM} = 'DEFAULT';
|
|
|
|
$SIG{ABRT} = 'DEFAULT';
|
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
exec( $daemon, @good_args ) or Fatal( "Can't exec: $!" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Fatal( "Can't fork: $!" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
# Sends the stop signal, without waiting around to see if the process died.
|
|
|
|
sub send_stop {
|
2015-06-23 01:37:26 +08:00
|
|
|
my ( $final, $process ) = @_;
|
2011-04-28 22:22:44 +08:00
|
|
|
|
2015-06-23 01:37:26 +08:00
|
|
|
my $command = $process->{command};
|
2015-05-22 03:37:08 +08:00
|
|
|
if ( $process->{pending} ) {
|
2015-06-23 01:37:26 +08:00
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
delete( $cmd_hash{$command} );
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Command '$command' removed from pending list at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
my $pid = $process->{pid};
|
|
|
|
if ( !$pid_hash{$pid} )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
2015-05-22 03:37:08 +08:00
|
|
|
dPrint( ZoneMinder::Logger::ERROR, "No process with command of '$command' pid $pid is running\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "'$command' sending stop to pid $pid at "
|
2015-04-09 01:29:38 +08:00
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
$process->{keepalive} = !$final;
|
2015-05-22 03:37:08 +08:00
|
|
|
kill( 'TERM', $pid );
|
2015-06-23 01:37:26 +08:00
|
|
|
return $pid;
|
2015-05-22 03:37:08 +08:00
|
|
|
} # end sub send_stop
|
2011-04-28 22:22:44 +08:00
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
sub kill_until_dead {
|
2015-06-23 01:37:26 +08:00
|
|
|
my ( $process ) = @_;
|
2011-04-28 22:22:44 +08:00
|
|
|
# Now check it has actually gone away, if not kill -9 it
|
|
|
|
my $count = 0;
|
2015-05-22 03:37:08 +08:00
|
|
|
while( $process and $$process{pid} and kill( 0, $$process{pid} ) )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
if ( $count++ > 5 )
|
|
|
|
{
|
2015-06-23 01:37:26 +08:00
|
|
|
dPrint( ZoneMinder::Logger::WARNING, "'$$process{command}' has not stopped at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
.". Sending KILL to pid $$process{pid}\n"
|
|
|
|
);
|
2015-05-22 03:37:08 +08:00
|
|
|
kill( 'KILL', $$process{pid} );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
2015-06-23 01:37:26 +08:00
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
sleep( 1 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 03:37:08 +08:00
|
|
|
sub _stop {
|
2015-06-23 01:37:26 +08:00
|
|
|
my ($final, $process ) = @_;
|
2015-05-22 03:37:08 +08:00
|
|
|
|
2015-06-23 01:37:26 +08:00
|
|
|
my $pid = send_stop( $final, $process );
|
|
|
|
return if ! $pid;
|
2015-07-16 04:08:02 +08:00
|
|
|
delete( $cmd_hash{$$process{command}} );
|
2015-06-23 01:37:26 +08:00
|
|
|
|
|
|
|
kill_until_dead( $process );
|
2015-05-22 03:37:08 +08:00
|
|
|
}
|
|
|
|
|
2011-04-28 22:22:44 +08:00
|
|
|
sub stop
|
|
|
|
{
|
2015-06-23 01:37:26 +08:00
|
|
|
my ( $daemon, @args ) = @_;
|
|
|
|
my $command = join(' ', $daemon, @args );
|
|
|
|
my $process = $cmd_hash{$command};
|
2015-05-22 03:37:08 +08:00
|
|
|
if ( !$process )
|
|
|
|
{
|
|
|
|
dPrint( ZoneMinder::Logger::WARNING, "Can't find process with command of '$command'\n" );
|
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
|
|
|
_stop( 1, $process );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub restart
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( $process )
|
|
|
|
{
|
|
|
|
if ( $process->{pid} )
|
|
|
|
{
|
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( defined($pid_hash{$cpid}) )
|
|
|
|
{
|
2015-05-22 03:37:08 +08:00
|
|
|
_stop( 0, $process );
|
2011-04-28 22:22:44 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
start( $daemon, @args );
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reload
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( $process )
|
|
|
|
{
|
|
|
|
if ( $process->{pid} )
|
|
|
|
{
|
|
|
|
kill( 'HUP', $process->{pid} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub logrot
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
logReinit();
|
2011-04-28 22:22:44 +08:00
|
|
|
foreach my $process ( values( %pid_hash ) )
|
|
|
|
{
|
|
|
|
if ( $process->{pid} && $process->{command} =~ /^zm.*\.pl/ )
|
|
|
|
{
|
|
|
|
kill( 'HUP', $process->{pid} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub reaper
|
|
|
|
{
|
|
|
|
my $saved_status = $!;
|
|
|
|
while ( (my $cpid = waitpid( -1, WNOHANG )) > 0 )
|
|
|
|
{
|
|
|
|
my $status = $?;
|
|
|
|
|
|
|
|
my $process = $pid_hash{$cpid};
|
|
|
|
delete( $pid_hash{$cpid} );
|
|
|
|
|
|
|
|
if ( !$process )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Can't find child with pid of '$cpid'\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
next;
|
|
|
|
}
|
|
|
|
|
|
|
|
$process->{stopped} = time();
|
|
|
|
$process->{runtime} = ($process->{stopped}-$process->{started});
|
|
|
|
delete( $process->{pid} );
|
|
|
|
|
|
|
|
my $exit_status = $status>>8;
|
|
|
|
my $exit_signal = $status&0xfe;
|
|
|
|
my $core_dumped = $status&0x01;
|
|
|
|
|
2015-06-23 01:37:26 +08:00
|
|
|
my $out_str = "'$process->{command}' ";
|
2011-04-28 22:22:44 +08:00
|
|
|
if ( $exit_signal )
|
|
|
|
{
|
|
|
|
if ( $exit_signal == 15 || $exit_signal == 14 ) # TERM or ALRM
|
|
|
|
{
|
|
|
|
$out_str .= "exited";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$out_str .= "crashed";
|
|
|
|
}
|
|
|
|
$out_str .= ", signal $exit_signal";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$out_str .= "exited ";
|
|
|
|
if ( $exit_status )
|
|
|
|
{
|
|
|
|
$out_str .= "abnormally, exit status $exit_status";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$out_str .= "normally";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#print( ", core dumped" ) if ( $core_dumped );
|
|
|
|
$out_str .= "\n";
|
|
|
|
|
|
|
|
if ( $exit_status == 0 )
|
|
|
|
{
|
|
|
|
Info( $out_str );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Error( $out_str );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $process->{keepalive} )
|
|
|
|
{
|
2015-06-23 01:37:26 +08:00
|
|
|
# Schedule for immediate restart
|
|
|
|
$cmd_hash{$process->{command}} = $process;
|
2013-12-17 05:32:02 +08:00
|
|
|
if ( !$process->{delay} || ($process->{runtime} > $Config{ZM_MAX_RESTART_DELAY} ) )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
|
|
|
#start( $process->{daemon}, @{$process->{args}} );
|
|
|
|
$process->{pending} = $process->{stopped};
|
|
|
|
$process->{delay} = 5;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$process->{pending} = $process->{stopped}+$process->{delay};
|
|
|
|
$process->{delay} *= 2;
|
|
|
|
# Limit the start delay to 15 minutes max
|
2013-12-17 05:32:02 +08:00
|
|
|
if ( $process->{delay} > $Config{ZM_MAX_RESTART_DELAY} )
|
2011-04-28 22:22:44 +08:00
|
|
|
{
|
2013-12-17 05:32:02 +08:00
|
|
|
$process->{delay} = $Config{ZM_MAX_RESTART_DELAY};
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$SIG{CHLD} = \&reaper;
|
|
|
|
$! = $saved_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub restartPending
|
|
|
|
{
|
|
|
|
# Restart any pending processes
|
|
|
|
foreach my $process ( values( %cmd_hash ) )
|
|
|
|
{
|
|
|
|
if ( $process->{pending} && $process->{pending} <= time() )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Starting pending process, $process->{command}\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
start( $process->{daemon}, @{$process->{args}} );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub shutdownAll
|
|
|
|
{
|
2015-06-23 01:37:26 +08:00
|
|
|
foreach my $pid ( keys %pid_hash ) {
|
|
|
|
# This is a quick fix because a SIGCHLD can happen and alter pid_hash while we are in here.
|
|
|
|
next if ! $pid_hash{$pid};
|
|
|
|
send_stop( 1, $pid_hash{$pid} );
|
2015-05-22 03:37:08 +08:00
|
|
|
}
|
2015-06-23 01:37:26 +08:00
|
|
|
foreach my $pid ( keys %pid_hash ) {
|
|
|
|
# This is a quick fix because a SIGCHLD can happen and alter pid_hash while we are in here.
|
|
|
|
next if ! $pid_hash{$pid};
|
2015-05-22 03:37:08 +08:00
|
|
|
|
2015-06-23 01:37:26 +08:00
|
|
|
my $process = $pid_hash{$pid};
|
2015-05-22 03:37:08 +08:00
|
|
|
|
2015-06-23 01:37:26 +08:00
|
|
|
kill_until_dead( $process );
|
|
|
|
delete( $cmd_hash{$$process{command}} );
|
|
|
|
delete( $pid_hash{$pid} );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
killAll( 5 );
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::INFO, "Server shutdown at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
|
|
|
|
."\n"
|
|
|
|
);
|
2016-05-25 01:13:17 +08:00
|
|
|
unlink( main::SOCK_FILE ) or Error( "Unable to unlink " . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
|
|
|
|
unlink( ZM_PID ) or Error( "Unable to unlink " . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID );
|
2011-04-28 22:22:44 +08:00
|
|
|
close( CLIENT );
|
|
|
|
close( SERVER );
|
|
|
|
exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
sub check
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( !$process )
|
|
|
|
{
|
|
|
|
cPrint( "unknown\n" );
|
|
|
|
}
|
|
|
|
elsif ( $process->{pending} )
|
|
|
|
{
|
|
|
|
cPrint( "pending\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( !$pid_hash{$cpid} )
|
|
|
|
{
|
|
|
|
cPrint( "stopped\n" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
cPrint( "running\n" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub status
|
|
|
|
{
|
|
|
|
my $daemon = shift;
|
|
|
|
my @args = @_;
|
|
|
|
|
|
|
|
if ( defined($daemon) )
|
|
|
|
{
|
|
|
|
my $command = $daemon;
|
|
|
|
$command .= ' '.join( ' ', ( @args ) ) if ( @args );
|
|
|
|
my $process = $cmd_hash{$command};
|
|
|
|
if ( !$process )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, "'$command' not running\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
return();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $process->{pending} )
|
|
|
|
{
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, "'$process->{command}' pending at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
my $cpid = $process->{pid};
|
|
|
|
if ( !$pid_hash{$cpid} )
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, "'$command' not running\n" );
|
2011-04-28 22:22:44 +08:00
|
|
|
return();
|
|
|
|
}
|
|
|
|
}
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, "'$process->{command}' running since "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
|
|
|
|
.", pid = $process->{pid}"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
foreach my $process ( values(%pid_hash) )
|
|
|
|
{
|
2015-04-09 01:29:38 +08:00
|
|
|
my $out_str = "'$process->{command}' running since "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
|
|
|
|
.", pid = $process->{pid}"
|
|
|
|
;
|
2011-04-28 22:22:44 +08:00
|
|
|
$out_str .= ", valid" if ( kill( 0, $process->{pid} ) );
|
|
|
|
$out_str .= "\n";
|
2011-06-21 17:19:10 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, $out_str );
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
foreach my $process ( values( %cmd_hash ) )
|
|
|
|
{
|
|
|
|
if ( $process->{pending} )
|
|
|
|
{
|
2015-04-09 01:29:38 +08:00
|
|
|
dPrint( ZoneMinder::Logger::DEBUG, "'$process->{command}' pending at "
|
|
|
|
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{pending}) )
|
|
|
|
."\n"
|
|
|
|
);
|
2011-04-28 22:22:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-12-23 18:22:32 +08:00
|
|
|
sub killAll
|
|
|
|
{
|
2011-06-21 17:19:10 +08:00
|
|
|
my $delay = shift;
|
|
|
|
sleep( $delay );
|
2015-05-22 03:37:08 +08:00
|
|
|
my $killall;
|
2015-06-23 01:56:27 +08:00
|
|
|
if ( '@HOST_OS@' eq 'BSD' )
|
2015-05-22 03:37:08 +08:00
|
|
|
{
|
2016-04-16 21:39:58 +08:00
|
|
|
$killall = 'killall -q -';
|
2015-06-23 01:56:27 +08:00
|
|
|
} elsif ( '@HOST_OS@' eq 'solaris' ) {
|
2015-06-23 01:37:26 +08:00
|
|
|
$killall = 'pkill -';
|
2015-05-22 03:37:08 +08:00
|
|
|
} else {
|
|
|
|
$killall = 'killall -q -s ';
|
|
|
|
}
|
2015-04-09 01:29:38 +08:00
|
|
|
foreach my $daemon ( @daemons )
|
|
|
|
{
|
2015-05-22 03:37:08 +08:00
|
|
|
my $cmd = $killall ."TERM $daemon";
|
|
|
|
Debug( $cmd );
|
|
|
|
qx( $cmd );
|
2015-04-09 01:29:38 +08:00
|
|
|
}
|
|
|
|
sleep( $delay );
|
2011-06-21 17:19:10 +08:00
|
|
|
foreach my $daemon ( @daemons )
|
|
|
|
{
|
2014-06-26 03:28:10 +08:00
|
|
|
my $cmd = $killall."KILL $daemon";
|
2011-06-21 17:19:10 +08:00
|
|
|
Debug( $cmd );
|
|
|
|
qx( $cmd );
|
|
|
|
}
|
2005-12-23 18:22:32 +08:00
|
|
|
}
|