diff --git a/scripts/zmpkg.pl.in b/scripts/zmpkg.pl.in index 46ea825c4..de5a29d02 100644 --- a/scripts/zmpkg.pl.in +++ b/scripts/zmpkg.pl.in @@ -105,6 +105,8 @@ if ( !$command || $command !~ /^(?:start|stop|restart|status|logrot|version)$/ ) } } $dbh = zmDbConnect() if ! $dbh; + # PP - Sane state check +isActiveSanityCheck(); # Move to the right place chdir( $Config{ZM_PATH_WEB} ) @@ -151,19 +153,17 @@ if ( $command eq "state" ) } } $sth->finish(); - #PP - lets go ahead and modify States DB - - Debug ("Marking $store_state as Enabled"); - # PP - Zero out other states being active - resetStates(); # PP - Now mark a specific state as active + resetStates(); + Info ("Marking $store_state as Enabled"); $sql = "update States set IsActive = '1' where Name = ?"; $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); $res = $sth->execute( $store_state ) or Fatal( "Can't execute: ".$sth->errstr() ); + # PP - zero out other states isActive $command = "restart"; } @@ -173,8 +173,6 @@ if ( $command =~ /^(start|stop|restart)$/ ) # We have to detaint to keep perl from complaining $command = $1; - # PP - if we are not switching to a custom state, zero out all isActive - resetStates() if (!$store_state); if ( systemdRunning() && !calledBysystem() ) { qx(@BINDIR@/zmsystemctl.pl $command); @@ -201,7 +199,7 @@ if ( $command =~ /^(?:stop|restart)$/ ) if ( $command =~ /^(?:start|restart)$/ ) { - my $status = runCommand( "zmdc.pl check" ); + my $status = runCommand( "zmdc.pl check" ); if ( $status eq "stopped" ) { @@ -307,8 +305,61 @@ if ( $command eq "logrot" ) exit( $retval ); -# PP - when the system is restarted/started/stopped, it will -# not be in a custom state, so lets keep the DB consistent +# PP - Make sure isActive is on and only one +sub isActiveSanityCheck +{ + + Info ("Sanity checking States table..."); + $dbh = zmDbConnect() if ! $dbh; + + # PP - First, make sure default exists and there is only one + my $sql = "select Name from States where Name = 'default'"; + my $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + my $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + + if ($sth->rows != 1) # PP - no row, or too many rows. Either case is an error + { + Info( "Fixing States table - either no default state or duplicate default states" ); + $sql = "delete from States where Name = 'default'"; + $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + + $sql = "insert into States (Name,Definition,IsActive) VALUES ('default','','1');"; + $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + + } + + + # PP - Now make sure no two states have IsActive=1 + $sql = "select Name from States where IsActive = '1'"; + $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + + if ( $sth->rows != 1 ) + { + Info( "Fixing States table so only one run state is active" ); + resetStates(); + $sql = "update States set IsActive='1' WHERE Name='default'"; + $sth = $dbh->prepare_cached( $sql ) + or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); + $res = $sth->execute() + or Fatal( "Can't execute: ".$sth->errstr() ); + + + } +} + + +# PP - zeroes out isActive for all states sub resetStates { $dbh = zmDbConnect() if ! $dbh;