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.
#
# ==========================================================================
#
# 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)
#
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/ ;
# Detaint our environment
$ ENV { PATH } = '/bin:/usr/bin' ;
$ ENV { SHELL } = '/bin/sh' if exists $ ENV { SHELL } ;
delete @ ENV { qw( IFS CDPATH ENV BASH_ENV ) } ;
2011-06-21 17:19:10 +08:00
logInit ( ) ;
2005-12-21 01:03:33 +08:00
2005-12-16 18:05:29 +08:00
my $ command = $ ARGV [ 0 ] ;
my $ state ;
2007-09-07 23:39:44 +08:00
my $ dbh = zmDbConnect ( ) ;
2005-12-16 18:05:29 +08:00
2006-07-04 18:34:21 +08:00
if ( ! $ command || $ command !~ /^(?:start|stop|restart|status|logrot)$/ )
2005-12-16 18:05:29 +08:00
{
if ( $ command )
{
# Check to see if it's a valid run state
2013-12-07 05:46:21 +08:00
my $ sql = 'select * from States where Name = ?' ;
2006-10-20 17:31:40 +08:00
my $ sth = $ dbh - > prepare_cached ( $ sql ) or Fatal ( "Can't prepare '$sql': " . $ dbh - > errstr ( ) ) ;
2013-12-07 05:46:21 +08:00
my $ res = $ sth - > execute ( $ command ) or Fatal ( "Can't execute: " . $ sth - > errstr ( ) ) ;
2005-12-16 18:05:29 +08:00
if ( $ state = $ sth - > fetchrow_hashref ( ) )
{
$ state - > { Name } = $ command ;
$ state - > { Definitions } = [] ;
2011-08-26 15:51:36 +08:00
foreach ( split ( /,/ , $ state - > { Definition } ) )
2005-12-16 18:05:29 +08:00
{
2011-08-26 15:51:36 +08:00
my ( $ id , $ function , $ enabled ) = split ( /:/ , $ _ ) ;
2005-12-23 00:58:29 +08:00
push ( @ { $ state - > { Definitions } } , { Id = > $ id , Function = > $ function , Enabled = > $ enabled } ) ;
2005-12-16 18:05:29 +08:00
}
$ command = 'state' ;
}
else
{
$ command = undef ;
}
}
if ( ! $ command )
{
2006-07-04 18:34:21 +08:00
print ( "Usage: zmpkg.pl <start|stop|restart|status|logrot|'state'>\n" ) ;
2005-12-16 18:05:29 +08:00
exit ( - 1 ) ;
}
}
# Move to the right place
2013-12-17 05:32:02 +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" )
{
Info ( "Updating DB: $state->{Name}\n" ) ;
my $ sql = "select * from Monitors order by Id asc" ;
2006-10-20 17:31:40 +08:00
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 ( ) ) ;
2005-12-16 18:05:29 +08:00
while ( my $ monitor = $ sth - > fetchrow_hashref ( ) )
{
foreach my $ definition ( @ { $ state - > { Definitions } } )
{
if ( $ monitor - > { Id } =~ /^$definition->{Id}$/ )
{
$ monitor - > { NewFunction } = $ definition - > { Function } ;
2005-12-23 00:58:29 +08:00
$ monitor - > { NewEnabled } = $ definition - > { Enabled } ;
2005-12-16 18:05:29 +08:00
}
}
#next if ( !$monitor->{NewFunction} );
$ monitor - > { NewFunction } = 'None' if ( ! $ monitor - > { NewFunction } ) ;
2009-06-29 23:27:48 +08:00
$ monitor - > { NewEnabled } = 0 if ( ! $ monitor - > { NewEnabled } ) ;
2005-12-23 00:58:29 +08:00
if ( $ monitor - > { Function } ne $ monitor - > { NewFunction } || $ monitor - > { Enabled } ne $ monitor - > { NewEnabled } )
2005-12-16 18:05:29 +08:00
{
2005-12-23 00:58:29 +08:00
my $ sql = "update Monitors set Function = ?, Enabled = ? where Id = ?" ;
2006-10-20 17:31:40 +08:00
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 ( ) ) ;
2005-12-16 18:05:29 +08:00
}
}
$ sth - > finish ( ) ;
$ command = "restart" ;
}
if ( $ command =~ /^(?:stop|restart)$/ )
{
my $ status = runCommand ( "zmdc.pl check" ) ;
if ( $ status eq "running" )
{
runCommand ( "zmdc.pl shutdown" ) ;
2008-07-14 23:21:16 +08:00
zmMemTidy ( ) ;
2005-12-16 18:05:29 +08:00
}
else
{
$ retval = 1 ;
}
}
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)$/ )
{
my $ status = runCommand ( "zmdc.pl check" ) ;
if ( $ status eq "stopped" )
{
2013-12-18 02:05:58 +08:00
if ( $ Config { ZM_DYN_DB_VERSION } and ( $ Config { ZM_DYN_DB_VERSION } ne ZM_VERSION ) )
2011-05-25 17:15:14 +08:00
{
2013-12-17 05:32:02 +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
if ( ! - e "@ZM_TMPDIR@" )
{
2011-05-30 18:39:26 +08:00
Debug ( "Recreating temporary directory '@ZM_TMPDIR@'" ) ;
2011-05-20 17:46:14 +08:00
mkdir ( "@ZM_TMPDIR@" , 0700 ) or Fatal ( "Can't create missing temporary directory '@ZM_TMPDIR@': $!" ) ;
my ( $ runName ) = getpwuid ( $> ) ;
2013-12-17 05:32:02 +08:00
if ( $ runName ne $ Config { ZM_WEB_USER } )
2011-05-20 17:46:14 +08:00
{
# Not running as web user, so should be root in whch case chown the temporary directory
2013-12-17 05:32:02 +08:00
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 , "@ZM_TMPDIR@" ) or Fatal ( "Can't change ownership of temporary directory '@ZM_TMPDIR@' to '" . $ Config { ZM_WEB_USER } . ":" . $ Config { ZM_WEB_GROUP } . "': $!" ) ;
2011-05-20 17:46:14 +08:00
}
}
2008-07-14 23:21:16 +08:00
zmMemTidy ( ) ;
2005-12-16 18:05:29 +08:00
runCommand ( "zmfix" ) ;
2005-12-23 00:58:29 +08:00
runCommand ( "zmdc.pl startup" ) ;
2005-12-16 18:05:29 +08:00
my $ sql = "select * from Monitors" ;
2006-10-20 17:31:40 +08:00
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 ( ) ) ;
2005-12-16 18:05:29 +08:00
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' )
{
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_OPT_FRAME_SERVER } )
2005-12-16 18:05:29 +08:00
{
runCommand ( "zmdc.pl start zmf -m $monitor->{Id}" ) ;
}
runCommand ( "zmdc.pl start zma -m $monitor->{Id}" ) ;
}
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_OPT_CONTROL } )
2005-12-16 18:05:29 +08:00
{
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" ) ;
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_RUN_AUDIT } )
2006-01-12 08:02:17 +08:00
{
2006-01-12 23:38:57 +08:00
runCommand ( "zmdc.pl start zmaudit.pl -c" ) ;
2006-01-12 08:02:17 +08:00
}
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_OPT_TRIGGERS } )
2005-12-16 18:05:29 +08:00
{
runCommand ( "zmdc.pl start zmtrigger.pl" ) ;
}
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_OPT_X10 } )
2005-12-16 18:05:29 +08:00
{
runCommand ( "zmdc.pl start zmx10.pl -c start" ) ;
}
runCommand ( "zmdc.pl start zmwatch.pl" ) ;
2013-12-17 05:32:02 +08:00
if ( $ Config { ZM_CHECK_FOR_UPDATES } )
2005-12-16 18:05:29 +08:00
{
runCommand ( "zmdc.pl start zmupdate.pl -c" ) ;
}
}
else
{
$ retval = 1 ;
}
}
if ( $ command eq "status" )
{
my $ status = runCommand ( "zmdc.pl check" ) ;
print ( STDOUT $ status . "\n" ) ;
}
2006-07-04 18:34:21 +08:00
if ( $ command eq "logrot" )
{
runCommand ( "zmdc.pl logrot" ) ;
}
2005-12-16 18:05:29 +08:00
exit ( $ retval ) ;
2013-12-17 05:32:02 +08:00
__END__