2015-10-08 17:43:56 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
# COMMON EXIT MODULE
|
|
|
|
####################################################################################################################################
|
2016-04-14 15:30:54 +02:00
|
|
|
package pgBackRest::Common::Exit;
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings FATAL => qw(all);
|
|
|
|
use Carp qw(confess);
|
2016-09-06 15:44:50 +02:00
|
|
|
use English '-no_match_vars';
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
|
2016-04-14 15:30:54 +02:00
|
|
|
use pgBackRest::Common::Exception;
|
|
|
|
use pgBackRest::Common::Lock;
|
|
|
|
use pgBackRest::Common::Log;
|
|
|
|
use pgBackRest::Config::Config;
|
2016-08-24 18:39:27 +02:00
|
|
|
use pgBackRest::Protocol::Protocol;
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Signal constants
|
|
|
|
####################################################################################################################################
|
|
|
|
use constant SIGNAL_HUP => 'HUP';
|
|
|
|
use constant SIGNAL_INT => 'INT';
|
|
|
|
use constant SIGNAL_TERM => 'TERM';
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# Hook important signals into exitSafe function
|
|
|
|
####################################################################################################################################
|
2016-09-06 15:44:50 +02:00
|
|
|
$SIG{&SIGNAL_HUP} = sub {exitSafe(ERROR_TERM, undef, SIGNAL_HUP)};
|
|
|
|
$SIG{&SIGNAL_INT} = sub {exitSafe(ERROR_TERM, undef, SIGNAL_INT)};
|
|
|
|
$SIG{&SIGNAL_TERM} = sub {exitSafe(ERROR_TERM, undef, SIGNAL_TERM)};
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# exitSafe
|
|
|
|
#
|
2016-09-06 15:35:02 +02:00
|
|
|
# Terminate all remotes and release locks.
|
2015-10-08 17:43:56 +02:00
|
|
|
####################################################################################################################################
|
|
|
|
sub exitSafe
|
|
|
|
{
|
|
|
|
# Assign function parameters, defaults, and log debug info
|
|
|
|
my
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
$iExitCode,
|
2016-09-06 15:44:50 +02:00
|
|
|
$oException,
|
|
|
|
$strSignal,
|
2015-10-08 17:43:56 +02:00
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '::exitSafe', \@_,
|
2016-09-06 15:44:50 +02:00
|
|
|
{name => 'iExitCode', required => false},
|
|
|
|
{name => 'oException', required => false},
|
|
|
|
{name => 'strSignal', required => false},
|
2015-10-08 17:43:56 +02:00
|
|
|
);
|
|
|
|
|
2016-12-04 01:01:17 +02:00
|
|
|
# Reset logging in case it was disabled when the exception/signal occurred
|
|
|
|
configLogging();
|
|
|
|
|
2016-06-23 00:01:18 +02:00
|
|
|
# Close the remote
|
2016-09-14 23:37:07 +02:00
|
|
|
protocolDestroy(undef, undef, defined($iExitCode) && ($iExitCode == 0 || $iExitCode == 1));
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
# Don't fail if the lock can't be released
|
|
|
|
eval
|
|
|
|
{
|
|
|
|
lockRelease(false);
|
2016-09-06 15:44:50 +02:00
|
|
|
}
|
2017-01-10 03:49:04 +02:00
|
|
|
# uncoverable branch false - this eval exists only to suppress lock errors so original error will not be lost
|
2016-09-06 15:44:50 +02:00
|
|
|
or do {};
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-09-06 15:44:50 +02:00
|
|
|
# If exit code is not defined then try to get it from the exception
|
|
|
|
if (!defined($iExitCode))
|
2015-10-08 17:43:56 +02:00
|
|
|
{
|
2016-09-06 15:44:50 +02:00
|
|
|
# If a backrest exception
|
|
|
|
if (isException($oException))
|
|
|
|
{
|
|
|
|
$iExitCode = $oException->code();
|
2016-12-04 01:01:17 +02:00
|
|
|
logException($oException);
|
2016-09-06 15:44:50 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$iExitCode = ERROR_UNHANDLED;
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-09-06 15:44:50 +02:00
|
|
|
&log(
|
|
|
|
ERROR,
|
|
|
|
'process terminated due to an unhandled exception' .
|
|
|
|
(defined($oException) ? ":\n${oException}" : ': [exception not defined]'),
|
|
|
|
$iExitCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
elsif ($iExitCode == ERROR_TERM)
|
|
|
|
{
|
2016-12-05 00:51:00 +02:00
|
|
|
&log(ERROR, "terminated on signal [SIG${strSignal}]", ERROR_TERM);
|
2016-09-06 15:44:50 +02:00
|
|
|
}
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-12-04 21:44:53 +02:00
|
|
|
# Log command end
|
|
|
|
commandEnd($iExitCode, $strSignal);
|
|
|
|
|
2016-09-06 15:44:50 +02:00
|
|
|
# Log return values if any
|
|
|
|
logDebugReturn
|
|
|
|
(
|
|
|
|
$strOperation,
|
|
|
|
{name => 'iExitCode', value => $iExitCode}
|
|
|
|
);
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-09-06 15:44:50 +02:00
|
|
|
exit $iExitCode;
|
2015-10-08 17:43:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(exitSafe);
|
|
|
|
|
|
|
|
1;
|