/
motion-proxy
executable file
·182 lines (147 loc) · 3.86 KB
/
motion-proxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/perl -w
# Options:
#
# -d (daemonize)
# -c <configfile> (default: /etc/motion-proxy/motion-proxy.conf)
# -D (debug)
use strict;
use diagnostics;
use warnings;
use POSIX qw(setsid);
use Filesys::Df;
use Log::Lite qw(logrotate logmode logpath log);
use MotionProxy::Constant;
use MotionProxy::Config;
use threads (
'yield',
'stack_size' => 128 * 4096,
'exit' => 'threads_only',
'stringify'
);
my $Config = MotionProxy::Constant::CONFIG;
my $Fork = 0;
my %Defaults = (
'LogPath' => '/tmp/' . MotionProxy::Constant::LOGNAME,
'Sleep' => 10,
'MaxUsage' => 70
);
my $i = -1;
while ( $i < $#ARGV ) {
++$i;
if ( $ARGV[$i] eq '-d' ) {
$Fork = 1;
next;
}
if ( $ARGV[$i] eq '-c' ) {
$Config = $ARGV[ $i + 1 ];
++$i;
next;
}
if ( $ARGV[$i] eq '-D' ) {
$main::Debug = 1;
logmode("debug"); #output to STDERR
$Fork = 0;
next;
}
die("Unrecognized arg: $ARGV[$i]");
}
unless ( -e $Config && -r $Config ) {
die( "Cannot access configuration: ", $Config );
}
$main::Configuration = new MotionProxy::Config( $Config, \%Defaults );
logpath( $main::Configuration->get('LogPath') );
logrotate("week"); #autocut logfile every week
logrotate("yes"); #enable autocut
&dolog( "Starting with config: ", $Config );
# Get set of directories used
my %watchDirs;
for my $cam (@MotionProxy::Camera::Instances) {
my $dir = $cam->{inpath};
$watchDirs{$dir} = $dir;
}
my $MaxUsage = $main::Configuration->get('MaxUsage');
my $Sleep = $main::Configuration->get('Sleep');
# If forking,
# wait till after configuration (with possibly relative paths)
# and, before starting camera service threads
my $PIDFile = $main::Configuration->get('PIDFile');
if ($Fork) {
daemonize();
}
sub StartAll {
for my $q ( values %MotionProxy::Queue::Instances ) {
print "Starting $q->{name} \n";
$q->{thread} = async { $q->start(); };
}
for my $q ( values %MotionProxy::Queue::Instances ) {
$q->{thread}->join();
print "$q->{name} joined! .\n";
}
# while (1) {
# if ( $self->{thread} ) {
# if ( $self->{thread}->is_running() ) {
# sleep(2);
# }
# else {
# print "Thread exited, restarting ...\n";
# $self->{conn}->force_last_request();
# $self->{conn}->close();
#
# undef $self->{conn};
# undef $self->{daemon};
# $self->start_thread();
# }
# }
# else {
# $self->start_thread();
# }
# }
}
# Hang on and monitor diskspace, etc.
sub main_loop {
# Start camera queues...
# MotionProxy::Queue::StartAll();
StartAll();
# threads->yield();
# while (1) {
# for my $dir ( keys %watchDirs ) {
# my $ref = df($dir);
# my $usage = $ref->{used} / $ref->{bavail};
# if ( $usage > $MaxUsage ) {
# &dielog("Maximum disk usage reached: $usage > $MaxUsage");
# }
# }
# if ($main::Debug) { print "Sleeping...\n"; }
# threads->yield();
# }
}
sub daemonize {
my $pid;
defined( $pid = fork() ) || die("can't fork: $!");
exit if $pid; # non-zero now means I am the parent
( setsid() != -1 ) || die("Can't start a new session: $!");
defined( $pid = fork() ) || die("can't fork: $!");
exit if $pid;
chdir("/") || die("can't chdir to /: $!");
open( STDIN, "< /dev/null" ) || die("can't read /dev/null: $!");
open( STDOUT, "> /dev/null" ) || die("can't write to /dev/null: $!");
open( STDERR, "> /dev/null" ) || die("can't dup stdout: $!");
umask 0;
system( "echo $$ > $PIDFile" );
dolog("PID File: $PIDFile");
}
sub dolog {
log( MotionProxy::Constant::LOGNAME, @_ );
}
sub dielog {
# log( MotionProxy::Constant::LOGNAME, @_ );
die(@_);
}
sub DIE_handler {
my($signal) = @_;
dolog(@_);
}
my $T = async { main_loop(); };
$T->join();
exit;
__END__