| File: | Dpkg/Exit.pm |
| Coverage: | 95.3% |
| line | stmt | bran | cond | sub | pod | time | code |
|---|---|---|---|---|---|---|---|
| 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 | =encoding utf8 | ||||||
| 18 | |||||||
| 19 - 27 | =head1 NAME Dpkg::Exit - program exit handlers =head1 DESCRIPTION The Dpkg::Exit module provides support functions to run handlers on exit. =cut | ||||||
| 28 | |||||||
| 29 | package Dpkg::Exit 2.00; | ||||||
| 30 | |||||||
| 31 | 3 3 3 | 7 5 47 | use strict; | ||||
| 32 | 3 3 3 | 5 5 105 | use warnings; | ||||
| 33 | |||||||
| 34 | our @EXPORT_OK = qw( | ||||||
| 35 | push_exit_handler | ||||||
| 36 | pop_exit_handler | ||||||
| 37 | run_exit_handlers | ||||||
| 38 | ); | ||||||
| 39 | |||||||
| 40 | 3 3 3 | 6 0 456 | use Exporter qw(import); | ||||
| 41 | |||||||
| 42 | my @handlers = (); | ||||||
| 43 | |||||||
| 44 - 52 | =head1 FUNCTIONS =over 4 =item push_exit_handler($func) Register a code reference into the exit function handlers stack. =cut | ||||||
| 53 | |||||||
| 54 | sub push_exit_handler { | ||||||
| 55 | 15 | 1 | 12 | my ($func) = shift; | |||
| 56 | |||||||
| 57 | 15 | 30 | _setup_exit_handlers() if @handlers == 0; | ||||
| 58 | 15 | 16 | push @handlers, $func; | ||||
| 59 | } | ||||||
| 60 | |||||||
| 61 - 65 | =item pop_exit_handler() Pop the last registered exit handler from the handlers stack. =cut | ||||||
| 66 | |||||||
| 67 | sub pop_exit_handler { | ||||||
| 68 | 3 | 1 | 6 | _reset_exit_handlers() if @handlers == 1; | |||
| 69 | 3 | 5 | pop @handlers; | ||||
| 70 | } | ||||||
| 71 | |||||||
| 72 - 76 | =item run_exit_handlers() Run the registered exit handlers. =cut | ||||||
| 77 | |||||||
| 78 | sub run_exit_handlers { | ||||||
| 79 | 15 | 1 | 31 | while (my $handler = pop @handlers) { | |||
| 80 | 12 | 426 | $handler->(); | ||||
| 81 | } | ||||||
| 82 | 12 | 455 | _reset_exit_handlers(); | ||||
| 83 | } | ||||||
| 84 | |||||||
| 85 | sub _exit_handler { | ||||||
| 86 | 3 | 5 | run_exit_handlers(); | ||||
| 87 | 0 | 0 | exit(127); | ||||
| 88 | } | ||||||
| 89 | |||||||
| 90 | my @SIGNAMES = qw(INT HUP QUIT); | ||||||
| 91 | my %SIGOLD; | ||||||
| 92 | |||||||
| 93 | sub _setup_exit_handlers | ||||||
| 94 | { | ||||||
| 95 | 9 | 15 | foreach my $signame (@SIGNAMES) { | ||||
| 96 | 27 | 42 | $SIGOLD{$signame} = $SIG{$signame}; | ||||
| 97 | 27 | 65 | $SIG{$signame} = \&_exit_handler; | ||||
| 98 | } | ||||||
| 99 | } | ||||||
| 100 | |||||||
| 101 | sub _reset_exit_handlers | ||||||
| 102 | { | ||||||
| 103 | 15 | 17 | foreach my $signame (@SIGNAMES) { | ||||
| 104 | 45 | 189 | $SIG{$signame} = $SIGOLD{$signame}; | ||||
| 105 | } | ||||||
| 106 | } | ||||||
| 107 | |||||||
| 108 | END { | ||||||
| 109 | 3 | 734 | local $?; | ||||
| 110 | 3 | 4 | run_exit_handlers(); | ||||
| 111 | } | ||||||
| 112 | |||||||
| 113 | =back | ||||||
| 114 | |||||||
| 115 - 131 | =head1 CHANGES =head2 Version 2.00 (dpkg 1.20.0) Hide variable: @handlers. =head2 Version 1.01 (dpkg 1.17.2) New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers() Deprecated variable: @handlers =head2 Version 1.00 (dpkg 1.15.6) Mark the module as public. =cut | ||||||
| 132 | |||||||
| 133 | 1; | ||||||