| 1 | package DBD::Gofer::Transport::pipeone; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # $Id: pipeone.pm 10087 2007-10-16 12:42:37Z Tim $ | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Copyright (c) 2007, Tim Bunce, Ireland | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # You may distribute under the terms of either the GNU General Public | ||||||||||||||||||||||||||||
| 8 | # License or the Artistic License, as specified in the Perl README file. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 8 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 8 | ▶ | use Fcntl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 8 | ▶ | use IO::Select; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 8 | ▶ | use IPC::Open3 qw(open3); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 8 | ▶ | use Symbol qw(gensym); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 8 | ▶ | use base qw(DBD::Gofer::Transport::Base); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | our $VERSION = "0.010088"; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 8 | __PACKAGE__->mk_accessors(qw( | |||||||||||||||||||||||||||
| 24 | connection_info | ||||||||||||||||||||||||||||
| 25 | go_perl | ||||||||||||||||||||||||||||
| 26 | )); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 336 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 336 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 31 | 336 | ▶ | $args->{go_perl} ||= do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$args->{"go_perl"} ||= do {
$INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X]
}
MC/DC0%0/2
$args->{"go_perl"} ||= do {
$INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X]
}
$args->{"go_perl"}
do {
$INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X]
}
Truth table33%1/3
A ||= B
A= $args->{"go_perl"}B= do {
$INC{"blib.pm"} ? [$^X, "-Mblib"] : [$^X]
}
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | ($INC{"blib.pm"}) ? [ $^X, '-Mblib' ] : [ $^X ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | }; | ||||||||||||||||||||||||||||
| 34 | 336 | ▶ | if (not ref $args->{go_perl}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | # user can override the perl to be used, either with an array ref | ||||||||||||||||||||||||||||
| 36 | # containing the command name and args to use, or with a string | ||||||||||||||||||||||||||||
| 37 | # (ie via the DSN) in which case, to enable args to be passed, | ||||||||||||||||||||||||||||
| 38 | # we split on two or more consecutive spaces (otherwise the path | ||||||||||||||||||||||||||||
| 39 | # to perl couldn't contain a space itself). | ||||||||||||||||||||||||||||
| 40 | 336 | $args->{go_perl} = [ split /\s{2,}/, $args->{go_perl} ]; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 336 | return $self->SUPER::new($args); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # nonblock($fh) puts filehandle into nonblocking mode | ||||||||||||||||||||||||||||
| 47 | 776 | ▶ | sub nonblock { | ||||||||||||||||||||||||||
|
Subroutine
nonblock: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 48 | 776 | my $fh = shift; | |||||||||||||||||||||||||||
| 49 | 776 | ▶ | my $flags = fcntl($fh, F_GETFL, 0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | or croak "Can't get flags for filehandle $fh: $!"; | ||||||||||||||||||||||||||||
| 51 | 776 | ▶ | fcntl($fh, F_SETFL, $flags | O_NONBLOCK) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | or croak "Can't make filehandle $fh nonblocking: $!"; | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 388 | ▶ | sub start_pipe_command { | ||||||||||||||||||||||||||
|
Subroutine
start_pipe_command: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 57 | 388 | my ($self, $cmd) = @_; | |||||||||||||||||||||||||||
| 58 | 388 | ▶ | $cmd = [ $cmd ] unless ref $cmd eq 'ARRAY'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # if it's important that the subprocess uses the same | ||||||||||||||||||||||||||||
| 61 | # (versions of) modules as us then the caller should | ||||||||||||||||||||||||||||
| 62 | # set PERL5LIB itself. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | # limit various forms of insanity, for now | ||||||||||||||||||||||||||||
| 65 | 388 | local $ENV{DBI_TRACE}; # use DBI_GOFER_TRACE instead | |||||||||||||||||||||||||||
| 66 | 388 | local $ENV{DBI_AUTOPROXY}; | |||||||||||||||||||||||||||
| 67 | 388 | local $ENV{DBI_PROFILE}; | |||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 388 | my ($wfh, $rfh, $efh) = (gensym, gensym, gensym); | |||||||||||||||||||||||||||
| 70 | 388 | ▶ | my $pid = open3($wfh, $rfh, $efh, @$cmd) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | or die "error starting @$cmd: $!\n"; | ||||||||||||||||||||||||||||
| 72 | 388 | ▶ | if ($self->trace) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | $self->trace_msg(sprintf("Started pid $pid: @$cmd {fd: w%d r%d e%d, ppid=$$}\n", fileno $wfh, fileno $rfh, fileno $efh),0); | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 388 | nonblock($rfh); | |||||||||||||||||||||||||||
| 76 | 388 | nonblock($efh); | |||||||||||||||||||||||||||
| 77 | 388 | my $ios = IO::Select->new($rfh, $efh); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | return { | ||||||||||||||||||||||||||||
| 80 | 388 | cmd=>$cmd, | |||||||||||||||||||||||||||
| 81 | pid=>$pid, | ||||||||||||||||||||||||||||
| 82 | wfh=>$wfh, rfh=>$rfh, efh=>$efh, | ||||||||||||||||||||||||||||
| 83 | ios=>$ios, | ||||||||||||||||||||||||||||
| 84 | }; | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 0 | ▶ | sub cmd_as_string { | ||||||||||||||||||||||||||
|
Subroutine
cmd_as_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 89 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 90 | # XXX meant to return a properly shell-escaped string suitable for system | ||||||||||||||||||||||||||||
| 91 | # but its only for debugging so that can wait | ||||||||||||||||||||||||||||
| 92 | 0 | my $connection_info = $self->connection_info; | |||||||||||||||||||||||||||
| 93 | 0 | ▶ | return join " ", map { (m/^[-:\w]*$/) ? $_ : "'$_'" } @{$connection_info->{cmd}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 380 | ▶ | sub transmit_request_by_transport { | ||||||||||||||||||||||||||
|
Subroutine
transmit_request_by_transport: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 98 | 380 | my ($self, $request) = @_; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 380 | my $frozen_request = $self->freeze_request($request); | |||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 380 | my $cmd = [ @{$self->go_perl}, qw(-MDBI::Gofer::Transport::pipeone -e run_one_stdio)]; | |||||||||||||||||||||||||||
| 103 | 380 | my $info = $self->start_pipe_command($cmd); | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 380 | my $wfh = delete $info->{wfh}; | |||||||||||||||||||||||||||
| 106 | # send frozen request | ||||||||||||||||||||||||||||
| 107 | 380 | local $\; | |||||||||||||||||||||||||||
| 108 | 380 | ▶ | print $wfh $frozen_request | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | or warn "error writing to @$cmd: $!\n"; | ||||||||||||||||||||||||||||
| 110 | # indicate that there's no more | ||||||||||||||||||||||||||||
| 111 | 380 | ▶ | close $wfh | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | or die "error closing pipe to @$cmd: $!\n"; | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 380 | $self->connection_info( $info ); | |||||||||||||||||||||||||||
| 115 | 380 | return; | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 760 | ▶ | sub read_response_from_fh { | ||||||||||||||||||||||||||
|
Subroutine
read_response_from_fh: called CC=15 SCAR=29.6
Pod
undocumented
| |||||||||||||||||||||||||||||
| 120 | 760 | my ($self, $fh_actions) = @_; | |||||||||||||||||||||||||||
| 121 | 760 | my $trace = $self->trace; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 760 | ▶ | my $info = $self->connection_info || die; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->connection_info || die()
MC/DC0%0/1
$self->connection_info || die()
$self->connection_info
Truth table50%1/2
A
A= $self->connection_info
| |||||||||||||||||||||||||||||
| 124 | 760 | my ($ios) = @{$info}{qw(ios)}; | |||||||||||||||||||||||||||
| 125 | 760 | my $errors = 0; | |||||||||||||||||||||||||||
| 126 | 760 | my $complete; | |||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 760 | ▶ | die "No handles to read response from" unless $ios->count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 760 | ▶ | while ($ios->count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1155 | my @readable = $ios->can_read(); | |||||||||||||||||||||||||||
| 132 | 1155 | for my $fh (@readable) { | |||||||||||||||||||||||||||
| 133 | 1520 | local $_; | |||||||||||||||||||||||||||
| 134 | 1520 | ▶ | my $actions = $fh_actions->{$fh} || die "panic: no action for $fh"; | ||||||||||||||||||||||||||
|
Condition50%1/2
$fh_actions->{$fh} || die("panic: no action for $fh")
MC/DC0%0/1
$fh_actions->{$fh} || die("panic: no action for $fh")
$fh_actions->{$fh}
Truth table50%1/2
A
A= $fh_actions->{$fh}
| |||||||||||||||||||||||||||||
| 135 | 1520 | my $rv = sysread($fh, $_='', 1024*31); # to fit in 32KB slab | |||||||||||||||||||||||||||
| 136 | 1520 | ▶ | unless ($rv) { # error (undef) or end of file (0) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 760 | my $action; | |||||||||||||||||||||||||||
| 138 | 760 | ▶ | unless (defined $rv) { # was an error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 0 | ▶ | $self->trace_msg("error on handle $fh: $!\n") if $trace >= 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $action = $actions->{error} || $actions->{eof}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$actions->{'error'} || $actions->{'eof'}
MC/DC0%0/2
$actions->{'error'} || $actions->{'eof'}
$actions->{'error'}
$actions->{'eof'}
Truth table0%0/3
A || B
A= $actions->{'error'}B= $actions->{'eof'}
| |||||||||||||||||||||||||||||
| 141 | 0 | ++$errors; | |||||||||||||||||||||||||||
| 142 | # XXX an error may be a permenent condition of the handle | ||||||||||||||||||||||||||||
| 143 | # if so we'll loop here - not good | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | else { | ||||||||||||||||||||||||||||
| 146 | 760 | $action = $actions->{eof}; | |||||||||||||||||||||||||||
| 147 | 760 | ▶ | $self->trace_msg("eof on handle $fh\n") if $trace >= 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | 760 | ▶ | if ($action->($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 760 | ▶ | $self->trace_msg("removing $fh from handle set\n") if $trace >= 4; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 760 | $ios->remove($fh); | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | 760 | next; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | # action returns true if the response is now complete | ||||||||||||||||||||||||||||
| 156 | # (we finish all handles | ||||||||||||||||||||||||||||
| 157 | 760 | ▶ | $actions->{read}->($fh) && ++$complete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | 1155 | ▶ | last if $complete; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 760 | return $errors; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 380 | ▶ | sub receive_response_by_transport { | ||||||||||||||||||||||||||
|
Subroutine
receive_response_by_transport: called CC=7 SCAR=23.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 166 | 380 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 380 | ▶ | my $info = $self->connection_info || die; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->connection_info || die()
MC/DC0%0/1
$self->connection_info || die()
$self->connection_info
Truth table50%1/2
A
A= $self->connection_info
| |||||||||||||||||||||||||||||
| 169 | 380 | my ($pid, $rfh, $efh, $ios, $cmd) = @{$info}{qw(pid rfh efh ios cmd)}; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 380 | my $frozen_response; | |||||||||||||||||||||||||||
| 172 | my $stderr_msg; | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | $self->read_response_from_fh( { | ||||||||||||||||||||||||||||
| 175 | $efh => { | ||||||||||||||||||||||||||||
| 176 | 0 | ▶ | error => sub { warn "error reading response stderr: $!"; 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 177 | 380 | ▶ | eof => sub { warn "eof on stderr" if 0; 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | read => sub { $stderr_msg .= $_; 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 179 | }, | ||||||||||||||||||||||||||||
| 180 | $rfh => { | ||||||||||||||||||||||||||||
| 181 | 0 | ▶ | error => sub { warn "error reading response: $!"; 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 182 | 380 | ▶ | eof => sub { warn "eof on stdout" if 0; 1 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 183 | 380 | ▶ | read => sub { $frozen_response .= $_; 0 }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||||||||||
| 185 | 380 | }); | |||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 380 | ▶ | waitpid $info->{pid}, 0 | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | or warn "waitpid: $!"; # XXX do something more useful? | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 380 | ▶ | die ref($self)." command (@$cmd) failed: $stderr_msg" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | if not $frozen_response; # no output on stdout at all | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # XXX need to be able to detect and deal with corruption | ||||||||||||||||||||||||||||
| 194 | 380 | my $response = $self->thaw_response($frozen_response); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 380 | ▶ | if ($stderr_msg) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 197 | # add stderr messages as warnings (for PrintWarn) | ||||||||||||||||||||||||||||
| 198 | 0 | ▶ | $response->add_err(0, $stderr_msg, undef, $self->trace) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
MC/DC0%0/2
$stderr_msg =~ /^Using .*blib/ and join($", @$cmd) =~ /-Mblib/
$stderr_msg =~ /^Using .*blib/
join($", @$cmd) =~ /-Mblib/
Truth table0%0/3
A and B
A= $stderr_msg =~ /^Using .*blib/B= join($", @$cmd) =~ /-Mblib/
| |||||||||||||||||||||||||||||
| 199 | # but ignore warning from old version of blib | ||||||||||||||||||||||||||||
| 200 | unless $stderr_msg =~ /^Using .*blib/ && "@$cmd" =~ /-Mblib/; | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 380 | return $response; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 8 | 1; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | __END__ | ||||||||||||||||||||||||||||