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);
|
|
|
|
|
|
|
|
use Exporter qw(import);
|
|
|
|
our @EXPORT = qw();
|
|
|
|
use File::Basename qw(dirname);
|
|
|
|
use Scalar::Util qw(blessed);
|
|
|
|
|
|
|
|
use lib dirname($0) . '/../lib';
|
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
|
|
|
|
####################################################################################################################################
|
|
|
|
$SIG{&SIGNAL_HUP} = sub {exitSafe(-1, SIGNAL_HUP)};
|
|
|
|
$SIG{&SIGNAL_INT} = sub {exitSafe(-1, SIGNAL_INT)};
|
|
|
|
$SIG{&SIGNAL_TERM} = sub {exitSafe(-1, SIGNAL_TERM)};
|
|
|
|
|
|
|
|
####################################################################################################################################
|
|
|
|
# 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,
|
|
|
|
$strSignal
|
|
|
|
) =
|
|
|
|
logDebugParam
|
|
|
|
(
|
2016-08-11 23:32:28 +02:00
|
|
|
__PACKAGE__ . '::exitSafe', \@_,
|
2015-10-08 17:43:56 +02:00
|
|
|
{name => 'iExitCode'},
|
|
|
|
{name => 'strSignal', required => false}
|
|
|
|
);
|
|
|
|
|
|
|
|
commandStop();
|
|
|
|
|
2016-06-23 00:01:18 +02:00
|
|
|
# Close the remote
|
|
|
|
protocolDestroy();
|
2015-10-08 17:43:56 +02:00
|
|
|
|
|
|
|
# Don't fail if the lock can't be released
|
|
|
|
eval
|
|
|
|
{
|
|
|
|
lockRelease(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
# Exit with code when defined
|
|
|
|
if ($iExitCode != -1)
|
|
|
|
{
|
|
|
|
exit $iExitCode;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Log error based on where the signal came from
|
2016-09-06 15:35:02 +02:00
|
|
|
&log(
|
|
|
|
ERROR,
|
|
|
|
'process terminated ' .
|
|
|
|
(defined($strSignal) ? "on a ${strSignal} signal" : 'due to an unhandled exception'),
|
|
|
|
defined($strSignal) ? ERROR_TERM : ERROR_UNHANDLED_EXCEPTION);
|
2015-10-08 17:43:56 +02:00
|
|
|
|
2016-09-04 15:25:20 +02:00
|
|
|
# If terminated by a signal exit with ERROR_TERM
|
2015-10-08 17:43:56 +02:00
|
|
|
exit ERROR_TERM if defined($strSignal);
|
|
|
|
|
|
|
|
# Return from function and log return values if any
|
|
|
|
return logDebugReturn($strOperation);
|
|
|
|
}
|
|
|
|
|
|
|
|
push @EXPORT, qw(exitSafe);
|
|
|
|
|
|
|
|
1;
|