From 88692cf04f479e762457322fac44f3581cf1fd6c Mon Sep 17 00:00:00 2001 From: Isaac Connor <isaac@zoneminder.com> Date: Wed, 16 Oct 2024 18:45:10 -0400 Subject: [PATCH] COde style, cleanups, remove STATE_TAPE --- scripts/zmx10.pl.in | 56 +++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/scripts/zmx10.pl.in b/scripts/zmx10.pl.in index bad8e3656..1b42f9759 100644 --- a/scripts/zmx10.pl.in +++ b/scripts/zmx10.pl.in @@ -75,9 +75,6 @@ $ENV{PATH} = '/bin:/usr/bin:/usr/local/bin'; $ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL}; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; -logInit(); -logSetSignal(); - my $command; my $unit_code; my $version; @@ -102,6 +99,16 @@ if ( $command eq 'start' ) { exit(); } +logInit(); +logSetSignal(); +my $zm_terminate = 0; +sub TermHandler { + Info('Received TERM, exiting'); + $zm_terminate = 1; +} +$SIG{TERM} = \&TermHandler; +$SIG{INT} = \&TermHandler; + socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!"); @@ -116,10 +123,8 @@ if ( !connect(CLIENT, $saddr) ) { # Parent process just sleep and fall through sleep(2); logReinit(); - socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) - or Fatal("Can't open socket: $!"); - connect(CLIENT, $saddr) - or Fatal("Can't connect: $!"); + socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!"); + connect(CLIENT, $saddr) or Fatal("Can't connect: $!"); } elsif ( defined($cpid) ) { setpgrp(); @@ -130,7 +135,6 @@ if ( !connect(CLIENT, $saddr) ) { } } # The server is there, connect to it -#print( "Writing commands\n" ); CLIENT->autoflush(); my $message = $command; $message .= ';'.$unit_code if $unit_code; @@ -141,7 +145,6 @@ while ( my $line = <CLIENT> ) { print("$line\n"); } close(CLIENT); -#print( "Finished writing, bye\n" ); exit; # @@ -173,8 +176,7 @@ our %pending_tasks; sub runServer { Info('X10 server starting'); - socket(SERVER, PF_UNIX, SOCK_STREAM, 0) - or Fatal("Can't open socket: $!"); + socket(SERVER, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!"); unlink(main::SOCK_FILE); my $saddr = sockaddr_un(main::SOCK_FILE); bind(SERVER, $saddr) or Fatal("Can't bind: $!"); @@ -185,7 +187,7 @@ sub runServer { $x10 = new X10::ActiveHome( port=>$Config{ZM_X10_DEVICE}, house_code=>$Config{ZM_X10_HOUSE_CODE}, - debug=>0 + debug=>1 ); loadTasks(); @@ -193,20 +195,22 @@ sub runServer { $x10->register_listener(\&x10listen); my $rin = ''; - vec($rin, fileno(SERVER),1) = 1; - vec($rin, $x10->select_fds(),1) = 1; - my $timeout = 0.2; - #print( 'F:'.fileno(SERVER)."\n" ); + vec($rin, fileno(SERVER), 1) = 1; + vec($rin, $x10->select_fds(), 1) = 1; + my $timeout = 1.0; # WHy .2? Why not 1s? + Debug(2, 'F:'.fileno(SERVER)."\n" ); my $reload = undef; my $reload_count = 0; my $reload_limit = $Config{ZM_X10_DB_RELOAD_INTERVAL} / $timeout; - while( 1 ) { + while ( !$zm_terminate ) { + Debug("Selecting for $timeout"); my $nfound = select(my $rout = $rin, undef, undef, $timeout); #print( "Off select, NF:$nfound, ER:$!\n" ); #print( vec( $rout, fileno(SERVER),1)."\n" ); #print( vec( $rout, $x10->select_fds(),1)."\n" ); if ( $nfound > 0 ) { - if ( vec($rout, fileno(SERVER),1) ) { + Debug("Nfound $nfound"); + if ( vec($rout, fileno(SERVER), 1) ) { my $paddr = accept(CLIENT, SERVER); my $message = <CLIENT>; @@ -267,7 +271,7 @@ sub runServer { } } # end if defined result close(CLIENT); - } elsif ( vec($rout, $x10->select_fds(),1) ) { + } elsif ( vec($rout, $x10->select_fds(), 1) ) { $x10->handle_input(); } else { Fatal('Bogus descriptor'); @@ -275,9 +279,11 @@ sub runServer { } elsif ( $nfound < 0 ) { if ( $! != EINTR ) { Fatal("Can't select: $!"); + } else { + Debug("Nfound $nfound $!"); } } else { - #print( "Select timed out\n" ); + Debug("Select timed out"); # Check for state changes foreach my $monitor_id ( sort(keys(%monitor_hash) ) ) { my $monitor = $monitor_hash{$monitor_id}; @@ -289,15 +295,13 @@ sub runServer { if ( defined( $monitor->{LastState} ) ) { my $task_list; if ( ($state == STATE_ALARM || $state == STATE_ALERT) - && ($monitor->{LastState} == STATE_IDLE || $monitor->{LastState} == STATE_TAPE) + && ($monitor->{LastState} == STATE_IDLE ) ) # Gone into alarm state { Debug("Applying ON_list for $monitor_id"); $task_list = $monitor->{ON_list}; - } elsif ( ($state == STATE_IDLE && $monitor->{LastState} != STATE_IDLE) - || ($state == STATE_TAPE && $monitor->{LastState} != STATE_TAPE) - ) # Come out of alarm state - { + } elsif ($state == STATE_IDLE && $monitor->{LastState} != STATE_IDLE) { + # Come out of alarm state Debug("Applying OFF_list for $monitor_id"); $task_list = $monitor->{OFF_list}; } @@ -656,6 +660,8 @@ sub x10listen { processTask($task); } } + } else { + Debug("Not for out house code ".$event->house_code().' eq '.$Config{ZM_X10_HOUSE_CODE}); } # end if correct house code Info('Got event - '.$event->as_string()); }