Fork/Serve.pm

filter: Statementstmt 45.4% 40 / 88 Branchbran 31.6% 19 / 60 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 37.5% 3 / 8 Podpod 0.0% 0 / 2 totaltotal 39.2% 62 / 158 CC 32 SCAR 40.6
CC
32
Cov
40%
serve
71.7
serve
71.7
me
3.7
CRAP 57.7
↑ Summary Fork.pm »
1 package AnyEvent::Fork::Serve;
2  
3 1 our $OWNER; # pid of process "owning" us
4  
5 # commands understood:
6 # e_val perlcode string...
7 # f_ork
8 # h_andle + fd
9 # a_rgs string...
10 # r_un func
11  
12 # the goal here is to keep this simple, small and efficient
13 3 ▶sub serve {
Subroutine serve: called CC=16 SCAR=42.7
Pod undocumented
14 0 ▶ local $^W = 0; # avoid spurious warnings
Subroutine serve: not called CC=16 SCAR=42.7
15  
16 0 undef &me; # free a tiny bit of memory
17  
18 0 my $master = shift;
19  
20 0 my @arg;
21  
22 0 my ($cmd, $fd);
23  
24 0 ▶ my $error = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
25 0 ▶ warn "[$0] ERROR: $_[0]\n";
Subroutine __ANON__: not called CC=1 SCAR=1.2
26 0 last;
27 0 };
28  
29 0 ▶ local *run_args = sub () { # AnyEvent::Fork::Serve::run_args
Subroutine __ANON__: not called CC=1 SCAR=1.2
30 0 ▶ my (@ret, @arg) = @arg; # copy and clear @arg
Subroutine __ANON__: called CC=1 SCAR=1.2
31 @ret
32 0 };
33  
34 0 while () {
35 # we manually reap child processes before we sleep, as local $SIG...
36 # will destroy existing child handlers instead of restoring them.
37 0 ▶ 1 while 0 < waitpid -1, 1; # WNOHANG is portably 1. prove me wrong.
Branch: 2 branches
BranchTrueFalseTotal
0 < waitpid(-1, 1) and do { '???' } 0 7 7
0 < waitpid(-1, 1) and do { '???' } 0 0 0
38  
39 # we must not ever read "too much" data, as we might accidentally read
40 # an IO::FDPass::send request.
41  
42 0 my $len;
43 0 ▶ sysread $master, $len, 5 - length $len, length $len or last
Branch: 4 branches
BranchTrueFalseTotal
5 > length $len and do { sysread $master, $len, 5 - length($len), length $len or last } 7 6 13
sysread $master, $len, 5 - length($len), length $len or last 6 1 7
5 > length $len and do { sysread $master, $len, 5 - length($len), length $len or last } 0 0 0
sysread $master, $len, 5 - length($len), length $len or last 0 0 0
44 while 5 > length $len;
45 0 ($cmd, $len) = unpack "a L", $len;
46  
47 0 my $buf;
48 0 ▶ sysread $master, $buf, $len - length $buf, length $buf or last
Branch: 4 branches
BranchTrueFalseTotal
$len > length $buf and do { sysread $master, $buf, $len - length($buf), length $buf or last } 2 6 8
sysread $master, $buf, $len - length($buf), length $buf or last 2 0 2
$len > length $buf and do { sysread $master, $buf, $len - length($buf), length $buf or last } 0 0 0
sysread $master, $buf, $len - length($buf), length $buf or last 0 0 0
49 while $len > length $buf;
50  
51 0 ▶ if ($cmd eq "h") {
Branch: 10 branches
BranchTrueFalseTotal
if ($cmd eq 'h') { } 2 4 6
elsif ($cmd eq 'a') { } 0 4 4
elsif ($cmd eq 'f') { } 2 2 4
elsif ($cmd eq 'e') { } 2 0 2
elsif ($cmd eq 'r') { } 0 0 0
if ($cmd eq 'h') { } 0 0 0
elsif ($cmd eq 'a') { } 0 0 0
elsif ($cmd eq 'f') { } 0 0 0
elsif ($cmd eq 'e') { } 0 0 0
elsif ($cmd eq 'r') { } 0 0 0
52 0 require IO::FDPass;
53 0 $fd = IO::FDPass::recv (fileno $master);
54 0 ▶ $fd >= 0 or $error->("AnyEvent::Fork::Serve: fd_recv() failed: $!");
Branch: 2 branches
BranchTrueFalseTotal
$fd >= 0 or &$error("AnyEvent::Fork::Serve: fd_recv() failed: $!") 2 0 2
$fd >= 0 or &$error("AnyEvent::Fork::Serve: fd_recv() failed: $!") 0 0 0
55 0 ▶ open my $fh, "+<&=$fd" or $error->("AnyEvent::Fork::Serve: open (fd_recv) failed: $!");
Branch: 2 branches
BranchTrueFalseTotal
open my $fh, "+<&=$fd" or &$error("AnyEvent::Fork::Serve: open (fd_recv) failed: $!") 2 0 2
open my $fh, "+<&=$fd" or &$error("AnyEvent::Fork::Serve: open (fd_recv) failed: $!") 0 0 0
56 0 push @arg, $fh;
57  
58 } elsif ($cmd eq "a") {
59 0 push @arg, unpack "(w/a*)*", $buf;
60  
61 } elsif ($cmd eq "f") {
62 0 my $pid = fork;
63  
64 0 ▶ if ($pid eq 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid eq 0) { } 1 1 2
if ($pid eq 0) { } 0 0 0
65 0 $0 = "$OWNER AnyEvent::Fork";
66 0 $master = pop @arg;
67  
68 } else {
69 0 pop @arg;
70  
71 0 ▶ $pid
Branch: 2 branches
BranchTrueFalseTotal
$pid or &$error("AnyEvent::Fork::Serve: fork() failed: $!") 1 0 1
$pid or &$error("AnyEvent::Fork::Serve: fork() failed: $!") 0 0 0
72 or $error->("AnyEvent::Fork::Serve: fork() failed: $!");
73 }
74  
75 } elsif ($cmd eq "e") {
76 0 ($cmd, @_) = unpack "(w/a*)*", $buf;
77  
78 # $cmd is allowed to access @_ and nothing else
79 package main;
80 0 ▶ eval $cmd;
Subroutine BEGIN: called CC=1 SCAR=1.2
81 0 ▶ $error->("$@") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
82
83 } elsif ($cmd eq "r") {
84 # we could free &serve etc., but this might just unshare
85 # memory that could be shared otherwise.
86 0 @_ = ($master, @arg);
87 0 $0 = "$OWNER $buf";
88 package main;
89 0 goto &$buf;
90  
91 } else {
92 0 $error->("AnyEvent::Fork::Serve received unknown request '$cmd' - stream corrupted?");
93 }
94 }
95  
96 1 shutdown $master, 1;
97 1 exit; # work around broken win32 perls
98 }
99  
100 # the entry point for new_exec
101 0 ▶sub me {
Subroutine me: not called CC=2 SCAR=13.0
Pod undocumented
102 #$^F = 2; # should always be the case
103  
104 3 ▶ open my $fh, "+<&=$ARGV[0]"
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "+<&=$ARGV[0]" or die "AnyEvent::Fork::Serve::me unable to open communication socket: $!\n" 0 0 0
105 or die "AnyEvent::Fork::Serve::me unable to open communication socket: $!\n";
106  
107 3 $OWNER = $ARGV[1];
108  
109 0 $0 = "$OWNER AnyEvent::Fork/exec";
110  
111 0 @ARGV = ();
112 0 @_ = $fh;
113 0 goto &serve;
114 }
115  
116 1 1
117  
↑ Summary Fork.pm »