File Coverage

blib/lib/IPC/Signal/Force.pm
Criterion Covered Total %
statement 18 27 66.6
branch 0 2 0.0
condition n/a
subroutine 6 7 85.7
pod 1 1 100.0
total 25 37 67.5


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   25901 { use 5.006; }
  1         4  
  1         43  
21 1     1   5 use warnings;
  1         1  
  1         42  
22 1     1   6 use strict;
  1         1  
  1         48  
23              
24 1     1   784 use IPC::Signal 1.00 qw(sig_num);
  1         899  
  1         97  
25 1     1   895 use POSIX qw(SIG_SETMASK SIG_UNBLOCK sigprocmask);
  1         7596  
  1         7  
26              
27             our $VERSION = "0.003";
28              
29 1     1   7942 use parent "Exporter";
  1         300  
  1         6  
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             =head1 SEE ALSO
83              
84             L,
85             L
86              
87             =head1 AUTHOR
88              
89             Andrew Main (Zefram)
90              
91             =head1 COPYRIGHT
92              
93             Copyright (C) 2004, 2007, 2010 Andrew Main (Zefram)
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             1;