3
# ==========================================================================
5
# ZoneMinder Package Control Script, $Date: 2009-06-29 16:27:48 +0100 (Mon, 29 Jun 2009) $, $Revision: 2935 $
6
# Copyright (C) 2001-2008 Philip Coombes
8
# This program is free software; you can redistribute it and/or
9
# modify it under the terms of the GNU General Public License
10
# as published by the Free Software Foundation; either version 2
11
# of the License, or (at your option) any later version.
13
# This program is distributed in the hope that it will be useful,
14
# but WITHOUT ANY WARRANTY; without even the implied warranty of
15
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
# GNU General Public License for more details.
18
# You should have received a copy of the GNU General Public License
19
# along with this program; if not, write to the Free Software
20
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
# ==========================================================================
24
# This script is used to start and stop the ZoneMinder package primarily to
25
# allow command line control for automatic restart on reboot (see zm script)
30
# ==========================================================================
32
# These are the elements you can edit to suit your installation
34
# ==========================================================================
36
use constant DBG_ID => "zmpkg"; # Tag that appears in debug to identify source
37
use constant DBG_LEVEL => 0; # 0 is errors, warnings and info only, > 0 for debug
39
# ==========================================================================
41
# Don't change anything below here
43
# ==========================================================================
49
use Time::HiRes qw/gettimeofday/;
51
# Detaint our environment
52
$ENV{PATH} = '/bin:/usr/bin';
53
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
54
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
56
zmDbgInit( DBG_ID, level=>DBG_LEVEL );
58
my $command = $ARGV[0];
62
my $dbh = zmDbConnect();
64
if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot)$/ )
68
# Check to see if it's a valid run state
69
my $sql = "select * from States where Name = '$command'";
70
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
71
my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() );
72
if ( $state = $sth->fetchrow_hashref() )
74
$state->{Name} = $command;
75
$state->{Definitions} = [];
76
foreach( split( ',', $state->{Definition} ) )
78
my ( $id, $function, $enabled ) = split( ':', $_ );
79
push( @{$state->{Definitions}}, { Id=>$id, Function=>$function, Enabled=>$enabled } );
90
print( "Usage: zmpkg.pl <start|stop|restart|status|logrot|'state'>\n" );
95
# Move to the right place
96
chdir( ZM_PATH_WEB ) or Fatal( "Can't chdir to '".ZM_PATH_WEB."': $!" );
100
Info( "Command: $command\n" );
104
# Determine the appropriate syntax for the su command
106
my ( $cmd_prefix, $cmd_suffix ) = getCmdFormat();
108
if ( $command eq "state" )
110
Info( "Updating DB: $state->{Name}\n" );
111
my $sql = "select * from Monitors order by Id asc";
112
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
113
my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() );
114
while( my $monitor = $sth->fetchrow_hashref() )
116
foreach my $definition ( @{$state->{Definitions}} )
118
if ( $monitor->{Id} =~ /^$definition->{Id}$/ )
120
$monitor->{NewFunction} = $definition->{Function};
121
$monitor->{NewEnabled} = $definition->{Enabled};
124
#next if ( !$monitor->{NewFunction} );
125
$monitor->{NewFunction} = 'None' if ( !$monitor->{NewFunction} );
126
$monitor->{NewEnabled} = 0 if ( !$monitor->{NewEnabled} );
127
if ( $monitor->{Function} ne $monitor->{NewFunction} || $monitor->{Enabled} ne $monitor->{NewEnabled} )
129
my $sql = "update Monitors set Function = ?, Enabled = ? where Id = ?";
130
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
131
my $res = $sth->execute( $monitor->{NewFunction}, $monitor->{NewEnabled}, $monitor->{Id} ) or Fatal( "Can't execute: ".$sth->errstr() );
136
$command = "restart";
139
if ( $command =~ /^(?:stop|restart)$/ )
141
my $status = runCommand( "zmdc.pl check" );
143
if ( $status eq "running" )
145
runCommand( "zmdc.pl shutdown" );
154
if ( $command =~ /^(?:start|restart)$/ )
156
my $status = runCommand( "zmdc.pl check" );
158
if ( $status eq "stopped" )
161
runCommand( "zmfix" );
162
runCommand( "zmdc.pl startup" );
164
my $sql = "select * from Monitors";
165
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
166
my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() );
167
while( my $monitor = $sth->fetchrow_hashref() )
169
if ( $monitor->{Function} ne 'None' )
171
if ( $monitor->{Type} eq 'Local' )
173
runCommand( "zmdc.pl start zmc -d $monitor->{Device}" );
177
runCommand( "zmdc.pl start zmc -m $monitor->{Id}" );
179
if ( $monitor->{Function} ne 'Monitor' )
181
if ( ZM_OPT_FRAME_SERVER )
183
runCommand( "zmdc.pl start zmf -m $monitor->{Id}" );
185
runCommand( "zmdc.pl start zma -m $monitor->{Id}" );
187
if ( ZM_OPT_CONTROL )
189
if ( $monitor->{Function} eq 'Modect' || $monitor->{Function} eq 'Mocord' )
191
if ( $monitor->{Controllable} && $monitor->{TrackMotion} )
193
runCommand( "zmdc.pl start zmtrack.pl -m $monitor->{Id}" );
201
# This is now started unconditionally
202
runCommand( "zmdc.pl start zmfilter.pl" );
205
runCommand( "zmdc.pl start zmaudit.pl -c" );
207
if ( ZM_OPT_TRIGGERS )
209
runCommand( "zmdc.pl start zmtrigger.pl" );
213
runCommand( "zmdc.pl start zmx10.pl -c start" );
215
runCommand( "zmdc.pl start zmwatch.pl" );
216
if ( ZM_CHECK_FOR_UPDATES )
218
runCommand( "zmdc.pl start zmupdate.pl -c" );
227
if ( $command eq "status" )
229
my $status = runCommand( "zmdc.pl check" );
231
print( STDOUT $status."\n" );
234
if ( $command eq "logrot" )
236
runCommand( "zmdc.pl logrot" );
243
Debug( "Testing valid shell syntax\n" );
245
my ( $name ) = getpwuid( $> );
246
if ( $name eq ZM_WEB_USER )
248
Debug( "Running as '$name', su commands not needed\n" );
252
my $null_command = "true";
254
my $prefix = "sudo -u ".ZM_WEB_USER." ";
256
my $command = $prefix.$null_command.$suffix;
257
Debug( "Testing \"$command\"\n" );
258
$command .= " > /dev/null 2>&1";
259
my $output = qx($command);
260
my $status = $? >> 8;
263
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
264
return( $prefix, $suffix );
269
Debug( "Test failed, '$output'\n" );
271
$prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='";
273
$command = $prefix.$null_command.$suffix;
274
Debug( "Testing \"$command\"\n" );
275
my $output = qx($command);
276
my $status = $? >> 8;
279
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
280
return( $prefix, $suffix );
285
Debug( "Test failed, '$output'\n" );
287
$prefix = "su ".ZM_WEB_USER." -c '";
289
$command = $prefix.$null_command.$suffix;
290
Debug( "Testing \"$command\"\n" );
291
$output = qx($command);
295
Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
296
return( $prefix, $suffix );
301
Debug( "Test failed, '$output'\n" );
305
Error( "Unable to find valid 'su' syntax\n" );
312
$command = ZM_PATH_BIN."/".$command;
315
$command = $cmd_prefix.$command.$cmd_suffix;
317
Debug( "Command: $command\n" );
318
my $output = qx($command);
319
my $status = $? >> 8;
321
if ( $status || DBG_LEVEL > 0 )
325
Error( "Unable to run \"$command\", output is \"$output\"\n" );
330
Debug( "Output: $output\n" );