Commit | Line | Data |
---|---|---|
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 | ||
17 | package Dpkg::Exit; | |
18 | ||
19 | use strict; | |
20 | use warnings; | |
21 | ||
22 | our $VERSION = '1.01'; | |
23 | our @EXPORT_OK = qw( | |
24 | push_exit_handler | |
25 | pop_exit_handler | |
26 | run_exit_handlers | |
27 | ); | |
28 | ||
29 | use Exporter qw(import); | |
30 | ||
31 | # XXX: Backwards compatibility, stop exporting on VERSION 2.00. | |
32 | ## no critic (Variables::ProhibitPackageVars) | |
33 | our @handlers = (); | |
34 | ## use critic | |
35 | ||
36 | =encoding utf8 | |
37 | ||
38 | =head1 NAME | |
39 | ||
40 | Dpkg::Exit - program exit handlers | |
41 | ||
42 | =head1 DESCRIPTION | |
43 | ||
44 | The 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 | ||
52 | Register a code reference into the exit function handlers stack. | |
53 | ||
54 | =cut | |
55 | ||
56 | sub push_exit_handler { | |
57 | my ($func) = shift; | |
58 | push @handlers, $func; | |
59 | } | |
60 | ||
61 | =item pop_exit_handler() | |
62 | ||
63 | Pop the last registered exit handler from the handlers stack. | |
64 | ||
65 | =cut | |
66 | ||
67 | sub pop_exit_handler { | |
68 | pop @handlers; | |
69 | } | |
70 | ||
71 | =item run_exit_handlers() | |
72 | ||
73 | Run the registered exit handlers. | |
74 | ||
75 | =cut | |
76 | ||
77 | sub run_exit_handlers { | |
78 | &$_() foreach (reverse @handlers); | |
79 | } | |
80 | ||
81 | sub _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 | ||
96 | New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers() | |
97 | ||
98 | Deprecated variable: @handlers | |
99 | ||
100 | =head2 Version 1.00 (dpkg 1.15.6) | |
101 | ||
102 | Mark the module as public. | |
103 | ||
104 | =cut | |
105 | ||
106 | 1; |