| 1 | =head1 NAME | ||||||||||
| 2 | |||||||||||
| 3 | IPC::Signal::Force - force default handling of a signal | ||||||||||
| 4 | |||||||||||
| 5 | =head1 SYNOPSIS | ||||||||||
| 6 | |||||||||||
| 7 | use IPC::Signal::Force qw(force_raise); | ||||||||||
| 8 | |||||||||||
| 9 | force_raise "TERM"; | ||||||||||
| 10 | |||||||||||
| 11 | =head1 DESCRIPTION | ||||||||||
| 12 | |||||||||||
| 13 | This module exports one function, C<force_raise>, which invokes a default | ||||||||||
| 14 | signal handler regardless of the signal's current handling. | ||||||||||
| 15 | |||||||||||
| 16 | =cut | ||||||||||
| 17 | |||||||||||
| 18 | package IPC::Signal::Force; | ||||||||||
| 19 | |||||||||||
| 20 | 2 | ▶ | { use 5.006; } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 2 | ▶ | use IPC::Signal 1.00 qw(sig_num); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 2 | ▶ | use POSIX qw(SIG_SETMASK SIG_UNBLOCK sigprocmask); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | |||||||||||
| 27 | 2 | our $VERSION = "0.003"; | |||||||||
| 28 | |||||||||||
| 29 | 2 | ▶ | use parent "Exporter"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | 2 | our @EXPORT_OK = qw(force_raise); | |||||||||
| 31 | |||||||||||
| 32 | =head1 FUNCTIONS | ||||||||||
| 33 | |||||||||||
| 34 | =over | ||||||||||
| 35 | |||||||||||
| 36 | =item force_raise(SIGNAL) | ||||||||||
| 37 | |||||||||||
| 38 | SIGNAL must be the name of a signal (e.g., "TERM"). The specified signal | ||||||||||
| 39 | is delivered to the current process, with the handler for the signal | ||||||||||
| 40 | temporarily reset to the default. The signal is also temporarily | ||||||||||
| 41 | unblocked if it was initially blocked. The overall effect is to | ||||||||||
| 42 | synchronously invoke the default handler for the signal, regardless of | ||||||||||
| 43 | how the signal would be handled the rest of the time. | ||||||||||
| 44 | |||||||||||
| 45 | This is mainly useful in a handler for the same signal, if the handler | ||||||||||
| 46 | wants to do something itself and also call the default handler. | ||||||||||
| 47 | For example, a handler for SIGTERM might shut down the program neatly | ||||||||||
| 48 | and then C<force_raise("TERM")>, which achieves a graceful shutdown | ||||||||||
| 49 | while also letting the parent process see that the process terminated | ||||||||||
| 50 | due to a signal rather than by C<exit()>. | ||||||||||
| 51 | |||||||||||
| 52 | A similar, but slightly more complex, case is a handler for SIGTSTP | ||||||||||
| 53 | (tty-initiated stop), which in a curses-style program might need to | ||||||||||
| 54 | restore sane tty settings, C<force_raise("TSTP")>, and then (after | ||||||||||
| 55 | the process has been restarted) reassert control of the tty and redraw | ||||||||||
| 56 | the screen. | ||||||||||
| 57 | |||||||||||
| 58 | =cut | ||||||||||
| 59 | |||||||||||
| 60 | sub force_raise($) { | ||||||||||
| 61 | 0 | ▶ | my($signame) = @_; | ||||||||
|
Subroutine
force_raise: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 62 | 0 | my $signum = sig_num($signame); | |||||||||
| 63 | 0 | my $sigset = new POSIX::SigSet $signum; | |||||||||
| 64 | 0 | my $mask = new POSIX::SigSet; | |||||||||
| 65 | 0 | local $SIG{$signame}; | |||||||||
| 66 | 0 | kill $signame, 0; | |||||||||
| 67 | 0 | sigprocmask(SIG_UNBLOCK, $sigset, $mask); | |||||||||
| 68 | 0 | ▶ | if($mask->ismember($signum)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 0 | sigprocmask(SIG_SETMASK, $mask, $mask); | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | =back | ||||||||||
| 74 | |||||||||||
| 75 | =head1 BUGS | ||||||||||
| 76 | |||||||||||
| 77 | If the signal in question is delivered from somewhere else while | ||||||||||
| 78 | C<force_raise> is executing, there is a race condition that makes it | ||||||||||
| 79 | is possible for the default signal handler to be called more than once. | ||||||||||
| 80 | There appears to be no way to avoid this in POSIX. | ||||||||||
| 81 | |||||||||||
| 82 | =head1 SEE ALSO | ||||||||||
| 83 | |||||||||||
| 84 | L<IPC::Signal>, | ||||||||||
| 85 | L<perlfunc/kill> | ||||||||||
| 86 | |||||||||||
| 87 | =head1 AUTHOR | ||||||||||
| 88 | |||||||||||
| 89 | Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 90 | |||||||||||
| 91 | =head1 COPYRIGHT | ||||||||||
| 92 | |||||||||||
| 93 | Copyright (C) 2004, 2007, 2010 Andrew Main (Zefram) <zefram@fysh.org> | ||||||||||
| 94 | |||||||||||
| 95 | =head1 LICENSE | ||||||||||
| 96 | |||||||||||
| 97 | This module is free software; you can redistribute it and/or modify it | ||||||||||
| 98 | under the same terms as Perl itself. | ||||||||||
| 99 | |||||||||||
| 100 | =cut | ||||||||||
| 101 | |||||||||||
| 102 | 2 | 1; | |||||||||