2015-04-22 16:39:53 -04:00
|
|
|
####################################################################################################################################
|
2015-08-29 14:20:46 -04:00
|
|
|
# COMMON LOCK MODULE
|
2015-04-22 16:39:53 -04:00
|
|
|
####################################################################################################################################
|
2016-04-14 09:30:54 -04:00
|
|
|
package pgBackRest::Common::Lock;
|
2015-04-22 16:39:53 -04:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
|
|
|
|
2015-06-13 18:25:49 -04:00
|
|
|
use Exporter qw(import);
|
2015-08-29 14:20:46 -04:00
|
|
|
our @EXPORT = qw();
|
2015-04-22 16:39:53 -04:00
|
|
|
use Fcntl qw(:DEFAULT :flock);
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
|
2016-04-14 09:30:54 -04:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Log;
|
2016-05-06 09:37:01 -04:00
|
|
|
use pgBackRest::Common::String;
|
2018-01-17 15:03:55 -05:00
|
|
|
use pgBackRest::Common::Wait;
|
2016-04-14 09:30:54 -04:00
|
|
|
use pgBackRest::Config::Config;
|
2017-06-09 17:51:41 -04:00
|
|
|
use pgBackRest::Storage::Helper;
|
2015-04-22 16:39:53 -04:00
|
|
|
|
2015-10-08 11:43:56 -04:00
|
|
|
####################################################################################################################################
|
|
|
|
# lockStopFileName
|
|
|
|
#
|
|
|
|
# Get the stop file name.
|
|
|
|
####################################################################################################################################
|
|
|
|
sub lockStopFileName
|
|
|
|
{
|
|
|
|
my $strStanza = shift;
|
|
|
|
|
2017-08-25 16:47:47 -04:00
|
|
|
return cfgOption(CFGOPT_LOCK_PATH) . (defined($strStanza) ? "/${strStanza}" : '/all') . '.stop';
|
2015-10-08 11:43:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# lockStopTest
|
|
|
|
#
|
2015-12-23 11:04:26 -05:00
|
|
|
# Test if a stop file exists for the current stanza or all stanzas.
|
2015-10-08 11:43:56 -04:00
|
|
|
####################################################################################################################################
|
|
|
|
sub lockStopTest
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
2018-01-03 12:23:33 -05:00
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$bStanzaStopRequired,
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
|
|
|
__PACKAGE__ . '::lockStopTest', \@_,
|
|
|
|
{name => 'bStanzaStopRequired', optional => true, default => false}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $bStopExists = false;
|
2015-10-08 11:43:56 -04:00
|
|
|
|
|
|
|
# Check the stanza first if it is specified
|
2017-08-25 16:47:47 -04:00
|
|
|
if (cfgOptionTest(CFGOPT_STANZA))
|
2015-10-08 11:43:56 -04:00
|
|
|
{
|
|
|
|
# Generate the stop file name
|
2017-08-25 16:47:47 -04:00
|
|
|
my $strStopFile = lockStopFileName(cfgOption(CFGOPT_STANZA));
|
2015-10-08 11:43:56 -04:00
|
|
|
|
|
|
|
if (-e $strStopFile)
|
|
|
|
{
|
2018-01-03 12:23:33 -05:00
|
|
|
# If the stop file exists and is required then set the flag to true
|
|
|
|
if ($bStanzaStopRequired)
|
|
|
|
{
|
|
|
|
$bStopExists = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'stop file exists for stanza ' . cfgOption(CFGOPT_STANZA), ERROR_STOP);
|
|
|
|
}
|
2015-10-08 11:43:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-03 12:23:33 -05:00
|
|
|
# If not looking for a specific stanza stop file, then check all stanzas
|
|
|
|
if (!$bStanzaStopRequired)
|
2015-10-08 11:43:56 -04:00
|
|
|
{
|
2018-01-03 12:23:33 -05:00
|
|
|
# Now check all stanzas
|
|
|
|
my $strStopFile = lockStopFileName();
|
|
|
|
|
|
|
|
if (-e $strStopFile)
|
|
|
|
{
|
|
|
|
confess &log(ERROR, 'stop file exists for all stanzas', ERROR_STOP);
|
|
|
|
}
|
2015-10-08 11:43:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
2018-01-03 12:23:33 -05:00
|
|
|
return logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'bStopExists', value => $bStopExists}
|
|
|
|
);
|
2015-10-08 11:43:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(lockStopTest);
|
|
|
|
|
2015-04-22 16:39:53 -04:00
|
|
|
1;
|