| 1 | package IPC::Run::Win32IO; | ||
| 2 | |||
| 3 | =pod | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | IPC::Run::Win32IO - helper routines for IPC::Run on Win32 platforms. | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | use IPC::Run::Win32IO; # Exports all by default | ||
| 12 | |||
| 13 | =head1 DESCRIPTION | ||
| 14 | |||
| 15 | IPC::Run needs to use sockets to redirect subprocess I/O so that the select() | ||
| 16 | loop will work on Win32. This seems to only work on WinNT and Win2K at this | ||
| 17 | time, not sure if it will ever work on Win95 or Win98. If you have experience | ||
| 18 | in this area, please contact me at barries@slaysys.com, thanks!. | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | A specialized IO class used on Win32. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | use Carp; | ||
| 29 | use IO::Handle; | ||
| 30 | use Socket; | ||
| 31 | require POSIX; | ||
| 32 | |||
| 33 | use vars qw{$VERSION}; | ||
| 34 | |||
| 35 | BEGIN { | ||
| 36 | $VERSION = '20260402.0'; | ||
| 37 | } | ||
| 38 | |||
| 39 | use Socket qw( IPPROTO_TCP TCP_NODELAY ); | ||
| 40 | use Symbol; | ||
| 41 | use Text::ParseWords; | ||
| 42 | use Win32::Process; | ||
| 43 | use IPC::Run::Debug qw( :default _debugging_level ); | ||
| 44 | use IPC::Run::Win32Helper qw( _inherit _dont_inherit ); | ||
| 45 | use Fcntl qw( O_TEXT O_RDONLY ); | ||
| 46 | |||
| 47 | use base qw( IPC::Run::IO ); | ||
| 48 | my @cleanup_fields; | ||
| 49 | |||
| 50 | BEGIN { | ||
| 51 | ## These fields will be set to undef in _cleanup to close | ||
| 52 | ## the handles. | ||
| 53 | @cleanup_fields = ( | ||
| 54 | 'SEND_THROUGH_TEMP_FILE', ## Set by WinHelper::optimize() | ||
| 55 | 'RECV_THROUGH_TEMP_FILE', ## Set by WinHelper::optimize() | ||
| 56 | 'TEMP_FILE_NAME', ## The name of the temp file, needed for | ||
| 57 | ## error reporting / debugging only. | ||
| 58 | |||
| 59 | 'PARENT_HANDLE', ## The handle of the socket for the parent | ||
| 60 | 'PUMP_SOCKET_HANDLE', ## The socket handle for the pump | ||
| 61 | 'PUMP_PIPE_HANDLE', ## The anon pipe handle for the pump | ||
| 62 | 'CHILD_HANDLE', ## The anon pipe handle for the child | ||
| 63 | |||
| 64 | 'TEMP_FILE_HANDLE', ## The Win32 filehandle for the temp file | ||
| 65 | ); | ||
| 66 | } | ||
| 67 | |||
| 68 | ## REMOVE OSFHandleOpen | ||
| 69 | use Win32API::File qw( | ||
| 70 | GetOsFHandle | ||
| 71 | OsFHandleOpenFd | ||
| 72 | OsFHandleOpen | ||
| 73 | FdGetOsFHandle | ||
| 74 | SetHandleInformation | ||
| 75 | SetFilePointer | ||
| 76 | HANDLE_FLAG_INHERIT | ||
| 77 | |||
| 78 | createFile | ||
| 79 | WriteFile | ||
| 80 | ReadFile | ||
| 81 | CloseHandle | ||
| 82 | |||
| 83 | FILE_ATTRIBUTE_TEMPORARY | ||
| 84 | FILE_FLAG_DELETE_ON_CLOSE | ||
| 85 | FILE_FLAG_WRITE_THROUGH | ||
| 86 | |||
| 87 | FILE_BEGIN | ||
| 88 | ); | ||
| 89 | |||
| 90 | # FILE_ATTRIBUTE_HIDDEN | ||
| 91 | # FILE_ATTRIBUTE_SYSTEM | ||
| 92 | |||
| 93 | BEGIN { | ||
| 94 | ## Force AUTOLOADED constants to be, well, constant by getting them | ||
| 95 | ## to AUTOLOAD before compilation continues. Sigh. | ||
| 96 | () = ( | ||
| 97 | SOL_SOCKET, | ||
| 98 | SO_REUSEADDR, | ||
| 99 | IPPROTO_TCP, | ||
| 100 | TCP_NODELAY, | ||
| 101 | HANDLE_FLAG_INHERIT, | ||
| 102 | ); | ||
| 103 | } | ||
| 104 | |||
| 105 | use constant temp_file_flags => ( FILE_ATTRIBUTE_TEMPORARY() | FILE_FLAG_DELETE_ON_CLOSE() | FILE_FLAG_WRITE_THROUGH() ); | ||
| 106 | |||
| 107 | # FILE_ATTRIBUTE_HIDDEN() | | ||
| 108 | # FILE_ATTRIBUTE_SYSTEM() | | ||
| 109 | my $tmp_file_counter; | ||
| 110 | my $tmp_dir; | ||
| 111 | |||
| 112 | sub _cleanup { | ||
| 113 | my IPC::Run::Win32IO $self = shift; | ||
| 114 | my ($harness) = @_; | ||
| 115 | |||
| 116 | $self->_recv_through_temp_file($harness) | ||
| 117 | if $self->{RECV_THROUGH_TEMP_FILE}; | ||
| 118 | |||
| 119 | CloseHandle( $self->{TEMP_FILE_HANDLE} ) | ||
| 120 | if defined $self->{TEMP_FILE_HANDLE}; | ||
| 121 | |||
| 122 | ## Explicitly close all socket/pipe handles before clearing the fields. | ||
| 123 | ## If these gensym handles are merely undef'd, they may survive until | ||
| 124 | ## global destruction when the underlying fd is already invalid, causing | ||
| 125 | ## "unable to close filehandle GEN## properly: Bad file descriptor | ||
| 126 | ## during global destruction" warnings. (GH#237) | ||
| 127 | for my $field (qw( CHILD_HANDLE PUMP_SOCKET_HANDLE PUMP_PIPE_HANDLE )) { | ||
| 128 | close( $self->{$field} ) | ||
| 129 | if defined $self->{$field} && defined fileno( $self->{$field} ); | ||
| 130 | } | ||
| 131 | |||
| 132 | $self->{$_} = undef for @cleanup_fields; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub _create_temp_file { | ||
| 136 | my IPC::Run::Win32IO $self = shift; | ||
| 137 | |||
| 138 | ## Create a hidden temp file that Win32 will delete when we close | ||
| 139 | ## it. | ||
| 140 | unless ( defined $tmp_dir ) { | ||
| 141 | $tmp_dir = File::Spec->catdir( File::Spec->tmpdir, "IPC-Run.tmp" ); | ||
| 142 | |||
| 143 | ## Trust in the user's umask. | ||
| 144 | ## This could possibly be a security hole, perhaps | ||
| 145 | ## we should offer an option. Hmmmm, really, people coding | ||
| 146 | ## security conscious apps should audit this code and | ||
| 147 | ## tell me how to make it better. Nice cop-out :). | ||
| 148 | unless ( -d $tmp_dir ) { | ||
| 149 | mkdir $tmp_dir or croak "$!: $tmp_dir"; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | $self->{TEMP_FILE_NAME} = File::Spec->catfile( | ||
| 154 | ## File name is designed for easy sorting and not conflicting | ||
| 155 | ## with other processes. This should allow us to use "t"runcate | ||
| 156 | ## access in CreateFile in case something left some droppings | ||
| 157 | ## around (which should never happen because we specify | ||
| 158 | ## FLAG_DELETE_ON_CLOSE. | ||
| 159 | ## heh, belt and suspenders are better than bug reports; God forbid | ||
| 160 | ## that NT should ever crash before a temp file gets deleted! | ||
| 161 | $tmp_dir, sprintf "Win32io-%06d-%08d", $$, $tmp_file_counter++ | ||
| 162 | ); | ||
| 163 | |||
| 164 | $self->{TEMP_FILE_HANDLE} = createFile( | ||
| 165 | $self->{TEMP_FILE_NAME}, | ||
| 166 | "trw", ## new, truncate, read, write | ||
| 167 | { | ||
| 168 | Flags => temp_file_flags, | ||
| 169 | }, | ||
| 170 | ) or croak "Can't create temporary file, $self->{TEMP_FILE_NAME}: $^E"; | ||
| 171 | |||
| 172 | $self->{TFD} = OsFHandleOpenFd $self->{TEMP_FILE_HANDLE}, 0; | ||
| 173 | $self->{FD} = undef; | ||
| 174 | |||
| 175 | _debug | ||
| 176 | "Win32 Optimizer: temp file (", | ||
| 177 | $self->{KFD}, | ||
| 178 | $self->{TYPE}, | ||
| 179 | $self->{TFD}, | ||
| 180 | ", fh ", | ||
| 181 | $self->{TEMP_FILE_HANDLE}, | ||
| 182 | "): ", | ||
| 183 | $self->{TEMP_FILE_NAME} | ||
| 184 | if _debugging_details; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _reset_temp_file_pointer { | ||
| 188 | my $self = shift; | ||
| 189 | SetFilePointer( $self->{TEMP_FILE_HANDLE}, 0, 0, FILE_BEGIN ) | ||
| 190 | or confess "$^E seeking on (fd $self->{TFD}) $self->{TEMP_FILE_NAME} for kid's fd $self->{KFD}"; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _send_through_temp_file { | ||
| 194 | my IPC::Run::Win32IO $self = shift; | ||
| 195 | |||
| 196 | _debug "Win32 optimizer: optimizing " . " $self->{KFD} $self->{TYPE} temp file instead of ", | ||
| 197 | ref $self->{SOURCE} || $self->{SOURCE} | ||
| 198 | if _debugging_details; | ||
| 199 | |||
| 200 | $self->_create_temp_file; | ||
| 201 | |||
| 202 | if ( defined ${ $self->{SOURCE} } ) { | ||
| 203 | my $bytes_written = 0; | ||
| 204 | my $data_ref; | ||
| 205 | if ( $self->binmode ) { | ||
| 206 | $data_ref = $self->{SOURCE}; | ||
| 207 | } | ||
| 208 | else { | ||
| 209 | my $data = ${ $self->{SOURCE} }; # Ugh, a copy. | ||
| 210 | $data =~ s/(?<!\r)\n/\r\n/g; | ||
| 211 | $data_ref = \$data; | ||
| 212 | } | ||
| 213 | |||
| 214 | WriteFile( | ||
| 215 | $self->{TEMP_FILE_HANDLE}, | ||
| 216 | $$data_ref, | ||
| 217 | 0, ## Write entire buffer | ||
| 218 | $bytes_written, | ||
| 219 | [], ## Not overlapped. | ||
| 220 | ) or croak "$^E writing $self->{TEMP_FILE_NAME} for kid to read on fd $self->{KFD}"; | ||
| 221 | _debug "Win32 optimizer: wrote $bytes_written to temp file $self->{TEMP_FILE_NAME}" | ||
| 222 | if _debugging_data; | ||
| 223 | |||
| 224 | $self->_reset_temp_file_pointer; | ||
| 225 | |||
| 226 | } | ||
| 227 | |||
| 228 | _debug "Win32 optimizer: kid to read $self->{KFD} from temp file on $self->{TFD}" | ||
| 229 | if _debugging_details; | ||
| 230 | } | ||
| 231 | |||
| 232 | sub _init_recv_through_temp_file { | ||
| 233 | my IPC::Run::Win32IO $self = shift; | ||
| 234 | |||
| 235 | $self->_create_temp_file; | ||
| 236 | } | ||
| 237 | |||
| 238 | ## TODO: Use the Win32 API in the select loop to see if the file has grown | ||
| 239 | ## and read it incrementally if it has. | ||
| 240 | sub _recv_through_temp_file { | ||
| 241 | my IPC::Run::Win32IO $self = shift; | ||
| 242 | |||
| 243 | ## This next line kicks in if the run() never got to initting things | ||
| 244 | ## and needs to clean up. | ||
| 245 | return undef unless defined $self->{TEMP_FILE_HANDLE}; | ||
| 246 | |||
| 247 | push @{ $self->{FILTERS} }, sub { | ||
| 248 | my ( undef, $out_ref ) = @_; | ||
| 249 | |||
| 250 | return undef unless defined $self->{TEMP_FILE_HANDLE}; | ||
| 251 | |||
| 252 | my $r; | ||
| 253 | my $s; | ||
| 254 | ReadFile( | ||
| 255 | $self->{TEMP_FILE_HANDLE}, | ||
| 256 | $s, | ||
| 257 | 999_999, ## Hmmm, should read the size. | ||
| 258 | $r, | ||
| 259 | [] | ||
| 260 | ) or croak "$^E reading from $self->{TEMP_FILE_NAME}"; | ||
| 261 | |||
| 262 | _debug "ReadFile( $self->{TFD} ) = $r chars '$s'" if _debugging_data; | ||
| 263 | |||
| 264 | return undef unless $r; | ||
| 265 | |||
| 266 | $s =~ s/\r\n/\n/g unless $self->binmode; | ||
| 267 | |||
| 268 | my $pos = pos $$out_ref; | ||
| 269 | $$out_ref .= $s; | ||
| 270 | pos($out_ref) = $pos; | ||
| 271 | return 1; | ||
| 272 | }; | ||
| 273 | |||
| 274 | my ($harness) = @_; | ||
| 275 | |||
| 276 | $self->_reset_temp_file_pointer; | ||
| 277 | |||
| 278 | 1 while $self->_do_filters($harness); | ||
| 279 | |||
| 280 | pop @{ $self->{FILTERS} }; | ||
| 281 | |||
| 282 | IPC::Run::_close( $self->{TFD} ); | ||
| 283 | } | ||
| 284 | |||
| 285 | =head1 SUBROUTINES | ||
| 286 | |||
| 287 | =over | ||
| 288 | |||
| 289 | =item poll | ||
| 290 | |||
| 291 | Windows version of IPC::Run::IP::poll. | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | =cut | ||
| 296 | |||
| 297 | sub poll { | ||
| 298 | my IPC::Run::Win32IO $self = shift; | ||
| 299 | |||
| 300 | return if $self->{SEND_THROUGH_TEMP_FILE} || $self->{RECV_THROUGH_TEMP_FILE}; | ||
| 301 | |||
| 302 | return $self->SUPER::poll(@_); | ||
| 303 | } | ||
| 304 | |||
| 305 | ## When threaded Perls get good enough, we should use threads here. | ||
| 306 | ## The problem with threaded perls is that they dup() all sorts of | ||
| 307 | ## filehandles and fds and don't allow sufficient control over | ||
| 308 | ## closing off the ones we don't want. | ||
| 309 | |||
| 310 | sub _spawn_pumper { | ||
| 311 | my ( $stdin, $stdout, $debug_fd, $binmode, $child_label, @opts ) = @_; | ||
| 312 | my ( $stdin_fd, $stdout_fd ) = ( fileno $stdin, fileno $stdout ); | ||
| 313 | |||
| 314 | _debug "pumper stdin = ", $stdin_fd if _debugging_details; | ||
| 315 | _debug "pumper stdout = ", $stdout_fd if _debugging_details; | ||
| 316 | _inherit $stdin_fd, $stdout_fd, $debug_fd; | ||
| 317 | my @I_options = map qq{"-I$_"}, @INC; | ||
| 318 | |||
| 319 | my $cmd_line = join( | ||
| 320 | " ", | ||
| 321 | qq{"$^X"}, | ||
| 322 | @I_options, | ||
| 323 | qw(-MIPC::Run::Win32Pump -e 1 ), | ||
| 324 | ## I'm using this clunky way of passing filehandles to the child process | ||
| 325 | ## in order to avoid some kind of premature closure of filehandles | ||
| 326 | ## problem I was having with VCP's test suite when passing them | ||
| 327 | ## via CreateProcess. All of the ## REMOVE code is stuff I'd like | ||
| 328 | ## to be rid of and the ## ADD code is what I'd like to use. | ||
| 329 | FdGetOsFHandle($stdin_fd), ## REMOVE | ||
| 330 | FdGetOsFHandle($stdout_fd), ## REMOVE | ||
| 331 | FdGetOsFHandle($debug_fd), ## REMOVE | ||
| 332 | $binmode ? 1 : 0, | ||
| 333 | $$, $^T, _debugging_level, qq{"$child_label"}, | ||
| 334 | @opts | ||
| 335 | ); | ||
| 336 | |||
| 337 | # open SAVEIN, "<&STDIN" or croak "$! saving STDIN"; #### ADD | ||
| 338 | # open SAVEOUT, ">&STDOUT" or croak "$! saving STDOUT"; #### ADD | ||
| 339 | # open SAVEERR, ">&STDERR" or croak "$! saving STDERR"; #### ADD | ||
| 340 | # _dont_inherit \*SAVEIN; #### ADD | ||
| 341 | # _dont_inherit \*SAVEOUT; #### ADD | ||
| 342 | # _dont_inherit \*SAVEERR; #### ADD | ||
| 343 | # open STDIN, "<&$stdin_fd" or croak "$! dup2()ing $stdin_fd (pumper's STDIN)"; #### ADD | ||
| 344 | # open STDOUT, ">&$stdout_fd" or croak "$! dup2()ing $stdout_fd (pumper's STDOUT)"; #### ADD | ||
| 345 | # open STDERR, ">&$debug_fd" or croak "$! dup2()ing $debug_fd (pumper's STDERR/debug_fd)"; #### ADD | ||
| 346 | |||
| 347 | _debug "pump cmd line: ", $cmd_line if _debugging_details; | ||
| 348 | |||
| 349 | my $process; | ||
| 350 | Win32::Process::Create( | ||
| 351 | $process, | ||
| 352 | $^X, | ||
| 353 | $cmd_line, | ||
| 354 | 1, ## Inherit handles | ||
| 355 | NORMAL_PRIORITY_CLASS, | ||
| 356 | ".", | ||
| 357 | ) or croak "$!: Win32::Process::Create()"; | ||
| 358 | |||
| 359 | # open STDIN, "<&SAVEIN" or croak "$! restoring STDIN"; #### ADD | ||
| 360 | # open STDOUT, ">&SAVEOUT" or croak "$! restoring STDOUT"; #### ADD | ||
| 361 | # open STDERR, ">&SAVEERR" or croak "$! restoring STDERR"; #### ADD | ||
| 362 | # close SAVEIN or croak "$! closing SAVEIN"; #### ADD | ||
| 363 | # close SAVEOUT or croak "$! closing SAVEOUT"; #### ADD | ||
| 364 | # close SAVEERR or croak "$! closing SAVEERR"; #### ADD | ||
| 365 | |||
| 366 | # In case of a sleep right here, need the IPC::Run::_close() treatment. | ||
| 367 | IPC::Run::_close fileno($stdin); | ||
| 368 | close $stdin; | ||
| 369 | IPC::Run::_close fileno($stdout); | ||
| 370 | close $stdout; | ||
| 371 | |||
| 372 | # Don't close $debug_fd, we need it, as do other pumpers. | ||
| 373 | |||
| 374 | # Pause a moment to allow the child to get up and running and emit | ||
| 375 | # debug messages. This does not always work. | ||
| 376 | # select undef, undef, undef, 1 if _debugging_details; | ||
| 377 | |||
| 378 | _debug "_spawn_pumper pid = ", $process->GetProcessID | ||
| 379 | if _debugging_data; | ||
| 380 | } | ||
| 381 | |||
| 382 | my $loopback = inet_aton "127.0.0.1"; | ||
| 383 | my $tcp_proto = getprotobyname('tcp'); | ||
| 384 | croak "$!: getprotobyname('tcp')" unless defined $tcp_proto; | ||
| 385 | |||
| 386 | sub _socket { | ||
| 387 | my ($server) = @_; | ||
| 388 | $server ||= gensym; | ||
| 389 | my $client = gensym; | ||
| 390 | |||
| 391 | my $listener = gensym; | ||
| 392 | socket $listener, PF_INET, SOCK_STREAM, $tcp_proto | ||
| 393 | or croak "$!: socket()"; | ||
| 394 | setsockopt $listener, SOL_SOCKET, SO_REUSEADDR, pack( "l", 0 ) | ||
| 395 | or croak "$!: setsockopt()"; | ||
| 396 | |||
| 397 | unless ( bind $listener, sockaddr_in( 0, $loopback ) ) { | ||
| 398 | croak "Error binding: $!"; | ||
| 399 | } | ||
| 400 | |||
| 401 | my ($port) = sockaddr_in( getsockname($listener) ); | ||
| 402 | |||
| 403 | _debug "win32 port = $port" if _debugging_details; | ||
| 404 | |||
| 405 | listen $listener, my $queue_size = 1 | ||
| 406 | or croak "$!: listen()"; | ||
| 407 | |||
| 408 | { | ||
| 409 | socket $client, PF_INET, SOCK_STREAM, $tcp_proto | ||
| 410 | or croak "$!: socket()"; | ||
| 411 | |||
| 412 | my $paddr = sockaddr_in( $port, $loopback ); | ||
| 413 | |||
| 414 | connect $client, $paddr | ||
| 415 | or croak "$!: connect()"; | ||
| 416 | |||
| 417 | croak "$!: accept" unless defined $paddr; | ||
| 418 | |||
| 419 | ## The windows "default" is SO_DONTLINGER, which should make | ||
| 420 | ## sure all socket data goes through. I have my doubts based | ||
| 421 | ## on experimentation, but nothing prompts me to set SO_LINGER | ||
| 422 | ## at this time... | ||
| 423 | setsockopt $client, IPPROTO_TCP, TCP_NODELAY, pack( "l", 0 ) | ||
| 424 | or croak "$!: setsockopt()"; | ||
| 425 | } | ||
| 426 | |||
| 427 | { | ||
| 428 | _debug "accept()ing on port $port" if _debugging_details; | ||
| 429 | my $paddr = accept( $server, $listener ); | ||
| 430 | croak "$!: accept()" unless defined $paddr; | ||
| 431 | } | ||
| 432 | |||
| 433 | _debug "win32 _socket = ( ", fileno $server, ", ", fileno $client, " ) on port $port" | ||
| 434 | if _debugging_details; | ||
| 435 | return ( $server, $client ); | ||
| 436 | } | ||
| 437 | |||
| 438 | sub _open_socket_pipe { | ||
| 439 | my IPC::Run::Win32IO $self = shift; | ||
| 440 | my ( $debug_fd, $parent_handle ) = @_; | ||
| 441 | |||
| 442 | my $is_send_to_child = $self->dir eq "<"; | ||
| 443 | |||
| 444 | $self->{CHILD_HANDLE} = gensym; | ||
| 445 | $self->{PUMP_PIPE_HANDLE} = gensym; | ||
| 446 | |||
| 447 | ## When $parent_handle is a SCALAR ref (e.g. '>pipe', \$fh), we must | ||
| 448 | ## not pass it to _socket() which expects a GLOB. Create the socket | ||
| 449 | ## with a fresh gensym and store the resulting handle back into the | ||
| 450 | ## caller's scalar. | ||
| 451 | my $scalar_ref; | ||
| 452 | if ( ref $parent_handle eq 'SCALAR' ) { | ||
| 453 | $scalar_ref = $parent_handle; | ||
| 454 | $parent_handle = undef; | ||
| 455 | } | ||
| 456 | |||
| 457 | ( | ||
| 458 | $self->{PARENT_HANDLE}, | ||
| 459 | $self->{PUMP_SOCKET_HANDLE} | ||
| 460 | ) = _socket $parent_handle; | ||
| 461 | |||
| 462 | $$scalar_ref = $self->{PARENT_HANDLE} if $scalar_ref; | ||
| 463 | |||
| 464 | ## These binmodes seem to have no effect on Win2K, but just to be safe | ||
| 465 | ## I do them. | ||
| 466 | binmode $self->{PARENT_HANDLE} or die $!; | ||
| 467 | binmode $self->{PUMP_SOCKET_HANDLE} or die $!; | ||
| 468 | |||
| 469 | _debug "PUMP_SOCKET_HANDLE = ", fileno $self->{PUMP_SOCKET_HANDLE} | ||
| 470 | if _debugging_details; | ||
| 471 | ##my $buf; | ||
| 472 | ##$buf = "write on child end of " . fileno( $self->{WRITE_HANDLE} ) . "\n\n\n\n\n"; | ||
| 473 | ##POSIX::write(fileno $self->{WRITE_HANDLE}, $buf, length $buf) or warn "$! in syswrite"; | ||
| 474 | ##$buf = "write on parent end of " . fileno( $self->{CHILD_HANDLE} ) . "\r\n"; | ||
| 475 | ##POSIX::write(fileno $self->{CHILD_HANDLE},$buf, length $buf) or warn "$! in syswrite"; | ||
| 476 | ## $self->{CHILD_HANDLE}->autoflush( 1 ); | ||
| 477 | ## $self->{WRITE_HANDLE}->autoflush( 1 ); | ||
| 478 | |||
| 479 | ## Now fork off a data pump and arrange to return the correct fds. | ||
| 480 | if ($is_send_to_child) { | ||
| 481 | pipe $self->{CHILD_HANDLE}, $self->{PUMP_PIPE_HANDLE} | ||
| 482 | or croak "$! opening child pipe"; | ||
| 483 | _debug "CHILD_HANDLE = ", fileno $self->{CHILD_HANDLE} | ||
| 484 | if _debugging_details; | ||
| 485 | _debug "PUMP_PIPE_HANDLE = ", fileno $self->{PUMP_PIPE_HANDLE} | ||
| 486 | if _debugging_details; | ||
| 487 | } | ||
| 488 | else { | ||
| 489 | pipe $self->{PUMP_PIPE_HANDLE}, $self->{CHILD_HANDLE} | ||
| 490 | or croak "$! opening child pipe"; | ||
| 491 | _debug "CHILD_HANDLE = ", fileno $self->{CHILD_HANDLE} | ||
| 492 | if _debugging_details; | ||
| 493 | _debug "PUMP_PIPE_HANDLE = ", fileno $self->{PUMP_PIPE_HANDLE} | ||
| 494 | if _debugging_details; | ||
| 495 | } | ||
| 496 | |||
| 497 | ## These binmodes seem to have no effect on Win2K, but just to be safe | ||
| 498 | ## I do them. | ||
| 499 | binmode $self->{CHILD_HANDLE}; | ||
| 500 | binmode $self->{PUMP_PIPE_HANDLE}; | ||
| 501 | |||
| 502 | ## No child should ever see this. | ||
| 503 | _dont_inherit $self->{PARENT_HANDLE}; | ||
| 504 | |||
| 505 | ## We clear the inherit flag so these file descriptors are not inherited. | ||
| 506 | ## It'll be dup()ed on to STDIN/STDOUT/STDERR before CreateProcess is | ||
| 507 | ## called and *that* fd will be inheritable. | ||
| 508 | _dont_inherit $self->{PUMP_SOCKET_HANDLE}; | ||
| 509 | _dont_inherit $self->{PUMP_PIPE_HANDLE}; | ||
| 510 | _dont_inherit $self->{CHILD_HANDLE}; | ||
| 511 | |||
| 512 | ## Need to return $self so the HANDLEs don't get freed. | ||
| 513 | ## Return $self, $parent_fd, $child_fd | ||
| 514 | my ( $parent_fd, $child_fd ) = ( | ||
| 515 | fileno $self->{PARENT_HANDLE}, | ||
| 516 | fileno $self->{CHILD_HANDLE} | ||
| 517 | ); | ||
| 518 | |||
| 519 | ## Both PUMP_..._HANDLEs will be closed, no need to worry about | ||
| 520 | ## inheritance. | ||
| 521 | _debug "binmode on" if _debugging_data && $self->binmode; | ||
| 522 | _spawn_pumper( | ||
| 523 | $is_send_to_child | ||
| 524 | ? ( $self->{PUMP_SOCKET_HANDLE}, $self->{PUMP_PIPE_HANDLE} ) | ||
| 525 | : ( $self->{PUMP_PIPE_HANDLE}, $self->{PUMP_SOCKET_HANDLE} ), | ||
| 526 | $debug_fd, | ||
| 527 | $self->binmode, | ||
| 528 | $child_fd . $self->dir . "pump" . $self->dir . $parent_fd, | ||
| 529 | ); | ||
| 530 | |||
| 531 | { | ||
| 532 | my $foo; | ||
| 533 | confess "PARENT_HANDLE no longer open" | ||
| 534 | unless POSIX::read( $parent_fd, $foo, 0 ); | ||
| 535 | } | ||
| 536 | |||
| 537 | _debug "win32_fake_pipe = ( $parent_fd, $child_fd )" | ||
| 538 | if _debugging_details; | ||
| 539 | |||
| 540 | $self->{FD} = $parent_fd; | ||
| 541 | $self->{TFD} = $child_fd; | ||
| 542 | } | ||
| 543 | |||
| 544 | sub _do_open { | ||
| 545 | my IPC::Run::Win32IO $self = shift; | ||
| 546 | |||
| 547 | if ( $self->{SEND_THROUGH_TEMP_FILE} ) { | ||
| 548 | return $self->_send_through_temp_file(@_); | ||
| 549 | } | ||
| 550 | elsif ( $self->{RECV_THROUGH_TEMP_FILE} ) { | ||
| 551 | return $self->_init_recv_through_temp_file(@_); | ||
| 552 | } | ||
| 553 | else { | ||
| 554 | return $self->_open_socket_pipe(@_); | ||
| 555 | } | ||
| 556 | } | ||
| 557 | |||
| 558 | 1; | ||
| 559 | |||
| 560 | =pod | ||
| 561 | |||
| 562 | =head1 AUTHOR | ||
| 563 | |||
| 564 | Barries Slaymaker <barries@slaysys.com>. Funded by Perforce Software, Inc. | ||
| 565 | |||
| 566 | =head1 COPYRIGHT | ||
| 567 | |||
| 568 | Copyright 2001, Barrie Slaymaker, All Rights Reserved. | ||
| 569 | |||
| 570 | You may use this under the terms of either the GPL 2.0 or the Artistic License. | ||
| 571 | |||
| 572 | =cut |