dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / Dpkg / Exit.pm
CommitLineData
1479465f
GJ
1# Copyright © 2002 Adam Heath <doogie@debian.org>
2# Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
3#
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17package Dpkg::Exit;
18
19use strict;
20use warnings;
21
22our $VERSION = '1.01';
23our @EXPORT_OK = qw(
24 push_exit_handler
25 pop_exit_handler
26 run_exit_handlers
27);
28
29use Exporter qw(import);
30
31# XXX: Backwards compatibility, stop exporting on VERSION 2.00.
32## no critic (Variables::ProhibitPackageVars)
33our @handlers = ();
34## use critic
35
36=encoding utf8
37
38=head1 NAME
39
40Dpkg::Exit - program exit handlers
41
42=head1 DESCRIPTION
43
44The Dpkg::Exit module provides support functions to run handlers on exit.
45
46=head1 FUNCTIONS
47
48=over 4
49
50=item push_exit_handler($func)
51
52Register a code reference into the exit function handlers stack.
53
54=cut
55
56sub push_exit_handler {
57 my ($func) = shift;
58 push @handlers, $func;
59}
60
61=item pop_exit_handler()
62
63Pop the last registered exit handler from the handlers stack.
64
65=cut
66
67sub pop_exit_handler {
68 pop @handlers;
69}
70
71=item run_exit_handlers()
72
73Run the registered exit handlers.
74
75=cut
76
77sub run_exit_handlers {
78 &$_() foreach (reverse @handlers);
79}
80
81sub _exit_handler {
82 run_exit_handlers();
83 exit(127);
84}
85
86$SIG{INT} = \&_exit_handler;
87$SIG{HUP} = \&_exit_handler;
88$SIG{QUIT} = \&_exit_handler;
89
90=back
91
92=head1 CHANGES
93
94=head2 Version 1.01 (dpkg 1.17.2)
95
96New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
97
98Deprecated variable: @handlers
99
100=head2 Version 1.00 (dpkg 1.15.6)
101
102Mark the module as public.
103
104=cut
105
1061;