问题
I want dlopen() every shared library in a specific directory. In order to do that,
what is the cleanest way to retrieve linux's library search path. Or Is there a quicker way of find a specific directory in that path ?
posix would be better.
回答1:
POSIX does not support a mechanism to find out the directories on the shared library search path (it does not mandate LD_LIBRARY_PATH
, for example), so any solution is inherently somewhat platform specific.
Linux presents some problems because the values to be used could be based on the contents of /etc/ld.so.conf
as well as any runtime value in LD_LIBRARY_PATH
environment variable; other systems present comparable problems. The default locations also vary by system - with /lib
and /usr/lib
being usual for 32-bit Linux machines but /lib64
and /usr/lib64
being used on at least some 64-bit machines. However, other platforms use other locations for 64-bit software. For example, Solaris uses /lib/sparcv9
and /usr/lib/sparcv9
, for example (though the docs mention /lib/64
and /usr/lib/64
, they're symlinks to the sparcv9
directories). Solaris also has environment variables LD_LIBRARY_PATH_64
and LD_LIBRARY_PATH_32
. HP-UX and AIX traditionally use other variables than LD_LIBRARY_PATH
-- SHLIB_PATH
and LIBPATH
, IIRC -- though I believe AIX now uses LD_LIBRARY_PATH
too. And, on Solaris, the tool for configuring shared libraries is 'crle' (configure runtime linking environment) and the analog of /etc/ld.so.conf
is either /var/ld/ld.config
or /var/ld/64/ld.config
. Also, of course, the extensions on shared libraries varies (.so
, .sl
, .dylib
, .bundle
, etc).
So, your solution will be platform-specific. You will need to decide on the the default locations, the environment variables to read, and the configuration file to read, and the relevant file extension. Given those, then it is mainly a SMOP - Simple Matter Of Programming:
- For each directory named by any of the sources:
- Open the relevant sub-directory (
opendir()
)- Read each file name (
readdir()
) in turn - Use
dlopen()
on the path of the relevant files. - Do whatever analysis is relevant to you.
- Use
dlclose()
- Read each file name (
- Use
closedir()
- Open the relevant sub-directory (
See also the notes in the comment below...the complete topic is modestly fraught with variations from platform to platform.
回答2:
I'm not sure it's possible to do that and be portable. Since this question is about Linux, portability may not be of paramount importance. Then I do not understand the POSIX constraint. Could you clarify?
You'll probably have to either implement the search functionality detailed in man 8 ld.so
, which includes scanning /etc/ld.so.conf in addition to LD_LIBRARY_PATH
, or make /lib/ld.so
do what you want for you and parse the output. A not-exactly-pretty command line for that could be:
export LD_PRELOAD=THISLIBRARYSODOESNOTEXIST
strace -s 4096 /bin/true 2>&1 | sed -n 's/^open("\([^"]*\)\/THISLIBRARYSODOESNOTEXIST".*$/\1\/YOURSUBDIRHERE/gp'
unset LD_PRELOAD
You can then enumerate files with the POSIX calls opendir(3)
and readdir(3)
.
来源:https://stackoverflow.com/questions/4213857/find-a-directory-in-shared-library-search-path