SigAction.pm

filter: Statementstmt 89.6% 95 / 106 Branchbran 54.7% 23 / 42 Conditioncond 40.0% 6 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 18 / 18 Podpod 62.5% 5 / 8 totaltotal 75.6% 149 / 197 CC 26 SCAR 17.3
CC
26
Cov
76%
_attrs_warning
17.8
timeout_call
6.2
mk_sig_action
6.1
CRAP 5.6
↑ Summary SigAction/Alarm.pm »
1 #
2 # Copyright (c) 2004-2016 Lincoln A. Baxter
3 #
4 # You may distribute under the terms of either the GNU General Public
5 # License or the Artistic License, as specified in the Perl README file,
6  
7 package Sys::SigAction;
8 8 require 5.005;
9 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use POSIX qw( :signal_h ceil INT_MAX ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 require Exporter;
13 8 ▶use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #support high resolution time transparently in timeout_call by defining
16 #the function sig_alarm() which calls Time::HiRes::alarm if available
17 #or core alarm with the ceil of the value passed otherwise.
18 #timeout_call uses sig_alarm()
19  
20 #replacement for alarm, factional second arg in floating point format:
21 8 ▶use Sys::SigAction::Alarm qw( ssa_alarm );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 sub sig_alarm
23 14 ▶{
Subroutine sig_alarm: called CC=1 SCAR=0.0
Pod documented
24 14 my $secs = shift;
25 14 ssa_alarm( $secs );
26 }
27  
28 #use Data::Dumper;
29  
30 8 @ISA = qw( Exporter );
31 8 @EXPORT_OK = qw( set_sig_handler timeout_call sig_name sig_number sig_alarm );
32 8 $VERSION = '0.24';
33  
34 8 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 8 my %signame = ();
36 8 my %signo = ();
37 {
38 8 ▶ defined $Config{sig_name} or die "This OS does not support signals?";
Branch: 1 branch
BranchTrueFalseTotal
defined $Sys::SigAction::Config{"sig_name"} or die "This OS does not support signals?" 8 0 8
39 8 my $i = 0; # Config prepends fake 0 signal called "ZERO".
40 8 my @numbers = split( ' ' ,$Config{sig_num} );
41 8 foreach my $name (split(' ', $Config{sig_name}))
42 {
43 544 $signo{$name} = $numbers[$i];
44 544 $signame{$signo{$name}} = $name;
45 #print "name=$name num=" .$numbers[$i] ."\n" ;
46 544 $i++;
47 }
48 }
49  
50 2 ▶sub sig_name {
Subroutine sig_name: called CC=2 SCAR=7.1
Pod documented
51 2 my ($sig) = @_;
52 2 ▶ return $sig if $sig !~ m/^\d+$/ ;
Branch: 1 branch
BranchTrueFalseTotal
unless $sig =~ /^\d+$/ 0 2 2
53 2 return $signame{$sig} ;
54 }
55 118 ▶sub sig_number {
Subroutine sig_number: called CC=2 SCAR=6.9
Pod documented
56 118 my ($sig) = @_;
57 118 ▶ return $sig if $sig =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $sig =~ /^\d+$/ 102 16 118
58 16 return $signo{$sig} ;
59 }
60 #if ( $] < 5008 ) {
61 # #over write definitions of sig_name and sig_number
62 # sub sig_name { warn "sig_name() not supported on perl versions < 5.8.0"; }
63 # sub sig_number { warn "sig_number() not supported on perl versions < 5.8.0"; }
64 #}
65  
66 8 ▶my $use_sigaction = ( $] >= 5.008 and $Config{d_sigaction} );
Condition33%1/3
$] >= 5.008 && $Sys::SigAction::Config{'d_sigaction'}
!ll&&!rl&&r
008
MC/DC0%0/2
$] >= 5.008 && $Sys::SigAction::Config{'d_sigaction'}
$] >= 5.008 $Sys::SigAction::Config{'d_sigaction'}
Truth table33%1/3
A && B
A= $] >= 5.008B= $Sys::SigAction::Config{'d_sigaction'}
ABresult
0X0
100
111
67  
68 sub _attrs_warning($)
69 39 ▶{
Subroutine _attrs_warning: called CC=5 SCAR=28.8
70 39 my ( $attrs ) = @_ ;
71 #my $act = POSIX::SigAction->new( $handler ,$mask ,$attrs->{flags} ,$attrs->{safe} );
72 #steve ( SPURKIS@cpan.org submitted http://rt.cpan.org/Ticket/Display.html?id=19916
73 # puts out the above line is a mis-interpretation of the API for POSIX::SigAcation
74 # so here is the fix (per his suggestion)... lab:
75 #
76 #http://rt.cpan.org/Public/Bug/Display.html?id=21777
77 #2006-09-29: in perl 5.8.0 (RH) $act->safe() is broken
78 # safe is not available until 5.8.2
79 # DAMN... it was in my docs too...
80 39 ▶ if ( exists( $attrs->{safe} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $attrs->{"safe"}) 0 39 39
81 {
82 0 ▶ if ( ( $] < 5.008002 ) && defined($attrs->{safe}) && $attrs->{safe} )
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.008002" and defined $attrs->{"safe"} and $attrs->{"safe"}) 0 0 0
Condition0%0/6
$] < "5.008002" and defined $attrs->{"safe"} and $attrs->{"safe"}
!ll&&!rl&&r
000
$] < "5.008002" and defined $attrs->{"safe"}
!ll&&!rl&&r
000
MC/DC0%0/3
$] < "5.008002" and defined $attrs->{"safe"} and $attrs->{"safe"}
$] < "5.008002" defined $attrs->{"safe"} $attrs->{"safe"}
Truth table0%0/4
A and B and C
A= $] < "5.008002"B= defined $attrs->{"safe"}C= $attrs->{"safe"}
ABCresult
0XX0
10X0
1100
1111
83 {
84 0 warn "safe mode is not supported in perl versions less than 5.8.2";
85 0 delete $attrs->{safe};
86 }
87 }
88  
89 }
90 sub set_sig_handler( $$;$$ )
91 39 ▶{
Subroutine set_sig_handler: called CC=4 SCAR=17.3
Pod documented
92 39 my ( $sig ,$handler ,$attrs ) = @_;
93 39 ▶ $attrs = {} if not defined $attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs 35 4 39
94 39 _attrs_warning($attrs);
95 39 ▶ if ( not $use_sigaction )
Branch: 1 branch
BranchTrueFalseTotal
unless ($use_sigaction) 0 39 39
96 {
97 #warn '$flags not supported in perl versions < 5.8' if $] < 5.008 and defined $flags;
98 0 $sig = sig_name( $sig );
99 0 my $ohandler = $SIG{$sig};
100 0 $SIG{$sig} = $handler;
101 0 ▶ return if not defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
102 0 return Sys::SigAction->new( $sig ,$ohandler );
103 }
104 39 my $act = mk_sig_action( $handler ,$attrs );
105 39 return set_sigaction( sig_number($sig) ,$act );
106 }
107 sub mk_sig_action($$)
108 39 ▶{
Subroutine mk_sig_action: called CC=6 SCAR=18.1
Pod undocumented
109 39 my ( $handler ,$attrs ) = @_;
110 39 ▶ die 'mk_sig_action requires perl 5.8.0 or later' if $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.008" 0 39 39
111 39 ▶ $attrs->{flags} = 0 if not defined $attrs->{flags};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs->{"flags"} 39 0 39
112 39 ▶ $attrs->{mask} = [] if not defined $attrs->{mask};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attrs->{"mask"} 35 4 39
113 #die '$sig is not defined' if not defined $sig;
114 #$sig = sig_number( $sig );
115 39 my @siglist = ();
116 39 foreach (@{$attrs->{mask}}) { push( @siglist ,sig_number($_)); };
117 39 my $mask = POSIX::SigSet->new( @siglist );
118  
119 39 my $act = POSIX::SigAction->new( $handler ,$mask ,$attrs->{flags} );
120  
121 #apply patch suggested by CPAN bugs
122 # http://rt.cpan.org/Ticket/Display.html?id=39599
123 # http://rt.cpan.org/Ticket/Display.html?id=39946 (these are dups)
124 #using safe mode with masking signals still breaks the masking of signals!
125 39 ▶ $act->safe($attrs->{safe}) if defined $attrs->{safe};
Branch: 1 branch
BranchTrueFalseTotal
if defined $attrs->{"safe"} 0 39 39
126 39 return $act;
127 }
128  
129  
130 sub set_sigaction($$)
131 72 ▶{
Subroutine set_sigaction: called CC=5 SCAR=16.3
Pod undocumented
132 72 my ( $sig ,$action ) = @_;
133 72 ▶ die 'set_sigaction() requires perl 5.8.0 or later' if $] < 5.008;
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.008" 0 72 72
134 72 ▶ die '$sig is not defined' if not defined $sig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig 0 72 72
135 72 ▶ die '$action is not a POSIX::SigAction' if not UNIVERSAL::isa( $action ,'POSIX::SigAction' );
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($action, "POSIX::SigAction") 0 72 72
136 72 $sig = sig_number( $sig );
137 72 ▶ if ( defined wantarray )
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 33 39 72
138 {
139 33 my $oact = POSIX::SigAction->new();
140 33 sigaction( $sig ,$action ,$oact );
141 33 return Sys::SigAction->new( $sig ,$oact );
142 }
143 else
144 {
145 39 sigaction( $sig ,$action );
146 }
147 }
148  
149 8 ▶use constant TIMEDOUT => {};
Subroutine BEGIN: called CC=1 SCAR=0.0
150 sub timeout_call( $$@ )
151 7 ▶{
Subroutine timeout_call: called CC=6 SCAR=18.3
Pod documented
152 7 my ( $timeout, $code, @args ) = @_;
153  
154 7 ▶ if (!$timeout) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($timeout) 0 7 7
155 0 &$code(@args);
156 0 return 0;
157 }
158  
159 7 my $timed_out = 0;
160 7 eval {
161 7 ▶ my $sa = set_sig_handler( SIGALRM ,sub { $timed_out = 1; die TIMEDOUT; } );
Subroutine __ANON__: called CC=1 SCAR=0.0
162 7 eval {
163 7 sig_alarm( $timeout );
164 7 &$code(@args);
165 };
166 7 sig_alarm(0);
167 7 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 7 0 7
168 };
169 7 ▶ die $@ if $@ and (not ref $@ or $@ != TIMEDOUT);
Branch: 1 branch
BranchTrueFalseTotal
if $@ and !ref($@) || $@ != {} 3 4 7
Condition83%5/6
$@ and !ref($@) || $@ != {}
!ll&&!rl&&r
043
not ref $@ or $@ != {}
l!l&&r!l&&!r
214
MC/DC66%2/3
$@ and !ref($@) || $@ != {}
$@ not ref $@ $@ != {}
Truth table75%3/4
A and not(B) or C
A= $@B= not ref $@C= $@ != {}
ABCresult
0XX0
1000
11X1
1011
170  
171 4 return $timed_out;
172 }
173 33 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
174 33 my ($class,$sig,$act) = @_;
175 33 bless { SIG=>$sig ,ACT => $act } ,$class ;
176 }
177 sub DESTROY
178 33 ▶{
Subroutine DESTROY: called CC=3 SCAR=14.2
179 33 ▶ if ( $use_sigaction )
Branch: 1 branch
BranchTrueFalseTotal
if ($use_sigaction) { } 33 0 33
180 {
181 33 set_sigaction( $_[0]->{'SIG'} ,$_[0]->{'ACT'} );
182 }
183 else
184 {
185 #set it to default if not defined (suppress undefined warning)
186 0 ▶ $SIG{$_[0]->{'SIG'}} = defined $_[0]->{'ACT'} ? $_[0]->{'ACT'} : 'DEFAULT' ;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{'ACT'} ? : 0 0 0
187 }
188 33 return;
189 }
190  
191 8 1;
192  
193 __END__
↑ Summary SigAction/Alarm.pm »