| 1 | package Sys::GNU::ldconfig; | ||||||||||||||||||||||||||||
| 2 | # $Id: ldconfig.pm 2456 2023-07-06 23:06:00Z fil $ | ||||||||||||||||||||||||||||
| 3 | # Copyright 2013 Philip Gwyn - All rights reserved | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use 5.00405; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use vars qw($VERSION @ISA @EXPORT); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | require Exporter; | |||||||||||||||||||||||||||
| 14 | 2 | @ISA = qw( Exporter ); | |||||||||||||||||||||||||||
| 15 | 2 | @EXPORT = qw( ld_lookup ld_root ); | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | sub DEBUG () { 0 } | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 2 | ▶ | use Config; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 2 | ▶ | use File::Basename qw( dirname ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | use File::Glob qw( bsd_glob ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 2 | ▶ | use File::Slurp qw( slurp ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 2 | ▶ | use File::Spec; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | ############################################################################# | ||||||||||||||||||||||||||||
| 28 | 2 | our $LD; | |||||||||||||||||||||||||||
| 29 | sub ld_lookup | ||||||||||||||||||||||||||||
| 30 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ld_lookup: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 31 | 2 | my( $name ) = @_; | |||||||||||||||||||||||||||
| 32 | 2 | ▶ | $LD ||= Sys::GNU::ldconfig->new; | ||||||||||||||||||||||||||
|
Condition66%2/3
$Sys::GNU::ldconfig::LD ||= "Sys::GNU::ldconfig"->new
MC/DC0%0/2
$Sys::GNU::ldconfig::LD ||= "Sys::GNU::ldconfig"->new
$Sys::GNU::ldconfig::LD
"Sys::GNU::ldconfig"->new
Truth table66%2/3
A ||= B
A= $Sys::GNU::ldconfig::LDB= "Sys::GNU::ldconfig"->new
| |||||||||||||||||||||||||||||
| 33 | 2 | return $LD->lookup( $name ); | |||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | sub ld_root | ||||||||||||||||||||||||||||
| 37 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ld_root: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 1 | my( $path ) = @_; | |||||||||||||||||||||||||||
| 39 | 1 | ▶ | $LD ||= Sys::GNU::ldconfig->new; | ||||||||||||||||||||||||||
|
Condition33%1/3
$Sys::GNU::ldconfig::LD ||= "Sys::GNU::ldconfig"->new
MC/DC0%0/2
$Sys::GNU::ldconfig::LD ||= "Sys::GNU::ldconfig"->new
$Sys::GNU::ldconfig::LD
"Sys::GNU::ldconfig"->new
Truth table33%1/3
A ||= B
A= $Sys::GNU::ldconfig::LDB= "Sys::GNU::ldconfig"->new
| |||||||||||||||||||||||||||||
| 40 | 1 | $LD->root( $path ); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ############################################################################# | ||||||||||||||||||||||||||||
| 44 | sub new | ||||||||||||||||||||||||||||
| 45 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 2 | my( $package ) = @_; | |||||||||||||||||||||||||||
| 47 | 2 | ▶ | $package = ref $package if ref $package; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 2 | my $self = bless { root => File::Spec->rootdir, | |||||||||||||||||||||||||||
| 49 | absroot => File::Spec->rootdir, | ||||||||||||||||||||||||||||
| 50 | dirs => [], | ||||||||||||||||||||||||||||
| 51 | have_dirs => 0 | ||||||||||||||||||||||||||||
| 52 | }, $package; | ||||||||||||||||||||||||||||
| 53 | 2 | return $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | ################################################# | ||||||||||||||||||||||||||||
| 57 | sub root | ||||||||||||||||||||||||||||
| 58 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
root: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 2 | my( $self, $path ) = @_; | |||||||||||||||||||||||||||
| 60 | 2 | ▶ | confess "Root '$path' doesn't exist" unless -d $path; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | 2 | $self->{root} = $path; | |||||||||||||||||||||||||||
| 62 | 2 | $self->{have_dirs} = 0; | |||||||||||||||||||||||||||
| 63 | 2 | $self->{dirs} = []; | |||||||||||||||||||||||||||
| 64 | 2 | return; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | ################################################# | ||||||||||||||||||||||||||||
| 69 | # The heart of the module | ||||||||||||||||||||||||||||
| 70 | sub lookup | ||||||||||||||||||||||||||||
| 71 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 72 | 8 | my( $self, $part ) = @_; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 8 | my $file = $self->_lookup( $part ); | |||||||||||||||||||||||||||
| 75 | 8 | ▶ | return unless defined $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 7 | return $self->_derooted( $file ); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | sub _lookup | ||||||||||||||||||||||||||||
| 80 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_lookup: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 81 | 8 | my( $self, $part ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 8 | ▶ | $part = "lib$part" unless $part =~ /^lib/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 8 | ▶ | $part = "$part.$Config{dlext}" unless $part =~ /\.\Q$Config{dlext}\E/; # allow .so.7 (for example) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 8 | DEBUG and warn "Looking for '$part'\n"; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 8 | ▶ | return $self->_chase_lib( $part ) if -e $part; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 8 | foreach my $dir ( $self->dirs ) { | |||||||||||||||||||||||||||
| 89 | 26 | my $file = File::Spec->catfile( $dir, $part ); | |||||||||||||||||||||||||||
| 90 | 26 | ▶ | return $self->_chase_lib( $file ) if -e $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 1 | return; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | # This logic is lifted from PAR::Packer | ||||||||||||||||||||||||||||
| 97 | # _chase_lib - find the runtime link of a shared library | ||||||||||||||||||||||||||||
| 98 | # Logic based on info found at the following sites: | ||||||||||||||||||||||||||||
| 99 | # http://lists.debian.org/lsb-spec/1999/05/msg00011.html | ||||||||||||||||||||||||||||
| 100 | # http://docs.sun.com/app/docs/doc/806-0641/6j9vuqujh?a=view#chapter5-97360 | ||||||||||||||||||||||||||||
| 101 | 7 | ▶ | sub _chase_lib { | ||||||||||||||||||||||||||
|
Subroutine
_chase_lib: called CC=9 SCAR=37.9
| |||||||||||||||||||||||||||||
| 102 | 7 | my ($self, $file) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 7 | ▶ | while ($Config{d_symlink} and -l $file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$Sys::GNU::ldconfig::Config{"d_symlink"} and -l $file
MC/DC0%0/2
$Sys::GNU::ldconfig::Config{"d_symlink"} and -l $file
$Sys::GNU::ldconfig::Config{"d_symlink"}
-l $file
Truth table33%1/3
A and B
A= $Sys::GNU::ldconfig::Config{"d_symlink"}B= -l $file
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if ($file =~ /^(.*?\.\Q$Config{dlext}\E\.\d+)\..*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | return $1 if -e $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | return $file if $file =~ /\.\Q$Config{dlext}\E\.\d+$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | my $dir = File::Basename::dirname($file); | |||||||||||||||||||||||||||
| 112 | 0 | $file = readlink($file); | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | unless (File::Spec->file_name_is_absolute($file)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | $file = File::Spec->rel2abs($file, $dir); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 7 | ▶ | if ($file =~ /^(.*?\.\Q$Config{dlext}\E\.\d+)\..*/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return $1 if -e $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 7 | return $file; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | ################################################# | ||||||||||||||||||||||||||||
| 127 | sub _rooted | ||||||||||||||||||||||||||||
| 128 | 29 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_rooted: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 129 | 29 | my( $self, $dir ) = @_; | |||||||||||||||||||||||||||
| 130 | 29 | my $root = $self->{root}; | |||||||||||||||||||||||||||
| 131 | 29 | ▶ | return $dir if $root eq $self->{absroot}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | 14 | return File::Spec->catdir( $root, $dir ); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ################################################# | ||||||||||||||||||||||||||||
| 136 | sub _derooted | ||||||||||||||||||||||||||||
| 137 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_derooted: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 138 | 7 | my( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 139 | 7 | my $root = $self->{root}; | |||||||||||||||||||||||||||
| 140 | 7 | ▶ | $file =~ s/^\Q$root// unless $root eq $self->{absroot}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | 7 | return $file; | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | ################################################# | ||||||||||||||||||||||||||||
| 145 | sub _list_dirs | ||||||||||||||||||||||||||||
| 146 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_list_dirs: called CC=5 SCAR=16.7
| |||||||||||||||||||||||||||||
| 147 | 3 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 148 | 3 | my @dirs; | |||||||||||||||||||||||||||
| 149 | 3 | ▶ | if( $ENV{$Config{ldlibpthname}} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | DEBUG and warn "Using $Config{ldlibpthname}\n"; | |||||||||||||||||||||||||||
| 151 | 0 | push @dirs, map { $self->_rooted( $_ ) } | |||||||||||||||||||||||||||
| 152 | 0 | split ':', $ENV{$Config{ldlibpthname}}; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | 3 | my $conf = File::Spec->catfile( $self->{root}, 'etc', 'ld.so.conf' ); | |||||||||||||||||||||||||||
| 155 | 3 | ▶ | push @dirs, $self->_read_conf( $conf ) if -f $conf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 3 | push @dirs, map { $self->_rooted( $_ ) } qw( /lib64 /lib /usr/lib64 /usr/lib ); | |||||||||||||||||||||||||||
| 158 | 3 | foreach my $dir ( @dirs ) { | |||||||||||||||||||||||||||
| 159 | 29 | ▶ | next unless -d $dir; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | 15 | DEBUG and warn "Search in $dir\n"; | |||||||||||||||||||||||||||
| 161 | 15 | push @{ $self->{dirs} }, $dir; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | 3 | $self->{have_dirs} = 1; | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | ################################################# | ||||||||||||||||||||||||||||
| 168 | sub _read_conf | ||||||||||||||||||||||||||||
| 169 | 7 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_conf: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 170 | 7 | my( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 171 | 7 | DEBUG and warn "Reading config '$file'\n"; | |||||||||||||||||||||||||||
| 172 | 7 | my $c = slurp( $file ); | |||||||||||||||||||||||||||
| 173 | 7 | my @dirs = split /[: \t\n,]+/, $c; | |||||||||||||||||||||||||||
| 174 | 7 | my @ret; | |||||||||||||||||||||||||||
| 175 | 7 | my $include_next = 0; | |||||||||||||||||||||||||||
| 176 | 7 | foreach my $dir ( @dirs ) { | |||||||||||||||||||||||||||
| 177 | 23 | ▶ | if( $include_next ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 178 | 3 | $include_next = 0; | |||||||||||||||||||||||||||
| 179 | 3 | push @ret, $self->_read_glob( $file, $dir ); | |||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | elsif( $dir eq 'include' ) { | ||||||||||||||||||||||||||||
| 182 | 3 | $include_next = 1; | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | else { | ||||||||||||||||||||||||||||
| 185 | 17 | push @ret, $self->_rooted( $dir ); | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | 7 | return @ret; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | sub _read_glob | ||||||||||||||||||||||||||||
| 193 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_read_glob: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 194 | 3 | my( $self, $file, $dir ) = @_; | |||||||||||||||||||||||||||
| 195 | 3 | my( $vol, $dirname, $glob ) = File::Spec->splitpath( $dir ); | |||||||||||||||||||||||||||
| 196 | 3 | my $root = $self->{root}; | |||||||||||||||||||||||||||
| 197 | 3 | ▶ | $root = dirname( $file ) unless File::Spec->file_name_is_absolute( $dir ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 3 | my $confdir = File::Spec->catdir( $root, $dirname ); | |||||||||||||||||||||||||||
| 199 | 3 | DEBUG and warn "Look in $confdir for $glob\n"; | |||||||||||||||||||||||||||
| 200 | 3 | my @ret; | |||||||||||||||||||||||||||
| 201 | 3 | foreach my $conf ( bsd_glob( File::Spec->catfile( $confdir, $glob ) ) ) { | |||||||||||||||||||||||||||
| 202 | 4 | ▶ | next unless -f $conf; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | 4 | push @ret, $self->_read_conf( $conf ); | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | 3 | return @ret; | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | ################################################# | ||||||||||||||||||||||||||||
| 210 | sub dirs | ||||||||||||||||||||||||||||
| 211 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
dirs: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 8 | my( $self ) = @_; | |||||||||||||||||||||||||||
| 213 | 8 | ▶ | $self->_list_dirs unless $self->{have_dirs}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 214 | 8 | return @{ $self->{dirs} }; | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 2 | 1; | |||||||||||||||||||||||||||
| 218 | __END__ | ||||||||||||||||||||||||||||