Google code style

This commit is contained in:
Isaac Connor 2018-04-25 10:48:50 -04:00
parent 117555a857
commit 27531750a4
1 changed files with 110 additions and 114 deletions

View File

@ -104,23 +104,23 @@ my @daemons = (
'zmtelemetry.pl'
);
if ($Config{ZM_OPT_USE_EVENTNOTIFICATION}) {
push @daemons,'zmeventnotification.pl';
if ( $Config{ZM_OPT_USE_EVENTNOTIFICATION} ) {
push @daemons,'zmeventnotification.pl';
}
my $command = shift @ARGV;
if( ! $command ) {
print( STDERR "No command given\n" );
if ( !$command ) {
print(STDERR "No command given\n");
pod2usage(-exitstatus => -1);
}
if ( $command eq 'version' ) {
print ZoneMinder::Base::ZM_VERSION."\n";
exit( 0 );
exit(0);
}
my $needs_daemon = $command !~ /(?:startup|shutdown|status|check|logrot|version)/;
my $daemon = shift( @ARGV );
my $daemon = shift @ARGV;
if ( $needs_daemon && ! $daemon ) {
print( STDERR "No daemon given\n" );
print(STDERR "No daemon given\n");
pod2usage(-exitstatus => -1);
}
my @args;
@ -130,7 +130,7 @@ if ( $needs_daemon ) {
if ( $daemon =~ /^${daemon_patt}$/ ) {
$daemon = $1;
} else {
print( STDERR "Invalid daemon '$daemon' specified" );
print(STDERR "Invalid daemon '$daemon' specified");
pod2usage(-exitstatus => -1);
}
}
@ -141,19 +141,19 @@ foreach my $arg ( @ARGV ) {
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ ) {
push( @args, $1 );
} else {
print( STDERR "Bogus argument '$arg' found" );
exit( -1 );
print(STDERR "Bogus argument '$arg' found");
exit(-1);
}
}
my $dbh = zmDbConnect();
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
my $saddr = sockaddr_un( SOCK_FILE );
my $server_up = connect( CLIENT, $saddr );
my $saddr = sockaddr_un(SOCK_FILE);
my $server_up = connect(CLIENT, $saddr);
if ( ! $server_up ) {
if ( !$server_up ) {
if ( $Config{ZM_SERVER_ID} ) {
use Sys::MemInfo qw(totalmem freemem totalswap freeswap);
use ZoneMinder::Server qw(CpuLoad);
@ -168,16 +168,16 @@ if ( ! $server_up ) {
exit();
}
if ( $command eq 'check' ) {
print( "stopped\n" );
print("stopped\n");
exit();
} elsif ( $command ne 'startup' ) {
print( "Unable to connect to server using socket at " . SOCK_FILE . "\n" );
print("Unable to connect to server using socket at " . SOCK_FILE . "\n");
exit( -1 );
}
# The server isn't there
print( "Starting server\n" );
close( CLIENT );
print("Starting server\n");
close(CLIENT);
if ( my $cpid = fork() ) {
# Parent process just sleep and fall through
@ -185,7 +185,7 @@ if ( ! $server_up ) {
# I'm still not sure why we need to re-init the logs
logInit();
socket( CLIENT, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
socket(CLIENT, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
my $attempts = 0;
while( !connect(CLIENT, $saddr) ) {
$attempts++;
@ -209,7 +209,6 @@ if ( ($command eq 'check') && !$daemon ) {
}
# The server is there, connect to it
#print( "Writing commands\n" );
CLIENT->autoflush();
my $message = join(';', $command, ( $daemon ? $daemon : () ), @args );
print(CLIENT $message);
@ -218,9 +217,7 @@ while( my $line = <CLIENT> ) {
chomp($line);
print("$line\n");
}
# And we're done!
close(CLIENT);
#print( "Finished writing, bye\n" );
exit;
@ -248,35 +245,35 @@ our %terminating_processes;
sub run {
my $fd = 0;
while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) ) {
POSIX::close( $fd++ );
while( $fd < POSIX::sysconf(&POSIX::_SC_OPEN_MAX) ) {
POSIX::close($fd++);
}
setpgrp();
logInit();
dPrint( ZoneMinder::Logger::INFO, 'Server starting at '
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
dPrint(ZoneMinder::Logger::INFO, 'Server starting at '
.strftime('%y/%m/%d %H:%M:%S', localtime())
."\n"
);
if ( open( my $PID, '>', ZM_PID ) ) {
print( $PID $$ );
close( $PID );
if ( open(my $PID, '>', ZM_PID) ) {
print($PID $$);
close($PID);
} else {
Error( "Can't open pid file at " . ZM_PID );
Error("Can't open pid file at " . ZM_PID);
}
# Tell any existing processes to die, wait 1 second between TERM and KILL
killAll( 1 );
killAll(1);
dPrint( ZoneMinder::Logger::INFO, 'Socket should be open at ' .main::SOCK_FILE );
dPrint(ZoneMinder::Logger::INFO, 'Socket should be open at ' .main::SOCK_FILE);
my $dbh = zmDbConnect(1);
socket( SERVER, PF_UNIX, SOCK_STREAM, 0 ) or Fatal( "Can't open socket: $!" );
unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if -e main::SOCK_FILE;
bind( SERVER, $saddr ) or Fatal( "Can't bind to " . main::SOCK_FILE . ": $!" );
listen( SERVER, SOMAXCONN ) or Fatal( "Can't listen: $!" );
socket(SERVER, PF_UNIX, SOCK_STREAM, 0) or Fatal("Can't open socket: $!");
unlink(main::SOCK_FILE) or Error('Unable to unlink ' . main::SOCK_FILE .". Error message was: $!") if -e main::SOCK_FILE;
bind(SERVER, $saddr) or Fatal("Can't bind to " . main::SOCK_FILE . ": $!");
listen(SERVER, SOMAXCONN) or Fatal("Can't listen: $!");
$SIG{CHLD} = \&reaper;
$SIG{INT} = \&shutdownAll;
@ -285,7 +282,7 @@ sub run {
$SIG{HUP} = \&logrot;
my $rin = '';
vec( $rin, fileno(SERVER), 1 ) = 1;
vec($rin, fileno(SERVER), 1) = 1;
my $win = $rin;
my $ein = $win;
my $timeout = 1;
@ -294,8 +291,8 @@ sub run {
if ( $Config{ZM_SERVER_ID} ) {
require ZoneMinder::Server;
$Server = new ZoneMinder::Server( $Config{ZM_SERVER_ID} );
dPrint( ZoneMinder::Logger::INFO, 'Loading Server record have ' . $$Server{Name} );
$Server = new ZoneMinder::Server($Config{ZM_SERVER_ID});
dPrint(ZoneMinder::Logger::INFO, 'Loading Server record have ' . $$Server{Name});
}
while( 1 ) {
@ -311,41 +308,41 @@ sub run {
}
$secs_count += 1;
}
my $nfound = select( my $rout = $rin, undef, undef, $timeout );
my $nfound = select(my $rout = $rin, undef, undef, $timeout);
if ( $nfound > 0 ) {
if ( vec( $rout, fileno(SERVER), 1 ) ) {
my $paddr = accept( CLIENT, SERVER );
if ( vec($rout, fileno(SERVER), 1) ) {
my $paddr = accept(CLIENT, SERVER);
my $message = <CLIENT>;
next if !$message;
my ( $command, $daemon, @args ) = split( /;/, $message );
my ( $command, $daemon, @args ) = split(';', $message);
if ( $command eq 'start' ) {
start( $daemon, @args );
start($daemon, @args);
} elsif ( $command eq 'stop' ) {
stop( $daemon, @args );
stop($daemon, @args);
} elsif ( $command eq 'restart' ) {
restart( $daemon, @args );
restart($daemon, @args);
} elsif ( $command eq 'reload' ) {
reload( $daemon, @args );
reload($daemon, @args);
} elsif ( $command eq 'startup' ) {
# Do nothing, this is all we're here for
dPrint( ZoneMinder::Logger::WARNING, "Already running, ignoring command '$command'\n" );
dPrint(ZoneMinder::Logger::WARNING, "Already running, ignoring command '$command'\n");
} elsif ( $command eq 'shutdown' ) {
shutdownAll();
} elsif ( $command eq 'check' ) {
check( $daemon, @args );
check($daemon, @args);
} elsif ( $command eq 'status' ) {
if ( $daemon ) {
status( $daemon, @args );
status($daemon, @args);
} else {
status();
}
} elsif ( $command eq 'logrot' ) {
logrot();
} else {
dPrint( ZoneMinder::Logger::ERROR, "Invalid command '$command'\n" );
dPrint(ZoneMinder::Logger::ERROR, "Invalid command '$command'\n");
}
close(CLIENT);
} else {
@ -370,18 +367,18 @@ sub run {
check_for_processes_to_kill();
} # end while
dPrint( ZoneMinder::Logger::INFO, 'Server exiting at '
dPrint(ZoneMinder::Logger::INFO, 'Server exiting at '
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
."\n"
);
if ( $Config{ZM_SERVER_ID} ) {
$dbh = zmDbConnect() if ! $dbh->ping();
if ( ! defined $dbh->do(q{UPDATE Servers SET Status='NotRunning' WHERE Id=?}, undef, $Config{ZM_SERVER_ID} ) ) {
if ( ! defined $dbh->do(q{UPDATE Servers SET Status='NotRunning' WHERE Id=?}, undef, $Config{ZM_SERVER_ID}) ) {
Error("Failed Updating status of Server record for Id=$Config{ZM_SERVER_ID}".$dbh->errstr());
}
}
unlink( main::SOCK_FILE ) or Error( 'Unable to unlink ' . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
unlink( ZM_PID ) or Error( 'Unable to unlink ' . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID );
unlink(main::SOCK_FILE) or Error('Unable to unlink ' . main::SOCK_FILE .". Error message was: $!") if ( -e main::SOCK_FILE );
unlink(ZM_PID) or Error('Unable to unlink ' . ZM_PID .". Error message was: $!") if ( -e ZM_PID );
exit();
}
@ -398,15 +395,15 @@ sub dPrint {
print CLIENT @_
}
if ( $logLevel == ZoneMinder::Logger::DEBUG ) {
Debug( @_ );
Debug(@_);
} elsif ( $logLevel == ZoneMinder::Logger::INFO ) {
Info( @_ );
Info(@_);
} elsif ( $logLevel == ZoneMinder::Logger::WARNING ) {
Warning( @_ );
Warning(@_);
} elsif ( $logLevel == ZoneMinder::Logger::ERROR ) {
Error( @_ );
Error(@_);
} elsif ( $logLevel == ZoneMinder::Logger::FATAL ) {
Fatal( @_ );
Fatal(@_);
}
}
@ -414,61 +411,60 @@ sub start {
my $daemon = shift;
my @args = @_;
my $command = join(' ', $daemon, @args );
my $command = join(' ', $daemon, @args);
my $process = $cmd_hash{$command};
if ( !$process ) {
# It's not running, or at least it's not been started by us
$process = { daemon=>$daemon, args=>\@args, command=>$command, keepalive=>!undef };
} elsif ( $process->{pid} && $pid_hash{$process->{pid}} ) {
dPrint( ZoneMinder::Logger::INFO, "'$process->{command}' already running at "
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
dPrint(ZoneMinder::Logger::INFO, "'$process->{command}' already running at "
.strftime('%y/%m/%d %H:%M:%S', localtime($process->{started}))
.", pid = $process->{pid}\n"
);
return();
return;
}
my $sigset = POSIX::SigSet->new;
my $blockset = POSIX::SigSet->new( SIGCHLD );
sigprocmask( SIG_BLOCK, $blockset, $sigset ) or Fatal( "Can't block SIGCHLD: $!" );
sigprocmask(SIG_BLOCK, $blockset, $sigset) or Fatal("Can't block SIGCHLD: $!");
if ( my $cpid = fork() ) {
logReinit();
$process->{pid} = $cpid;
$process->{started} = time();
delete( $process->{pending} );
delete $process->{pending};
dPrint( ZoneMinder::Logger::INFO, "'$command' starting at "
.strftime( '%y/%m/%d %H:%M:%S', localtime( $process->{started}) )
dPrint(ZoneMinder::Logger::INFO, "'$command' starting at "
.strftime('%y/%m/%d %H:%M:%S', localtime($process->{started}))
.", pid = $process->{pid}\n"
);
$cmd_hash{$process->{command}} = $pid_hash{$cpid} = $process;
sigprocmask( SIG_SETMASK, $sigset ) or Fatal( "Can't restore SIGCHLD: $!" );
} elsif ( defined($cpid ) ) {
sigprocmask(SIG_SETMASK, $sigset) or Fatal("Can't restore SIGCHLD: $!");
} elsif ( defined($cpid) ) {
# Force reconnection to the db.
$dbh = zmDbConnect(1);
logReinit();
dPrint( ZoneMinder::Logger::INFO, "'".join( ' ', ( $daemon, @args ) )
."' started at "
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
dPrint(ZoneMinder::Logger::INFO, "'$command' started at "
.strftime('%y/%m/%d %H:%M:%S', localtime())
."\n"
);
if ( $daemon =~ /^${daemon_patt}$/ ) {
$daemon = $Config{ZM_PATH_BIN}.'/'.$1;
} else {
Fatal( "Invalid daemon '$daemon' specified" );
Fatal("Invalid daemon '$daemon' specified");
}
my @good_args;
foreach my $arg ( @args ) {
# Detaint arguments, if they look ok
if ( $arg =~ /^(-{0,2}[\w\/?&=.-]+)$/ ) {
push( @good_args, $1 );
push @good_args, $1;
} else {
Fatal( "Bogus argument '$arg' found" );
Fatal("Bogus argument '$arg' found");
}
}
@ -476,8 +472,8 @@ sub start {
zmDbDisconnect();
my $fd = 0;
while( $fd < POSIX::sysconf( &POSIX::_SC_OPEN_MAX ) ) {
POSIX::close( $fd++ );
while( $fd < POSIX::sysconf(&POSIX::_SC_OPEN_MAX) ) {
POSIX::close($fd++);
}
# Child process
@ -486,11 +482,11 @@ sub start {
$SIG{TERM} = 'DEFAULT';
$SIG{ABRT} = 'DEFAULT';
exec( $daemon, @good_args ) or Fatal( "Can't exec: $!" );
exec($daemon, @good_args) or Fatal("Can't exec: $!");
} else {
Fatal( "Can't fork: $!" );
Fatal("Can't fork: $!");
}
}
} # end sub start
# Sends the stop signal, without waiting around to see if the process died.
sub send_stop {
@ -499,9 +495,9 @@ sub send_stop {
my $command = $process->{command};
if ( $process->{pending} ) {
delete( $cmd_hash{$command} );
dPrint( ZoneMinder::Logger::INFO, "Command '$command' removed from pending list at "
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
delete $cmd_hash{$command};
dPrint(ZoneMinder::Logger::INFO, "Command '$command' removed from pending list at "
.strftime('%y/%m/%d %H:%M:%S', localtime())
."\n"
);
return();
@ -509,12 +505,12 @@ sub send_stop {
my $pid = $process->{pid};
if ( !$pid_hash{$pid} ) {
dPrint( ZoneMinder::Logger::ERROR, "No process with command of '$command' pid $pid is running\n" );
dPrint(ZoneMinder::Logger::ERROR, "No process with command of '$command' pid $pid is running\n");
return();
}
dPrint( ZoneMinder::Logger::INFO, "'$command' sending stop to pid $pid at "
.strftime( '%y/%m/%d %H:%M:%S', localtime() )
dPrint(ZoneMinder::Logger::INFO, "'$command' sending stop to pid $pid at "
.strftime('%y/%m/%d %H:%M:%S', localtime())
."\n"
);
$process->{keepalive} = !$final;
@ -522,8 +518,7 @@ sub send_stop {
$process->{pending} = 0;
$terminating_processes{$command} = $process;
kill( 'TERM', $pid );
kill('TERM', $pid);
return $pid;
} # end sub send_stop
@ -531,9 +526,10 @@ sub check_for_processes_to_kill {
# Turn off SIGCHLD
my $sigset = POSIX::SigSet->new;
my $blockset = POSIX::SigSet->new(SIGCHLD);
sigprocmask(SIG_BLOCK, $blockset, $sigset ) or die "dying at block...\n";
sigprocmask(SIG_BLOCK, $blockset, $sigset) or die "dying at block...\n";
foreach my $command ( %terminating_processes ) {
my $process = $cmd_hash{$command};
Debug("Have process $command at pid $$process{pid} $$process{term_sent_at}");
if ( $$process{term_sent_at} - time > KILL_DELAY ) {
dPrint(ZoneMinder::Logger::WARNING, "'$$process{command}' has not stopped at "
.strftime('%y/%m/%d %H:%M:%S', localtime())
@ -551,11 +547,11 @@ sub stop {
my $command = join(' ', $daemon, @args );
my $process = $cmd_hash{$command};
if ( !$process ) {
dPrint( ZoneMinder::Logger::WARNING, "Can't find process with command of '$command'\n" );
dPrint(ZoneMinder::Logger::WARNING, "Can't find process with command of '$command'\n");
return;
}
send_stop( 1, $process );
send_stop(1, $process);
}
# restart is the same as stop, except that we flag the processes for restarting once it dies
@ -563,7 +559,7 @@ sub stop {
sub restart {
my ( $daemon, @args ) = @_;
my $command = join(' ', $daemon, @args );
my $command = join(' ', $daemon, @args);
dPrint(ZoneMinder::Logger::DEBUG, "Restarting $command\n");
my $process = $cmd_hash{$command};
if ( !$process ) {
@ -580,7 +576,7 @@ sub reload {
my $daemon = shift;
my @args = @_;
my $command = join(' ', $daemon, @args ) ;
my $command = join(' ', $daemon, @args);
my $process = $cmd_hash{$command};
if ( $process ) {
if ( $process->{pid} ) {
@ -591,7 +587,7 @@ sub reload {
sub logrot {
logReinit();
foreach my $process ( values(%pid_hash) ) {
foreach my $process ( values %pid_hash ) {
if ( $process->{pid} ) {
# && $process->{command} =~ /^zm.*\.pl/ ) {
kill('HUP', $process->{pid});
@ -601,14 +597,14 @@ sub logrot {
sub reaper {
my $saved_status = $!;
while ( (my $cpid = waitpid( -1, WNOHANG )) > 0 ) {
while ( (my $cpid = waitpid(-1, WNOHANG)) > 0 ) {
my $status = $?;
my $process = $pid_hash{$cpid};
delete $pid_hash{$cpid};
if ( !$process ) {
dPrint( ZoneMinder::Logger::INFO, "Can't find child with pid of '$cpid'\n" );
dPrint(ZoneMinder::Logger::INFO, "Can't find child with pid of '$cpid'\n");
next;
}
delete $terminating_processes{$$process{command}};
@ -643,9 +639,9 @@ sub reaper {
$out_str .= "\n";
if ( $exit_status == 0 ) {
Info( $out_str );
Info($out_str);
} else {
Error( $out_str );
Error($out_str);
}
if ( $process->{keepalive} ) {
@ -685,7 +681,7 @@ sub shutdownAll {
foreach my $pid ( keys %pid_hash ) {
# This is a quick fix because a SIGCHLD can happen and alter pid_hash while we are in here.
next if ! $pid_hash{$pid};
send_stop( 1, $pid_hash{$pid} );
send_stop(1, $pid_hash{$pid});
}
while ( %terminating_processes ) {
check_for_processes_to_kill();
@ -695,10 +691,10 @@ sub shutdownAll {
.strftime('%y/%m/%d %H:%M:%S', localtime())
."\n"
);
unlink( main::SOCK_FILE ) or Error( "Unable to unlink " . main::SOCK_FILE .". Error message was: $!" ) if ( -e main::SOCK_FILE );
unlink( ZM_PID ) or Error( "Unable to unlink " . ZM_PID .". Error message was: $!" ) if ( -e ZM_PID );
close( CLIENT );
close( SERVER );
unlink(main::SOCK_FILE) or Error("Unable to unlink " . main::SOCK_FILE .". Error message was: $!") if ( -e main::SOCK_FILE );
unlink(ZM_PID) or Error("Unable to unlink " . ZM_PID .". Error message was: $!") if ( -e ZM_PID );
close(CLIENT);
close(SERVER);
exit();
}
@ -706,18 +702,18 @@ sub check {
my $daemon = shift;
my @args = @_;
my $command = join( ' ', $daemon, @args );
my $command = join(' ', $daemon, @args);
my $process = $cmd_hash{$command};
if ( !$process ) {
cPrint( "unknown\n" );
cPrint("unknown\n");
} elsif ( $process->{pending} ) {
cPrint( "pending\n" );
cPrint("pending\n");
} else {
my $cpid = $process->{pid};
if ( ! $pid_hash{$cpid} ) {
cPrint( "stopped\n" );
cPrint("stopped\n");
} else {
cPrint( "running\n" );
cPrint("running\n");
}
}
}
@ -785,14 +781,14 @@ sub killAll {
}
foreach my $daemon ( @daemons ) {
my $cmd = $killall ."TERM $daemon";
Debug( $cmd );
qx( $cmd );
Debug($cmd);
qx($cmd);
}
sleep( $delay );
sleep($delay);
foreach my $daemon ( @daemons ) {
my $cmd = $killall."KILL $daemon";
Debug( $cmd );
qx( $cmd );
Debug($cmd);
qx($cmd);
}
}
1;