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

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 13 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 103 CC 2 SCAR 11.5
CC
2
Cov
0%
BEGIN
6.0
BEGIN
2.0
BEGIN
2.0
CRAP 3.1
1 package IPC::Run::Win32Pump;
2  
3 =pod
4  
5 =head1 NAME
6  
7 IPC::Run::Win32Pump - helper processes to shovel data to/from parent, child
8  
9 =head1 SYNOPSIS
10  
11 Internal use only; see IPC::Run::Win32IO and best of luck to you.
12  
13 =head1 DESCRIPTION
14  
15 See L<IPC::Run::Win32Helper|IPC::Run::Win32Helper> for details. This
16 module is used in subprocesses that are spawned to shovel data to/from
17 parent processes from/to their child processes. Where possible, pumps
18 are optimized away.
19  
20 NOTE: This is not a real module: it's a script in module form, designed
21 to be run like
22  
23 $^X -MIPC::Run::Win32Pumper -e 1 ...
24  
25 It parses a bunch of command line parameters from IPC::Run::Win32IO.
26  
27 =cut
28  
29 use strict;
30 use warnings;
31 use vars qw{$VERSION};
32  
33 BEGIN {
34 $VERSION = '20260401.0';
35 }
36  
37 use Win32API::File qw(
38 OsFHandleOpen
39 );
40  
41 my ( $stdin_fh, $stdout_fh, $debug_fh, $binmode, $parent_pid, $parent_start_time, $debug, $child_label );
42  
43 BEGIN {
44 ( $stdin_fh, $stdout_fh, $debug_fh, $binmode, $parent_pid, $parent_start_time, $debug, $child_label ) = @ARGV;
45 ## Rather than letting IPC::Run::Debug export all-0 constants
46 ## when not debugging, we do it manually in order to not even
47 ## load IPC::Run::Debug.
48 if ($debug) {
49 eval "use IPC::Run::Debug qw( :default _debug_init ); 1;"
50 or die $@;
51 }
52 else {
53 eval <<STUBS_END or die $@;
54 sub _debug {}
55 sub _debug_init {}
56 sub _debugging() { 0 }
57 sub _debugging_data() { 0 }
58 sub _debugging_details() { 0 }
59 sub _debugging_gory_details() { 0 }
60 1;
61 STUBS_END
62 }
63 }
64  
65 ## For some reason these get created with binmode on. AAargh, gotta #### REMOVE
66 ## do it by hand below. #### REMOVE
67 if ($debug) { #### REMOVE
68 close STDERR; #### REMOVE
69 OsFHandleOpen( \*STDERR, $debug_fh, "w" ) #### REMOVE
70 or print "$! opening STDERR as Win32 handle $debug_fh in pumper $$"; #### REMOVE
71 } #### REMOVE
72 close STDIN; #### REMOVE
73 OsFHandleOpen( \*STDIN, $stdin_fh, "r" ) #### REMOVE
74 or die "$! opening STDIN as Win32 handle $stdin_fh in pumper $$"; #### REMOVE
75 close STDOUT; #### REMOVE
76 OsFHandleOpen( \*STDOUT, $stdout_fh, "w" ) #### REMOVE
77 or die "$! opening STDOUT as Win32 handle $stdout_fh in pumper $$"; #### REMOVE
78  
79 binmode STDIN;
80 binmode STDOUT;
81 $| = 1;
82 select STDERR;
83 $| = 1;
84 select STDOUT;
85  
86 $child_label ||= "pump";
87 _debug_init(
88 $parent_pid,
89 $parent_start_time,
90 $debug,
91 fileno STDERR,
92 $child_label,
93 );
94  
95 _debug "Entered" if _debugging_details;
96  
97 # No need to close all fds; win32 doesn't seem to pass any on to us.
98 $| = 1;
99 my $buf;
100 my $total_count = 0;
101 while (1) {
102 my $count = sysread STDIN, $buf, 10_000;
103 last unless $count;
104 if (_debugging_gory_details) {
105 my $msg = "'$buf'";
106 substr( $msg, 100, -1 ) = '...' if length $msg > 100;
107 $msg =~ s/\n/\\n/g;
108 $msg =~ s/\r/\\r/g;
109 $msg =~ s/\t/\\t/g;
110 $msg =~ s/([\000-\037\177-\277])/sprintf "\0x%02x", ord $1/eg;
111 _debug sprintf( "%5d chars revc: ", $count ), $msg;
112 }
113 $total_count += $count;
114 $buf =~ s/\r//g unless $binmode;
115 if (_debugging_gory_details) {
116 my $msg = "'$buf'";
117 substr( $msg, 100, -1 ) = '...' if length $msg > 100;
118 $msg =~ s/\n/\\n/g;
119 $msg =~ s/\r/\\r/g;
120 $msg =~ s/\t/\\t/g;
121 $msg =~ s/([\000-\037\177-\277])/sprintf "\0x%02x", ord $1/eg;
122 _debug sprintf( "%5d chars sent: ", $count ), $msg;
123 }
124 print $buf;
125 }
126  
127 _debug "Exiting, transferred $total_count chars" if _debugging_details;
128  
129 ## Perform a graceful socket shutdown. Windows defaults to SO_DONTLINGER,
130 ## which should cause a "graceful shutdown in the background" on sockets.
131 ## but that's only true if the process closes the socket manually, it
132 ## seems; if the process exits and lets the OS clean up, the OS is not
133 ## so kind. STDOUT is not always a socket, of course, but it won't hurt
134 ## to close a pipe and may even help. With a closed source OS, who
135 ## can tell?
136 ##
137 ## In any case, this close() is one of the main reasons we have helper
138 ## processes; if the OS closed socket fds gracefully when an app exits,
139 ## we'd just redirect the client directly to what is now the pump end
140 ## of the socket. As it is, however, we need to let the client play with
141 ## pipes, which don't have the abort-on-app-exit behavior, and then
142 ## adapt to the sockets in the helper processes to allow the parent to
143 ## select.
144 ##
145 ## Possible alternatives / improvements:
146 ##
147 ## 1) use helper threads instead of processes. I don't trust perl's threads
148 ## as of 5.005 or 5.6 enough (which may be myopic of me).
149 ##
150 ## 2) figure out if/how to get at WaitForMultipleObjects() with pipe
151 ## handles. May be able to take the Win32 handle and pass it to
152 ## Win32::Event::wait_any, dunno.
153 ##
154 ## 3) Use Inline::C or a hand-tooled XS module to do helper threads.
155 ## This would be faster than #1, but would require a ppm distro.
156 ##
157 close STDOUT;
158 close STDERR;
159  
160 1;
161  
162 =pod
163  
164 =head1 AUTHOR
165  
166 Barries Slaymaker <barries@slaysys.com>. Funded by Perforce Software, Inc.
167  
168 =head1 COPYRIGHT
169  
170 Copyright 2001, Barrie Slaymaker, All Rights Reserved.
171  
172 You may use this under the terms of either the GPL 2.0 ir the Artistic License.
173  
174 =cut