File Coverage

blib/lib/IPC/Signal/Force.pm
Criterion Covered Total %
statement 17 26 65.3
branch 0 2 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 24 36 66.6


line stmt bran cond sub pod time code
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, 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 1     1   49958 { use 5.006; }
  1         3  
21 1     1   4 use warnings;
  1         2  
  1         23  
22 1     1   5 use strict;
  1         1  
  1         24  
23              
24 1     1   223 use IPC::Signal 1.00 qw(sig_num);
  1         474  
  1         47  
25 1     1   257 use POSIX qw(SIG_SETMASK SIG_UNBLOCK sigprocmask);
  1         4299  
  1         4  
26              
27             our $VERSION = "0.004";
28              
29 1     1   1294 use parent "Exporter";
  1         207  
  1         4  
30             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, 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.
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, 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     0 1   my($signame) = @_;
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 0         if($mask->ismember($signum)) {
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 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             Perl's treatment of signals under threading is broken. C,
83             as with anything interesting around signals, can't be expected to work
84             in multi-threaded Perl.
85              
86             =head1 SEE ALSO
87              
88             L,
89             L
90              
91             =head1 AUTHOR
92              
93             Andrew Main (Zefram)
94              
95             =head1 COPYRIGHT
96              
97             Copyright (C) 2004, 2007, 2010, 2017
98             Andrew Main (Zefram)
99              
100             =head1 LICENSE
101              
102             This module is free software; you can redistribute it and/or modify it
103             under the same terms as Perl itself.
104              
105             =cut
106              
107             1;