在共享库搜索路径中查找目录

发布于 2024-10-03 02:54:41 字数 109 浏览 3 评论 0原文

我想要 dlopen() 特定目录中的每个共享库。为了做到这一点,
检索 linux 的库搜索路径的最干净的方法是什么。或者是否有更快的方法来查找该路径中的特定目录?
posix 会更好。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二手情话 2024-10-10 02:54:41

POSIX 不支持查找共享库搜索路径上的目录的机制(例如,它不强制LD_LIBRARY_PATH),因此任何解决方案本质上都是特定于平台的。

Linux 存在一些问题,因为要使用的值可能基于 /etc/ld.so.conf 的内容以及 LD_LIBRARY_PATH 环境变量中的任何运行时值;其他系统也存在类似的问题。默认位置也因系统而异 - /lib/usr/lib 通常用于 32 位 Linux 机器,但 /lib64 和 < code>/usr/lib64 至少在某些 64 位机器上使用。但是,其他平台使用 64 位软件的其他位置。例如,Solaris 使用 /lib/sparcv9/usr/lib/sparcv9 (尽管文档提到了 /lib/64/usr/lib/64,它们是 sparcv9 目录的符号链接)。 Solaris 还具有环境变量 LD_LIBRARY_PATH_64LD_LIBRARY_PATH_32。 HP-UX 和 AIX 传统上使用 LD_LIBRARY_PATH 之外的其他变量 - SHLIB_PATHLIBPATH、IIRC - 尽管我相信 AIX 现在使用 LD_LIBRARY_PATH 也是如此。并且,在 Solaris 上,用于配置共享库的工具是“crle”(配置运行时链接环境),/etc/ld.so.conf 的类似工具是 /var/ld/ ld.config/var/ld/64/ld.config。当然,共享库的扩展名也各不相同(.so.sl.dylib.bundle > 等)。

因此,您的解决方案将是特定于平台的。您需要决定默认位置、要读取的环境变量、要读取的配置文件以及相关的文件扩展名。鉴于这些,那么它主要是一个 SMOP - 简单的编程问题:

  • 对于由任何源命名的每个目录:
    • 打开相关子目录(opendir()
      • 依次读取每个文件名(readdir()
      • 在相关文件的路径上使用dlopen()
      • 进行与您相关的任何分析。
      • 使用dlclose()
    • 使用closedir()

另请参阅下面评论中的注释...完整的主题在不同平台之间存在一定的差异。

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()
    • Use closedir()

See also the notes in the comment below...the complete topic is modestly fraught with variations from platform to platform.

寂寞花火° 2024-10-10 02:54:41

我不确定是否可以做到这一点并且可以便携。由于这个问题是关于 Linux 的,因此可移植性可能不是最重要的。那我就不明白POSIX约束了。你能澄清一下吗?

您可能必须实现 man 8 ld.so 中详细介绍的搜索功能,其中除了 LD_LIBRARY_PATH 之外还包括扫描 /etc/ld.so.conf,或者让 /lib/ld.so 做你想做的事并解析输出。一个不太漂亮的命令行可能是:

export LD_PRELOAD=THISLIBRARYSODOESNOTEXIST
strace -s 4096 /bin/true 2>&1 | sed -n 's/^open("\([^"]*\)\/THISLIBRARYSODOESNOTEXIST".*$/\1\/YOURSUBDIRHERE/gp'
unset LD_PRELOAD

然后您可以使用 POSIX 调用 opendir(3)readdir(3) 枚举文件。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文