| 1 |
package FFI::Probe::Runner; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use 5.008004; |
| 6 |
use Capture::Tiny qw( capture ); |
| 7 |
use FFI::Probe::Runner::Result; |
| 8 |
|
| 9 |
# ABSTRACT: Probe runner for FFI |
| 10 |
our $VERSION = '2.12'; # VERSION |
| 11 |
|
| 12 |
|
| 13 |
sub new |
| 14 |
{ |
| 15 |
my($class, %args) = @_; |
| 16 |
|
| 17 |
$args{exe} ||= do { |
| 18 |
require FFI::Platypus::ShareConfig; |
| 19 |
require File::Spec; |
| 20 |
require Config; |
| 21 |
File::Spec->catfile(FFI::Platypus::ShareConfig::dist_dir('FFI::Platypus'), 'probe', 'bin', "dlrun$Config::Config{exe_ext}"); |
| 22 |
}; |
| 23 |
|
| 24 |
defined $args{flags} or $args{flags} = '-'; |
| 25 |
|
| 26 |
die "probe runner executable not found at: $args{exe}" unless -f $args{exe}; |
| 27 |
|
| 28 |
my $self = bless { |
| 29 |
exe => $args{exe}, |
| 30 |
flags => $args{flags}, |
| 31 |
}, $class; |
| 32 |
$self; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
sub exe { shift->{exe} } |
| 37 |
sub flags { shift->{flags} } |
| 38 |
|
| 39 |
|
| 40 |
sub verify |
| 41 |
{ |
| 42 |
my($self) = @_; |
| 43 |
my $exe = $self->exe; |
| 44 |
my($out, $err, $ret) = capture { |
| 45 |
$! = 0; |
| 46 |
system $exe, 'verify', 'self'; |
| 47 |
}; |
| 48 |
return 1 if $ret == 0 && $out =~ /dlrun verify self ok/; |
| 49 |
print $out; |
| 50 |
print STDERR $err; |
| 51 |
die "verify failed"; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
sub run |
| 56 |
{ |
| 57 |
my($self, $dll, @args) = @_; |
| 58 |
my $exe = $self->exe; |
| 59 |
my $flags = $self->flags; |
| 60 |
my($out, $err, $ret) = capture { |
| 61 |
my @cmd = ($exe, $dll, $flags, @args); |
| 62 |
$! = 0; |
| 63 |
system @cmd; |
| 64 |
$?; |
| 65 |
}; |
| 66 |
FFI::Probe::Runner::Result->new( |
| 67 |
stdout => $out, |
| 68 |
stderr => $err, |
| 69 |
rv => $ret >> 8, |
| 70 |
signal => $ret & 127, |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
1; |
| 75 |
|
| 76 |
__END__ |
| 77 |
|
| 78 |
=pod |
| 79 |
|
| 80 |
=encoding UTF-8 |
| 81 |
|
| 82 |
=head1 NAME |
| 83 |
|
| 84 |
FFI::Probe::Runner - Probe runner for FFI |
| 85 |
|
| 86 |
=head1 VERSION |
| 87 |
|
| 88 |
version 2.12 |
| 89 |
|
| 90 |
=head1 SYNOPSIS |
| 91 |
|
| 92 |
use FFI::Probe::Runner; |
| 93 |
|
| 94 |
my $runner = FFI::Probe::Runner->new; |
| 95 |
$runner->run('foo.so'); |
| 96 |
|
| 97 |
=head1 DESCRIPTION |
| 98 |
|
| 99 |
This class executes code in a dynamic library for probing and detecting platform |
| 100 |
properties. |
| 101 |
|
| 102 |
=head1 CONSTRUCTOR |
| 103 |
|
| 104 |
=head2 new |
| 105 |
|
| 106 |
my $runner = FFI::Probe::Runner->new(%args); |
| 107 |
|
| 108 |
Creates a new instance. |
| 109 |
|
| 110 |
=over 4 |
| 111 |
|
| 112 |
=item exe |
| 113 |
|
| 114 |
The path to the dlrun wrapper. The default is usually correct. |
| 115 |
|
| 116 |
=item flags |
| 117 |
|
| 118 |
The flags to pass into C<dlopen>. The default is C<RTLD_LAZY> on Unix |
| 119 |
and C<0> on windows.. |
| 120 |
|
| 121 |
=back |
| 122 |
|
| 123 |
=head1 METHODS |
| 124 |
|
| 125 |
=head2 exe |
| 126 |
|
| 127 |
my $exe = $runner->exe; |
| 128 |
|
| 129 |
The path to the dlrun wrapper. |
| 130 |
|
| 131 |
=head2 flags |
| 132 |
|
| 133 |
my $flags = $runner->flags; |
| 134 |
|
| 135 |
The flags to pass into C<dlopen>. |
| 136 |
|
| 137 |
=head2 verify |
| 138 |
|
| 139 |
$runner->verify; |
| 140 |
|
| 141 |
Verifies the dlrun wrapper is working. Throws an exception in the event of failure. |
| 142 |
|
| 143 |
=head2 run |
| 144 |
|
| 145 |
$runner->run($dll, @args); |
| 146 |
|
| 147 |
Runs the C<dlmain> function in the given dynamic library, passing in the |
| 148 |
given arguments. Returns a L<FFI::Probe::Runner::Result> object which |
| 149 |
contains the results. |
| 150 |
|
| 151 |
=head1 AUTHOR |
| 152 |
|
| 153 |
Author: Graham Ollis E<lt>[email protected]<gt> |
| 154 |
|
| 155 |
Contributors: |
| 156 |
|
| 157 |
Bakkiaraj Murugesan (bakkiaraj) |
| 158 |
|
| 159 |
Dylan Cali (calid) |
| 160 |
|
| 161 |
pipcet |
| 162 |
|
| 163 |
Zaki Mughal (zmughal) |
| 164 |
|
| 165 |
Fitz Elliott (felliott) |
| 166 |
|
| 167 |
Vickenty Fesunov (vyf) |
| 168 |
|
| 169 |
Gregor Herrmann (gregoa) |
| 170 |
|
| 171 |
Shlomi Fish (shlomif) |
| 172 |
|
| 173 |
Damyan Ivanov |
| 174 |
|
| 175 |
Ilya Pavlov (Ilya33) |
| 176 |
|
| 177 |
Petr Písař (ppisar) |
| 178 |
|
| 179 |
Mohammad S Anwar (MANWAR) |
| 180 |
|
| 181 |
Håkon Hægland (hakonhagland, HAKONH) |
| 182 |
|
| 183 |
Meredith (merrilymeredith, MHOWARD) |
| 184 |
|
| 185 |
Diab Jerius (DJERIUS) |
| 186 |
|
| 187 |
Eric Brine (IKEGAMI) |
| 188 |
|
| 189 |
szTheory |
| 190 |
|
| 191 |
José Joaquín Atria (JJATRIA) |
| 192 |
|
| 193 |
Pete Houston (openstrike, HOUSTON) |
| 194 |
|
| 195 |
Lukas Mai (MAUKE) |
| 196 |
|
| 197 |
=head1 COPYRIGHT AND LICENSE |
| 198 |
|
| 199 |
This software is copyright (c) 2015-2022 by Graham Ollis. |
| 200 |
|
| 201 |
This is free software; you can redistribute it and/or modify it under |
| 202 |
the same terms as the Perl 5 programming language system itself. |
| 203 |
|
| 204 |
=cut |