Linux getpwnam () library dependencies
I had a program, the system call was getpwnam()
not being executed at runtime. To debug this, I decided to run getpwnam()
in isolation with this code (it appeared on the forum):
#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 enough, it depends on the presence libnss_compat-2.3.5.so
:
With libnss_compat:
./pwnam root
home dir = /root
Without libnss_compat:
./pwnam root
getpwnam failed
So my question is; why getpwnam()
depends on libnss_compat*.so
? I found out with the command nm -D
what libc-2.3.5.so
is the lib that provides getpwnam()
.
readelf -d
shows that, libc
in turn, depends only on ld.so.1
. This, in turn, does not depend on anything. So why does it actually libnss_compat
have an impact?
Thanks for your help everyone!
source to share
NSS is a nameservice switch, a library that can look up user information from a variety of sources (traditional password files, Network Information Service , LDAP). getpwnam
can be defined in libc
, but this will load the actual NSS library at runtime. Looking inside libc
I find
$ strings /lib/x86_64-linux-gnu/libc.so.6 | grep libnss
libnss_
libnss_
libnss_%s.so.%d.%d
The last line is obviously a format string for snprintf
which is used to create the name of the actual implementation library to load using dlopen
. The implementation is defined with /etc/nsswitch.conf
.
EDIT I found a place in the Glibc sources where the library is loaded . It's not (no more?) With help snprintf
, but the principle is the same.
source to share