| 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 | =head1 NAME | ||||||||||
| 20 | |||||||||||
| 21 | Dpkg::Exit - program exit handlers | ||||||||||
| 22 | |||||||||||
| 23 | =head1 DESCRIPTION | ||||||||||
| 24 | |||||||||||
| 25 | The Dpkg::Exit module provides support functions to run handlers on exit. | ||||||||||
| 26 | |||||||||||
| 27 | =cut | ||||||||||
| 28 | |||||||||||
| 29 | package Dpkg::Exit 2.00; | ||||||||||
| 30 | |||||||||||
| 31 | 1 | ▶ | use v5.36; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 32 | |||||||||||
| 33 | 1 | our @EXPORT_OK = qw( | |||||||||
| 34 | push_exit_handler | ||||||||||
| 35 | pop_exit_handler | ||||||||||
| 36 | run_exit_handlers | ||||||||||
| 37 | ); | ||||||||||
| 38 | |||||||||||
| 39 | 1 | ▶ | use Exporter qw(import); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | |||||||||||
| 41 | 1 | my @handlers = (); | |||||||||
| 42 | |||||||||||
| 43 | =head1 FUNCTIONS | ||||||||||
| 44 | |||||||||||
| 45 | =over 4 | ||||||||||
| 46 | |||||||||||
| 47 | =item push_exit_handler($func) | ||||||||||
| 48 | |||||||||||
| 49 | Register a code reference into the exit function handlers stack. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | 5 | ▶ | sub push_exit_handler { | ||||||||
|
Subroutine
push_exit_handler: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 54 | 5 | my $func = shift; | |||||||||
| 55 | |||||||||||
| 56 | 5 | ▶ | _setup_exit_handlers() if @handlers == 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 5 | push @handlers, $func; | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | =item pop_exit_handler() | ||||||||||
| 61 | |||||||||||
| 62 | Pop the last registered exit handler from the handlers stack. | ||||||||||
| 63 | |||||||||||
| 64 | =cut | ||||||||||
| 65 | |||||||||||
| 66 | 1 | ▶ | sub pop_exit_handler { | ||||||||
|
Subroutine
pop_exit_handler: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 67 | 1 | ▶ | _reset_exit_handlers() if @handlers == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 1 | pop @handlers; | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | =item run_exit_handlers() | ||||||||||
| 72 | |||||||||||
| 73 | Run the registered exit handlers. | ||||||||||
| 74 | |||||||||||
| 75 | =cut | ||||||||||
| 76 | |||||||||||
| 77 | 5 | ▶ | sub run_exit_handlers { | ||||||||
|
Subroutine
run_exit_handlers: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 78 | 5 | ▶ | while (my $handler = pop @handlers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 4 | $handler->(); | |||||||||
| 80 | } | ||||||||||
| 81 | 4 | _reset_exit_handlers(); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | 1 | ▶ | sub _exit_handler { | ||||||||
|
Subroutine
_exit_handler: called CC=1 SCAR=1.2
| |||||||||||
| 85 | 1 | run_exit_handlers(); | |||||||||
| 86 | 0 | exit(127); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | 1 | my @SIGNAMES = qw(INT HUP QUIT); | |||||||||
| 90 | 1 | my %SIGOLD; | |||||||||
| 91 | |||||||||||
| 92 | sub _setup_exit_handlers | ||||||||||
| 93 | 3 | ▶ | { | ||||||||
|
Subroutine
_setup_exit_handlers: called CC=2 SCAR=6.9
| |||||||||||
| 94 | 3 | foreach my $signame (@SIGNAMES) { | |||||||||
| 95 | 9 | $SIGOLD{$signame} = $SIG{$signame}; | |||||||||
| 96 | 9 | $SIG{$signame} = \&_exit_handler; | |||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | |||||||||||
| 100 | sub _reset_exit_handlers | ||||||||||
| 101 | 5 | ▶ | { | ||||||||
|
Subroutine
_reset_exit_handlers: called CC=2 SCAR=6.9
| |||||||||||
| 102 | 5 | foreach my $signame (@SIGNAMES) { | |||||||||
| 103 | 15 | $SIG{$signame} = $SIGOLD{$signame}; | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 1 | ▶ | END { | ||||||||
|
Subroutine
END: called CC=1 SCAR=0.0
| |||||||||||
| 108 | 1 | local $?; | |||||||||
| 109 | 1 | run_exit_handlers(); | |||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | =back | ||||||||||
| 113 | |||||||||||
| 114 | =head1 CHANGES | ||||||||||
| 115 | |||||||||||
| 116 | =head2 Version 2.00 (dpkg 1.20.0) | ||||||||||
| 117 | |||||||||||
| 118 | Hide variable: @handlers. | ||||||||||
| 119 | |||||||||||
| 120 | =head2 Version 1.01 (dpkg 1.17.2) | ||||||||||
| 121 | |||||||||||
| 122 | New functions: push_exit_handler(), pop_exit_handler(), run_exit_handlers() | ||||||||||
| 123 | |||||||||||
| 124 | Deprecated variable: @handlers | ||||||||||
| 125 | |||||||||||
| 126 | =head2 Version 1.00 (dpkg 1.15.6) | ||||||||||
| 127 | |||||||||||
| 128 | Mark the module as public. | ||||||||||
| 129 | |||||||||||
| 130 | =cut | ||||||||||
| 131 | |||||||||||
| 132 | 1 | 1; | |||||||||