2002-09-24 06:08:50 +08:00
|
|
|
//
|
2002-12-10 21:21:41 +08:00
|
|
|
// ZoneMinder Analysis Daemon, $Date$, $Revision$
|
2006-01-17 18:56:30 +08:00
|
|
|
// Copyright (C) 2003, 2004, 2005, 2006 Philip Coombes
|
2002-09-24 06:08:50 +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.
|
|
|
|
//
|
|
|
|
|
2002-12-24 20:01:28 +08:00
|
|
|
#include <getopt.h>
|
2003-03-26 20:00:16 +08:00
|
|
|
#include <signal.h>
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
#include "zm.h"
|
|
|
|
#include "zm_db.h"
|
|
|
|
#include "zm_monitor.h"
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
void zm_die_handler( int signal )
|
2002-11-07 20:25:48 +08:00
|
|
|
{
|
2004-01-28 01:21:56 +08:00
|
|
|
#if HAVE_DECL_STRSIGNAL
|
|
|
|
char * error = strsignal(signal);
|
|
|
|
size_t errorStringSize = strlen(error) + strlen("Got signal (), crashing.");
|
|
|
|
char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char.
|
|
|
|
(void) snprintf(errorString, errorStringSize, "Got signal (%s), crashing.", error);
|
2004-01-15 05:26:47 +08:00
|
|
|
|
2004-03-17 18:28:07 +08:00
|
|
|
Error(( (const char *)errorString ));
|
2004-01-28 01:21:56 +08:00
|
|
|
free(errorString);
|
|
|
|
#else /* HAVE_DECL_STRSIGNAL */
|
2004-03-17 18:28:07 +08:00
|
|
|
Error(( "Got signal %d, crashing", signal ));
|
2004-01-28 01:21:56 +08:00
|
|
|
#endif /* HAVE_DECL_STRSIGNAL */
|
2002-11-07 20:25:48 +08:00
|
|
|
exit( signal );
|
|
|
|
}
|
|
|
|
|
2003-10-08 20:53:07 +08:00
|
|
|
bool zma_terminate = false;
|
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
void zm_term_handler( int signal )
|
2002-09-23 18:11:04 +08:00
|
|
|
{
|
2004-01-28 01:21:56 +08:00
|
|
|
#if HAVE_DECL_STRSIGNAL
|
|
|
|
char * error = strsignal(signal);
|
|
|
|
size_t errorStringSize = strlen(error) + strlen("Got signal (), exiting.");
|
|
|
|
char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char.
|
|
|
|
(void) snprintf(errorString, errorStringSize, "Got signal (%s), exiting.", error);
|
2004-01-15 05:26:47 +08:00
|
|
|
|
2004-01-28 01:21:56 +08:00
|
|
|
Info(( (const char *)errorString ));
|
|
|
|
free(errorString);
|
|
|
|
#else /* HAVE_DECL_STRSIGNAL */
|
|
|
|
Info(( "Got TERM signal, exiting" ));
|
|
|
|
#endif /* HAVE_DECL_STRSIGNAL */
|
2003-10-08 20:53:07 +08:00
|
|
|
zma_terminate = true;
|
2002-09-23 18:11:04 +08:00
|
|
|
}
|
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
bool zma_reload = false;
|
|
|
|
|
|
|
|
void zm_hup_handler( int signal )
|
|
|
|
{
|
|
|
|
#if HAVE_DECL_STRSIGNAL
|
|
|
|
char * error = strsignal(signal);
|
|
|
|
size_t errorStringSize = strlen(error) + strlen("Got signal (), reloading.");
|
|
|
|
char * errorString =(char *) malloc(errorStringSize + 1); // plus 1 for termination char.
|
|
|
|
(void) snprintf(errorString, errorStringSize, "Got signal (%s), reloading.", error);
|
|
|
|
|
|
|
|
Info(( (const char *)errorString ));
|
|
|
|
free(errorString);
|
|
|
|
#else /* HAVE_DECL_STRSIGNAL */
|
|
|
|
Info(( "Got HUP signal, reloading" ));
|
|
|
|
#endif /* HAVE_DECL_STRSIGNAL */
|
|
|
|
zma_reload = true;
|
|
|
|
}
|
|
|
|
|
2002-12-24 20:01:28 +08:00
|
|
|
void Usage()
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2002-12-24 20:01:28 +08:00
|
|
|
fprintf( stderr, "zma -m <monitor_id>\n" );
|
|
|
|
fprintf( stderr, "Options:\n" );
|
|
|
|
fprintf( stderr, " -m, --monitor <monitor_id> : Specify which monitor to use\n" );
|
|
|
|
fprintf( stderr, " -h, --help : This screen\n" );
|
|
|
|
exit( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
int id = -1;
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
{"monitor", 1, 0, 'm'},
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
|
|
int c = getopt_long (argc, argv, "m:h", long_options, &option_index);
|
|
|
|
if (c == -1)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'm':
|
|
|
|
id = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
|
|
|
Usage();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
//fprintf( stderr, "?? getopt returned character code 0%o ??\n", c );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (optind < argc)
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Extraneous options, " );
|
|
|
|
while (optind < argc)
|
|
|
|
printf ("%s ", argv[optind++]);
|
|
|
|
printf ("\n");
|
|
|
|
Usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( id < 0 )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Bogus monitor %d\n", id );
|
|
|
|
Usage();
|
|
|
|
exit( 0 );
|
|
|
|
}
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2005-01-16 01:45:51 +08:00
|
|
|
char dbg_id_string[16];
|
|
|
|
snprintf( dbg_id_string, sizeof(dbg_id_string), "m%d", id );
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2005-01-16 01:45:51 +08:00
|
|
|
zmDbgInit( "zma", dbg_id_string, 0 );
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
zmLoadConfig();
|
|
|
|
|
2003-06-12 22:29:54 +08:00
|
|
|
Monitor *monitor = Monitor::Load( id, true, Monitor::ANALYSIS );
|
2002-09-16 17:19:24 +08:00
|
|
|
|
2002-12-24 20:01:28 +08:00
|
|
|
if ( monitor )
|
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
Info(( "In mode %d/%d, warming up", monitor->GetFunction(), monitor->Enabled() ));
|
2002-11-07 20:25:48 +08:00
|
|
|
|
2005-05-16 17:27:06 +08:00
|
|
|
if ( config.opt_frame_server )
|
2003-04-22 22:15:59 +08:00
|
|
|
{
|
|
|
|
Event::OpenFrameSocket( monitor->Id() );
|
|
|
|
}
|
|
|
|
|
2002-12-24 20:01:28 +08:00
|
|
|
sigset_t block_set;
|
|
|
|
sigemptyset( &block_set );
|
|
|
|
struct sigaction action, old_action;
|
2002-11-07 20:25:48 +08:00
|
|
|
|
2005-12-23 00:46:25 +08:00
|
|
|
action.sa_handler = zm_hup_handler;
|
|
|
|
action.sa_mask = block_set;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigaction( SIGHUP, &action, &old_action );
|
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
action.sa_handler = zm_term_handler;
|
2002-12-24 20:01:28 +08:00
|
|
|
action.sa_mask = block_set;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigaction( SIGTERM, &action, &old_action );
|
2002-11-07 20:25:48 +08:00
|
|
|
|
2003-03-26 20:00:16 +08:00
|
|
|
action.sa_handler = zm_die_handler;
|
2002-12-24 20:01:28 +08:00
|
|
|
action.sa_mask = block_set;
|
|
|
|
action.sa_flags = 0;
|
|
|
|
sigaction( SIGBUS, &action, &old_action );
|
|
|
|
sigaction( SIGSEGV, &action, &old_action );
|
2002-11-07 20:25:48 +08:00
|
|
|
|
2003-10-08 20:53:07 +08:00
|
|
|
while( !zma_terminate )
|
2002-09-16 17:19:24 +08:00
|
|
|
{
|
2002-12-24 20:01:28 +08:00
|
|
|
// Process the next image
|
|
|
|
sigprocmask( SIG_BLOCK, &block_set, 0 );
|
|
|
|
if ( !monitor->Analyse() )
|
2002-09-23 18:11:04 +08:00
|
|
|
{
|
2005-12-23 00:46:25 +08:00
|
|
|
usleep( monitor->Active()?ZM_SAMPLE_RATE:ZM_SUSPENDED_RATE );
|
|
|
|
}
|
|
|
|
if ( zma_reload )
|
|
|
|
{
|
|
|
|
monitor->Reload();
|
|
|
|
zma_reload = false;
|
2002-09-23 18:11:04 +08:00
|
|
|
}
|
2002-12-24 20:01:28 +08:00
|
|
|
sigprocmask( SIG_UNBLOCK, &block_set, 0 );
|
2002-09-23 18:11:04 +08:00
|
|
|
}
|
2003-10-08 20:53:07 +08:00
|
|
|
delete monitor;
|
2002-12-24 20:01:28 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Can't find monitor with id of %d\n", id );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|
2002-10-28 22:33:57 +08:00
|
|
|
return( 0 );
|
2002-09-16 17:19:24 +08:00
|
|
|
}
|