From 61445620aaf319db7d2697cd93050d5bf91af048 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Fri, 8 Jun 2018 07:53:23 -0500 Subject: [PATCH 1/2] create the pid file before doing anything else (#2114) * create the pid file before doing anything else * update comment --- scripts/zmdc.pl.in | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index b65388892..d8565cfe9 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -245,6 +245,16 @@ our %terminating_processes; our $zm_terminate = 0; sub run { + + # Call this first otherwise stdout/stderror redirects to the pidfile = bad + if ( open(my $PID, '>', ZM_PID) ) { + print($PID $$); + close($PID); + } else { + # Log not initialized at this point so use die instead + die "Can't open pid file at ".ZM_PID."\n"; + } + my $fd = 0; while( $fd < POSIX::sysconf(&POSIX::_SC_OPEN_MAX) ) { POSIX::close($fd++); @@ -259,13 +269,6 @@ sub run { ."\n" ); - if ( open(my $PID, '>', ZM_PID) ) { - print($PID $$); - close($PID); - } else { - Error("Can't open pid file at " . ZM_PID); - } - # Tell any existing processes to die, wait 1 second between TERM and KILL killAll(1); From 7c32e4d86ccb417d93493ad89032aa342d399a2b Mon Sep 17 00:00:00 2001 From: Isaac Connor Date: Fri, 8 Jun 2018 09:15:19 -0400 Subject: [PATCH 2/2] Move zmDbConnect up before logInit because the db handle got closed --- scripts/zmdc.pl.in | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/zmdc.pl.in b/scripts/zmdc.pl.in index d8565cfe9..aa4cbc488 100644 --- a/scripts/zmdc.pl.in +++ b/scripts/zmdc.pl.in @@ -262,6 +262,8 @@ sub run { setpgrp(); + # dbh got closed with the rest of the fd's above, so need to reconnect. + my $dbh = zmDbConnect(1); logInit(); dPrint(ZoneMinder::Logger::INFO, 'Server starting at ' @@ -273,7 +275,6 @@ sub run { killAll(1); 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 . ": $!");