File Coverage

blib/lib/Siebel/Srvrmgr/IPC.pm
Criterion Covered Total %
statement 24 46 52.1
branch 3 14 21.4
condition n/a
subroutine 8 11 72.7
pod 2 2 100.0
total 37 73 50.6


line stmt bran cond sub pod time code
1             package Siebel::Srvrmgr::IPC;
2              
3             =pod
4              
5             =head1 NAME
6              
7             Siebel::Srvrmgr::IPC - IPC functionality for Siebel::Srvrmgr classes.
8              
9             =head1 SYNOPSIS
10              
11             use Siebel::Srvrmgr::IPC qw(safe_open3);
12              
13             my ( $pid, $write_h, $read_h, $error_h ) = safe_open3( \@params );
14              
15             =head1 DESCRIPTION
16              
17             This module exports a single function (C<safe_open3>) used for running a external program, reading it's STDOUT, STDERR and writing to STDIN by
18             using IPC.
19              
20             This module is based on L<IPC::Open3::Callback> from Lucas Theisen (see SEE ALSO section).
21              
22             =cut
23              
24             require Exporter;
25             our @ISA = qw(Exporter);
26             our @EXPORT = qw(safe_open3 check_system);
27              
28 5     5   4952 use IPC::Open3;
  5         12513  
  5         208  
29 5     5   20 use Symbol 'gensym';
  5         5  
  5         128  
30 5     5   2029 use IO::Socket;
  5         69121  
  5         19  
31 5     5   1937 use Config;
  5         8  
  5         173  
32 5     5   798 use POSIX qw(WIFEXITED WEXITSTATUS WIFSIGNALED WTERMSIG WIFSTOPPED);
  5         8394  
  5         35  
33              
34             =pod
35              
36             =head1 EXPORTS
37              
38             =head2 safe_open3
39              
40             C<safe_open3> functions executes a "safe" version of L<IPC::Open3> that will execute additional processing required for using C<select> in Microsoft
41             Windows OS (if automatically detected). For other OS's, the default functionality of L<IPC::Open3> is used.
42              
43             Expects as parameter an array reference with the external program to execute, including the arguments for it.
44              
45             Returns (in this order):
46              
47             =over
48              
49             =item 1.
50              
51             The PID of the child process executing the external program.
52              
53             =item 2.
54              
55             The writer handle to the child process.
56              
57             =item 3.
58              
59             The reader handle to the child process.
60              
61             =item 4.
62              
63             The error handle for the child process.
64              
65             =back
66              
67             =cut
68              
69             sub safe_open3 {
70              
71 2 50   2 1 29 return ( $Config{osname} eq 'MSWin32' )
72             ? Siebel::Srvrmgr::IPC::_mswin_open3( $_[0] )
73             : Siebel::Srvrmgr::IPC::_default_open3( $_[0] );
74              
75             }
76              
77             sub _mswin_open3 {
78              
79 0     0   0 my $cmd_ref = shift;
80              
81 0         0 my ( $inRead, $inWrite ) = Siebel::Srvrmgr::IPC::_mswin_pipe();
82 0         0 my ( $outRead, $outWrite ) = Siebel::Srvrmgr::IPC::_mswin_pipe();
83 0         0 my ( $errRead, $errWrite ) = Siebel::Srvrmgr::IPC::_mswin_pipe();
84              
85 0         0 my $pid = open3(
86             '>&' . fileno($inRead),
87             '<&' . fileno($outWrite),
88             '<&' . fileno($errWrite),
89 0         0 @{$cmd_ref}
90             );
91              
92 0         0 return ( $pid, $inWrite, $outRead, $errRead );
93             }
94              
95             sub _mswin_pipe {
96              
97 0     0   0 my ( $read, $write ) =
98             IO::Socket->socketpair( AF_UNIX, SOCK_STREAM, PF_UNSPEC );
99              
100 0         0 Siebel::Srvrmgr::IPC::_check_shutdown( 'read', $read->shutdown(SHUT_WR) )
101             ; # No more writing for reader
102 0         0 Siebel::Srvrmgr::IPC::_check_shutdown( 'write', $write->shutdown(SHUT_RD) )
103             ; # No more reading for writer
104              
105 0         0 return ( $read, $write );
106              
107             }
108              
109             sub _check_shutdown {
110              
111 0     0   0 my $which = shift; # which handle name will be partly shutdown
112 0         0 my $ret = shift;
113              
114 0 0       0 unless ( defined($ret) ) {
115              
116 0         0 die "first argument of shutdown($which) is not a valid filehandle";
117              
118             }
119             else {
120              
121 0 0       0 die "An error ocurred when trying shutdown($which): $!"
122             if ( $ret == 0 );
123              
124             }
125              
126             }
127              
128             sub _default_open3 {
129              
130 2     2   4 my $cmd_ref = shift;
131              
132 2         13 my ( $inFh, $outFh, $errFh ) = ( gensym(), gensym(), gensym() );
133 2         52 return ( open3( $inFh, $outFh, $errFh, @{$cmd_ref} ), $inFh, $outFh,
  2         11  
134             $errFh );
135             }
136              
137             =head2 check_system
138              
139             For non-Windows systems, returns additional information about the child process created by a C<system> call as a string. Also, it returns a boolean (in Perl sense)
140             indicating if this is a error (1) or not (0);
141              
142             Expects as parameter the environment variable C<${^CHILD_ERROR_NATIVE}> value, available right after the C<system> call.
143              
144             =cut
145              
146             # :TODO:22-09-2014 13:26:35:: should implement exceptions to this
147             sub check_system {
148              
149 3     3 1 13 my $child_error = shift;
150              
151 3 50       118 unless ( $Config{osname} eq 'MSWin32' ) {
152              
153 3 50       24 if ( WIFEXITED($child_error) ) {
154              
155             return
156 3         42 'Child process terminate with call to exit() with return code = '
157             . WEXITSTATUS($child_error), 0;
158              
159             }
160              
161 0 0         if ( WIFSIGNALED($child_error) ) {
162              
163 0           return 'Child process terminated due signal: '
164             . WTERMSIG($child_error), 1;
165              
166             }
167              
168 0 0         if ( WIFSTOPPED($child_error) ) {
169              
170 0           return 'Child process was stopped with ' . WSTOPSIG($child_error),
171             1;
172              
173             }
174             else {
175              
176 0           return 'Not able to check child process information', undef;
177              
178             }
179              
180             }
181             else {
182              
183 0           return undef, undef;
184              
185             }
186              
187             }
188              
189             =pod
190              
191             =head1 SEE ALSO
192              
193             =over
194              
195             =item *
196              
197             L<https://github.com/lucastheisen/ipc-open3-callback>
198              
199             =item *
200              
201             L<IPC::Open3>
202              
203             =item *
204              
205             L<IO::Socket>
206              
207             =back
208              
209             =head1 AUTHOR
210              
211             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
212              
213             =head1 COPYRIGHT AND LICENSE
214              
215             This software is copyright (c) 2013 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
216              
217             This file is part of Siebel Monitoring Tools.
218              
219             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
220             it under the terms of the GNU General Public License as published by
221             the Free Software Foundation, either version 3 of the License, or
222             (at your option) any later version.
223              
224             Siebel Monitoring Tools is distributed in the hope that it will be useful,
225             but WITHOUT ANY WARRANTY; without even the implied warranty of
226             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
227             GNU General Public License for more details.
228              
229             You should have received a copy of the GNU General Public License
230             along with Siebel Monitoring Tools. If not, see <http://www.gnu.org/licenses/>.
231              
232             =cut
233              
234             1;