OpenSSH/OSTracer.pm untested

filter: Statementstmt 0.0% 0 / 54 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 111 CC 16 SCAR 54.4
CC
16
Cov
0%
trace
272.0
BEGIN
2.0
BEGIN
2.0
CRAP 229.4
1 package Net::OpenSSH::OSTracer;
2  
3 our $VERSION = '0.65_06';
4  
5 use strict;
6 use warnings;
7  
8 use POSIX;
9  
10 our $cmd;
11 our $type;
12 our $output;
13 our $sudo;
14 our $delay;
15  
16 our @EXTRA_ARGS;
17  
18 my %type_by_os = (linux => 'strace',
19 openbsd => 'ktrace',
20 freebsd => 'ktrace',
21 netbsd => 'ktrace',
22 bsd => 'ktrace',
23 'hp-ux' => 'tusc',
24 aix => 'truss',
25 solaris => 'truss');
26  
27 sub trace {
28 my $class = shift;
29 my ($cmd, $type) = ($cmd, $type); # copy globals
30  
31  
32 if (not defined $type) {
33 my $os = lc $^O;
34 if ( defined $cmd and $cmd =~ /([sk]trace|k?truss|tusc)$/) {
35 $type = $1;
36 }
37 elsif ($os =~ /(linux|openbsd|freebsd|netbsd|bsd|hp-ux|aix|solaris)/) {
38 $type = $type_by_os{$1};
39 }
40 else {
41 Net::OpenSSH::_debug("unable to determine tracer type for OS $os");
42 return;
43 }
44 }
45  
46 my $output1 = (defined $output ? $output : "/tmp/net_openssh_master") . ".$$";
47 my $file = "$output1.$type";
48 my $err = "$output1.txt";
49  
50 $cmd = $type unless defined $cmd;
51  
52 my @args;
53 if ($type eq 'strace') {
54 @args = (-o => $file, -p => $$, -s => 1024, '-fx');
55 }
56 elsif ($type eq 'ktruss') {
57 @args = (-o => $file, -p => $$, -m => 1024, '-d');
58 }
59 elsif ($type eq 'ktrace') {
60 @args = (-f => $file, -p => $$, '-id');
61 }
62 elsif ($type eq 'tusc') {
63 @args = (-o => $file, -b => 1024, '-fa', $$)
64 }
65 elsif ($type eq 'truss') {
66 @args = (-o => $file, -faep => $$);
67 }
68 else {
69 Net::OpenSSH::_debug("tracer type $type not supported");
70 return
71 }
72  
73 my @cmd = (defined $sudo ? ($sudo, '-A', $cmd) : $cmd);
74  
75 my $pid = fork;
76 unless ($pid) {
77 unless (defined $pid) {
78 Net::OpenSSH::_debug("unable to launch tracer, fork failed: $!");
79 return;
80 }
81 my ($in, $out);
82 if (open $in, '</dev/null' and
83 open $out, '>', $err and
84 POSIX::dup2(fileno $in, 0) and
85 POSIX::dup2(fileno $out, 1) and
86 POSIX::dup2(fileno $out, 2)) {
87 exec (@cmd, @EXTRA_ARGS, @args);
88 }
89 else {
90 eval { Net::OpenSSH::_debug("Unable to redirect tracer IO: $!") };
91 }
92 POSIX::_exit(1);
93 }
94 sleep (defined $delay ? $delay : 1); # wait for the tracer to come up
95 Net::OpenSSH::_debug("tracer attached, ssh pid: $$, tracer pid: $pid");
96 1;
97 }
98  
99 1;
100  
101 __END__