HalfDuplex/Ptyish.pm

filter: Statementstmt 83.2% 129 / 155 Branchbran 47.6% 40 / 84 Conditioncond 26.9% 7 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 87.1% 27 / 31 Podpod 100% 7 / 7 totaltotal 65.8% 210 / 319 CC 54 SCAR 24.2
CC
54
Cov
66%
kill
37.1
_wait_for_inactive
30.0
_select_loop
15.0
CRAP 11.2
« HalfDuplex/JobControl.pm ↑ Summary HalfDuplex/SysctlPoll.pm »
1 #!/usr/bin/env perl
2 # vim: fdm=marker sw=4 et
3 package IO::Pty::HalfDuplex::Ptyish;
4 # Notes on design {{{
5 # IO::Pty::HalfDuplex operates by mimicing a job-control shell. A process
6 # is done sending data when it calls read, which we notice because it
7 # results in Stopped (tty input). So far, fairly simple. Complications
8 # arise because of races, and also because shells are required to run in
9 # the managed tty, and be the parent of the process; this forces us to use
10 # a stub process and simple IPC.
11 # }}}
12 # POD header {{{
13  
14 =head1 NAME
15  
16 IO::Pty::HalfDuplex::Ptyish - Base class for pty-using HalfDuplex backends
17  
18 =head1 SYNOPSIS
19  
20 package IO::Pty::HalfDuplex::PTrace;
21  
22 use base 'IO::Pty::HalfDuplex::Ptyish';
23  
24 sub shell {
25 my %args = @_;
26  
27 #start subprocess
28 syswrite $args->{info_pipe}, pack("N", $pid);
29  
30 while(1) {
31 # wait for subprocess to block
32 if (subprocess died) {
33 syswrite $args->{info_pipe}, "d" . pack("CC", $sig, $code);
34 POSIX::_exit();
35 }
36  
37 syswrite $args->{info_pipe}, "r";
38 sysread $args->{ctl_pipe}, $_, 1;
39  
40 # continue subprocess
41 }
42 }
43  
44 1;
45  
46 =head1 DESCRIPTION
47  
48 C<IO::Pty::HalfDuplex::Ptyish> is the base class for pty-using HalfDuplex
49 backends. It implements the HalfDuplex methods by opening a pty and starting
50 a slave process to control the child; this slave communicates with the main
51 process using a pair of pipes. Subclasses must implement the C<shell()>
52 method, with the following specification:
53  
54 =head2 $pty->shell(info_pipe => $status_fh, ctl_pipe => $control_fh,
55 command => \@argv)
56  
57 C<shell> forks and starts the child process as if by C<exec(@argv)>. It then
58 writes the PID of the child in C<pack "N"> format to $status_fh, and enters
59 an infinite loop in the parent. Each time the child stops waiting for input,
60 the character "r" is written to $status_fd; the client process will request
61 a restart by putting more data into the pty buffer and writing "s" to
62 $control_fh. When the child exits, write a "d" to $status_fd, followed
63 by the child's exit signal or 0 and exit code or 0, each in C<pack "C"> format.
64 The shell then calls _exit.
65  
66 =cut
67  
68 # }}}
69 # Imports {{{
70 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 4 ▶use base 'IO::Pty::HalfDuplex';
Subroutine BEGIN: called CC=1 SCAR=0.0
73 4 ▶use POSIX qw(:unistd_h :sys_wait_h :signal_h EIO);
Subroutine BEGIN: called CC=1 SCAR=0.0
74 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 4 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 4 ▶use Time::HiRes qw(time);
Subroutine BEGIN: called CC=1 SCAR=0.0
77 4 our $_infinity = 1e1000;
78 # }}}
79 # new {{{
80 # Most of this is handled by IO::Pty, thankfully
81  
82 6 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
83 6 my $class = shift;
84 6 my $self = {
85 # options
86 buffer_size => 8192,
87 @_,
88  
89 # state
90 pty => undef,
91 active => 0,
92 exit_code => undef,
93 };
94  
95 6 bless $self, $class;
96  
97 6 $self->{pty} = new IO::Pty;
98  
99 6 return $self;
100 }
101 # }}}
102 5 ▶sub spawn {
Subroutine spawn: called CC=10 SCAR=23.6
Pod documented
103 5 my $self = shift;
104 5 my $slave = $self->{pty}->slave;
105  
106 5 ▶ croak "Attempt to spawn a subprocess when one is already running"
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_active 0 5 5
107 if $self->is_active;
108  
109 5 ▶ pipe (my $p1r, my $p1w) || croak "Failed to create a pipe";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $p1r, my $p1w or croak("Failed to create a pipe") 5 0 5
110 5 ▶ pipe (my $p2r, my $p2w) || croak "Failed to create a pipe";
Branch: 1 branch
BranchTrueFalseTotal
pipe my $p2r, my $p2w or croak("Failed to create a pipe") 5 0 5
111  
112 5 $self->{info_pipe} = $p1r;
113 5 $self->{ctl_pipe} = $p2w;
114  
115 5 ▶ defined ($self->{shell_pid} = fork) || croak "fork: $!";
Branch: 1 branch
BranchTrueFalseTotal
defined($self->{"shell_pid"} = fork) or croak("fork: $!") 5 0 5
116  
117 5 ▶ unless ($self->{shell_pid}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"shell_pid"}) 2 3 5
118 2 close $p1r;
119 2 close $p2w;
120 2 $self->{pty}->make_slave_controlling_terminal;
121 2 close $self->{pty};
122 2 $slave->set_raw;
123 # reopen the standard file descriptors in the child to point to the
124 # pty rather than wherever they have been pointing during the script's
125 # execution
126 2 ▶ open(STDIN, "<&" . $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . $slave->fileno or carp("Couldn't reopen STDIN for reading") 2 0 2
127 or carp "Couldn't reopen STDIN for reading";
128 2 ▶ open(STDOUT, ">&" . $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . $slave->fileno or carp("Couldn't reopen STDOUT for writing") 2 0 2
129 or carp "Couldn't reopen STDOUT for writing";
130 2 ▶ open(STDERR, ">&" . $slave->fileno)
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, '>&' . $slave->fileno or carp("Couldn't reopen STDERR for writing") 2 0 2
131 or carp "Couldn't reopen STDERR for writing";
132 2 close $slave;
133  
134 2 $self->_shell(info_pipe => $p1w, ctl_pipe => $p2r,
135 command => [@_]);
136 }
137  
138 3 close $p1w;
139 3 close $p2r;
140 3 $self->{pty}->close_slave;
141 3 $self->{pty}->set_raw;
142  
143 3 my ($rcpid);
144 3 my $syncd = sysread($self->{info_pipe}, $rcpid, 4);
145  
146 3 ▶ unless ($syncd == 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($syncd == 4) 0 3 3
147 0 croak "Cannot sync with child: $!";
148 }
149 3 $self->{slave_pgid} = unpack "N", $rcpid;
150  
151 3 $self->{read_buffer} = $self->{write_buffer} = '';
152 3 $self->{sent_sync} = 0; $self->{active} = 1;
153 3 $self->{timeout} = $self->{exit_code} = $self->{exit_sig} = undef;
154 }
155 # }}}
156 # I/O on shell pipes {{{
157 # Process a wait result from the shell
158 7 ▶sub _handle_info_read {
Subroutine _handle_info_read: called CC=7 SCAR=21.1
159 7 my $self = shift;
160 7 my $ibuf;
161  
162 7 my $ret = sysread $self->{info_pipe}, $ibuf, 1;
163  
164 7 ▶ if ($ret == 0) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ret == 0) { } 3 4 7
elsif ($ibuf eq 'd') { } 3 1 4
elsif ($ibuf eq 'r') { } 1 0 1
165 # Shell has exited
166 3 $self->{sent_sync} = 0;
167 3 $self->{active} = 0;
168 # FreeBSD 7 (and presumably other BSDkin) requires the pty output
169 # buffer to be drained before any session leader can exit.
170 3 $self->_handle_pty_drain;
171 # Reap the shell
172 3 waitpid($self->{shell_pid}, 0);
173  
174 3 ▶ if (!defined $self->{exit_code}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{"exit_code"}) 0 3 3
175 # Get the shell crash code
176 0 ▶ $self->{exit_sig} = WIFSIGNALED($?) ? WTERMSIG($?) : 0;
Branch: 1 branch
BranchTrueFalseTotal
WIFSIGNALED($?) ? : 0 0 0
177 0 ▶ $self->{exit_code} = WIFEXITED($?) ? WEXITSTATUS($?) : 0;
Branch: 1 branch
BranchTrueFalseTotal
WIFEXITED($?) ? : 0 0 0
178 }
179 } elsif ($ibuf eq 'd') {
180 3 sysread $self->{info_pipe}, $ibuf, 2;
181  
182 3 @{$self}{"exit_sig","exit_code"} = unpack "CC", $ibuf;
183 } elsif ($ibuf eq 'r') {
184 1 $self->{sent_sync} = 0;
185 }
186 }
187  
188 1 ▶sub _handle_pty_write {
Subroutine _handle_pty_write: called CC=2 SCAR=7.1
189 1 my ($self, $ref) = @_;
190  
191 my $ct = syswrite $self->{pty}, $self->{write_buffer}
192 1 ▶ or die "write(pty): $!";
Branch: 1 branch
BranchTrueFalseTotal
my $ct = syswrite($self->{"pty"}, $self->{"write_buffer"}) or die "write(pty): $!" 1 0 1
193  
194 1 $self->{write_buffer} = substr($self->{write_buffer}, $ct);
195 }
196  
197 8 ▶sub _handle_pty_read {
Subroutine _handle_pty_read: called CC=4 SCAR=15.6
198 8 my ($self) = @_;
199  
200 return if defined (sysread $self->{pty}, $self->{read_buffer},
201 8 ▶ $self->{buffer_size}, length $self->{read_buffer});
Branch: 1 branch
BranchTrueFalseTotal
if defined sysread($self->{"pty"}, $self->{"read_buffer"}, $self->{"buffer_size"}, length $self->{"read_buffer"}) 3 5 8
202  
203 # Under Linux, any pty read can randomly return EIO if the
204 # session leader exits racily.
205 5 ▶ return if $! == &POSIX::EIO and $^O eq "linux";
Branch: 1 branch
BranchTrueFalseTotal
if $! == &POSIX::EIO and $^O eq "linux" 5 0 5
Condition33%1/3
$! == &POSIX::EIO and $^O eq "linux"
!ll&&!rl&&r
005
MC/DC0%0/2
$! == &POSIX::EIO and $^O eq "linux"
$! == &POSIX::EIO $^O eq "linux"
Truth table33%1/3
A and B
A= $! == &POSIX::EIOB= $^O eq "linux"
ABresult
0X0
100
111
206  
207 0 die "read(pty): $!";
208 }
209  
210 3 ▶sub _handle_pty_drain {
Subroutine _handle_pty_drain: called CC=6 SCAR=21.6
211 3 my ($self) = @_;
212  
213 3 while (1) {
214 my $got = sysread $self->{pty}, $self->{read_buffer},
215 3 $self->{buffer_size}, length $self->{read_buffer};
216  
217 3 ▶ return if defined $got && $got == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $got and $got == 0 0 3 3
Condition33%1/3
defined $got and $got == 0
!ll&&!rl&&r
300
MC/DC0%0/2
defined $got and $got == 0
defined $got $got == 0
Truth table33%1/3
A and B
A= defined $gotB= $got == 0
ABresult
0X0
100
111
218 3 ▶ next if defined $got;
Branch: 1 branch
BranchTrueFalseTotal
if defined $got 0 3 3
219  
220 # Under Linux, any pty read can randomly return EIO if the
221 # session leader exits racily.
222 3 ▶ return if $! == &POSIX::EIO and $^O eq "linux";
Branch: 1 branch
BranchTrueFalseTotal
if $! == &POSIX::EIO and $^O eq "linux" 3 0 3
Condition33%1/3
$! == &POSIX::EIO and $^O eq "linux"
!ll&&!rl&&r
003
MC/DC0%0/2
$! == &POSIX::EIO and $^O eq "linux"
$! == &POSIX::EIO $^O eq "linux"
Truth table33%1/3
A and B
A= $! == &POSIX::EIOB= $^O eq "linux"
ABresult
0X0
100
111
223  
224 0 die "drain(pty): $!";
225 }
226 }
227 # }}}
228 # Read internals {{{
229 # A little something to make all these select loops nicer
230 8 ▶sub _select_loop {
Subroutine _select_loop: called CC=12 SCAR=27.1
231 8 my ($self, $block, $pred) = splice @_, 0, 3;
232  
233 8 ▶ while ($pred->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$pred()) 11 8 19
234 11 my %mask = ('r' => '', 'w' => '', 'x' => '');
235  
236 my $tmo = !$block ? 0 :
237 11 ▶ defined $self->{timeout} ? $self->{timeout} - time : undef;
Branch: 2 branches
BranchTrueFalseTotal
!$block ? : 1 10 11
defined $self->{'timeout'} ? : 0 10 10
238  
239 11 for (@_) {
240 23 ▶ vec($mask{$_->[1]}, fileno($_->[0]), 1) = 1
Branch: 1 branch
BranchTrueFalseTotal
if @$_ < 4 or $_->[3] 23 0 23
Condition33%1/3
@$_ < 4 or $_->[3]
l!l&&r!l&&!r
2300
MC/DC0%0/2
@$_ < 4 or $_->[3]
@$_ < 4 $_->[3]
Truth table33%1/3
A or B
A= @$_ < 4B= $_->[3]
ABresult
1X1
011
000
241 if @$_ < 4 || $_->[3];
242 }
243  
244 11 ▶ return 1 if ($tmo||0)< 0 || !select($mask{r}, $mask{w}, $mask{x}, $tmo);
Branch: 1 branch
BranchTrueFalseTotal
if ($tmo || 0) < 0 or not select($mask{"r"}, $mask{"w"}, $mask{"x"}, $tmo) 0 11 11
Condition40%2/5
($tmo || 0) < 0 or not select($mask{"r"}, $mask{"w"}, $mask{"x"}, $tmo)
l!l&&r!l&&!r
0011
$tmo || 0
l!l
011
MC/DC0%0/2
($tmo || 0) < 0 or not select($mask{"r"}, $mask{"w"}, $mask{"x"}, $tmo)
$tmo not select($mask{"r"}, $mask{"w"}, $mask{"x"}, $tmo)
Truth table33%1/3
A or not(B)
A= $tmoB= not select($mask{"r"}, $mask{"w"}, $mask{"x"}, $tmo)
ABresult
1X1
011
000
245  
246 11 for (@_) {
247 23 ▶ $_->[2]() if vec($mask{$_->[1]}, fileno($_->[0]), 1);
Branch: 1 branch
BranchTrueFalseTotal
if vec $mask{$_->[1]}, fileno $_->[0], 1 16 7 23
248 }
249 }
250 }
251  
252 # We want to return when the slave has processed all input. We have to
253 # break it up into pty-buffer-sized chunks, though.
254 4 ▶sub _process_wait {
Subroutine _process_wait: called CC=1 SCAR=0.0
255 4 my ($self) = shift;
256  
257 14 ▶ $self->_select_loop(1 => sub{ $self->{sent_sync} },
Subroutine __ANON__: called CC=1 SCAR=0.0
258 7 ▶ [ $self->{info_pipe}, r => sub { $self->_handle_info_read() } ],
Subroutine __ANON__: called CC=1 SCAR=0.0
259 4 ▶ [ $self->{pty}, r => sub { $self->_handle_pty_read() } ]);
Subroutine __ANON__: called CC=1 SCAR=0.0
260 }
261  
262 # Send as much data as possible
263 4 ▶sub _process_send {
Subroutine _process_send: called CC=1 SCAR=0.4
264 4 my ($self) = @_;
265  
266 5 ▶ $self->_select_loop(0 => sub{ $self->{write_buffer} ne '' },
Subroutine __ANON__: called CC=1 SCAR=0.0
267 0 ▶ [ $self->{info_pipe}, r => sub { $self->_handle_info_read() } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 ▶ [ $self->{pty}, r => sub { $self->_handle_pty_read() } ],
Subroutine __ANON__: not called CC=1 SCAR=6.9
269 4 ▶ [ $self->{pty}, w => sub { $self->_handle_pty_write() } ]);
Subroutine __ANON__: called CC=1 SCAR=0.0
270 }
271  
272 4 ▶sub _send_sync {
Subroutine _send_sync: called CC=2 SCAR=7.0
273 4 my $self = shift;
274 4 ▶ return if $self->{sent_sync};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sent_sync"} 0 4 4
275 4 syswrite $self->{ctl_pipe}, "s";
276 4 $self->{sent_sync} = 1;
277 }
278 # }}}
279 # I/O operations {{{
280  
281 4 ▶sub recv {
Subroutine recv: called CC=6 SCAR=20.2
Pod documented
282 4 my ($self, $timeout) = @_;
283  
284 4 ▶ if (! $self->is_active) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_active) 0 4 4
285 0 carp "Reading from dead slave";
286 0 return;
287 }
288  
289 4 ▶ $self->{timeout} = defined $timeout ? $timeout + time : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $timeout ? : 0 4 4
290  
291 do {
292 4 $self->_process_send();
293 4 $self->_send_sync();
294 4 ▶ return undef if $self->_process_wait();
Branch: 1 branch
BranchTrueFalseTotal
if $self->_process_wait 0 4 4
295 4 ▶ } while ($self->{write_buffer} ne '' && $self->{active});
Branch: 1 branch
BranchTrueFalseTotal
$self->{"write_buffer"} ne "" and $self->{"active"} and do { do { $self->_process_send; $self->_send_sync; return undef if $self->_process_wait } } 0 4 4
Condition33%1/3
$self->{"write_buffer"} ne "" and $self->{"active"}
!ll&&!rl&&r
400
MC/DC0%0/2
$self->{"write_buffer"} ne "" and $self->{"active"}
$self->{"write_buffer"} ne "" $self->{"active"}
Truth table33%1/3
A and B
A= $self->{"write_buffer"} ne ""B= $self->{"active"}
ABresult
0X0
100
111
296  
297 4 my $t = $self->{read_buffer};
298 4 $self->{read_buffer} = '';
299 4 $t;
300 }
301  
302 1 ▶sub write {
Subroutine write: called CC=2 SCAR=8.4
Pod documented
303 1 my ($self, $text) = @_;
304  
305 1 ▶ if (! $self->is_active) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->is_active) 0 1 1
306 0 carp "Writing to dead slave";
307 0 return;
308 }
309  
310 1 $self->{write_buffer} .= $text;
311 }
312  
313 19 ▶sub is_active {
Subroutine is_active: called CC=1 SCAR=0.0
Pod documented
314 19 my $self = shift;
315  
316 19 return $self->{active};
317 }
318  
319 0 ▶sub _wait_for_inactive {
Subroutine _wait_for_inactive: not called CC=5 SCAR=34.0
320 0 my $self = shift;
321 0 my $targ = shift;
322  
323 0 ▶ $targ = defined $targ ? $targ + time : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $targ ? : 0 0 0
324  
325 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$targ > &IO::Pty::HalfDuplex::Ptyish::time() and $self->is_active and do { do { $self->recv(defined $targ ? $targ - &IO::Pty::HalfDuplex::Ptyish::time() : undef) } } 0 0 0
Condition0%0/3
$targ > &IO::Pty::HalfDuplex::Ptyish::time() and $self->is_active
!ll&&!rl&&r
000
MC/DC0%0/2
$targ > &IO::Pty::HalfDuplex::Ptyish::time() and $self->is_active
$targ > &IO::Pty::HalfDuplex::Ptyish::time() $self->is_active
Truth table0%0/3
A and B
A= $targ > &IO::Pty::HalfDuplex::Ptyish::time()B= $self->is_active
ABresult
0X0
100
111
326 0 ▶ $self->recv(defined $targ ? $targ - time : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $targ ? : 0 0 0
327 } while ($targ > time && $self->is_active);
328  
329 0 !$self->is_active;
330 }
331 # }}}
332 # kill() {{{
333 4 ▶sub kill {
Subroutine kill: called CC=8 SCAR=36.1
Pod documented
334 4 my $self = shift;
335  
336 4 ▶ if (@_ < 2) { @_ = (TERM => 3, KILL => 3); }
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 2) 4 0 4
337  
338 4 ▶ return 1 if !$self->is_active;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_active 4 0 4
339  
340 0 ▶ while (@_ >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) 0 0 0
341 0 my ($sig, $tme) = splice @_, 0, 2;
342
343 kill $sig => -$self->{slave_pgid}
344 0 ▶ or return undef;
Branch: 1 branch
BranchTrueFalseTotal
CORE::kill $sig, -$self->{"slave_pgid"} or return undef 0 0 0
345  
346 0 ▶ $tme = defined $tme ? $tme : $_infinity;
Branch: 1 branch
BranchTrueFalseTotal
defined $tme ? : 0 0 0
347  
348 0 ▶ if ($tme && $self->_wait_for_inactive($tme)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tme and $self->_wait_for_inactive($tme)) 0 0 0
Condition0%0/3
$tme and $self->_wait_for_inactive($tme)
!ll&&!rl&&r
000
MC/DC0%0/2
$tme and $self->_wait_for_inactive($tme)
$tme $self->_wait_for_inactive($tme)
Truth table0%0/3
A and B
A= $tmeB= $self->_wait_for_inactive($tme)
ABresult
0X0
100
111
349 0 return 1;
350 }
351 }
352  
353 0 return 0;
354 }
355 # }}}
356 # close() {{{
357 4 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
358 4 my $self = shift;
359  
360 4 $self->kill;
361 4 close $self->{pty};
362 4 $self->{pty} = undef;
363 }
364 # }}}
365 # documentation tail {{{
366  
367 0 ▶sub _shell {
Subroutine _shell: not called CC=1 SCAR=6.9
368 0 my $class = ref(shift);
369  
370 0 die ($class eq 'IO::Pty::HalfDuplex::Ptyish')
371 ? "You must subclass Ptyish, not use it directly"
372 : "You need to override shell() in Ptyish subclasses";
373 }
374  
375 4 1;
376  
377 __END__
« HalfDuplex/JobControl.pm ↑ Summary HalfDuplex/SysctlPoll.pm »