Search code examples
linuxhostnamegethostbyname

process-local override of name resolution?


I have test code that I want to have a couple of hostnames resolve to the loopback while testing. When deployed, this code will use the normal system name resolution as appropriate. Test and deployment host are recent linux distros (SLES11SP1, e.g.).

I'd like to override hostname resolution for a single process, without being superuser. Is there a way to manipulate the nsswitch/hostsbehavior in such a narrow fashion?

Yes, of course I could override the hostnames themselves, but I prefer not to (unless this feature really isn't available).


EDIT:

glibc's HOSTALIASES feature sounds like exactly what I want -- but its availability/effectiveness seems inconsistent among the hosts I surveyed. At some point, it was added to be among a list of insecure environment variables. But does that mean it's ignored globally or only in suid binaries? Will it still work for programs which do getnameinfo()?


More edit: IMO, HOSTALIAS wins hands down. Disabling nscd is a workaround for platforms which don't respect it -- like mine (SuSE). And maybe they will release a fix.


Solution

  • LD_LIBRARY_PATH for the win!

    http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

    Also: What is the LD_PRELOAD trick?

    Also: http://www.linuxjournal.com/article/7795