~ubuntu-branches/ubuntu/natty/zoneminder/natty

« back to all changes in this revision

Viewing changes to scripts/zmpkg.pl.in

  • Committer: Bazaar Package Importer
  • Author(s): Peter Howard
  • Date: 2009-09-11 07:02:50 UTC
  • mfrom: (1.2.2 upstream)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090911070250-k6rtdg43xi46nix4
Tags: 1.24.2-1
Initial release of zoneminder 1.24.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl -wT
 
2
#
 
3
# ==========================================================================
 
4
#
 
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
 
7
#
 
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.
 
12
#
 
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.
 
17
#
 
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.
 
21
#
 
22
# ==========================================================================
 
23
#
 
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)
 
26
#
 
27
use strict;
 
28
use bytes;
 
29
 
 
30
# ==========================================================================
 
31
#
 
32
# These are the elements you can edit to suit your installation
 
33
#
 
34
# ==========================================================================
 
35
 
 
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
 
38
 
 
39
# ==========================================================================
 
40
#
 
41
# Don't change anything below here
 
42
#
 
43
# ==========================================================================
 
44
 
 
45
@EXTRA_PERL_LIB@
 
46
use ZoneMinder;
 
47
use DBI;
 
48
use POSIX;
 
49
use Time::HiRes qw/gettimeofday/;
 
50
 
 
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)};
 
55
 
 
56
zmDbgInit( DBG_ID, level=>DBG_LEVEL );
 
57
 
 
58
my $command = $ARGV[0];
 
59
 
 
60
my $state;
 
61
 
 
62
my $dbh = zmDbConnect();
 
63
 
 
64
if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot)$/ )
 
65
{
 
66
        if ( $command )
 
67
        {
 
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() )
 
73
                {
 
74
                        $state->{Name} = $command;
 
75
                        $state->{Definitions} = [];
 
76
                        foreach( split( ',', $state->{Definition} ) )
 
77
                        {
 
78
                                my ( $id, $function, $enabled ) = split( ':', $_ );
 
79
                                push( @{$state->{Definitions}}, { Id=>$id, Function=>$function, Enabled=>$enabled } );
 
80
                        }
 
81
                        $command = 'state';
 
82
                }
 
83
                else
 
84
                {
 
85
                        $command = undef;
 
86
                }
 
87
        }
 
88
        if ( !$command )
 
89
        {
 
90
                print( "Usage: zmpkg.pl <start|stop|restart|status|logrot|'state'>\n" );
 
91
                exit( -1 );
 
92
        }
 
93
}
 
94
 
 
95
# Move to the right place
 
96
chdir( ZM_PATH_WEB ) or Fatal( "Can't chdir to '".ZM_PATH_WEB."': $!" );
 
97
 
 
98
my $dbg_id = "";
 
99
 
 
100
Info( "Command: $command\n" );
 
101
 
 
102
my $retval = 0;
 
103
 
 
104
# Determine the appropriate syntax for the su command
 
105
 
 
106
my ( $cmd_prefix, $cmd_suffix ) = getCmdFormat();
 
107
 
 
108
if ( $command eq "state" )
 
109
{
 
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() )
 
115
        {
 
116
                foreach my $definition ( @{$state->{Definitions}} )
 
117
                {
 
118
                        if ( $monitor->{Id} =~ /^$definition->{Id}$/ )
 
119
                        {
 
120
                                $monitor->{NewFunction} = $definition->{Function};
 
121
                                $monitor->{NewEnabled} = $definition->{Enabled};
 
122
                        }
 
123
                }
 
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} )
 
128
                {
 
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() );
 
132
                }
 
133
        }
 
134
        $sth->finish();
 
135
 
 
136
        $command = "restart";
 
137
}
 
138
 
 
139
if ( $command =~ /^(?:stop|restart)$/ )
 
140
{
 
141
        my $status = runCommand( "zmdc.pl check" );
 
142
 
 
143
        if ( $status eq "running" )
 
144
        {
 
145
                runCommand( "zmdc.pl shutdown" );
 
146
                zmMemTidy();
 
147
        }
 
148
        else
 
149
        {
 
150
                $retval = 1;
 
151
        }
 
152
}
 
153
 
 
154
if ( $command =~ /^(?:start|restart)$/ )
 
155
{
 
156
        my $status = runCommand( "zmdc.pl check" );
 
157
 
 
158
        if ( $status eq "stopped" )
 
159
        {
 
160
                zmMemTidy();
 
161
                runCommand( "zmfix" );
 
162
                runCommand( "zmdc.pl startup" );
 
163
 
 
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() )
 
168
                {
 
169
                        if ( $monitor->{Function} ne 'None' )
 
170
                        {
 
171
                                if ( $monitor->{Type} eq 'Local' )
 
172
                                {
 
173
                                        runCommand( "zmdc.pl start zmc -d $monitor->{Device}" );
 
174
                                }
 
175
                                else
 
176
                                {
 
177
                                        runCommand( "zmdc.pl start zmc -m $monitor->{Id}" );
 
178
                                }
 
179
                                if ( $monitor->{Function} ne 'Monitor' )
 
180
                                {
 
181
                                        if ( ZM_OPT_FRAME_SERVER )
 
182
                                        {
 
183
                                                runCommand( "zmdc.pl start zmf -m $monitor->{Id}" );
 
184
                                        }
 
185
                                        runCommand( "zmdc.pl start zma -m $monitor->{Id}" );
 
186
                                }
 
187
                                if ( ZM_OPT_CONTROL )
 
188
                                {
 
189
                                        if ( $monitor->{Function} eq 'Modect' || $monitor->{Function} eq 'Mocord' )
 
190
                                        {
 
191
                                                if ( $monitor->{Controllable} && $monitor->{TrackMotion} )
 
192
                                                {
 
193
                                                        runCommand( "zmdc.pl start zmtrack.pl -m $monitor->{Id}" );
 
194
                                                }
 
195
                                        }
 
196
                                }
 
197
                        }
 
198
                }
 
199
                $sth->finish();
 
200
 
 
201
                # This is now started unconditionally
 
202
                runCommand( "zmdc.pl start zmfilter.pl" );
 
203
                if ( ZM_RUN_AUDIT )
 
204
                {
 
205
                        runCommand( "zmdc.pl start zmaudit.pl -c" );
 
206
                }
 
207
                if ( ZM_OPT_TRIGGERS )
 
208
                {
 
209
                        runCommand( "zmdc.pl start zmtrigger.pl" );
 
210
                }
 
211
                if ( ZM_OPT_X10 )
 
212
                {
 
213
                        runCommand( "zmdc.pl start zmx10.pl -c start" );
 
214
                }
 
215
                runCommand( "zmdc.pl start zmwatch.pl" );
 
216
                if ( ZM_CHECK_FOR_UPDATES )
 
217
                {
 
218
                        runCommand( "zmdc.pl start zmupdate.pl -c" );
 
219
                }
 
220
        }
 
221
        else
 
222
        {
 
223
                $retval = 1;
 
224
        }
 
225
}
 
226
 
 
227
if ( $command eq "status" )
 
228
{
 
229
        my $status = runCommand( "zmdc.pl check" );
 
230
 
 
231
        print( STDOUT $status."\n" );
 
232
}
 
233
 
 
234
if ( $command eq "logrot" )
 
235
{
 
236
        runCommand( "zmdc.pl logrot" );
 
237
}
 
238
 
 
239
exit( $retval );
 
240
 
 
241
sub getCmdFormat
 
242
{
 
243
        Debug( "Testing valid shell syntax\n" );
 
244
 
 
245
        my ( $name ) = getpwuid( $> );
 
246
        if ( $name eq ZM_WEB_USER )
 
247
        {
 
248
                Debug( "Running as '$name', su commands not needed\n" );
 
249
                return( "" );
 
250
        }
 
251
 
 
252
        my $null_command = "true";
 
253
 
 
254
        my $prefix = "sudo -u ".ZM_WEB_USER." ";
 
255
        my $suffix = "";
 
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;
 
261
        if ( !$status )
 
262
        {
 
263
                Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
 
264
                return( $prefix, $suffix );
 
265
        }
 
266
        else
 
267
        {
 
268
                chomp( $output );
 
269
                Debug( "Test failed, '$output'\n" );
 
270
 
 
271
                $prefix = "su ".ZM_WEB_USER." --shell=/bin/sh --command='";
 
272
                $suffix = "'";
 
273
                $command = $prefix.$null_command.$suffix;
 
274
                Debug( "Testing \"$command\"\n" );
 
275
                my $output = qx($command);
 
276
                my $status = $? >> 8;
 
277
                if ( !$status )
 
278
                {
 
279
                        Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
 
280
                        return( $prefix, $suffix );
 
281
                }
 
282
                else
 
283
                {
 
284
                        chomp( $output );
 
285
                        Debug( "Test failed, '$output'\n" );
 
286
 
 
287
                        $prefix = "su ".ZM_WEB_USER." -c '";
 
288
                        $suffix = "'";
 
289
                        $command = $prefix.$null_command.$suffix;
 
290
                        Debug( "Testing \"$command\"\n" );
 
291
                        $output = qx($command);
 
292
                        $status = $? >> 8;
 
293
                        if ( !$status )
 
294
                        {
 
295
                                Debug( "Test ok, using format \"$prefix<command>$suffix\"\n" );
 
296
                                return( $prefix, $suffix );
 
297
                        }
 
298
                        else
 
299
                        {
 
300
                                chomp( $output );
 
301
                                Debug( "Test failed, '$output'\n" );
 
302
                        }
 
303
                }
 
304
        }
 
305
        Error( "Unable to find valid 'su' syntax\n" );
 
306
        exit( -1 );
 
307
}
 
308
 
 
309
sub runCommand
 
310
{
 
311
        my $command = shift;
 
312
        $command = ZM_PATH_BIN."/".$command;
 
313
        if ( $cmd_prefix )
 
314
        {
 
315
                $command = $cmd_prefix.$command.$cmd_suffix;
 
316
        }
 
317
        Debug( "Command: $command\n" );
 
318
        my $output = qx($command);
 
319
        my $status = $? >> 8;
 
320
        chomp( $output );
 
321
        if ( $status || DBG_LEVEL > 0 )
 
322
        {
 
323
                if ( $status )
 
324
                {
 
325
                        Error( "Unable to run \"$command\", output is \"$output\"\n" );
 
326
                        exit( -1 );
 
327
                }
 
328
                else
 
329
                {
 
330
                        Debug( "Output: $output\n" );
 
331
                }
 
332
        }
 
333
        return( $output );
 
334
}