[英]Linux getpwnam() library dependencies
I had a program whose system call getpwnam()
was failing at runtime. 我有一个程序,其系统调用
getpwnam()
在运行时失败。 To debug this, I decided to run getpwnam()
in isolation with this code (it came from a forum): 为了调试这个,我决定用这个代码(它来自一个论坛)
getpwnam()
运行getpwnam()
):
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
struct passwd *pw;
if (argc != 2) {
printf("usage: %s username\n", argv[0]);
exit(0);
}
pw = getpwnam(argv[1]);
if (pw == NULL)
printf("getpwnam failed\n");
else
printf("home dir = %s\n", pw->pw_dir);
exit(0);
}
Oddly it seems to depend on libnss_compat-2.3.5.so
being present: 奇怪的是它似乎依赖于
libnss_compat-2.3.5.so
存在:
With libnss_compat: 使用libnss_compat:
./pwnam root home dir = /root
Without libnss_compat: 没有libnss_compat:
./pwnam root getpwnam failed
So my question is; 所以我的问题是; why is
getpwnam()
dependent on libnss_compat*.so
? 为什么
getpwnam()
依赖于libnss_compat*.so
? I found out with the nm -D
command that libc-2.3.5.so
is the lib which provides getpwnam()
. 我发现使用
nm -D
命令, libc-2.3.5.so
是提供getpwnam()
的lib。
readelf -d
shows me that libc
in turn only depends on ld.so.1
. readelf -d
告诉我, libc
反过来只依赖于ld.so.1
。 That in turn depends on nothing. 这反过来又取决于什么。 So why on earth is
libnss_compat
having an impact? 那么为什么
libnss_compat
产生影响呢?
Thanks for your help everyone!! 谢谢大家的帮助!!
NSS is the Name Service Switch, a library that can look up user information in various sources (traditional password files, Network Information Service , LDAP). NSS是名称服务交换机,可以在各种来源(传统密码文件, 网络信息服务 ,LDAP)中查找用户信息的库。
getpwnam
may be defined in libc
, but that will load the actual NSS library at runtime. getpwnam
可以在libc
中定义,但是它会在运行时加载实际的NSS库。 Looking inside libc
, I find 我发现,在
libc
里面看
$ strings /lib/x86_64-linux-gnu/libc.so.6 | grep libnss
libnss_
libnss_
libnss_%s.so.%d.%d
The last line is clearly a format string for snprintf
that is used to construct the name of the actual implementation library to load using dlopen
. 最后一行显然是
snprintf
的格式字符串,用于构造要使用dlopen
加载的实际实现库的名称。 The implementation is determined using /etc/nsswitch.conf
. 使用
/etc/nsswitch.conf
确定实现。
EDIT I found the place in the Glibc sources where the library is loaded . 编辑我在Glibc源中找到了加载库的位置 。 It's not (no longer?) using
snprintf
, but the principle is still the same. 它不是(不再?)使用
snprintf
,但原理仍然相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.