scripts/mk: On dpkg-build-api >= 1 include buildtools.mk in default.mk
[dpkg.git] / scripts / Dpkg / Exit.pm
blobfe781fcb9fcf0f4ab27141fb8af433586002f246
1 # Copyright © 2002 Adam Heath <doogie@debian.org>
2 # Copyright © 2012-2013 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
17 =encoding utf8
19 =head1 NAME
21 Dpkg::Exit - program exit handlers
23 =head1 DESCRIPTION
25 The Dpkg::Exit module provides support functions to run handlers on exit.
27 =cut
29 package Dpkg::Exit 2.00;
31 use strict;
32 use warnings;
34 our @EXPORT_OK = qw(
35 push_exit_handler
36 pop_exit_handler
37 run_exit_handlers
40 use Exporter qw(import);
42 my @handlers = ();
44 =head1 FUNCTIONS
46 =over 4
48 =item push_exit_handler($func)
50 Register a code reference into the exit function handlers stack.
52 =cut
54 sub push_exit_handler {
55 my ($func) = shift;
57 _setup_exit_handlers() if @handlers == 0;
58 push @handlers, $func;
61 =item pop_exit_handler()
63 Pop the last registered exit handler from the handlers stack.
65 =cut
67 sub pop_exit_handler {
68 _reset_exit_handlers() if @handlers == 1;
69 pop @handlers;
72 =item run_exit_handlers()
74 Run the registered exit handlers.
76 =cut
78 sub run_exit_handlers {
79 while (my $handler = pop @handlers) {
80 $handler->();
82 _reset_exit_handlers();
85 sub _exit_handler {
86 run_exit_handlers();
87 exit(127);
90 my @SIGNAMES = qw(INT HUP QUIT);
91 my %SIGOLD;
93 sub _setup_exit_handlers
95 foreach my $signame (@SIGNAMES) {
96 $SIGOLD{$signame} = $SIG{$signame};
97 $SIG{$signame} = \&_exit_handler;
101 sub _reset_exit_handlers
103 foreach my $signame (@SIGNAMES) {
104 $SIG{$signame} = $SIGOLD{$signame};
108 END {
109 local $?;
110 run_exit_handlers();
113 =back
115 =head1 CHANGES
117 =head2 Version 2.00 (dpkg 1.20.0)
119 Hide variable: @handlers.
121 =head2 Version 1.01 (dpkg 1.17.2)
123 New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers()
125 Deprecated variable: @handlers
127 =head2 Version 1.00 (dpkg 1.15.6)
129 Mark the module as public.
131 =cut