transform_exceptions
);
+has lsof_command => (is => 'ro', default => sub { 'lsof' });
+
sub gather {
my ($self) = @_;
return transform_exceptions {
sub _open_lsof_pipe {
my ($self) = @_;
- return handle_from_command 'lsof -F0';
+ my $lsof = $self->lsof_command;
+ return handle_from_command "$lsof -F0";
}
1;
package System::Introspector::Util;
use Exporter 'import';
use IPC::Run qw( run );
+use IPC::Open2;
+use File::Spec;
+use Scalar::Util qw( blessed );
our @EXPORT_OK = qw(
handle_from_command
sub handle_from_command {
my ($command) = @_;
- open my $pipe, '-|', $command
- or fail "Unable to read from command '$command': $!";
+ my $pipe;
+ local $@;
+ my $ok = eval {
+ my $out;
+ my $child_pid = open2($out, File::Spec->devnull, $command);
+ my @lines = <$out>;
+ waitpid $child_pid, 0;
+ my $content = join '', @lines;
+ my $status = $? >> 8;
+ open $pipe, '<', \$content;
+ 1;
+ };
+ unless ($ok) {
+ my $err = $@;
+ die $err
+ if blessed($err) and $err->isa('System::Introspector::_Exception');
+ fail "Error from command '$command': $err";
+ }
return $pipe;
}
ok($handles, 'received filehandle data');
ok(not(grep { not keys %$_ } @$handles), 'keys in all entries');
+do {
+ my $fail_probe = System::Introspector::Probe::FileHandles->new(
+ lsof_command => 'lsoffakethisonedoesntexistatleastihopenot',
+ );
+ my $fail_data;
+ $fail_data = $fail_probe->gather;
+ ok $fail_data, 'received data';
+};
+
done_testing;