blib/lib/IPC/Run/Win32Helper.pm untested

filter: Statementstmt 0.0% 0 / 218 Branchbran 0.0% 0 / 150 Conditioncond 0.0% 0 / 93 MC/DCmcdc 0.0% 0 / 60 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 550 CC 75 SCAR 64.5
CC
75
Cov
0%
optimize
1332.0
win32_spawn
506.0
win32_parse_cmd_line
72.0
CRAP 632.0
1 package IPC::Run::Win32Helper;
2  
3 =pod
4  
5 =head1 NAME
6  
7 IPC::Run::Win32Helper - helper routines for IPC::Run on Win32 platforms.
8  
9 =head1 SYNOPSIS
10  
11 use IPC::Run::Win32Helper; # 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() loop
16 will work on Win32. This seems to only work on WinNT and Win2K at this time, not
17 sure if it will ever work on Win95 or Win98. If you have experience in this area, please
18 contact me at barries@slaysys.com, thanks!.
19  
20 =cut
21  
22 use strict;
23 use warnings;
24 use Carp;
25 use IO::Handle;
26 use vars qw{ $VERSION @ISA @EXPORT };
27  
28 BEGIN {
29 $VERSION = '20260402.0';
30 @ISA = qw( Exporter );
31 @EXPORT = qw(
32 win32_spawn
33 win32_parse_cmd_line
34 _dont_inherit
35 _inherit
36 );
37 }
38  
39 require POSIX;
40  
41 use File::Spec ();
42 use Win32 ();
43 use Win32::Process;
44 use Win32::ShellQuote ();
45 use IPC::Run::Debug;
46 use Win32API::File qw(
47 FdGetOsFHandle
48 SetHandleInformation
49 HANDLE_FLAG_INHERIT
50 );
51  
52 # Replace Win32API::File::INVALID_HANDLE_VALUE, which does not match the C ABI
53 # on 64-bit builds (https://github.com/chorny/Win32API-File/issues/13).
54 use constant C_ABI_INVALID_HANDLE_VALUE => length( pack 'P', undef ) == 4
55 ? 0xffffffff
56 : 0xffffffff << 32 | 0xffffffff;
57  
58 ## Takes an fd or a GLOB ref, never never never a Win32 handle.
59 sub _dont_inherit {
60 for (@_) {
61 next unless defined $_;
62 my $fd = $_;
63 $fd = fileno $fd if ref $fd;
64 _debug "disabling inheritance of ", $fd if _debugging_details;
65 my $osfh = FdGetOsFHandle $fd;
66  
67 # Contrary to documentation, $! has the failure reason
68 # (https://github.com/chorny/Win32API-File/issues/14)
69 croak "$!: FdGetOsFHandle( $fd )"
70 if !defined $osfh || $osfh == C_ABI_INVALID_HANDLE_VALUE;
71  
72 SetHandleInformation( $osfh, HANDLE_FLAG_INHERIT, 0 );
73 }
74 }
75  
76 sub _inherit { #### REMOVE
77 for (@_) { #### REMOVE
78 next unless defined $_; #### REMOVE
79 my $fd = $_; #### REMOVE
80 $fd = fileno $fd if ref $fd; #### REMOVE
81 _debug "enabling inheritance of ", $fd if _debugging_details; #### REMOVE
82 my $osfh = FdGetOsFHandle $fd; #### REMOVE
83  
84 # Contrary to documentation, $! has the failure reason
85 # (https://github.com/chorny/Win32API-File/issues/14)
86 croak "$!: FdGetOsFHandle( $fd )"
87 if !defined $osfh || $osfh == C_ABI_INVALID_HANDLE_VALUE;
88 #### REMOVE
89 SetHandleInformation( $osfh, HANDLE_FLAG_INHERIT, 1 ); #### REMOVE
90 } #### REMOVE
91 } #### REMOVE
92 #### REMOVE
93 #sub _inherit {
94 # for ( @_ ) {
95 # next unless defined $_;
96 # my $osfh = GetOsFHandle $_;
97 # croak $^E if ! defined $osfh || $osfh == INVALID_HANDLE_VALUE;
98 # SetHandleInformation( $osfh, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT );
99 # }
100 #}
101  
102 =pod
103  
104 =head1 FUNCTIONS
105  
106 =over
107  
108 =item optimize()
109  
110 Most common incantations of C<run()> (I<not> C<harness()>, C<start()>,
111 or C<finish()>) now use temporary files to redirect input and output
112 instead of pumper processes.
113  
114 Temporary files are used when sending to child processes if input is
115 taken from a scalar with no filter subroutines. This is the only time
116 we can assume that the parent is not interacting with the child's
117 redirected input as it runs.
118  
119 Temporary files are used when receiving from children when output is
120 to a scalar or subroutine with or without filters, but only if
121 the child in question closes its inputs or takes input from
122 unfiltered SCALARs or named files. Normally, a child inherits its STDIN
123 from its parent; to close it, use "0<&-" or the C<< noinherit => 1 >> option.
124 If data is sent to the child from CODE refs, filehandles or from
125 scalars through filters than the child's outputs will not be optimized
126 because C<optimize()> assumes the parent is interacting with the child.
127 It is ok if the output is filtered or handled by a subroutine, however.
128  
129 This assumes that all named files are real files (as opposed to named
130 pipes) and won't change; and that a process is not communicating with
131 the child indirectly (through means not visible to IPC::Run).
132 These can be an invalid assumptions, but are the 99% case.
133 Write me if you need an option to enable or disable optimizations; I
134 suspect it will work like the C<binary()> modifier.
135  
136 To detect cases that you might want to optimize by closing inputs, try
137 setting the C<IPCRUNDEBUG> environment variable to the special C<notopt>
138 value:
139  
140 C:> set IPCRUNDEBUG=notopt
141 C:> my_app_that_uses_IPC_Run.pl
142  
143 =item optimizer() rationalizations
144  
145 Only for that limited case can we be sure that it's ok to batch all the
146 input in to a temporary file. If STDIN is from a SCALAR or from a named
147 file or filehandle (again, only in C<run()>), then outputs to CODE refs
148 are also assumed to be safe enough to batch through a temp file,
149 otherwise only outputs to SCALAR refs are batched. This can cause a bit
150 of grief if the parent process benefits from or relies on a bit of
151 "early returns" coming in before the child program exits. As long as
152 the output is redirected to a SCALAR ref, this will not be visible.
153 When output is redirected to a subroutine or (deprecated) filters, the
154 subroutine will not get any data until after the child process exits,
155 and it is likely to get bigger chunks of data at once.
156  
157 The reason for the optimization is that, without it, "pumper" processes
158 are used to overcome the inconsistencies of the Win32 API. We need to
159 use anonymous pipes to connect to the child processes' stdin, stdout,
160 and stderr, yet select() does not work on these. select() only works on
161 sockets on Win32. So for each redirected child handle, there is
162 normally a "pumper" process that connects to the parent using a
163 socket--so the parent can select() on that fd--and to the child on an
164 anonymous pipe--so the child can read/write a pipe.
165  
166 Using a socket to connect directly to the child (as at least one MSDN
167 article suggests) seems to cause the trailing output from most children
168 to be lost. I think this is because child processes rarely close their
169 stdout and stderr explicitly, and the winsock dll does not seem to flush
170 output when a process that uses it exits without explicitly closing
171 them.
172  
173 Because of these pumpers and the inherent slowness of Win32
174 CreateProcess(), child processes with redirects are quite slow to
175 launch; so this routine looks for the very common case of
176 reading/writing to/from scalar references in a run() routine and
177 converts such reads and writes in to temporary file reads and writes.
178  
179 Such files are marked as FILE_ATTRIBUTE_TEMPORARY to increase speed and
180 as FILE_FLAG_DELETE_ON_CLOSE so it will be cleaned up when the child
181 process exits (for input files). The user's default permissions are
182 used for both the temporary files and the directory that contains them,
183 hope your Win32 permissions are secure enough for you. Files are
184 created with the Win32API::File defaults of
185 FILE_SHARE_READ|FILE_SHARE_WRITE.
186  
187 Setting the debug level to "details" or "gory" will give detailed
188 information about the optimization process; setting it to "basic" or
189 higher will tell whether or not a given call is optimized. Setting
190 it to "notopt" will highlight those calls that aren't optimized.
191  
192 =cut
193  
194 sub optimize {
195 my ($h) = @_;
196  
197 my @kids = @{ $h->{KIDS} };
198  
199 my $saw_pipe;
200  
201 my ( $ok_to_optimize_outputs, $veto_output_optimization );
202  
203 for my $kid (@kids) {
204 ( $ok_to_optimize_outputs, $veto_output_optimization ) = ()
205 unless $saw_pipe;
206  
207 _debug "Win32 optimizer: (kid $kid->{NUM}) STDIN piped, carrying over ok of non-SCALAR output optimization"
208 if _debugging_details && $ok_to_optimize_outputs;
209 _debug "Win32 optimizer: (kid $kid->{NUM}) STDIN piped, carrying over veto of non-SCALAR output optimization"
210 if _debugging_details && $veto_output_optimization;
211  
212 if ( $h->{noinherit} && !$ok_to_optimize_outputs ) {
213 _debug "Win32 optimizer: (kid $kid->{NUM}) STDIN not inherited from parent oking non-SCALAR output optimization"
214 if _debugging_details && $ok_to_optimize_outputs;
215 $ok_to_optimize_outputs = 1;
216 }
217  
218 for ( @{ $kid->{OPS} } ) {
219 if ( substr( $_->{TYPE}, 0, 1 ) eq "<" ) {
220 if ( $_->{TYPE} eq "<" ) {
221 if ( @{ $_->{FILTERS} } > 1 ) {
222 ## Can't assume that the filters are idempotent.
223 }
224 elsif (ref $_->{SOURCE} eq "SCALAR"
225 || ref $_->{SOURCE} eq "GLOB"
226 || UNIVERSAL::isa( $_, "IO::Handle" ) ) {
227 if ( $_->{KFD} == 0 ) {
228 _debug
229 "Win32 optimizer: (kid $kid->{NUM}) 0$_->{TYPE}",
230 ref $_->{SOURCE},
231 ", ok to optimize outputs"
232 if _debugging_details;
233 $ok_to_optimize_outputs = 1;
234 }
235 $_->{SEND_THROUGH_TEMP_FILE} = 1;
236 next;
237 }
238 elsif ( !ref $_->{SOURCE} && defined $_->{SOURCE} ) {
239 if ( $_->{KFD} == 0 ) {
240 _debug
241 "Win32 optimizer: (kid $kid->{NUM}) 0<$_->{SOURCE}, ok to optimize outputs",
242 if _debugging_details;
243 $ok_to_optimize_outputs = 1;
244 }
245 next;
246 }
247 }
248 _debug
249 "Win32 optimizer: (kid $kid->{NUM}) ",
250 $_->{KFD},
251 $_->{TYPE},
252 defined $_->{SOURCE}
253 ? ref $_->{SOURCE}
254 ? ref $_->{SOURCE}
255 : $_->{SOURCE}
256 : defined $_->{FILENAME} ? $_->{FILENAME}
257 : "",
258 @{ $_->{FILTERS} } > 1 ? " with filters" : (),
259 ", VETOING output opt."
260 if _debugging_details || _debugging_not_optimized;
261 $veto_output_optimization = 1;
262 }
263 elsif ( $_->{TYPE} eq "close" && $_->{KFD} == 0 ) {
264 $ok_to_optimize_outputs = 1;
265 _debug "Win32 optimizer: (kid $kid->{NUM}) saw 0<&-, ok to optimize outputs"
266 if _debugging_details;
267 }
268 elsif ( $_->{TYPE} eq "dup" && $_->{KFD2} == 0 ) {
269 $veto_output_optimization = 1;
270 _debug "Win32 optimizer: (kid $kid->{NUM}) saw 0<&$_->{KFD2}, VETOING output opt."
271 if _debugging_details || _debugging_not_optimized;
272 }
273 elsif ( $_->{TYPE} eq "|" ) {
274 $saw_pipe = 1;
275 }
276 }
277  
278 if ( !$ok_to_optimize_outputs && !$veto_output_optimization ) {
279 _debug "Win32 optimizer: (kid $kid->{NUM}) child STDIN not redirected, VETOING non-SCALAR output opt."
280 if _debugging_details || _debugging_not_optimized;
281 $veto_output_optimization = 1;
282 }
283  
284 if ( $ok_to_optimize_outputs && $veto_output_optimization ) {
285 $ok_to_optimize_outputs = 0;
286 _debug "Win32 optimizer: (kid $kid->{NUM}) non-SCALAR output optimizations VETOed"
287 if _debugging_details || _debugging_not_optimized;
288 }
289  
290 ## SOURCE/DEST ARRAY means it's a filter.
291 ## TODO: think about checking to see if the final input/output of
292 ## a filter chain (an ARRAY SOURCE or DEST) is a scalar...but
293 ## we may be deprecating filters.
294  
295 for ( @{ $kid->{OPS} } ) {
296 if ( $_->{TYPE} eq ">" ) {
297 if (
298 ref $_->{DEST} eq "SCALAR"
299 || (
300 (
301 @{ $_->{FILTERS} } > 1
302 || ref $_->{DEST} eq "CODE"
303 || ref $_->{DEST} eq "ARRAY" ## Filters?
304 )
305 && ( $ok_to_optimize_outputs && !$veto_output_optimization )
306 )
307 ) {
308 $_->{RECV_THROUGH_TEMP_FILE} = 1;
309 next;
310 }
311 _debug
312 "Win32 optimizer: NOT optimizing (kid $kid->{NUM}) ",
313 $_->{KFD},
314 $_->{TYPE},
315 defined $_->{DEST}
316 ? ref $_->{DEST}
317 ? ref $_->{DEST}
318 : $_->{SOURCE}
319 : defined $_->{FILENAME} ? $_->{FILENAME}
320 : "",
321 @{ $_->{FILTERS} } ? " with filters" : (),
322 if _debugging_details;
323 }
324 }
325 }
326  
327 }
328  
329 =pod
330  
331 =item win32_parse_cmd_line
332  
333 @words = win32_parse_cmd_line( q{foo "bar baz" qux} );
334  
335 returns 3 words. Parses a Windows command-line string into arguments
336 following the
337 L<standard Microsoft C/C++ rules|https://docs.microsoft.com/en-us/cpp/cpp/main-function-command-line-args#parsing-c-command-line-arguments>:
338  
339 =over 4
340  
341 =item *
342  
343 Arguments are delimited by whitespace (space or tab).
344  
345 =item *
346  
347 Double quotes group arguments containing whitespace.
348  
349 =item *
350  
351 Single quotes are B<not> special (unlike Unix shells).
352  
353 =item *
354  
355 Backslashes are literal unless immediately preceding a double quote:
356 2N backslashes + C<"> = N backslashes + start/end quote;
357 2N+1 backslashes + C<"> = N backslashes + literal C<">.
358  
359 =back
360  
361 =cut
362  
363 sub win32_parse_cmd_line {
364 my $line = shift;
365 return unless defined $line && length $line;
366  
367 my @args;
368 my $pos = 0;
369 my $len = length $line;
370  
371 while ( $pos < $len ) {
372 ## Skip whitespace (space and tab only per Windows rules).
373 $pos++ while $pos < $len && substr( $line, $pos, 1 ) =~ /[ \t]/;
374 last if $pos >= $len;
375  
376 my $arg = '';
377 my $in_quote = 0;
378  
379 while ( $pos < $len ) {
380 my $c = substr( $line, $pos, 1 );
381  
382 ## Unquoted whitespace ends the argument.
383 last if !$in_quote && $c =~ /[ \t]/;
384  
385 if ( $c eq '\\' ) {
386 ## Count consecutive backslashes.
387 my $bs = 0;
388 $bs++, $pos++
389 while $pos < $len && substr( $line, $pos, 1 ) eq '\\';
390  
391 if ( $pos < $len && substr( $line, $pos, 1 ) eq '"' ) {
392 ## Backslashes followed by double quote.
393 $arg .= '\\' x int( $bs / 2 );
394 if ( $bs % 2 ) {
395 ## Odd count: last backslash escapes the quote.
396 $arg .= '"';
397 $pos++;
398 }
399 else {
400 ## Even count: quote toggles grouping.
401 $in_quote = !$in_quote;
402 $pos++;
403 }
404 }
405 else {
406 ## Backslashes not before a quote — all literal.
407 $arg .= '\\' x $bs;
408 }
409 }
410 elsif ( $c eq '"' ) {
411 $in_quote = !$in_quote;
412 $pos++;
413 }
414 else {
415 $arg .= $c;
416 $pos++;
417 }
418 }
419  
420 push @args, $arg;
421 }
422  
423 return @args;
424 }
425  
426 =pod
427  
428 =item win32_spawn
429  
430 Spawns a child process, possibly with STDIN, STDOUT, and STDERR (file descriptors 0, 1, and 2, respectively) redirected.
431  
432 B<LIMITATIONS>.
433  
434 Cannot redirect higher file descriptors due to lack of support for this in the
435 Win32 environment.
436  
437 This can be worked around by marking a handle as inheritable in the
438 parent (or leaving it marked; this is the default in perl), obtaining its
439 Win32 handle with C<Win32API::GetOSFHandle(FH)> or
440 C<Win32API::FdGetOsFHandle($fd)> and passing it to the child using the command
441 line, the environment, or any other IPC mechanism (it's a plain old integer).
442 The child can then use C<OsFHandleOpen()> or C<OsFHandleOpenFd()> and possibly
443 C<<open FOO ">&BAR">> or C<<open FOO ">&$fd>> as need be. Ach, the pain!
444  
445 =cut
446  
447 sub _save {
448 my ( $saved, $saved_as, $fd ) = @_;
449  
450 ## We can only save aside the original fds once.
451 return if exists $saved->{$fd};
452  
453 my $saved_fd = IPC::Run::_dup($fd);
454 _dont_inherit $saved_fd;
455  
456 $saved->{$fd} = $saved_fd;
457 $saved_as->{$saved_fd} = $fd;
458  
459 _dont_inherit $saved->{$fd};
460 }
461  
462 sub _dup2_gently {
463 my ( $saved, $saved_as, $fd1, $fd2 ) = @_;
464 _save $saved, $saved_as, $fd2;
465  
466 if ( exists $saved_as->{$fd2} ) {
467 ## The target fd is colliding with a saved-as fd, gotta bump
468 ## the saved-as fd to another fd.
469 my $orig_fd = delete $saved_as->{$fd2};
470 my $saved_fd = IPC::Run::_dup($fd2);
471 _dont_inherit $saved_fd;
472  
473 $saved->{$orig_fd} = $saved_fd;
474 $saved_as->{$saved_fd} = $orig_fd;
475 }
476 _debug "moving $fd1 to kid's $fd2" if _debugging_details;
477 IPC::Run::_dup2_rudely( $fd1, $fd2 );
478 }
479  
480 sub win32_spawn {
481 my ( $cmd, $ops ) = @_;
482  
483 my ( $app, $cmd_line );
484 my $need_pct = 0;
485 if ( UNIVERSAL::isa( $cmd, 'IPC::Run::Win32Process' ) ) {
486 $app = $cmd->{lpApplicationName};
487 $cmd_line = $cmd->{lpCommandLine};
488 }
489 elsif ( $cmd->[0] !~ /\.(bat|cmd) *$/i ) {
490 $app = $cmd->[0];
491 $cmd_line = Win32::ShellQuote::quote_native(@$cmd);
492 }
493 else {
494 # Batch file, so follow the batch-specific guidance of
495 # https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessa
496 # There's no one true way to locate cmd.exe. In the unlikely event that
497 # %COMSPEC% is missing, fall back on a Windows API. We could search
498 # %PATH% like _wsystem() does. That would be prone to security bugs,
499 # and one fallback is enough.
500 $app = (
501 $ENV{COMSPEC}
502 || File::Spec->catfile(
503 Win32::GetFolderPath(Win32::CSIDL_SYSTEM),
504 'cmd.exe'
505 )
506 );
507  
508 # Win32 rejects attempts to create files with names containing certain
509 # characters. Ignore most, but reject the subset that might otherwise
510 # cause us to execute the wrong file instead of failing cleanly.
511 if ( $cmd->[0] =~ /["\r\n\0]/ ) {
512 croak "invalid batch file name";
513 }
514  
515 # Make cmd.exe see the batch file name as quoted. Suppose we instead
516 # used caret escapes, as we do for arguments. cmd.exe could then "break
517 # the command token at the first occurrence of <space> , ; or ="
518 # (https://stackoverflow.com/a/4095133).
519 my @parts = qq{"$cmd->[0]"};
520  
521 # cmd.exe will strip escapes once when parsing our $cmd_line and again
522 # where the batch file injects the argument via %*, %1, etc. Compensate
523 # by adding one extra cmd_escape layer.
524 if ( @$cmd > 1 ) {
525 my @q = Win32::ShellQuote::quote_cmd( @{$cmd}[ 1 .. $#{$cmd} ] );
526 push @parts, map { Win32::ShellQuote::cmd_escape($_) } @q;
527 }
528  
529 # One can't stop cmd.exe from expanding %var%, so inject each literal %
530 # via an environment variable. Delete that variable before the real
531 # child can see it. See
532 # https://www.dostips.com/forum/viewtopic.php?f=3&t=10131 for more on
533 # this technique and the limitations of alternatives.
534 $cmd_line = join ' ', @parts;
535 if ( $cmd_line =~ s/%/%ipcrunpct%/g ) {
536 $cmd_line = qq{/c "set "ipcrunpct=" & $cmd_line"};
537 $need_pct = 1;
538 }
539 else {
540 $cmd_line = qq{/c "$cmd_line"};
541 }
542 }
543 _debug "app: ", $app
544 if _debugging;
545 _debug "cmd line: ", $cmd_line
546 if _debugging;
547  
548 ## NOTE: The debug pipe write handle is passed to pump processes as STDOUT.
549 ## and is not to the "real" child process, since they would not know
550 ## what to do with it...unlike Unix, we have no code executing in the
551 ## child before the "real" child is exec()ed.
552  
553 my %saved; ## Map of parent's orig fd -> saved fd
554 my %saved_as; ## Map of parent's saved fd -> orig fd, used to
555 ## detect collisions between a KFD and the fd a
556 ## parent's fd happened to be saved to.
557  
558 for my $op (@$ops) {
559 _dont_inherit $op->{FD} if defined $op->{FD};
560  
561 if ( defined $op->{KFD} && $op->{KFD} > 2 ) {
562 ## TODO: Detect this in harness()
563 ## TODO: enable temporary redirections if ever necessary, not
564 ## sure why they would be...
565 ## 4>&1 1>/dev/null 1>&4 4>&-
566 croak "Can't redirect fd #", $op->{KFD}, " on Win32";
567 }
568  
569 ## This is very similar logic to IPC::Run::_do_kid_and_exit().
570 if ( defined $op->{TFD} ) {
571 unless ( $op->{TFD} == $op->{KFD} ) {
572 _dup2_gently \%saved, \%saved_as, $op->{TFD}, $op->{KFD};
573 _dont_inherit $op->{TFD};
574 }
575 }
576 elsif ( $op->{TYPE} eq "dup" ) {
577 _dup2_gently \%saved, \%saved_as, $op->{KFD1}, $op->{KFD2}
578 unless $op->{KFD1} == $op->{KFD2};
579 }
580 elsif ( $op->{TYPE} eq "close" ) {
581 _save \%saved, \%saved_as, $op->{KFD};
582 IPC::Run::_close( $op->{KFD} );
583 }
584 elsif ( $op->{TYPE} eq "init" ) {
585 ## TODO: detect this in harness()
586 croak "init subs not allowed on Win32";
587 }
588 }
589  
590 ## Mark all open file handles as non-inheritable except for the fds
591 ## that have been explicitly set up for the child (0, 1, 2). This
592 ## prevents caller-owned handles (e.g. File::Temp directories) from
593 ## being inherited and keeping resources locked for the lifetime of
594 ## the child process.
595 ## See https://github.com/cpan-authors/IPC-Run/issues/141
596 {
597 my %kid_fds;
598 for my $op (@$ops) {
599 $kid_fds{ $op->{KFD} } = 1 if defined $op->{KFD};
600 $kid_fds{ $op->{KFD1} } = 1 if defined $op->{KFD1};
601 $kid_fds{ $op->{KFD2} } = 1 if defined $op->{KFD2};
602 }
603 ## On Win32, KFDs are always 0, 1, or 2 (higher fds are rejected
604 ## above), so anything not in %kid_fds can be made non-inheritable.
605 ## Use a conservative upper bound; the CRT default is 512.
606 my $max_fds = 512;
607 for my $fd ( 0 .. $max_fds - 1 ) {
608 next if $kid_fds{$fd};
609 my $osfh = FdGetOsFHandle($fd);
610 next unless defined $osfh && $osfh != C_ABI_INVALID_HANDLE_VALUE;
611 SetHandleInformation( $osfh, HANDLE_FLAG_INHERIT, 0 );
612 }
613 }
614  
615 local $ENV{ipcrunpct} = '%' if $need_pct;
616 my $process;
617 Win32::Process::Create(
618 $process,
619 $app,
620 $cmd_line,
621 1, ## Inherit handles
622 0, ## Inherit parent priority class. Was NORMAL_PRIORITY_CLASS
623 ".",
624 )
625 or do {
626 my $err = Win32::FormatMessage( Win32::GetLastError() );
627 $err =~ s/\r?\n$//s;
628 croak "$err: Win32::Process::Create()";
629 };
630  
631 for my $orig_fd ( keys %saved ) {
632 IPC::Run::_dup2_rudely( $saved{$orig_fd}, $orig_fd );
633 IPC::Run::_close( $saved{$orig_fd} );
634 }
635  
636 return ( $process->GetProcessID(), $process );
637 }
638  
639 1;
640  
641 =pod
642  
643 =back
644  
645 =head1 AUTHOR
646  
647 Barries Slaymaker <barries@slaysys.com>. Funded by Perforce Software, Inc.
648  
649 =head1 COPYRIGHT
650  
651 Copyright 2001, Barrie Slaymaker, All Rights Reserved.
652  
653 You may use this under the terms of either the GPL 2.0 or the Artistic License.
654  
655 =cut