| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package IPC::Simple; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: simple, non-blocking IPC | 
| 3 |  |  |  |  |  |  | $IPC::Simple::VERSION = '0.07'; | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 4 |  |  | 4 |  | 252955 | use strict; | 
|  | 4 |  |  |  |  | 40 |  | 
|  | 4 |  |  |  |  | 100 |  | 
| 6 | 4 |  |  | 4 |  | 19 | use warnings; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 93 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 4 |  |  | 4 |  | 17 | use Carp; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 171 |  | 
| 9 | 4 |  |  | 4 |  | 19 | use AnyEvent qw(); | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 72 |  | 
| 10 | 4 |  |  | 4 |  | 2473 | use AnyEvent::Handle qw(); | 
|  | 4 |  |  |  |  | 66438 |  | 
|  | 4 |  |  |  |  | 109 |  | 
| 11 | 4 |  |  | 4 |  | 1981 | use IPC::Open3 qw(open3); | 
|  | 4 |  |  |  |  | 13560 |  | 
|  | 4 |  |  |  |  | 199 |  | 
| 12 | 4 |  |  | 4 |  | 1675 | use POSIX qw(:sys_wait_h); | 
|  | 4 |  |  |  |  | 21762 |  | 
|  | 4 |  |  |  |  | 20 |  | 
| 13 | 4 |  |  | 4 |  | 5130 | use Symbol qw(gensym); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 162 |  | 
| 14 |  |  |  |  |  |  |  | 
| 15 | 4 |  |  | 4 |  | 1594 | use IPC::Simple::Channel qw(); | 
|  | 4 |  |  |  |  | 8 |  | 
|  | 4 |  |  |  |  | 80 |  | 
| 16 | 4 |  |  | 4 |  | 1355 | use IPC::Simple::Group qw(); | 
|  | 4 |  |  |  |  | 11 |  | 
|  | 4 |  |  |  |  | 71 |  | 
| 17 | 4 |  |  | 4 |  | 1341 | use IPC::Simple::Message; | 
|  | 4 |  |  |  |  | 10 |  | 
|  | 4 |  |  |  |  | 193 |  | 
| 18 |  |  |  |  |  |  |  | 
| 19 | 4 |  |  | 4 |  | 23 | use constant STATE_READY    => 0; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 177 |  | 
| 20 | 4 |  |  | 4 |  | 17 | use constant STATE_RUNNING  => 1; | 
|  | 4 |  |  |  |  | 6 |  | 
|  | 4 |  |  |  |  | 171 |  | 
| 21 | 4 |  |  | 4 |  | 31 | use constant STATE_STOPPING => 2; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 186 |  | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | BEGIN{ | 
| 24 | 4 |  |  | 4 |  | 18 | use base 'Exporter'; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 336 |  | 
| 25 | 4 |  |  | 4 |  | 6337 | our @EXPORT_OK = qw( | 
| 26 |  |  |  |  |  |  | spawn | 
| 27 |  |  |  |  |  |  | process_group | 
| 28 |  |  |  |  |  |  | ); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 32 |  |  |  |  |  |  | # Convenience constructor | 
| 33 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 34 |  |  |  |  |  |  | sub spawn ($;%) { | 
| 35 | 5 |  |  | 5 | 1 | 311 | my ($cmd, @args) = @_; | 
| 36 | 5 |  |  |  |  | 38 | return IPC::Simple->new(cmd => $cmd, @args); | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub process_group { | 
| 40 | 1 |  |  | 1 | 1 | 11 | return IPC::Simple::Group->new(@_); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 44 |  |  |  |  |  |  | # Constructor | 
| 45 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 46 |  |  |  |  |  |  | sub new { | 
| 47 | 5 |  |  | 5 | 0 | 17 | my ($class, %param) = @_; | 
| 48 | 5 | 50 |  |  |  | 20 | my $cmd     = ref $param{cmd} ? $param{cmd} : [ $param{cmd} ]; | 
| 49 | 5 | 50 |  |  |  | 17 | my $eol     = defined $param{eol} ? $param{eol} : "\n"; | 
| 50 | 5 |  | 66 |  |  | 28 | my $name    = $param{name} || "@$cmd"; | 
| 51 | 5 |  |  |  |  | 10 | my $recv_cb = $param{recv_cb}; | 
| 52 | 5 |  |  |  |  | 9 | my $term_cb = $param{term_cb}; | 
| 53 |  |  |  |  |  |  |  | 
| 54 | 5 |  |  |  |  | 51 | bless{ | 
| 55 |  |  |  |  |  |  | name        => $name, | 
| 56 |  |  |  |  |  |  | cmd         => $cmd, | 
| 57 |  |  |  |  |  |  | eol         => $eol, | 
| 58 |  |  |  |  |  |  | recv_cb     => $recv_cb, | 
| 59 |  |  |  |  |  |  | term_cb     => $term_cb, | 
| 60 |  |  |  |  |  |  | run_state   => STATE_READY, | 
| 61 |  |  |  |  |  |  | pid         => undef, | 
| 62 |  |  |  |  |  |  | handle_in   => undef, | 
| 63 |  |  |  |  |  |  | handle_out  => undef, | 
| 64 |  |  |  |  |  |  | handle_err  => undef, | 
| 65 |  |  |  |  |  |  | exit_status => undef, | 
| 66 |  |  |  |  |  |  | exit_code   => undef, | 
| 67 |  |  |  |  |  |  | messages    => undef, | 
| 68 |  |  |  |  |  |  | kill_timer  => undef, | 
| 69 |  |  |  |  |  |  | }, $class; | 
| 70 |  |  |  |  |  |  | } | 
| 71 |  |  |  |  |  |  |  | 
| 72 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 73 |  |  |  |  |  |  | # State accessor and predicates | 
| 74 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 75 |  |  |  |  |  |  | sub run_state { | 
| 76 | 44 |  |  | 44 | 0 | 71 | my $self = shift; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 44 | 100 |  |  |  | 108 | if (@_) { | 
| 79 | 13 |  |  |  |  | 37 | my $new_state = shift; | 
| 80 | 13 |  |  |  |  | 54 | $self->debug('run state changed to %d', $new_state); | 
| 81 | 13 |  |  |  |  | 20 | $self->{run_state} = $new_state; | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 44 |  |  |  |  | 184 | return $self->{run_state}; | 
| 85 |  |  |  |  |  |  | } | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 9 |  |  | 9 | 0 | 23 | sub is_ready    { $_[0]->run_state == STATE_READY } | 
| 88 | 16 |  |  | 16 | 0 | 4839 | sub is_running  { $_[0]->run_state == STATE_RUNNING } | 
| 89 | 6 |  |  | 6 | 0 | 15 | sub is_stopping { $_[0]->run_state == STATE_STOPPING } | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 92 |  |  |  |  |  |  | # Other accessors | 
| 93 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 94 | 8 |  |  | 8 | 1 | 23 | sub name        { $_[0]->{name} } | 
| 95 | 0 |  |  | 0 | 1 | 0 | sub pid         { $_[0]->{pid} } | 
| 96 | 0 |  |  | 0 | 1 | 0 | sub exit_status { $_[0]->{exit_status} } | 
| 97 | 1 |  |  | 1 | 1 | 9 | sub exit_code   { $_[0]->{exit_code} } | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 100 |  |  |  |  |  |  | # Ensure the process is cleaned up when the object is garbage collected. | 
| 101 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 102 |  |  |  |  |  |  | sub DESTROY { | 
| 103 | 0 |  |  | 0 |  | 0 | my $self = shift; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # Localize globals to avoid affecting global state during shutdown | 
| 106 | 0 |  |  |  |  | 0 | local ($., $@, $!, $^E, $?); | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 0 | 0 | 0 |  |  | 0 | if ($self->{pid} && waitpid($self->{pid}, WNOHANG) == 0) { | 
| 109 | 0 |  |  |  |  | 0 | kill 'KILL', $self->{pid}; | 
| 110 | 0 |  |  |  |  | 0 | waitpid $self->{pid}, 0; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | } | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 115 |  |  |  |  |  |  | # Logs debug messages | 
| 116 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 117 |  |  |  |  |  |  | sub debug { | 
| 118 | 43 |  |  | 43 | 0 | 80 | my $self = shift; | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 43 | 50 |  |  |  | 114 | if ($ENV{IPC_SIMPLE_DEBUG}) { | 
| 121 | 0 |  |  |  |  | 0 | my $msg = sprintf shift, @_; | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 0 |  |  |  |  | 0 | my ($pkg, $file, $line) = caller; | 
| 124 | 0 |  | 0 |  |  | 0 | my $pid = $self->{pid} || '(ready)'; | 
| 125 | 0 |  |  |  |  | 0 | my $ts = time; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 0 |  |  |  |  | 0 | warn "<$pkg:$line | $ts | pid:$pid> $msg\n"; | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 132 |  |  |  |  |  |  | # Launch and helpers | 
| 133 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 134 |  |  |  |  |  |  | sub launch { | 
| 135 | 5 |  |  | 5 | 1 | 9496 | my $self = shift; | 
| 136 |  |  |  |  |  |  |  | 
| 137 | 5 | 50 |  |  |  | 16 | if ($self->is_running) { | 
| 138 | 0 |  |  |  |  | 0 | croak 'already running'; | 
| 139 |  |  |  |  |  |  | } | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 5 | 50 |  |  |  | 18 | if ($self->is_stopping) { | 
| 142 | 0 |  |  |  |  | 0 | croak 'process is terminating'; | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 5 |  |  |  |  | 15 | my $cmd = $self->{cmd}; | 
| 146 |  |  |  |  |  |  |  | 
| 147 | 5 |  |  |  |  | 28 | $self->debug('launching: %s', "@$cmd"); | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 5 | 50 |  |  |  | 24 | my $pid = open3(my $in, my $out, my $err = gensym, @$cmd) | 
| 150 |  |  |  |  |  |  | or croak $!; | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 5 |  |  |  |  | 17761 | $self->debug('process launched with pid %d', $pid); | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 5 |  |  |  |  | 71 | $self->run_state(STATE_RUNNING); | 
| 155 |  |  |  |  |  |  |  | 
| 156 | 5 |  |  |  |  | 18 | $self->{exit_status} = undef; | 
| 157 | 5 |  |  |  |  | 23 | $self->{exit_code}   = undef; | 
| 158 | 5 |  |  |  |  | 27 | $self->{kill_timer}  = undef; | 
| 159 | 5 |  |  |  |  | 21 | $self->{pid}         = $pid; | 
| 160 | 5 |  |  |  |  | 53 | $self->{handle_err}  = $self->_build_input_handle($err, IPC_STDERR); | 
| 161 | 5 |  |  |  |  | 98 | $self->{handle_out}  = $self->_build_input_handle($out, IPC_STDOUT); | 
| 162 | 5 |  |  |  |  | 19 | $self->{handle_in}   = $self->_build_output_handle($in); | 
| 163 | 5 |  |  |  |  | 128 | $self->{messages}    = IPC::Simple::Channel->new; | 
| 164 |  |  |  |  |  |  |  | 
| 165 | 5 |  |  |  |  | 98 | return 1; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | sub _build_output_handle { | 
| 169 | 5 |  |  | 5 |  | 83 | my ($self, $fh) = @_; | 
| 170 |  |  |  |  |  |  |  | 
| 171 |  |  |  |  |  |  | # set non-blocking | 
| 172 | 5 |  |  |  |  | 26 | AnyEvent::fh_unblock($fh); | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | my $handle = AnyEvent::Handle->new( | 
| 175 |  |  |  |  |  |  | fh => $fh, | 
| 176 | 0 |  |  | 0 |  | 0 | on_error => sub{ $self->_on_error(IPC_STDIN, @_) }, | 
| 177 | 5 |  |  |  |  | 147 | ); | 
| 178 |  |  |  |  |  |  |  | 
| 179 | 5 |  |  |  |  | 349 | return $handle; | 
| 180 |  |  |  |  |  |  | } | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | sub _build_input_handle { | 
| 183 | 10 |  |  | 10 |  | 79 | my ($self, $fh, $type) = @_; | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | # set non-blocking | 
| 186 | 10 |  |  |  |  | 123 | AnyEvent::fh_unblock($fh); | 
| 187 |  |  |  |  |  |  |  | 
| 188 |  |  |  |  |  |  | my $handle = AnyEvent::Handle->new( | 
| 189 |  |  |  |  |  |  | fh       => $fh, | 
| 190 | 2 |  |  | 2 |  | 173 | on_eof   => sub{ $self->terminate }, | 
| 191 | 2 |  |  | 2 |  | 208 | on_error => sub{ $self->_on_error($type, @_) }, | 
| 192 | 0 |  |  | 0 |  | 0 | on_read  => sub{ $self->_on_read($type, @_) }, | 
| 193 | 10 |  |  |  |  | 467 | ); | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | # push an initial read to prime the queue | 
| 196 | 10 |  |  |  |  | 7793 | $self->_push_read($handle, $type); | 
| 197 |  |  |  |  |  |  |  | 
| 198 | 10 |  |  |  |  | 897 | return $handle; | 
| 199 |  |  |  |  |  |  | } | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | sub _on_error { | 
| 202 | 2 |  |  | 2 |  | 5 | my ($self, $type, $handle, $fatal, $msg) = @_; | 
| 203 | 2 |  |  |  |  | 13 | $self->_queue_message(IPC_ERROR, $msg); | 
| 204 |  |  |  |  |  |  |  | 
| 205 | 2 | 50 |  |  |  | 4 | if ($fatal) { | 
| 206 | 2 |  |  |  |  | 6 | $self->terminate; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  | } | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | sub _on_exit { | 
| 211 | 3 |  |  | 3 |  | 9 | my ($self, $status) = @_; | 
| 212 | 3 |  |  |  |  | 18 | undef $self->{kill_timer}; | 
| 213 | 3 |  |  |  |  | 22 | $self->run_state(STATE_READY); | 
| 214 | 3 |  | 100 |  |  | 17 | $self->{exit_status} = $status || 0; | 
| 215 | 3 |  |  |  |  | 9 | $self->{exit_code} = $self->{exit_status} >> 8; | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | $self->debug('child (pid %s) exited with status %d (exit code: %d)', | 
| 218 |  |  |  |  |  |  | $self->{pid} || '(no pid)', | 
| 219 |  |  |  |  |  |  | $self->{exit_status}, | 
| 220 |  |  |  |  |  |  | $self->{exit_code}, | 
| 221 | 3 |  | 50 |  |  | 15 | ); | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | # May not be set yet if launch fails early enough | 
| 224 | 3 | 50 |  |  |  | 29 | if ($self->{messages}) { | 
| 225 | 3 |  |  |  |  | 17 | $self->{messages}->shutdown; | 
| 226 |  |  |  |  |  |  | } | 
| 227 |  |  |  |  |  |  | } | 
| 228 |  |  |  |  |  |  |  | 
| 229 |  |  |  |  |  |  | sub _on_read { | 
| 230 | 0 |  |  | 0 |  | 0 | my ($self, $type, $handle) = @_; | 
| 231 | 0 |  |  |  |  | 0 | $self->debug('read event type=%s', $type); | 
| 232 | 0 |  |  |  |  | 0 | $self->_push_read($handle, $type); | 
| 233 |  |  |  |  |  |  | } | 
| 234 |  |  |  |  |  |  |  | 
| 235 |  |  |  |  |  |  | sub _push_read { | 
| 236 | 10 |  |  | 10 |  | 27 | my ($self, $handle, $type) = @_; | 
| 237 |  |  |  |  |  |  | $handle->push_read(line => $self->{eol}, sub{ | 
| 238 | 4 |  |  | 4 |  | 606 | my ($handle, $line) = @_; | 
| 239 | 4 |  |  |  |  | 13 | chomp $line; | 
| 240 | 4 |  |  |  |  | 19 | $self->_queue_message($type, $line); | 
| 241 | 10 |  |  |  |  | 153 | }); | 
| 242 |  |  |  |  |  |  | } | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | sub _queue_message { | 
| 245 | 6 |  |  | 6 |  | 15 | my ($self, $type, $msg) = @_; | 
| 246 | 6 |  |  |  |  | 19 | $self->debug('buffered type=%s, msg="%s"', $type, $msg); | 
| 247 |  |  |  |  |  |  |  | 
| 248 | 6 |  |  |  |  | 46 | my $message = IPC::Simple::Message->new( | 
| 249 |  |  |  |  |  |  | source  => $self, | 
| 250 |  |  |  |  |  |  | type    => $type, | 
| 251 |  |  |  |  |  |  | message => $msg, | 
| 252 |  |  |  |  |  |  | ); | 
| 253 |  |  |  |  |  |  |  | 
| 254 | 6 | 100 |  |  |  | 16 | if ($self->{recv_cb}) { | 
| 255 | 4 |  |  |  |  | 14 | $self->{recv_cb}->($message); | 
| 256 |  |  |  |  |  |  | } else { | 
| 257 | 2 |  |  |  |  | 7 | $self->{messages}->put($message); | 
| 258 |  |  |  |  |  |  | } | 
| 259 |  |  |  |  |  |  | } | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 262 |  |  |  |  |  |  | # Send a signal to the process | 
| 263 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 264 |  |  |  |  |  |  | sub signal { | 
| 265 | 5 |  |  | 5 | 1 | 30 | my ($self, $signal) = @_; | 
| 266 | 5 | 50 |  |  |  | 18 | if ($self->{pid}) { | 
| 267 | 5 |  |  |  |  | 18 | $self->debug('sending %s to pid %d', $signal, $self->{pid}); | 
| 268 | 5 |  |  |  |  | 288 | kill $signal, $self->{pid}; | 
| 269 |  |  |  |  |  |  | } | 
| 270 |  |  |  |  |  |  | } | 
| 271 |  |  |  |  |  |  |  | 
| 272 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 273 |  |  |  |  |  |  | # Stopping the process and waiting on it to complete | 
| 274 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 275 |  |  |  |  |  |  | sub terminate { | 
| 276 | 9 |  |  | 9 | 1 | 25 | my $self = shift; | 
| 277 | 9 |  |  |  |  | 17 | my $timeout = shift; | 
| 278 |  |  |  |  |  |  |  | 
| 279 | 9 | 100 |  |  |  | 22 | if ($self->is_running) { | 
| 280 | 5 |  |  |  |  | 19 | $self->signal('TERM'); | 
| 281 | 5 |  |  |  |  | 25 | $self->run_state(STATE_STOPPING); | 
| 282 |  |  |  |  |  |  |  | 
| 283 | 5 |  |  |  |  | 24 | $self->{handle_in}->push_shutdown; | 
| 284 | 5 |  |  |  |  | 187 | $self->{handle_out}->push_shutdown; | 
| 285 | 5 |  |  |  |  | 90 | $self->{handle_err}->push_shutdown; | 
| 286 |  |  |  |  |  |  |  | 
| 287 | 5 | 100 |  |  |  | 161 | if (defined $timeout) { | 
| 288 |  |  |  |  |  |  | $self->{kill_timer} = AnyEvent->timer( | 
| 289 |  |  |  |  |  |  | after => $timeout, | 
| 290 |  |  |  |  |  |  | cb => sub{ | 
| 291 | 0 |  |  | 0 |  | 0 | $self->signal('KILL'); | 
| 292 | 0 |  |  |  |  | 0 | undef $self->{kill_timer}; | 
| 293 |  |  |  |  |  |  | }, | 
| 294 | 3 |  |  |  |  | 79 | ); | 
| 295 |  |  |  |  |  |  | } | 
| 296 |  |  |  |  |  |  |  | 
| 297 | 5 | 100 |  |  |  | 135 | if ($self->{term_cb}) { | 
| 298 | 2 |  |  |  |  | 6 | $self->{term_cb}->($self); | 
| 299 |  |  |  |  |  |  | } | 
| 300 |  |  |  |  |  |  | } | 
| 301 |  |  |  |  |  |  | } | 
| 302 |  |  |  |  |  |  |  | 
| 303 |  |  |  |  |  |  | sub join { | 
| 304 | 3 |  |  | 3 | 1 | 7 | my $self = shift; | 
| 305 |  |  |  |  |  |  |  | 
| 306 | 3 | 50 |  |  |  | 9 | return if $self->is_ready; | 
| 307 |  |  |  |  |  |  |  | 
| 308 | 3 |  |  |  |  | 12 | $self->debug('waiting for process to exit, pid %d', $self->{pid}); | 
| 309 |  |  |  |  |  |  |  | 
| 310 | 3 |  |  |  |  | 57 | my $done = AnyEvent->condvar; | 
| 311 |  |  |  |  |  |  |  | 
| 312 | 3 |  |  |  |  | 527 | my $timer; $timer = AnyEvent->timer( | 
| 313 |  |  |  |  |  |  | after => 0, | 
| 314 |  |  |  |  |  |  | interval => 0.01, | 
| 315 |  |  |  |  |  |  | cb => sub{ | 
| 316 |  |  |  |  |  |  | # non-blocking waitpid returns 0 if the pid is still alive | 
| 317 | 3 | 50 |  | 3 |  | 321 | if (waitpid($self->{pid}, WNOHANG) != 0) { | 
| 318 | 3 |  |  |  |  | 10 | my $status = $?; | 
| 319 |  |  |  |  |  |  |  | 
| 320 |  |  |  |  |  |  | # another waiter might have already called _on_exit | 
| 321 | 3 | 50 |  |  |  | 10 | unless ($self->is_ready) { | 
| 322 | 3 |  |  |  |  | 17 | $self->_on_exit($?); | 
| 323 |  |  |  |  |  |  | } | 
| 324 |  |  |  |  |  |  |  | 
| 325 | 3 |  |  |  |  | 41 | $done->send; | 
| 326 |  |  |  |  |  |  | } | 
| 327 |  |  |  |  |  |  | }, | 
| 328 | 3 |  |  |  |  | 40 | ); | 
| 329 |  |  |  |  |  |  |  | 
| 330 | 3 |  |  |  |  | 117 | $done->recv; | 
| 331 |  |  |  |  |  |  | } | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 334 |  |  |  |  |  |  | # Messages | 
| 335 |  |  |  |  |  |  | #------------------------------------------------------------------------------- | 
| 336 |  |  |  |  |  |  | sub send { | 
| 337 | 1 |  |  | 1 | 1 | 4 | my ($self, $msg) = @_; | 
| 338 | 1 |  |  |  |  | 5 | $self->debug('sending "%s"', $msg); | 
| 339 | 1 |  |  |  |  | 10 | $self->{handle_in}->push_write($msg . $self->{eol}); | 
| 340 | 1 |  |  |  |  | 101 | 1; | 
| 341 |  |  |  |  |  |  | } | 
| 342 |  |  |  |  |  |  |  | 
| 343 |  |  |  |  |  |  | sub recv { | 
| 344 | 2 |  |  | 2 | 1 | 54 | my ($self, $type) = @_; | 
| 345 | 2 |  |  |  |  | 7 | $self->debug('waiting on message from pid %d', $self->{pid}); | 
| 346 | 2 |  |  |  |  | 9 | $self->{messages}->get; | 
| 347 |  |  |  |  |  |  | } | 
| 348 |  |  |  |  |  |  |  | 
| 349 |  |  |  |  |  |  | 1; | 
| 350 |  |  |  |  |  |  |  | 
| 351 |  |  |  |  |  |  | __END__ |