HalfDuplex/SysctlPoll.pm untested

filter: Statementstmt 0.0% 0 / 41 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 68 CC 7 SCAR 22.9
CC
7
Cov
0%
_shell_loop
20.0
_shell_spawn
12.0
BEGIN
6.0
CRAP 9.9
1 #!/usr/bin/env perl
2 # vim: fdm=marker sw=4 et
3 # Documentation head {{{
4  
5 =head1 NAME
6  
7 IO::Pty::HalfDuplex::SysctlPoll - wait for blocking reads using sysctl
8  
9 =head1 SYNOPSIS
10  
11 IO::Pty::HalfDuplex->new(backend => 'SysctlPoll')
12  
13 =head1 CAVEATS
14  
15 C<IO::Pty::HalfDuplex::SyctlPoll> needs to poll, and will waste a certain
16 amount of CPU time while the child runs.
17  
18 Otherwise it is probably the most robust backend.
19  
20 =head1 BUGS
21  
22 See L<IO::Pty::HalfDuplex>.
23  
24 =head1 COPYRIGHT AND LICENSE
25  
26 Copyright 2008-2009 Stefan O'Rear.
27  
28 This program is free software; you can redistribute it and/or modify it
29 under the same terms as Perl itself.
30  
31 =cut
32  
33 # }}}
34 # header {{{
35 package IO::Pty::HalfDuplex::SysctlPoll;
36  
37 use strict;
38 use warnings;
39 use POSIX '_exit', ':sys_wait_h', 'tcsetpgrp', 'setpgid';
40  
41 use base 'IO::Pty::HalfDuplex::Ptyish';
42  
43 BEGIN {
44 die "XS code for IO::Pty::HalfDuplex::SysctlPoll not built."
45 unless __PACKAGE__->can('_is_waiting');
46 }
47  
48 # }}}
49 # control loop and startup {{{
50 # Wait for, and process, commands
51 sub _shell_loop {
52 my $self = shift;
53  
54 while(1) {
55 my $buf = '';
56 sysread($self->{ctl_pipe}, $buf, 1) > 0 or die "read(ctl): $!";
57  
58 my $lag = 0.05;
59  
60 while (!_is_waiting($self->{slave_pid})) {
61 if (waitpid($self->{slave_pid}, &POSIX::WNOHANG) > 0) {
62 syswrite $self->{info_pipe}, "d" .
63 chr(WIFSIGNALED($?) ? WTERMSIG($?) : 0) .
64 chr(WIFEXITED($?) ? WEXITSTATUS($?) : 0);
65
66 _exit 0;
67 }
68  
69 select undef, undef, undef, ($lag *= 1.1);
70 }
71  
72 syswrite($self->{info_pipe}, "r");
73 }
74 }
75  
76 # This routine is responsible for creating the proper environment for the
77 # slave to run in.
78 sub _shell_spawn {
79 my $self = shift;
80  
81 $self->{slave_pid} = fork;
82  
83 die "fork: $!" unless defined $self->{slave_pid};
84  
85 unless ($self->{slave_pid}) {
86 my $pid = $$;
87 $SIG{TTOU} = 'IGNORE';
88 setpgid($pid, $pid);
89 tcsetpgrp(0, $pid);
90 $SIG{TTOU} = 'DEFAULT';
91  
92 exec(@{$self->{command}});
93 die "exec: $!";
94 }
95  
96 syswrite($self->{info_pipe}, pack('N', $self->{slave_pid}));
97 }
98  
99 sub _shell {
100 my $self = shift;
101 %$self = (
102 %$self,
103 pid => $$,
104 @_
105 );
106  
107 $self->_shell_spawn();
108 $self->_shell_loop();
109 }
110 1;
111 # }}}