| 1 | package IO::Trace; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 47 | ▶ | use 5.006000; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 47 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 47 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 47 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 47 | ▶ | use Getopt::Long qw(GetOptions Configure); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 47 | ▶ | use IPC::Open3 qw(open3); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 47 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 47 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 47 | ▶ | use IO::File qw(O_WRONLY O_TRUNC O_CREAT SEEK_CUR); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 47 | our @EXPORT = qw(iotrace); | |||||||||||||||||||||||||||||||||||||||||
| 14 | 47 | our $VERSION = '0.024'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | # Magic Timer Settings | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 47 | our $has_hires = eval { require Time::HiRes; 1 }; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 47 | our $patience_idle = 8000; # Seconds to wait for I/O while process is still running | |||||||||||||||||||||||||||||||||||||||||
| 19 | 47 | ▶ | our $patience_kill_mute = $has_hires ? 5.2 : 6; # Seconds to wait for "mute" process to terminate. If all its handles are closed, then force SIGTERM and SIGKILL if still running after waiting this long. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 47 | our $heartbeat_grind_mute = 0.2; # Interval Seconds between SIGCHLD check to test if "mute" child died yet. | |||||||||||||||||||||||||||||||||||||||||
| 21 | 47 | our $implicit_close_before_chld = 0.08; # Maximum Seconds to wait for SIGCHLD after all handles have been closed in order to consider it an implicit close instead of explicit close. | |||||||||||||||||||||||||||||||||||||||||
| 22 | 47 | ▶ | our $implicit_chld_before_close = $has_hires ? 0.25 : 1; # Maximum Seconds after receiving SIGCHLD to consider a close to be implicit. Any longer is considered an explicit close. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 47 | ▶ | our $patience_zombie_breather = $has_hires ? 5.6 : 7; # Maximum Seconds of uninterrupted silence after receiving SIGCHLD to wait for the process to close all handles. Any longer, then all open pipes are implicitly slapped closed to sufficate the zombie process. Then it is reaped to log the correct exit status, and the descent process should receive a SIGPIPE if ever attempting to write to its output handle in the future. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 316 | ▶ | sub now { $has_hires ? Time::HiRes::time() : time } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
now: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub usage { | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 2 | ▶ | $0 =~ /([^\/]+)$/ and return "Usage> $1 -o <output_log> CMD [ARGS]\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
usage: called CC=2 SCAR=7.6
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | sub iotrace { | ||||||||||||||||||||||||||||||||||||||||||
| 32 | 46 | ▶ | my $self = __PACKAGE__->new; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
iotrace: called CC=9 SCAR=22.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 46 | ▶ | my @args = @_ ? @_ : @ARGV or die usage; # No args is Error | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | 45 | ▶ | $self->parse_commandline(@args) or die usage; # Broken args parsing is Error | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 45 | ▶ | $self->{help} and print usage and exit; # Showing --help usage is not Error | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"help"} and print usage()
MC/DC50%1/2
$self->{"help"} and print usage()
$self->{"help"}
print usage()
Truth table66%2/3
A and B
A= $self->{"help"}B= print usage()
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 44 | ▶ | $self->{version} and print "iotrace -- version $VERSION\n" and exit; # Show version | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"version"} and print "iotrace -- version $IO::Trace::VERSION\n"
MC/DC50%1/2
$self->{"version"} and print "iotrace -- version $IO::Trace::VERSION\n"
$self->{"version"}
print "iotrace -- version $IO::Trace::VERSION\n"
Truth table66%2/3
A and B
A= $self->{"version"}B= print "iotrace -- version $IO::Trace::VERSION\n"
| |||||||||||||||||||||||||||||||||||||||||||
| 37 | 43 | $self->{child_died} = 0; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 43 | ▶ | local $SIG{CHLD} = sub { $self->{child_died} = now; }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 43 | $self->run_trace; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 41 | my $exit_status = $self->finish_child; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 41 | ▶ | if (defined wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | return $exit_status; | |||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 41 | exit $exit_status; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | sub new { | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 46 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 46 | ▶ | my $args = shift || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 50 | 46 | return bless $args, $self; | |||||||||||||||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | sub parse_commandline { | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 45 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_commandline: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 45 | Configure(qw[require_order bundling]); | |||||||||||||||||||||||||||||||||||||||||
| 56 | 45 | local @ARGV = @_; | |||||||||||||||||||||||||||||||||||||||||
| 57 | my $ret = GetOptions | ||||||||||||||||||||||||||||||||||||||||||
| 58 | "V" => \($self->{version} = 0), | ||||||||||||||||||||||||||||||||||||||||||
| 59 | "o=s" => \($self->{output_log_file}), | ||||||||||||||||||||||||||||||||||||||||||
| 60 | "v+" => \($self->{verbose} = 0), | ||||||||||||||||||||||||||||||||||||||||||
| 61 | "x+" => \($self->{heX_ify} = 0), | ||||||||||||||||||||||||||||||||||||||||||
| 62 | "t+" => \($self->{timing} = 0), | ||||||||||||||||||||||||||||||||||||||||||
| 63 | "f+" => \($self->{follow_fork} = 0), | ||||||||||||||||||||||||||||||||||||||||||
| 64 | "q+" => \($self->{quiet} = 0), # Ignored | ||||||||||||||||||||||||||||||||||||||||||
| 65 | "s=i" => \($self->{size_of_strings}), # Ignored | ||||||||||||||||||||||||||||||||||||||||||
| 66 | "e=s" => \($self->{events}), # Ignored | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 45 | "help|h"=> \($self->{help}), | |||||||||||||||||||||||||||||||||||||||||
| 68 | ; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 45 | $self->{run} = [@ARGV]; | |||||||||||||||||||||||||||||||||||||||||
| 70 | 45 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | sub t { | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 276 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
t: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 276 | my $h = ""; | |||||||||||||||||||||||||||||||||||||||||
| 76 | 276 | ▶ | if ($self->{follow_fork} == 1) { $h .= "$self->{pid} "; } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | 276 | ▶ | if ($self->{timing}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 97 | ▶ | my $now = $self->{timing} > 1 ? now : time; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 97 | my @t = localtime $now; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 97 | $h .= sprintf "%02d:%02d:%02d", $t[2], $t[1], $t[0]; | |||||||||||||||||||||||||||||||||||||||||
| 81 | 97 | ▶ | $h .= sprintf ".%06d", 1000000*($now - int($now)) if $self->{timing} > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 97 | $h .= " "; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 276 | return $h; | |||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | sub log { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 276 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
log: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 89 | 276 | my $line = join "", @_; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 276 | $line =~ s/\s*$/\n/; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 276 | $self->{log}->print($self->t().$line); | |||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | # Escape strings like strace does | ||||||||||||||||||||||||||||||||||||||||||
| 95 | sub e { | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 282 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
e: called CC=3 SCAR=12.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 282 | my $chars = shift; | |||||||||||||||||||||||||||||||||||||||||
| 98 | 282 | ▶ | if ($self->{heX_ify} > 1) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | # -xx: Super Hex Encode everything | ||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $chars =~ s/([\s\S])/sprintf "\\x%02x", ord($1)/eg; | |||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||
| 102 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 103 | # Both \\ and \" really need to be escaped | ||||||||||||||||||||||||||||||||||||||||||
| 104 | # But add other helpful chars to make it easier for Perl to read too. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | 282 | $chars =~ s/([\\\"\'\$\@])/\\$1/g; | |||||||||||||||||||||||||||||||||||||||||
| 106 | # Special backslash escape chars for easy legibility | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 282 | $chars =~ s/\t/\\t/g; | |||||||||||||||||||||||||||||||||||||||||
| 108 | 282 | $chars =~ s/\r/\\r/g; | |||||||||||||||||||||||||||||||||||||||||
| 109 | 282 | $chars =~ s/\n/\\n/g; | |||||||||||||||||||||||||||||||||||||||||
| 110 | 282 | ▶ | if ($self->{heX_ify}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | # -x: Hex Encode only non-ascii chars | ||||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | $chars =~ s/([^\ -\~])/sprintf "\\x%02x", ord $1/eg; | |||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 115 | # Default is octal encoding non-ascii only | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 282 | $chars =~ s/([^\ -\~])/sprintf "\\%03o", ord $1/eg; | |||||||||||||||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 282 | return qq{"$chars"}; | |||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | sub run_trace { | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 43 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run_trace: called CC=4 SCAR=14.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 43 | ▶ | if ($self->{timing} > 1 and not $has_hires) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->{"timing"} > 1 and not $IO::Trace::has_hires
MC/DC0%0/2
$self->{"timing"} > 1 and not $IO::Trace::has_hires
$self->{"timing"} > 1
not $IO::Trace::has_hires
Truth table66%2/3
A and not(B)
A= $self->{"timing"} > 1B= not $IO::Trace::has_hires
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | # If requesting to log high precision, but without HiRes, then just change "-tt" to "-t" to show integer time | ||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | $self->{timing} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||
| 128 | 43 | $self->spawn; | |||||||||||||||||||||||||||||||||||||||||
| 129 | 41 | $self->open_output_log; | |||||||||||||||||||||||||||||||||||||||||
| 130 | 115 | ▶ | $self->log("execve(".$self->e($self->{full}).", [".join(', ', map { $self->e($_) } @{ $self->{run} }).'], '.( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | $self->{verbose} ? ( | ||||||||||||||||||||||||||||||||||||||||||
| 132 | 41 | '['.join(', ', map { $self->e("$_=$ENV{$_}") } sort keys %ENV).']' | |||||||||||||||||||||||||||||||||||||||||
| 133 | ) : | ||||||||||||||||||||||||||||||||||||||||||
| 134 | \%ENV.' /* '.(scalar keys %ENV).' vars */' | ||||||||||||||||||||||||||||||||||||||||||
| 135 | ).") = 0"); | ||||||||||||||||||||||||||||||||||||||||||
| 136 | 41 | $self->io_loop; | |||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | sub spawn { | ||||||||||||||||||||||||||||||||||||||||||
| 140 | 43 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=11 SCAR=24.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 141 | 43 | my $full = $self->{run}->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 142 | 43 | ▶ | if (eval { require File::Which; 1; } and $full =~ m{^([\w\-\.]+)$}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
eval {
do {
require File::Which;
1
}
} and $full =~ /^([\w\-\.]+)$/
MC/DC50%1/2
eval {
do {
require File::Which;
1
}
} and $full =~ /^([\w\-\.]+)$/
eval {
do {
require File::Which;
1
}
}
$full =~ /^([\w\-\.]+)$/
Truth table66%2/3
A and B
A= eval {
do {
require File::Which;
1
}
}B= $full =~ /^([\w\-\.]+)$/
| |||||||||||||||||||||||||||||||||||||||||||
| 143 | 5 | $full = File::Which::which($1); | |||||||||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||||||||||
| 145 | 43 | ▶ | if (!$full or $full =~ m{^/} && !-x $full) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
not $full or $full =~ m[^/] and not -x $full
$full =~ m[^/] and not -x $full
MC/DC66%2/3
not $full or $full =~ m[^/] and not -x $full
not $full
$full =~ m[^/]
not -x $full
Truth table75%3/4
not(A) or B and not(C)
A= not $fullB= $full =~ m[^/]C= not -x $full
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 2 | die "$self->{run}->[0]: No such file or directory\n"; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 41 | $self->{in} = IO::Handle->new; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 41 | $self->{out} = IO::Handle->new; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 41 | $self->{err} = IO::Handle->new; | |||||||||||||||||||||||||||||||||||||||||
| 151 | # open3 can't vivify STDERR from undef for some reason | ||||||||||||||||||||||||||||||||||||||||||
| 152 | 41 | my @r = @{ $self->{run} }; | |||||||||||||||||||||||||||||||||||||||||
| 153 | 41 | ▶ | $r[0] = $full if $full; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 154 | 41 | ▶ | $self->{full} = $full || $r[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$full || $r[0]
MC/DC0%0/2
$full || $r[0]
$full
$r[0]
Truth table33%1/3
A || B
A= $fullB= $r[0]
| |||||||||||||||||||||||||||||||||||||||||||
| 155 | # Launch target program | ||||||||||||||||||||||||||||||||||||||||||
| 156 | 41 | ▶ | $self->{pid} = open3 $self->{in}, $self->{out}, $self->{err}, @r or die "$r[0]: fork exec failure: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | # Map each handle to its corresponding handle | ||||||||||||||||||||||||||||||||||||||||||
| 158 | $self->{proxy} = { | ||||||||||||||||||||||||||||||||||||||||||
| 159 | fileno($self->{in}) => *STDIN, | ||||||||||||||||||||||||||||||||||||||||||
| 160 | fileno($self->{out}) => *STDOUT, | ||||||||||||||||||||||||||||||||||||||||||
| 161 | fileno($self->{err}) => *STDERR, | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | fileno(*STDIN) => $self->{in}, | ||||||||||||||||||||||||||||||||||||||||||
| 164 | fileno(*STDOUT) => $self->{out}, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | fileno(*STDERR) => $self->{err}, | ||||||||||||||||||||||||||||||||||||||||||
| 166 | 41 | }; | |||||||||||||||||||||||||||||||||||||||||
| 167 | 41 | $self->{implicitly_closed} = {}; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 41 | $self->{sel} = IO::Select->new(values %{ $self->{proxy} }); | |||||||||||||||||||||||||||||||||||||||||
| 169 | 41 | $self->{writers} = IO::Select->new($self->{in}, \*STDOUT, \*STDERR); | |||||||||||||||||||||||||||||||||||||||||
| 170 | 41 | foreach my $fh ($self->{writers}->handles) { | |||||||||||||||||||||||||||||||||||||||||
| 171 | # Test if writer handle is seek()able | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 123 | ▶ | if (seek $fh, 0, SEEK_CUR) { # Offset +0 from Current Position doesn't actually move the pointer | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | # YUP! Must be a real file. Don't monitor it since nobody will be able to close() the "other end" anyways. | ||||||||||||||||||||||||||||||||||||||||||
| 174 | # We must remove it from the Select loop to prevent super grinding since we always "can_read" a real file. | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 3 | $self->{sel}->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 176 | 3 | $self->{writers}->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | 41 | return $self->{pid}; | |||||||||||||||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | sub open_output_log { | ||||||||||||||||||||||||||||||||||||||||||
| 183 | 41 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
open_output_log: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 41 | my $output_log_file = $self->{output_log_file}; | |||||||||||||||||||||||||||||||||||||||||
| 185 | 41 | ▶ | if (defined $output_log_file) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 186 | 38 | ▶ | $output_log_file .= ".$self->{pid}" if $self->{follow_fork} > 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 38 | ▶ | $self->{log} = IO::File->new($output_log_file, O_WRONLY | O_TRUNC | O_CREAT ) or die "$output_log_file: open failure: $!\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | # Spew all the trace info mixed in with the normal STDERR stream if no -o option provided: | ||||||||||||||||||||||||||||||||||||||||||
| 191 | 3 | $self->{log} = IO::Handle->new_from_fd(fileno(STDERR), ">"); | |||||||||||||||||||||||||||||||||||||||||
| 192 | } | ||||||||||||||||||||||||||||||||||||||||||
| 193 | 41 | $self->{log}->autoflush(1); | |||||||||||||||||||||||||||||||||||||||||
| 194 | 41 | return $self->{log}; | |||||||||||||||||||||||||||||||||||||||||
| 195 | } | ||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||
| 197 | sub io_loop { | ||||||||||||||||||||||||||||||||||||||||||
| 198 | 41 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
io_loop: called CC=26 SCAR=32.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 199 | # Loop while still open handles or during brief implicit close detection | ||||||||||||||||||||||||||||||||||||||||||
| 200 | 41 | ▶ | while ($self->{sel}->handles or keys %{ $self->{implicitly_closed} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"sel"}->handles or keys %{$$self{"implicitly_closed"};}
MC/DC100%2/2
$self->{"sel"}->handles or keys %{$$self{"implicitly_closed"};}
$self->{"sel"}->handles
keys %{$$self{"implicitly_closed"};}
Truth table100%3/3
A or B
A= $self->{"sel"}->handlesB= keys %{$$self{"implicitly_closed"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | my $maximum_timeout = | ||||||||||||||||||||||||||||||||||||||||||
| 202 | 189 | ▶ | keys %{ $self->{implicitly_closed} } ? $implicit_close_before_chld : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 203 | 189 | ▶ | $self->{child_died} ? $patience_zombie_breather : | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 204 | $patience_idle; | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 189 | ▶ | my @ready = $self->{sel}->count ? $self->{sel}->can_read($maximum_timeout) : do {select undef,undef,undef, $maximum_timeout; ()}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 206 | 189 | foreach my $fh (@ready) { | |||||||||||||||||||||||||||||||||||||||||
| 207 | 189 | ▶ | defined(my $fn = fileno $fh) || next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 208 | 185 | ▶ | my $pr = $self->{proxy}->{$fn} or die "Fileno $fn: Impossible Implementation Crash! $!\n";; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 209 | # Find original fileno (STDIN=0, STDOUT=1, STDERR=2): | ||||||||||||||||||||||||||||||||||||||||||
| 210 | 185 | ▶ | my $real_fileno = $fn < 3 ? $fn : fileno($pr); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 211 | 185 | ▶ | if ($self->{writers}->exists($fh)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 212 | # $fh should only be written to, so if it's suddenly "READABLE", | ||||||||||||||||||||||||||||||||||||||||||
| 213 | # that means it woke up due to the pipe being close()d on the other end. | ||||||||||||||||||||||||||||||||||||||||||
| 214 | # Never attempt to actually read from a "writers" handle. | ||||||||||||||||||||||||||||||||||||||||||
| 215 | # Only log explicit close(). Don't bother if it's probably just an implicit close() upon exit. | ||||||||||||||||||||||||||||||||||||||||||
| 216 | 18 | ▶ | $self->log("close($real_fileno) = 0") if !$self->{child_died}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | # Close both sides since there's nowhere for any data to go anymore: | ||||||||||||||||||||||||||||||||||||||||||
| 218 | 18 | $self->{sel}->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 219 | 18 | $self->{sel}->remove($pr); | |||||||||||||||||||||||||||||||||||||||||
| 220 | 18 | $self->{writers}->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 221 | 18 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 222 | 18 | close $pr; | |||||||||||||||||||||||||||||||||||||||||
| 223 | 18 | next; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | 167 | my $bytes = sysread($fh, (my $buffer), 16384); | |||||||||||||||||||||||||||||||||||||||||
| 226 | # Only STDIN (fileno = 0) is "read", otherwise "write" | ||||||||||||||||||||||||||||||||||||||||||
| 227 | 167 | ▶ | my $op = $real_fileno ? "write" : "read"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 167 | ▶ | $self->log("$op($real_fileno, ".$self->e($buffer).", $bytes) = $bytes") if $bytes or $op eq "read"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$bytes or $op eq "read"
MC/DC100%2/2
$bytes or $op eq "read"
$bytes
$op eq "read"
Truth table100%3/3
A or B
A= $bytesB= $op eq "read"
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 167 | ▶ | if ($bytes) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | # Forward non-empty packet to the proxy file handle | ||||||||||||||||||||||||||||||||||||||||||
| 231 | 62 | syswrite($pr, $buffer, $bytes); | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 234 | # Getting ZERO bytes always means the file handle just closed. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | # Quit listening to this anymore: | ||||||||||||||||||||||||||||||||||||||||||
| 236 | 105 | $self->{sel}->remove($fh); | |||||||||||||||||||||||||||||||||||||||||
| 237 | # And then close the file handle for real: | ||||||||||||||||||||||||||||||||||||||||||
| 238 | 105 | close($fh); | |||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | # And quit listening on the corresponding handle too: | ||||||||||||||||||||||||||||||||||||||||||
| 241 | 105 | $self->{sel}->remove($pr); | |||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | 105 | ▶ | if ($real_fileno == 0 # Immediately close corresponding handle for STDIN immediately in order to signal target program its input stream has ended. | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$real_fileno == 0 or $self->{"child_died"} and now() > $self->{"child_died"} + $implicit_chld_before_close
$self->{"child_died"} and now() > $self->{"child_died"} + $implicit_chld_before_close
MC/DC33%1/3
$real_fileno == 0 or $self->{"child_died"} and now() > $self->{"child_died"} + $implicit_chld_before_close
$real_fileno == 0
$self->{"child_died"}
now() > $self->{"child_died"} + $implicit_chld_before_close
Truth table75%3/4
A or B and C
A= $real_fileno == 0B= $self->{"child_died"}C= now() > $self->{"child_died"} + $implicit_chld_before_close
| |||||||||||||||||||||||||||||||||||||||||||
| 244 | or $self->{child_died} && now > $self->{child_died} + $implicit_chld_before_close) { # Or if it exited long enough ago, then this must have been an explicit output handle close from a backgrounded process, and it's safe to log, (even if it was implicitly closed by the grandchild or descendent process). | ||||||||||||||||||||||||||||||||||||||||||
| 245 | 34 | close($pr); | |||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # If it was STDIN (fd 0), then the invoker probably closed it before the target program. But just log the close acting like the target program called close itself (even if STDIN is being ignored by the target program). | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 34 | $self->log("close($real_fileno) = 0"); | |||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||
| 250 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # An output handle (STDOUT fd 1 or STDERR fd 2) was just closed, but it might have just been an implicit close since the process is still running or exited too recently. | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # So don't log it yet. Just flag it for now. Then keep a close eye on the {child_died} timer to determine which way it was. | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # The goal is to mimic the same behavior as the target process whether to do an implicit or explicit close. | ||||||||||||||||||||||||||||||||||||||||||
| 254 | 71 | $self->{implicitly_closed}->{$real_fileno} = $pr; | |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||
| 258 | 189 | ▶ | if (!@ready and keys %{ $self->{implicitly_closed} }) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not @ready and keys %{$$self{"implicitly_closed"};}
MC/DC100%2/2
not @ready and keys %{$$self{"implicitly_closed"};}
not @ready
keys %{$$self{"implicitly_closed"};}
Truth table100%3/3
not(A) and B
A= not @readyB= keys %{$$self{"implicitly_closed"};}
| |||||||||||||||||||||||||||||||||||||||||||
| 259 | # Implicit detection timeout exceeded. No more waiting allowed. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | # All implicitly_closed file handles must be imminently closed! | ||||||||||||||||||||||||||||||||||||||||||
| 261 | 42 | ▶ | if (!$self->{sel}->count and $self->{child_died}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not $self->{"sel"}->count and $self->{"child_died"}
MC/DC100%2/2
not $self->{"sel"}->count and $self->{"child_died"}
not $self->{"sel"}->count
$self->{"child_died"}
Truth table100%3/3
not(A) and B
A= not $self->{"sel"}->countB= $self->{"child_died"}
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | # Must exit immediately in order to implicitly close any implicitly_closed handles | ||||||||||||||||||||||||||||||||||||||||||
| 263 | 33 | last; | |||||||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||||||||
| 265 | # Otherwise, must explicitly close them all now and log it, then continue the select loop. | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 9 | foreach my $fileno (keys %{ $self->{implicitly_closed} }) { | |||||||||||||||||||||||||||||||||||||||||
| 267 | 9 | close(delete $self->{implicitly_closed}->{$fileno}); | |||||||||||||||||||||||||||||||||||||||||
| 268 | 9 | $self->log("close($fileno) = 0"); | |||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||||||||||||||||
| 271 | 156 | ▶ | if ($self->{child_died}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 272 | 39 | ▶ | if (now > $self->{child_died} + $patience_zombie_breather) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Ran out of patience. Just leave to implicitly close all handles. | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||
| 276 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 277 | # Zombie handle said something, so restart the timer, and start the waiting all over again.. | ||||||||||||||||||||||||||||||||||||||||||
| 278 | 39 | $self->{child_died} = now; | |||||||||||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | sub finish_child { | ||||||||||||||||||||||||||||||||||||||||||
| 285 | 41 | ▶ | my $self = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
finish_child: called CC=10 SCAR=28.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 286 | 41 | ▶ | if (!$self->{child_died}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 287 | # If process still running, then all handles must have already been closed. So just wait a little bit for termination. | ||||||||||||||||||||||||||||||||||||||||||
| 288 | 8 | my $patient = $patience_kill_mute + now; | |||||||||||||||||||||||||||||||||||||||||
| 289 | 8 | ▶ | select undef,undef,undef,$heartbeat_grind_mute while !$self->{child_died} and $patient > now; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $self->{"child_died"} and $patient > now()
MC/DC50%1/2
not $self->{"child_died"} and $patient > now()
not $self->{"child_died"}
$patient > now()
Truth table66%2/3
not(A) and B
A= not $self->{"child_died"}B= $patient > now()
| |||||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||||
| 291 | # If STILL running, then offer a little bit more help to terminate | ||||||||||||||||||||||||||||||||||||||||||
| 292 | 8 | ▶ | !$self->{child_died} and kill TERM => $self->{pid} and sleep 1 and kill KILL => $self->{pid}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
not $self->{"child_died"} and kill "TERM", $self->{"pid"} and sleep 1
not $self->{"child_died"} and kill "TERM", $self->{"pid"}
MC/DC0%0/3
not $self->{"child_died"} and kill "TERM", $self->{"pid"} and sleep 1
not $self->{"child_died"}
kill "TERM", $self->{"pid"}
sleep 1
Truth table25%1/4
not(A) and B and C
A= not $self->{"child_died"}B= kill "TERM", $self->{"pid"}C= sleep 1
| |||||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||
| 295 | # Block waiting for process to exit | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 41 | waitpid( $self->{pid}, 0 ); | |||||||||||||||||||||||||||||||||||||||||
| 297 | 41 | $self->{child_error} = $?; | |||||||||||||||||||||||||||||||||||||||||
| 298 | 41 | my $signal = $self->{child_error} & 127; | |||||||||||||||||||||||||||||||||||||||||
| 299 | 41 | my $child_exit_status = $self->{child_error} >> 8; | |||||||||||||||||||||||||||||||||||||||||
| 300 | 41 | ▶ | if ($signal) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | 0 | ▶ | my $sig_name = eval { require Config; %Config::Config and [split / /, $Config::Config{sig_name}]->[$signal] } || $signal; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
eval {
do {
require Config;
%Config::Config and [split(/ /, $Config::Config{'sig_name'}, 0)]->[$signal]
}
} || $signal
MC/DC0%0/2
eval {
do {
require Config;
%Config::Config and [split(/ /, $Config::Config{'sig_name'}, 0)]->[$signal]
}
} || $signal
eval {
do {
require Config;
%Config::Config and [split(/ /, $Config::Config{'sig_name'}, 0)]->[$signal]
}
}
$signal
Truth table0%0/3
A || B
A= eval {
do {
require Config;
%Config::Config and [split(/ /, $Config::Config{'sig_name'}, 0)]->[$signal]
}
}B= $signal
| |||||||||||||||||||||||||||||||||||||||||||
| 302 | 0 | $self->log("--- GOT SIG$sig_name ($signal) ---"); | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | 41 | $self->log("exit_group($child_exit_status) = ?"); | |||||||||||||||||||||||||||||||||||||||||
| 305 | 41 | $self->log("+++ exited with $child_exit_status +++"); | |||||||||||||||||||||||||||||||||||||||||
| 306 | 41 | $self->{log}->close; | |||||||||||||||||||||||||||||||||||||||||
| 307 | 41 | return $child_exit_status; | |||||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | 47 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 311 | __END__ | ||||||||||||||||||||||||||||||||||||||||||