lib/Remote/Perl/Transport.pm

filter: Statementstmt 95.2% 80 / 84 Branchbran 61.9% 26 / 42 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 9 totaltotal 79.7% 122 / 153 CC 26 SCAR 17.0
CC
26
Cov
84%
connect
12.0
read_stderr
5.3
_wait_for_child
4.5
CRAP 5.5
« lib/Remote/Perl/ModuleServer.pm ↑ Summary blib/lib/Remote/Perl/FlowControl.pm »
1 132 ▶use v5.36;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Remote::Perl::Transport;
3 132 our $VERSION = '0.005';
4  
5 132 ▶use autodie qw(open close);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 132 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 132 ▶use POSIX qw(WNOHANG);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # Wraps a bidirectional pipe to a child process.
10 #
11 # The child's stdin <- we write protocol bytes
12 # The child's stdout -> we read protocol bytes
13 # The child's stderr -> we read for debug/error logging (separate channel)
14 #
15 # All handles are set to binmode (raw bytes, no encoding layers).
16  
17 1522 ▶sub new($class, %args) {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
18 # cmd => arrayref of command + arguments (no shell expansion)
19 return bless {
20 cmd => $args{cmd},
21 1522 pid => undef,
22 in_fh => undef, # write end -> child stdin
23 out_fh => undef, # read end <- child stdout
24 err_fh => undef, # read end <- child stderr
25 }, $class;
26 }
27  
28 1522 ▶sub connect($self) {
Subroutine connect: called CC=11 SCAR=24.9
Pod undocumented
29 1522 $SIG{PIPE} = 'IGNORE';
30  
31 # We fork+exec manually instead of using open3 so we can call setpgrp()
32 # in the child. This puts the transport process (e.g. ssh) in its own
33 # process group, preventing terminal-generated signals (Ctrl-C -> SIGINT)
34 # from reaching it directly. Signals are instead forwarded through the
35 # protocol by the parent's signal handlers.
36 1522 ▶ pipe(my $child_in_r, my $child_in_w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_in_r, my $child_in_w or die "pipe: $!\n" 1522 0 1522
37 1522 ▶ pipe(my $child_out_r, my $child_out_w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_out_r, my $child_out_w or die "pipe: $!\n" 1522 0 1522
38 1522 ▶ pipe(my $child_err_r, my $child_err_w) or die "pipe: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $child_err_r, my $child_err_w or die "pipe: $!\n" 1522 0 1522
39  
40 1522 ▶ my $pid = fork() // die "fork: $!\n";
Condition50%1/2
fork // die("fork: $!\n")
l!l
15220
MC/DC0%0/1
fork // die("fork: $!\n")
fork
Truth table50%1/2
A
A= fork
Aresult
11
00
41  
42 1522 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 99 1423 1522
43 # Child: new process group so parent's signals don't reach us.
44 99 setpgrp(0, 0);
45  
46 99 close($child_in_w);
47 99 close($child_out_r);
48 99 close($child_err_r);
49  
50 99 ▶ open(STDIN, '<&', $child_in_r) or die "dup stdin: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
&Remote::Perl::Transport::open("STDIN", "<&", $child_in_r) or die "dup stdin: $!\n" 99 0 99
51 99 ▶ open(STDOUT, '>&', $child_out_w) or die "dup stdout: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
&Remote::Perl::Transport::open("STDOUT", ">&", $child_out_w) or die "dup stdout: $!\n" 99 0 99
52 99 ▶ open(STDERR, '>&', $child_err_w) or die "dup stderr: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
&Remote::Perl::Transport::open("STDERR", ">&", $child_err_w) or die "dup stderr: $!\n" 99 0 99
53 99 close($_) for $child_in_r, $child_out_w, $child_err_w;
54  
55 99 ▶ exec(@{ $self->{cmd} }) or die "exec: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
exec @{$self->{"cmd"};} or die "exec: $!\n" 0 0 0
56 }
57  
58 1423 close($child_in_r);
59 1423 close($child_out_w);
60 1423 close($child_err_w);
61  
62 1423 binmode($child_in_w);
63 1423 binmode($child_out_r);
64 1423 binmode($child_err_r);
65  
66 1423 $self->{pid} = $pid;
67 1423 $self->{in_fh} = $child_in_w;
68 1423 $self->{out_fh} = $child_out_r;
69 1423 $self->{err_fh} = $child_err_r;
70 }
71  
72 # Write raw bytes to child stdin. Returns number of bytes written.
73 9007 ▶sub write_bytes($self, $data) {
Subroutine write_bytes: called CC=3 SCAR=11.0
Pod undocumented
74 9007 my $total = length($data);
75 9007 my $written = 0;
76 9007 ▶ while ($written < $total) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written < $total) 9007 9007 18014
77 9007 my $n = syswrite($self->{in_fh}, $data, $total - $written, $written);
78 9007 ▶ die "syswrite: $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 9007 9007
79 9007 $written += $n;
80 }
81 9007 return $written;
82 }
83  
84 # Read up to $len bytes from child stdout.
85 # Returns undef on EOF, dies on error.
86 8453 ▶sub read_bytes($self, $len) {
Subroutine read_bytes: called CC=3 SCAR=11.0
Pod undocumented
87 8453 my $data;
88 8453 my $n = sysread($self->{out_fh}, $data, $len);
89 8453 ▶ die "sysread: $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 8453 8453
90 8453 ▶ return if $n == 0; # EOF
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 1 8452 8453
91 8452 return $data;
92 }
93  
94 # Read up to $len bytes from child stderr (non-blocking).
95 # Returns '' if nothing available, undef on EOF.
96 1 ▶sub read_stderr($self, $len = 4096) {
Subroutine read_stderr: called CC=5 SCAR=16.7
Pod undocumented
97 1 ▶ return '' unless $self->stderr_ready(0);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->stderr_ready(0) 0 1 1
98 1 my $data;
99 1 my $n = sysread($self->{err_fh}, $data, $len);
100 1 ▶ die "sysread stderr: $!\n" unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 1 1
101 1 ▶ return if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 1 1
102 1 return $data;
103 }
104  
105 # Return true if child stderr has data ready within $timeout seconds.
106 3 ▶sub stderr_ready($self, $timeout = 0) {
Subroutine stderr_ready: called CC=2 SCAR=6.9
Pod undocumented
107 3 return IO::Select->new($self->{err_fh})->can_read($timeout);
108 }
109  
110 # Expose raw filehandle and pid for callers building their own IO::Select sets.
111 14204 ▶sub out_fh($self) { $self->{out_fh} }
Subroutine out_fh: called CC=1 SCAR=0.0
Pod undocumented
112 4 ▶sub pid($self) { $self->{pid} }
Subroutine pid: called CC=1 SCAR=0.0
Pod undocumented
113  
114 # Close stdin (signals EOF to child) and wait for it to exit.
115 1422 ▶sub disconnect($self) {
Subroutine disconnect: called CC=2 SCAR=7.0
Pod undocumented
116 1422 ▶ if ($self->{in_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_fh"}) 1422 0 1422
117 1422 close($self->{in_fh});
118 1422 $self->{in_fh} = undef;
119 }
120 1422 $self->_wait_for_child;
121 }
122  
123 1423 ▶sub _wait_for_child($self) {
Subroutine _wait_for_child: called CC=4 SCAR=15.0
124 1423 ▶ return unless $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"pid"} 0 1423 1423
125 1423 my $pid = $self->{pid};
126  
127 # Poll for up to 5 seconds, then escalate.
128 1423 my $deadline = time + 5;
129 1423 ▶ while (time < $deadline) {
Branch: 1 branch
BranchTrueFalseTotal
if (time < $deadline) 8675 0 8675
130 8675 my $result = waitpid($pid, WNOHANG);
131 8675 ▶ if ($result == $pid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($result == $pid) 1423 7252 8675
132 1423 $self->{pid} = undef;
133 1423 return $?;
134 }
135 7252 select(undef, undef, undef, 0.05); # 50 ms sleep
136 }
137 0 kill('TERM', $pid);
138 0 sleep 1;
139 0 waitpid($pid, WNOHANG);
140 0 $self->{pid} = undef;
141 }
142  
143 1400 ▶sub DESTROY($self) {
Subroutine DESTROY: called CC=3 SCAR=11.0
144 1400 ▶ if ($self->{in_fh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"in_fh"}) 1 1399 1400
145 1 close($self->{in_fh});
146 1 $self->{in_fh} = undef;
147 }
148 1400 ▶ $self->_wait_for_child if $self->{pid};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pid"} 1 1399 1400
149 }
150  
151 132 1;
152  
153 __END__
« lib/Remote/Perl/ModuleServer.pm ↑ Summary blib/lib/Remote/Perl/FlowControl.pm »