2003-04-22 22:17:04 +08:00
|
|
|
|
//
|
|
|
|
|
// ZoneMinder Image File Writer Implementation, $Date$, $Revision$
|
2008-07-25 17:33:23 +08:00
|
|
|
|
// Copyright (C) 2001-2008 Philip Coombes
|
2003-04-22 22:17:04 +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.
|
|
|
|
|
//
|
|
|
|
|
|
2015-06-21 06:11:20 +08:00
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
|
|
zmf - The ZoneMinder Frame daemon
|
|
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
|
|
|
|
|
zmf -m <monitor_id>
|
|
|
|
|
zmf --monitor <monitor_id>
|
|
|
|
|
zmf -h
|
|
|
|
|
zmf --help
|
|
|
|
|
zmf -v
|
|
|
|
|
zmf --version
|
|
|
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
|
|
This is an optional daemon that can run in concert with the Analysis daemon and
|
|
|
|
|
whose function it is to actually write captured frames to disk. This frees up
|
|
|
|
|
the Analysis daemon to do more analysis (!) and so keep up with the Capture
|
|
|
|
|
daemon better. If it isn’t running or dies then the Analysis daemon just writes
|
|
|
|
|
them itself.
|
|
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
|
|
|
|
|
|
|
|
|
-m, --monitor_id - ID of the monitor to use
|
|
|
|
|
-h, --help - Display usage information
|
|
|
|
|
-v, --version - Print the installed version of ZoneMinder
|
|
|
|
|
|
|
|
|
|
=cut
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
2003-04-22 22:17:04 +08:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <getopt.h>
|
|
|
|
|
#include <signal.h>
|
|
|
|
|
|
2003-05-16 18:27:41 +08:00
|
|
|
|
#include "zm.h"
|
2003-04-22 22:17:04 +08:00
|
|
|
|
#include "zm_db.h"
|
2006-04-05 20:22:27 +08:00
|
|
|
|
#include "zm_signal.h"
|
2003-04-22 22:17:04 +08:00
|
|
|
|
#include "zm_monitor.h"
|
|
|
|
|
|
|
|
|
|
#include "zmf.h"
|
|
|
|
|
|
|
|
|
|
int OpenSocket( int monitor_id )
|
|
|
|
|
{
|
|
|
|
|
int sd = socket( AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
|
if ( sd < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't create socket: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
return( -1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char sock_path[PATH_MAX] = "";
|
2005-05-16 17:27:06 +08:00
|
|
|
|
snprintf( sock_path, sizeof(sock_path), "%s/zmf-%d.sock", config.path_socks, monitor_id );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
if ( unlink( sock_path ) < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Warning( "Can't unlink '%s': %s", sock_path, strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
2004-04-20 00:02:17 +08:00
|
|
|
|
strncpy( addr.sun_path, sock_path, sizeof(addr.sun_path) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
|
|
|
|
|
|
if ( bind( sd, (struct sockaddr *)&addr, strlen(addr.sun_path)+sizeof(addr.sun_family)) < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't bind: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( listen( sd, SOMAXCONN ) < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't listen: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
return( -1 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct sockaddr_un rem_addr;
|
|
|
|
|
socklen_t rem_addr_len = sizeof(rem_addr);
|
|
|
|
|
int new_sd = -1;
|
|
|
|
|
if ( (new_sd = accept( sd, (struct sockaddr *)&rem_addr, &rem_addr_len )) < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't accept: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
|
|
|
|
close( sd );
|
|
|
|
|
|
|
|
|
|
sd = new_sd;
|
|
|
|
|
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Info( "Frame server socket open, awaiting images" );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
return( sd );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ReopenSocket( int &sd, int monitor_id )
|
|
|
|
|
{
|
|
|
|
|
close( sd );
|
|
|
|
|
return( sd = OpenSocket( monitor_id ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Usage()
|
|
|
|
|
{
|
|
|
|
|
fprintf( stderr, "zmf -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" );
|
2015-01-07 09:54:49 +08:00
|
|
|
|
fprintf( stderr, " -v, --version : Report the installed version of ZoneMinder\n" );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
exit( 0 );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main( int argc, char *argv[] )
|
|
|
|
|
{
|
2013-10-07 07:37:50 +08:00
|
|
|
|
self = argv[0];
|
|
|
|
|
|
|
|
|
|
srand( getpid() * time( 0 ) );
|
2009-04-15 04:18:57 +08:00
|
|
|
|
|
2003-04-22 22:17:04 +08:00
|
|
|
|
int id = -1;
|
|
|
|
|
|
|
|
|
|
static struct option long_options[] = {
|
|
|
|
|
{"monitor", 1, 0, 'm'},
|
|
|
|
|
{"help", 0, 0, 'h'},
|
2015-01-07 09:54:49 +08:00
|
|
|
|
{"version", 0, 0, 'v'},
|
2003-04-22 22:17:04 +08:00
|
|
|
|
{0, 0, 0, 0}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
int option_index = 0;
|
|
|
|
|
|
2015-01-07 09:54:49 +08:00
|
|
|
|
int c = getopt_long (argc, argv, "m:h:v", long_options, &option_index);
|
2003-04-22 22:17:04 +08:00
|
|
|
|
if (c == -1)
|
|
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
|
{
|
|
|
|
|
case 'm':
|
|
|
|
|
id = atoi(optarg);
|
|
|
|
|
break;
|
|
|
|
|
case 'h':
|
|
|
|
|
case '?':
|
|
|
|
|
Usage();
|
|
|
|
|
break;
|
2015-01-07 09:54:49 +08:00
|
|
|
|
case 'v':
|
2015-05-30 05:22:20 +08:00
|
|
|
|
std::cout << ZM_VERSION << "\n";
|
2015-01-07 09:54:49 +08:00
|
|
|
|
exit(0);
|
2003-04-22 22:17:04 +08:00
|
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
|
char log_id_string[16];
|
|
|
|
|
snprintf( log_id_string, sizeof(log_id_string), "m%d", id );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
|
2004-12-29 00:46:48 +08:00
|
|
|
|
zmLoadConfig();
|
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
|
logInit( "zmf" );
|
2011-09-08 00:07:23 +08:00
|
|
|
|
|
|
|
|
|
ssedetect();
|
2011-06-21 17:19:10 +08:00
|
|
|
|
|
2007-12-30 02:50:53 +08:00
|
|
|
|
Monitor *monitor = Monitor::Load( id, false, Monitor::QUERY );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
|
|
|
|
|
if ( !monitor )
|
|
|
|
|
{
|
|
|
|
|
fprintf( stderr, "Can't find monitor with id of %d\n", id );
|
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
|
|
|
|
|
2004-10-13 17:37:38 +08:00
|
|
|
|
char capt_path[PATH_MAX];
|
|
|
|
|
char anal_path[PATH_MAX];
|
2007-09-05 00:08:55 +08:00
|
|
|
|
snprintf( capt_path, sizeof(capt_path), "%s/%d/%%s/%%0%dd-capture.jpg", config.dir_events, monitor->Id(), config.event_image_digits );
|
|
|
|
|
snprintf( anal_path, sizeof(anal_path), "%s/%d/%%s/%%0%dd-analyse.jpg", config.dir_events, monitor->Id(), config.event_image_digits );
|
2006-04-05 20:22:27 +08:00
|
|
|
|
zmSetDefaultTermHandler();
|
|
|
|
|
zmSetDefaultDieHandler();
|
|
|
|
|
|
2003-04-22 22:17:04 +08:00
|
|
|
|
sigset_t block_set;
|
|
|
|
|
sigemptyset( &block_set );
|
|
|
|
|
|
|
|
|
|
int sd = OpenSocket( monitor->Id() );
|
|
|
|
|
|
2004-01-15 05:26:47 +08:00
|
|
|
|
FrameHeader frame_header = { 0, 0, false, 0 };
|
|
|
|
|
//unsigned char *image_data = 0;
|
2003-04-22 22:17:04 +08:00
|
|
|
|
|
|
|
|
|
fd_set rfds;
|
|
|
|
|
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
timeout.tv_sec = 1;
|
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
|
while( 1 )
|
|
|
|
|
{
|
|
|
|
|
struct timeval temp_timeout = timeout;
|
|
|
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
|
FD_SET(sd, &rfds);
|
|
|
|
|
int n_found = select( sd+1, &rfds, NULL, NULL, &temp_timeout );
|
|
|
|
|
if( n_found == 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Debug( 1, "Select timed out" );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else if ( n_found < 0)
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Select error: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
ReopenSocket( sd, monitor->Id() );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
continue;
|
2003-04-22 22:17:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sigprocmask( SIG_BLOCK, &block_set, 0 );
|
|
|
|
|
|
2004-03-17 18:27:49 +08:00
|
|
|
|
int n_bytes = read( sd, &frame_header, sizeof(frame_header) );
|
|
|
|
|
if ( n_bytes != sizeof(frame_header) )
|
2003-04-22 22:17:04 +08:00
|
|
|
|
{
|
2004-03-17 18:27:49 +08:00
|
|
|
|
if ( n_bytes < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't read frame header: %s", strerror(errno) );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
|
|
|
|
else if ( n_bytes > 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Incomplete read of frame header, %d bytes only", n_bytes );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Warning( "Socket closed at remote end" );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
2003-04-22 22:17:04 +08:00
|
|
|
|
ReopenSocket( sd, monitor->Id() );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
continue;
|
2003-04-22 22:17:04 +08:00
|
|
|
|
}
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Debug( 1, "Read frame header, expecting %ld bytes of image", frame_header.image_length );
|
2003-11-05 22:50:33 +08:00
|
|
|
|
static unsigned char image_data[ZM_MAX_IMAGE_SIZE];
|
2013-08-19 02:28:55 +08:00
|
|
|
|
|
|
|
|
|
// Read for pipe and loop until bytes expected have been read or an error occures
|
|
|
|
|
int bytes_read = 0;
|
|
|
|
|
do
|
2003-04-22 22:17:04 +08:00
|
|
|
|
{
|
2013-08-19 02:28:55 +08:00
|
|
|
|
n_bytes = read( sd, image_data+bytes_read, frame_header.image_length-bytes_read );
|
|
|
|
|
if (n_bytes < 0) break; // break on error
|
2013-09-15 09:13:09 +08:00
|
|
|
|
if (n_bytes < (int)frame_header.image_length)
|
2004-03-17 18:27:49 +08:00
|
|
|
|
{
|
2013-08-19 02:28:55 +08:00
|
|
|
|
// print some informational messages
|
|
|
|
|
if (bytes_read == 0)
|
|
|
|
|
{
|
2013-10-08 03:40:38 +08:00
|
|
|
|
Debug(4,"Image read : Short read %d bytes of %d expected bytes",n_bytes,frame_header.image_length);
|
2013-08-19 02:28:55 +08:00
|
|
|
|
}
|
2013-09-15 09:13:09 +08:00
|
|
|
|
else if (bytes_read+n_bytes == (int)frame_header.image_length)
|
2013-08-19 02:28:55 +08:00
|
|
|
|
{
|
2013-10-08 03:40:38 +08:00
|
|
|
|
Debug(5,"Image read : Read rest of short read: %d bytes read total of %d bytes",n_bytes,frame_header.image_length);
|
2013-08-19 02:28:55 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2013-10-08 03:40:38 +08:00
|
|
|
|
Debug(6,"Image read : continuing, read %d bytes (%d so far)", n_bytes, bytes_read+n_bytes);
|
2013-08-19 02:28:55 +08:00
|
|
|
|
}
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
2013-08-19 02:28:55 +08:00
|
|
|
|
bytes_read+= n_bytes;
|
|
|
|
|
} while (n_bytes>0 && (bytes_read < (ssize_t)frame_header.image_length) );
|
|
|
|
|
|
|
|
|
|
// Print errors if there was a problem
|
|
|
|
|
if ( n_bytes < 1 )
|
|
|
|
|
{
|
|
|
|
|
Error( "Only read %d bytes of %d\n", bytes_read, frame_header.image_length);
|
|
|
|
|
if ( n_bytes < 0 )
|
2004-03-17 18:27:49 +08:00
|
|
|
|
{
|
2013-08-19 02:28:55 +08:00
|
|
|
|
Error( "Can't read frame image data: %s", strerror(errno) );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Warning( "Socket closed at remote end" );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
}
|
2003-04-22 22:17:04 +08:00
|
|
|
|
ReopenSocket( sd, monitor->Id() );
|
2004-03-17 18:27:49 +08:00
|
|
|
|
continue;
|
2003-04-22 22:17:04 +08:00
|
|
|
|
}
|
2013-08-19 02:28:55 +08:00
|
|
|
|
|
2007-09-05 00:08:55 +08:00
|
|
|
|
static char subpath[PATH_MAX] = "";
|
2013-08-19 02:28:55 +08:00
|
|
|
|
if ( config.use_deep_storage )
|
|
|
|
|
{
|
|
|
|
|
struct tm *time = localtime( &frame_header.event_time );
|
|
|
|
|
snprintf( subpath, sizeof(subpath), "%02d/%02d/%02d/%02d/%02d/%02d", time->tm_year-100, time->tm_mon+1, time->tm_mday, time->tm_hour, time->tm_min, time->tm_sec );
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
snprintf( subpath, sizeof(subpath), "%ld", frame_header.event_id );
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-22 22:17:04 +08:00
|
|
|
|
static char path[PATH_MAX] = "";
|
2007-09-05 00:08:55 +08:00
|
|
|
|
snprintf( path, sizeof(path), frame_header.alarm_frame?anal_path:capt_path, subpath, frame_header.frame_id );
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Debug( 1, "Got image, writing to %s", path );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
|
|
|
|
|
FILE *fd = 0;
|
|
|
|
|
if ( (fd = fopen( path, "w" )) < 0 )
|
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't fopen '%s': %s", path, strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
2004-01-15 05:26:47 +08:00
|
|
|
|
if ( 0 == fwrite( image_data, frame_header.image_length, 1, fd ) )
|
2003-04-22 22:17:04 +08:00
|
|
|
|
{
|
2008-07-14 22:43:47 +08:00
|
|
|
|
Error( "Can't fwrite image data: %s", strerror(errno) );
|
2003-04-22 22:17:04 +08:00
|
|
|
|
exit( -1 );
|
|
|
|
|
}
|
|
|
|
|
fclose( fd );
|
|
|
|
|
|
|
|
|
|
sigprocmask( SIG_UNBLOCK, &block_set, 0 );
|
|
|
|
|
}
|
2014-11-15 05:17:44 +08:00
|
|
|
|
logTerm();
|
|
|
|
|
zmDbClose();
|
2003-04-22 22:17:04 +08:00
|
|
|
|
}
|