Access.pm

filter: Statementstmt 12.6% 20 / 158 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.0% 7 / 20 Podpod 0.0% 0 / 12 totaltotal 10.2% 27 / 264 CC 35 SCAR 39.6
CC
35
Cov
9%
disconnect
132.0
start_command
72.0
recv
56.0
CRAP 52.2
« Methods.pm ↑ Summary Access/xnm.pm »
1 #
2 # $Id: Access.pm,v 1.31 2003/03/02 11:12:09 dsw Exp $
3 #
4 # COPYRIGHT AND LICENSE
5 # Copyright (c) 2001-2003, Juniper Networks, Inc.
6 # All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 # 1. Redistributions of source code must retain the above
11 # copyright notice, this list of conditions and the following
12 # disclaimer.
13 # 2. Redistributions in binary form must reproduce the above
14 # copyright notice, this list of conditions and the following disclaimer
15 # in the documentation and/or other materials provided with the
16 # distribution.
17 # 3. The name of the copyright owner may not be used to
18 # endorse or promote products derived from this software without specific
19 # prior written permission.
20 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 # DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 # STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
29 # IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 # POSSIBILITY OF SUCH DAMAGE.
31 #
32 # The JUNOS::Access package is meant to isolate the various access
33 # methods from the rest of the code.
34 #
35  
36 package JUNOS::Access;
37  
38 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use JUNOS::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use IO::Pty;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 #
44 # POSIX::Termios is an odd API; we need to create a 'termios'
45 # controller to pass all the requests thru. A single controller
46 # can be used against a number of handles, so we'll go ahead and
47 # make one up front.
48 #
49 2 ▶use POSIX qw(:termios_h setsid);
Subroutine BEGIN: called CC=1 SCAR=0.0
50 2 ▶use vars qw($termios);
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
53 2 $termios = new POSIX::Termios;
54 }
55  
56 sub new
57 0 ▶{
Subroutine new: not called CC=4 SCAR=30.0
Pod undocumented
58 0 my($class, $args) = @_;
59 0 my %args = %$args;
60 0 my $self = { %args };
61 0 $self->{JUNOS_Device} = $args;
62  
63 0 ▶ $class = ref $class || $class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
64 0 my $access = $args{access};
65 0 ▶ if ($args{access}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"access"}) 0 0 0
66 0 $access =~ s/-/_/g;
67 }
68  
69 0 ▶ $class .= "::" . $access if $access;
Branch: 1 branch
BranchTrueFalseTotal
if $access 0 0 0
70  
71 0 bless $self, $class;
72 }
73  
74 sub start
75 0 ▶{
Subroutine start: not called CC=1 SCAR=6.9
Pod undocumented
76 0 my($self) = @_;
77 0 $self->{JUNOS_Device}->report_error("no " . ref $_[0] . "::start defined");
78 0 undef;
79 }
80  
81 sub connect
82 0 ▶{
Subroutine connect: not called CC=1 SCAR=6.9
Pod undocumented
83 0 my($self, %args) = @_;
84  
85 0 tracept("IO");
86 0 $self->start(%args);
87 }
88  
89 sub disconnect
90 0 ▶{
Subroutine disconnect: not called CC=11 SCAR=48.8
Pod undocumented
91 0 my($self) = @_;
92 0 my($dead, $deadline);
93 0 my $kid = $self->{PID};
94  
95 0 ▶ if( $self->{INPUT} ) { close($self->{INPUT}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"INPUT"}) 0 0 0
96 0 ▶ if( $self->{OUTPUT} ) { close($self->{OUTPUT}); }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"OUTPUT"}) 0 0 0
97 0 $self->{INPUT} = undef;
98 0 $self->{OUTPUT} = undef;
99  
100 # Wait 5 seconds for the child to die a natural death.
101 0 ▶ for ($deadline = time() + 5; $deadline < time();) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deadline < time) 0 0 0
102 0 $dead = waitpid($kid, &WNOHANG);
103 0 ▶ return 1 if $dead == $kid;
Branch: 1 branch
BranchTrueFalseTotal
if $dead == $kid 0 0 0
104 0 sleep 1;
105 }
106  
107 # Ask nicely...
108 0 ▶ return 1 if kill("TERM", $kid) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if kill("TERM", $kid) == 0 0 0 0
109  
110 # Wait 10 seconds to see if they've got manners.
111 0 ▶ for ($deadline = time() + 10; $deadline < time();) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deadline < time) 0 0 0
112 0 $dead = waitpid($kid, &WNOHANG);
113 0 ▶ return 1 if $dead == $kid;
Branch: 1 branch
BranchTrueFalseTotal
if $dead == $kid 0 0 0
114 0 sleep 1;
115 }
116  
117 # No more Mister Nice Guy....
118 0 ▶ return 1 if kill("KILL", $kid) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if kill("KILL", $kid) == 0 0 0 0
119  
120 # Wait 15 seconds for the real death....
121 0 ▶ for ($deadline = time() + 15; $deadline < time();) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deadline < time) 0 0 0
122 0 $dead = waitpid($kid, &WNOHANG);
123 0 ▶ return 1 if $dead == $kid;
Branch: 1 branch
BranchTrueFalseTotal
if $dead == $kid 0 0 0
124 0 sleep 1;
125 }
126  
127 # Nothing worked. Sigh...
128 0 return undef;
129 }
130  
131 sub send
132 0 ▶{
Subroutine send: not called CC=3 SCAR=24.8
Pod undocumented
133 0 my($self, @data) = @_;
134 0 my $data = join("", @data);
135  
136 0 trace("IO", "send: [[[[[$data]]]]]");
137 0 tracept("IO");
138  
139 0 ▶ return if (!$self->{OUTPUT});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"OUTPUT"} 0 0 0
140  
141 #$self->{send_data} .= $data;
142 #print "SEND: $data\n";
143 0 my $rc = syswrite($self->{OUTPUT}, $data, length($data));
144 0 ▶ $self->{seen_eof} = 1 if $rc <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rc <= 0 0 0 0
145 0 $rc;
146 }
147  
148 sub recv
149 0 ▶{
Subroutine recv: not called CC=7 SCAR=40.3
Pod undocumented
150 0 my($self, $timeout) = @_;
151 0 my $data;
152  
153 0 tracept("IO");
154 0 ▶ return if (!$self->{INPUT});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"INPUT"} 0 0 0
155 #print "SSF:\n",$self->{send_data},"\n";
156 #print "Going to sysread...\n";
157 0 my $len = sysread($self->{INPUT}, $data, 0x2000);
158 #print "Done sysread $len\n";
159  
160 0 ▶ $self->{seen_eof} = 1 if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 0 0 0
161 0 ▶ $self->{seen_eof} = 1 if $len == 0 && eof($self->{INPUT});
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 and CORE::eof $self->{"INPUT"} 0 0 0
Condition0%0/3
$len == 0 and CORE::eof $self->{"INPUT"}
!ll&&!rl&&r
000
MC/DC0%0/2
$len == 0 and CORE::eof $self->{"INPUT"}
$len == 0 CORE::eof $self->{"INPUT"}
Truth table0%0/3
A and B
A= $len == 0B= CORE::eof $self->{"INPUT"}
ABresult
0X0
100
111
162 0 ▶ if ($len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) 0 0 0
163 0 ▶ $self->{JUNOS_Device}->report_error("recv failed: " . (eof($self->{INPUT}) ? "EOF" : "dead"));
Branch: 1 branch
BranchTrueFalseTotal
CORE::eof $self->{'INPUT'} ? : 0 0 0
164 0 return;
165 }
166  
167 #print "RECV: $data\n";
168 0 trace("IO", "recv: $len [[[[[$data]]]]]");
169  
170 0 $data;
171 }
172  
173 #
174 # Have we hit end-of-file yet?
175 #
176 sub eof
177 0 ▶{
Subroutine eof: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my($self) = @_;
179  
180 0 $self->{seen_eof};
181 }
182  
183 #
184 # start a command using unix-domain socket pairs; not currently used
185 #
186 sub start_command_sockets
187 0 ▶{
Subroutine start_command_sockets: not called CC=6 SCAR=37.4
Pod undocumented
188 0 my $self = shift;
189  
190 0 tracept("IO");
191  
192 0 my($s1a, $s1b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, 0);
193 0 my($s2a, $s2b) = IO::Socket->socketpair(AF_UNIX, SOCK_STREAM, 0);
194  
195 0 my $pid = fork;
196 0 ▶ if ($pid < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid < 0) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
197 0 $self->{JUNOS_Device}->report_error("fork failed");
198 0 return;
199 } elsif ($pid == 0) {
200 0 trace("Child", "Child $pid/", $$, " cmd: ", join(" ", @_));
201 0 trace("Child", "Child FH: ", fileno($s1a), "/", fileno($s2a));
202  
203 0 ▶ open(STDIN, "<&=" . fileno($s1a)) || die "dup of stdin failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&=' . fileno($s1a) or die "dup of stdin failed" 0 0 0
204 0 ▶ open(STDOUT, ">&=" . fileno($s2a)) || die "dup of stdout failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&=' . fileno($s2a) or die "dup of stdout failed" 0 0 0
205 0 ▶ open(STDERR, ">&STDOUT") || die "dup of stderr failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "dup of stderr failed" 0 0 0
206  
207 0 shutdown($s1a, 1);
208 0 close($s1b);
209 0 shutdown($s2a, 0);
210 0 close($s2b);
211  
212 0 exec @_;
213 0 die;
214  
215 } else {
216  
217 0 close($s1a);
218 0 shutdown($s1b, 0);
219 0 close($s2a);
220 0 shutdown($s2b, 1);
221  
222 0 $self->{INPUT} = $s2b;
223 0 $self->{OUTPUT} = $s1b;
224 }
225  
226 0 1;
227 }
228  
229 #
230 # Initialize a tty's termio setting: turn off echo.
231 #
232 sub init_tty
233 0 ▶{
Subroutine init_tty: not called CC=2 SCAR=17.9
Pod undocumented
234 0 my($self, $fd) = @_;
235  
236 0 ▶ unless ($termios->getattr(fileno($fd))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($JUNOS::Access::termios->getattr(fileno $fd)) 0 0 0
237 0 $self->{JUNOS_Device}->report_error("getattr failed on pty");
238 0 return;
239 }
240 0 my $lflags = $termios->getlflag;
241 0 $lflags &= ~(&ECHO);
242 0 $termios->setlflag($lflags);
243 0 $termios->setattr(fileno($fd));
244 0 1;
245 }
246  
247 #
248 # Start a command in a sub-process under a pty
249 #
250 sub start_command
251 0 ▶{
Subroutine start_command: not called CC=8 SCAR=42.8
Pod undocumented
252 0 my $self = shift;
253  
254 0 tracept("IO");
255  
256 0 my $pty = new IO::Pty;
257 0 my $slave = $pty->slave;
258  
259 0 ▶ unless ($slave) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($slave) 0 0 0
260 0 $self->{JUNOS_Device}->report_error("cannot create pty slave");
261 0 return;
262 }
263
264 0 trace("IO", "Access: pty $pty $$pty ", $pty->ttyname);
265 0 trace("IO", "Access: slv $slave $$slave ", $slave->ttyname);
266  
267 0 ▶ &init_tty($self, $slave) || return;
Branch: 1 branch
BranchTrueFalseTotal
&init_tty($self, $slave) or return 0 0 0
268  
269 0 my $pid = fork;
270 0 ▶ if ($pid < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pid < 0) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
271 0 $self->{JUNOS_Device}->report_error("fork failed");
272 0 return;
273 } elsif ($pid == 0) {
274 0 trace("Child", "Child $pid/", $$, " cmd: ", join(" ", @_));
275 0 trace("Child", "Child FH: ", fileno($pty), "/", fileno($slave));
276  
277 # Turn the pty/tty into our stdin/stdout/stderr
278 0 close(STDIN);
279 0 ▶ open(STDIN, "<&" . fileno($slave)) || die "dup of stdin failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDIN, '<&' . fileno($slave) or die "dup of stdin failed" 0 0 0
280 0 shutdown(STDIN, 1);
281 0 close(STDOUT);
282 0 ▶ open(STDOUT, ">&" . fileno($slave)) || die "dup of stdout failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, '>&' . fileno($slave) or die "dup of stdout failed" 0 0 0
283 0 shutdown(STDOUT, 0);
284 0 close(STDERR);
285 0 ▶ open(STDERR, ">&STDOUT") || die "dup of stderr failed";
Branch: 1 branch
BranchTrueFalseTotal
open STDERR, ">&STDOUT" or die "dup of stderr failed" 0 0 0
286  
287 #
288 # We need to disassociate ourselves from the controlling tty.
289 # It would be nice to make the pty/tty we just made into our
290 # controlling tty, but this does not seem to be working....
291 #
292 0 setsid;
293  
294 # Close off the original pty/tty handles
295 0 close($slave);
296 0 close($pty);
297  
298 0 exec @_;
299 0 die;
300  
301 } else {
302 # Close off the slave side of pty/tty pair
303 0 close($slave);
304  
305 # Record the pty side for later i/o
306 0 $self->{INPUT} = $pty;
307 0 $self->{OUTPUT} = $pty;
308 }
309  
310 # Record the pid of the process we just made
311 0 $self->{PID} = $pid;
312  
313 # If something bad happens kill this process off...
314 $SIG{INT} = $SIG{__DIE__} = $SIG{HUP} = $SIG{TERM} =
315 0 ▶ sub { kill 9, $pid; exit };
Subroutine __ANON__: not called CC=1 SCAR=6.9
316  
317 0 1;
318 }
319  
320 sub incoming
321 0 ▶{
Subroutine incoming: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my($self) = @_;
323 0 $self->{JUNOS_Device}->report_error("Unhandled incoming data");
324 0 return;
325 }
326  
327 #
328 # Implemented by subclass if necessary (xnm_auth stuff for instance)
329 #
330 0 ▶sub authenticate {
Subroutine authenticate: not called CC=1 SCAR=6.9
Pod undocumented
331 0 1;
332 }
333  
334 2 1;
335  
336 __END__
« Methods.pm ↑ Summary Access/xnm.pm »