| 1 | # $Id: Fork.pm,v 1.12 2010/02/09 08:40:41 dk Exp $ | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | package IO::Lambda::Fork; | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use base qw(IO::Lambda); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | our $DEBUG = $IO::Lambda::DEBUG{fork} || 0; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$IO::Lambda::DEBUG{'fork'} || 0
MC/DC0%0/1
$IO::Lambda::DEBUG{'fork'} || 0
$IO::Lambda::DEBUG{'fork'}
Truth table50%1/2
A
A= $IO::Lambda::DEBUG{'fork'}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Socket; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use POSIX; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Storable qw(thaw freeze); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use IO::Lambda qw(:all :dev); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | use IO::Lambda::Signal qw(pid); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | our @EXPORT_OK = qw(new_process process new_forked forked); | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | our %EXPORT_TAGS = (all => \@EXPORT_OK); | |||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 0 | ▶ | sub _d { "forked(" . _o($_[0]) . ")" } | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_d: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | # return pid and socket | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | sub new_process(&) | ||||||||||||||||||||||||||||||||||||||||||||||
| 27 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 44 | ▶ | my $cb = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_process: called CC=10 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 44 | my $r = IO::Handle-> new; | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | 44 | my $w = IO::Handle-> new; | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | 44 | socketpair( $r, $w, AF_UNIX, SOCK_STREAM, PF_UNSPEC); | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 44 | $w-> blocking(0); | |||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 44 | my $pid = fork; | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | 44 | ▶ | unless ( defined $pid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | warn "fork() failed:$!\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | close($w); | |||||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | close($r); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 0 | return ( undef, $! ); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 44 | ▶ | if ( $pid == 0) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 44 | 8 | close($w); | |||||||||||||||||||||||||||||||||||||||||||||
| 45 | 8 | ▶ | warn "process($$) started\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 8 | ▶ | eval { $cb-> ($r) if $cb; }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 8 | ▶ | warn "process($$) ended\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 8 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 8 | close($r); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 8 | ▶ | POSIX::exit($@ ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 36 | ▶ | warn "forked pid=$pid\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 36 | close($r); | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 36 | return ($pid, $w); | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | # simple fork, return only $? and $! | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | sub process(&) | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | ▶ | my $cb = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
process: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | lambda { | ||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | my $pid = fork; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 0 | ▶ | return undef, $! unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 0 | ▶ | unless ( $pid) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 0 | ▶ | warn "process($$) started\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | eval { $cb->(); }; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | ▶ | warn "process($$) ended\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | warn $@ if $@; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | POSIX::exit($@ ? 1 : 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | warn "forked pid=$pid\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | context $pid; | |||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | &pid(); | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # return output from a subprocess | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | sub new_forked(&) | ||||||||||||||||||||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 44 | ▶ | my $cb = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_forked: called CC=1 SCAR=0.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||
| 88 | my ( $pid, $r) = new_process { | ||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 8 | ▶ | my @ret; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=14.3
| |||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 8 | my $socket = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 8 | ▶ | eval { @ret = $cb-> () if $cb }; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 8 | ▶ | my $msg = $@ ? [ 0, $@ ] : [ 1, @ret ]; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 8 | ▶ | warn "process($$) ended: [@$msg]\n" if $DEBUG > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 8 | print $socket freeze($msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 44 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||
| 97 | lambda { | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 36 | ▶ | return undef, undef, $r unless defined $pid; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.7
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 36 | my $buf = ''; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 36 | context readbuf, $r, \ $buf, undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 102 | tail { | ||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 32 | ▶ | my ( $ok, $error) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=7 SCAR=22.4
| |||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 32 | my @ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 32 | ▶ | ($ok,$error) = (0,$!) unless close($r); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 32 | ▶ | unless ( $ok) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 0 | @ret = ( undef, $error); | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 32 | my $msg; | |||||||||||||||||||||||||||||||||||||||||||||
| 112 | 32 | eval { $msg = thaw $buf }; | |||||||||||||||||||||||||||||||||||||||||||||
| 113 | 32 | ▶ | unless ( $msg and ref($msg) and ref($msg) eq 'ARRAY') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition33%2/6
$msg and ref $msg and ref $msg eq "ARRAY"
$msg and ref $msg
MC/DC0%0/3
$msg and ref $msg and ref $msg eq "ARRAY"
$msg
ref $msg
ref $msg eq "ARRAY"
Truth table25%1/4
A and B and C
A= $msgB= ref $msgC= ref $msg eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | 0 | @ret = ( undef, $@); | |||||||||||||||||||||||||||||||||||||||||||||
| 115 | } elsif ( 0 == shift @$msg) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | @ret = ( undef, @$msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 32 | @ret = ( 1, @$msg); | |||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||
| 122 | 32 | context $pid; | |||||||||||||||||||||||||||||||||||||||||||||
| 123 | pid { | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 31 | ▶ | warn "pid($pid): exitcode=$?, [@ret]\n" if $DEBUG > 1; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 31 | return shift, @ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 36 | }}} | |||||||||||||||||||||||||||||||||||||||||||||
| 127 | 36 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # simpler version of new_forked | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | sub forked(&) | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 48 | ▶ | my $cb = shift; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
forked: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 133 | lambda { | ||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 44 | ▶ | context &new_forked($cb); | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 135 | tail { | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 31 | ▶ | my ( $pid, $ok, @ret) = @_; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 31 | return @ret; | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 36 | }} | |||||||||||||||||||||||||||||||||||||||||||||
| 139 | 48 | } | |||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||
| 143 | __DATA__ | ||||||||||||||||||||||||||||||||||||||||||||||