| 1 | package Fork::Utils; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.012; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use base qw/ Exporter /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Config (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use POSIX (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Carp qw/ croak /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||
| 11 | 1 | our @EXPORT_OK = qw/ safe_exec /; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | sub safe_exec { | ||||||||||||||||||||||||||
|
Subroutine
safe_exec: called CC=12 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 2 | my ( %options ) = @_; | |||||||||||||||||||||||||||
| 16 | 2 | my ( $code, $args, $sigset, $replace_mask ) = @options{ qw/code args sigset replace_mask/ }; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | croak( 'Argument $code must be a CODE reference.' ) if ( ref( $code ) ne 'CODE' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | croak( 'Argument $args must be an ARRAY reference.' ) if ( $args && ref( $args ) ne 'ARRAY' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$args and ref $args ne "ARRAY"
MC/DC0%0/2
$args and ref $args ne "ARRAY"
$args
ref $args ne "ARRAY"
Truth table33%1/3
A and B
A= $argsB= ref $args ne "ARRAY"
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | croak( 'Argument $sigset must be an ARRAY reference.' ) if ( $sigset && ref( $sigset ) ne 'ARRAY' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$sigset and ref $sigset ne "ARRAY"
MC/DC0%0/2
$sigset and ref $sigset ne "ARRAY"
$sigset
ref $sigset ne "ARRAY"
Truth table33%1/3
A and B
A= $sigsetB= ref $sigset ne "ARRAY"
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 2 | state $sig_nums = [ split( /\s+/, $Config::Config{'sig_num'} ) ]; | |||||||||||||||||||||||||||
| 23 | 2 | state $sig_names = [ split( /\s+/, $Config::Config{'sig_name'} ) ]; | |||||||||||||||||||||||||||
| 24 | 2 | my %signame2signum = (); | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | for my $i ( 0 .. $#{$sig_nums} ) { | |||||||||||||||||||||||||||
| 27 | 136 | $signame2signum{ $sig_names->[ $i ] } = $sig_nums->[ $i ]; | |||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 2 | my $new_sig_set = new POSIX::SigSet (); | |||||||||||||||||||||||||||
| 31 | 2 | my $old_sig_set = new POSIX::SigSet (); | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 2 | $new_sig_set->emptyset(); | |||||||||||||||||||||||||||
| 34 | 2 | $old_sig_set->emptyset(); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | ▶ | $sigset = [] if ( ! $sigset ); # let's use empty mask by default | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 2 | foreach my $sig_name ( grep { $_ } @{ $sigset } ) { | |||||||||||||||||||||||||||
| 39 | 2 | ▶ | $new_sig_set->addset( $signame2signum{ $sig_name } ) if ( $signame2signum{ $sig_name } ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | ▶ | if ( ! $replace_mask ) { # add signals into the current mask | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 2 | POSIX::sigprocmask( POSIX::SIG_BLOCK, $new_sig_set, $old_sig_set ); | |||||||||||||||||||||||||||
| 44 | } else { # replace the current signla mask | ||||||||||||||||||||||||||||
| 45 | 0 | POSIX::sigprocmask( POSIX::SIG_SETMASK, $new_sig_set, $old_sig_set ); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 2 | ▶ | my $result = eval{ $code->( @{ $args || [] } ); }; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args || []
MC/DC0%0/1
$args || []
$args
Truth table50%1/2
A
A= $args
| |||||||||||||||||||||||||||||
| 49 | 2 | my $error = $@; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # we don't use POSIX::SIG_UNBLOCK because we can occasionally unblock some signals that were blocked previously | ||||||||||||||||||||||||||||
| 52 | 2 | POSIX::sigprocmask( POSIX::SIG_SETMASK, $old_sig_set ); | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | $@ = $error; # restore the error if it was replaced by POSIX::sigprocmask | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2 | return( $result ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 1 | 1; | |||||||||||||||||||||||||||
| 60 | __END__ | ||||||||||||||||||||||||||||