2005-12-16 18:05:29 +08:00
|
|
|
#!/usr/bin/perl -wT
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Package 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-11 20:39:32 +08:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmpkg.pl - ZoneMinder Package Control Script
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
zmpkg.pl {start|stop|restart|status|logrot|'state'|version}
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This script is used to start and stop the ZoneMinder package primarily to
|
|
|
|
allow command line control for automatic restart on reboot (see zm script)
|
|
|
|
|
|
|
|
=cut
|
2005-12-16 18:05:29 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# Don't change anything below here
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:05:29 +08:00
|
|
|
use ZoneMinder;
|
|
|
|
use DBI;
|
|
|
|
use POSIX;
|
|
|
|
use Time::HiRes qw/gettimeofday/;
|
2015-04-11 20:39:32 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
# Detaint our environment
|
|
|
|
$ENV{PATH} = '/bin:/usr/bin';
|
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
2015-06-21 21:30:46 +08:00
|
|
|
my $store_state=""; # PP - will remember state name passed
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
logInit();
|
2005-12-21 01:03:33 +08:00
|
|
|
|
2015-04-11 20:41:07 +08:00
|
|
|
my $command = $ARGV[0]||'';
|
2015-01-07 22:05:16 +08:00
|
|
|
if ( $command eq 'version' ) {
|
2015-04-11 20:39:32 +08:00
|
|
|
print ZoneMinder::Base::ZM_VERSION . "\n";
|
|
|
|
exit(0);
|
2015-01-07 22:05:16 +08:00
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
my $state;
|
|
|
|
|
2015-01-07 22:05:16 +08:00
|
|
|
my $dbh;
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-01-07 22:05:16 +08:00
|
|
|
if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot|version)$/ )
|
2005-12-16 18:05:29 +08:00
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
if ( $command )
|
|
|
|
{
|
|
|
|
$dbh = zmDbConnect();
|
|
|
|
# Check to see if it's a valid run state
|
|
|
|
my $sql = 'select * from States where Name = ?';
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
my $res = $sth->execute( $command )
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
if ( $state = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
$state->{Name} = $command;
|
|
|
|
$state->{Definitions} = [];
|
|
|
|
foreach( split( /,/, $state->{Definition} ) )
|
|
|
|
{
|
|
|
|
my ( $id, $function, $enabled ) = split( /:/, $_ );
|
|
|
|
push( @{$state->{Definitions}},
|
|
|
|
{ Id=>$id, Function=>$function, Enabled=>$enabled }
|
|
|
|
);
|
|
|
|
}
|
2015-06-21 21:30:46 +08:00
|
|
|
$store_state=$command; # PP - Remember the name that was passed to search in DB
|
2015-04-11 20:39:32 +08:00
|
|
|
$command = 'state';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$command = undef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !$command )
|
|
|
|
{
|
|
|
|
pod2usage(-exitstatus => -1);
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
2015-01-07 22:05:16 +08:00
|
|
|
$dbh = zmDbConnect() if ! $dbh;
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
# Move to the right place
|
2015-04-11 20:39:32 +08:00
|
|
|
chdir( $Config{ZM_PATH_WEB} )
|
|
|
|
or Fatal( "Can't chdir to '".$Config{ZM_PATH_WEB}."': $!" );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
my $dbg_id = "";
|
|
|
|
|
|
|
|
Info( "Command: $command\n" );
|
|
|
|
|
|
|
|
my $retval = 0;
|
|
|
|
|
|
|
|
if ( $command eq "state" )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
Info( "Updating DB: $state->{Name}\n" );
|
2015-07-16 04:18:05 +08:00
|
|
|
my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=? ORDER BY Id ASC' : 'SELECT * FROM Monitors ORDER BY Id ASC';
|
2015-04-11 20:39:32 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
2015-07-16 04:18:05 +08:00
|
|
|
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID}: () )
|
2015-04-11 20:39:32 +08:00
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
foreach my $definition ( @{$state->{Definitions}} )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Id} =~ /^$definition->{Id}$/ )
|
|
|
|
{
|
|
|
|
$monitor->{NewFunction} = $definition->{Function};
|
|
|
|
$monitor->{NewEnabled} = $definition->{Enabled};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#next if ( !$monitor->{NewFunction} );
|
|
|
|
$monitor->{NewFunction} = 'None'
|
|
|
|
if ( !$monitor->{NewFunction} );
|
|
|
|
$monitor->{NewEnabled} = 0
|
|
|
|
if ( !$monitor->{NewEnabled} );
|
|
|
|
if ( $monitor->{Function} ne $monitor->{NewFunction}
|
|
|
|
|| $monitor->{Enabled} ne $monitor->{NewEnabled}
|
|
|
|
)
|
|
|
|
{
|
|
|
|
my $sql = "update Monitors set Function = ?, Enabled = ? where Id = ?";
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
my $res = $sth->execute( $monitor->{NewFunction}, $monitor->{NewEnabled}, $monitor->{Id} )
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sth->finish();
|
2015-06-21 21:30:46 +08:00
|
|
|
#PP - lets go ahead and modify States DB
|
|
|
|
|
|
|
|
|
|
|
|
Debug ("Marking $store_state as Enabled");
|
|
|
|
# PP - Zero out other states being active
|
|
|
|
resetStates();
|
|
|
|
# PP - Now mark a specific state as active
|
|
|
|
$sql = "update States set IsActive = '1' where Name = ?";
|
|
|
|
$sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
$res = $sth->execute( $store_state )
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
2015-04-11 20:39:32 +08:00
|
|
|
|
|
|
|
$command = "restart";
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2014-08-10 21:47:11 +08:00
|
|
|
# Check if we are running systemd and if we have been called by the system
|
|
|
|
if ( $command =~ /^(start|stop|restart)$/ )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
# We have to detaint to keep perl from complaining
|
|
|
|
$command = $1;
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-06-21 21:30:46 +08:00
|
|
|
# PP - if we are not switching to a custom state, zero out all isActive
|
|
|
|
resetStates() if (!$store_state);
|
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
if ( systemdRunning() && !calledBysystem() ) {
|
|
|
|
qx(@BINDIR@/zmsystemctl.pl $command);
|
|
|
|
$command = "";
|
|
|
|
}
|
2014-08-10 21:47:11 +08:00
|
|
|
}
|
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
if ( $command =~ /^(?:stop|restart)$/ )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $status = runCommand( "zmdc.pl check" );
|
|
|
|
|
|
|
|
if ( $status eq "running" )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl shutdown" );
|
|
|
|
zmMemTidy();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$retval = 1;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2013-12-07 05:46:21 +08:00
|
|
|
#runCommand( "zmupdate.pl -f" );
|
2011-04-19 17:27:55 +08:00
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
if ( $command =~ /^(?:start|restart)$/ )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $status = runCommand( "zmdc.pl check" );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
if ( $status eq "stopped" )
|
|
|
|
{
|
|
|
|
if ( $Config{ZM_DYN_DB_VERSION}
|
2015-05-19 12:44:41 +08:00
|
|
|
and ( $Config{ZM_DYN_DB_VERSION} ne ZM_VERSION )
|
2015-04-11 20:39:32 +08:00
|
|
|
)
|
2011-05-25 17:15:14 +08:00
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
Fatal( "Version mismatch, system is version ".ZM_VERSION
|
|
|
|
.", database is ".$Config{ZM_DYN_DB_VERSION}
|
|
|
|
.", please run zmupdate.pl to update."
|
|
|
|
);
|
2011-05-25 17:15:14 +08:00
|
|
|
exit( -1 );
|
|
|
|
}
|
|
|
|
|
2011-05-20 17:46:14 +08:00
|
|
|
# Recreate the temporary directory if it's been wiped
|
2015-04-11 20:39:32 +08:00
|
|
|
verifyFolder("@ZM_TMPDIR@");
|
2014-10-06 00:25:41 +08:00
|
|
|
|
|
|
|
# Recreate the run directory if it's been wiped
|
2015-04-11 20:39:32 +08:00
|
|
|
verifyFolder("@ZM_RUNDIR@");
|
2014-10-06 00:25:41 +08:00
|
|
|
|
|
|
|
# Recreate the sock directory if it's been wiped
|
2015-04-11 20:39:32 +08:00
|
|
|
verifyFolder("@ZM_SOCKDIR@");
|
|
|
|
|
|
|
|
zmMemTidy();
|
|
|
|
runCommand( "zmdc.pl startup" );
|
|
|
|
|
2015-07-16 04:18:05 +08:00
|
|
|
my $sql = $Config{ZM_SERVER_ID} ? 'SELECT * FROM Monitors WHERE ServerId=?' : 'SELECT * FROM Monitors';
|
2015-04-11 20:39:32 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
2015-07-16 04:18:05 +08:00
|
|
|
my $res = $sth->execute( $Config{ZM_SERVER_ID} ? $Config{ZM_SERVER_ID} : () )
|
2015-04-11 20:39:32 +08:00
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
while( my $monitor = $sth->fetchrow_hashref() )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Function} ne 'None' )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Type} eq 'Local' )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmc -d $monitor->{Device}" );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmc -m $monitor->{Id}" );
|
|
|
|
}
|
|
|
|
if ( $monitor->{Function} ne 'Monitor' )
|
|
|
|
{
|
|
|
|
if ( $Config{ZM_OPT_FRAME_SERVER} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmf -m $monitor->{Id}" );
|
|
|
|
}
|
|
|
|
runCommand( "zmdc.pl start zma -m $monitor->{Id}" );
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_CONTROL} )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Function} eq 'Modect' || $monitor->{Function} eq 'Mocord' )
|
|
|
|
{
|
|
|
|
if ( $monitor->{Controllable} && $monitor->{TrackMotion} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmtrack.pl -m $monitor->{Id}" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$sth->finish();
|
|
|
|
|
|
|
|
# This is now started unconditionally
|
|
|
|
runCommand( "zmdc.pl start zmfilter.pl" );
|
|
|
|
if ( $Config{ZM_RUN_AUDIT} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmaudit.pl -c" );
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_TRIGGERS} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmtrigger.pl" );
|
|
|
|
}
|
|
|
|
if ( $Config{ZM_OPT_X10} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmx10.pl -c start" );
|
|
|
|
}
|
|
|
|
runCommand( "zmdc.pl start zmwatch.pl" );
|
|
|
|
if ( $Config{ZM_CHECK_FOR_UPDATES} )
|
|
|
|
{
|
|
|
|
runCommand( "zmdc.pl start zmupdate.pl -c" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$retval = 1;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( $command eq "status" )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $status = runCommand( "zmdc.pl check" );
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
print( STDOUT $status."\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2006-07-04 18:34:21 +08:00
|
|
|
if ( $command eq "logrot" )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
runCommand( "zmdc.pl logrot" );
|
2006-07-04 18:34:21 +08:00
|
|
|
}
|
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
exit( $retval );
|
2013-12-17 05:32:02 +08:00
|
|
|
|
2015-06-21 21:30:46 +08:00
|
|
|
# PP - when the system is restarted/started/stopped, it will
|
|
|
|
# not be in a custom state, so lets keep the DB consistent
|
|
|
|
sub resetStates
|
|
|
|
{
|
|
|
|
$dbh = zmDbConnect() if ! $dbh;
|
|
|
|
my $sql = "update States set IsActive = '0'";
|
|
|
|
my $sth = $dbh->prepare_cached( $sql )
|
|
|
|
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
|
|
|
my $res = $sth->execute()
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-10 21:47:11 +08:00
|
|
|
sub systemdRunning
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $result = 0;
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-05-19 12:44:41 +08:00
|
|
|
my $output = qx(ps -o comm="" -p 1);
|
2015-04-11 20:39:32 +08:00
|
|
|
chomp( $output );
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
if ($output =~ /systemd/) {
|
|
|
|
$result = 1;
|
|
|
|
}
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
return $result;
|
2014-08-10 21:47:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
sub calledBysystem
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $result = 0;
|
|
|
|
my $ppid = getppid();
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-05-19 12:44:41 +08:00
|
|
|
my $output = qx(ps -o comm="" -p $ppid);
|
2015-04-11 20:39:32 +08:00
|
|
|
chomp( $output );
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
if ($output =~ /^(?:systemd|init)$/) {
|
|
|
|
$result = 1;
|
|
|
|
}
|
2014-08-10 21:47:11 +08:00
|
|
|
|
2015-04-11 20:39:32 +08:00
|
|
|
return $result;
|
2014-08-10 21:47:11 +08:00
|
|
|
}
|
2014-10-06 00:25:41 +08:00
|
|
|
|
|
|
|
sub verifyFolder
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
my $folder = shift;
|
2014-10-06 00:25:41 +08:00
|
|
|
|
|
|
|
# Recreate the temporary directory if it's been wiped
|
|
|
|
if ( !-e $folder )
|
|
|
|
{
|
|
|
|
Debug( "Recreating directory '$folder'" );
|
2015-04-11 20:39:32 +08:00
|
|
|
mkdir( "$folder", 0774 )
|
|
|
|
or Fatal( "Can't create missing temporary directory '$folder': $!" );
|
2014-10-06 00:25:41 +08:00
|
|
|
my ( $runName ) = getpwuid( $> );
|
|
|
|
if ( $runName ne $Config{ZM_WEB_USER} )
|
|
|
|
{
|
2015-04-11 20:39:32 +08:00
|
|
|
# Not running as web user, so should be root in which case
|
|
|
|
# chown the directory
|
|
|
|
my ( $webName, $webPass, $webUid, $webGid ) = getpwnam( $Config{ZM_WEB_USER} )
|
|
|
|
or Fatal( "Can't get user details for web user '"
|
|
|
|
.$Config{ZM_WEB_USER}."': $!"
|
|
|
|
);
|
|
|
|
chown( $webUid, $webGid, "$folder" )
|
|
|
|
or Fatal( "Can't change ownership of directory '$folder' to '"
|
|
|
|
.$Config{ZM_WEB_USER}.":".$Config{ZM_WEB_GROUP}."': $!"
|
|
|
|
);
|
2014-10-06 00:25:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 05:32:02 +08:00
|
|
|
__END__
|