0

Recently, I'm working on development of the network application which is base on Linux platform(2.6.32). My scenario is that the device need to send data to server periodically. Every time the network code calls the function getaddinfo() firstly, it would return the server IP address, and then I passed this value to socket interface or libcurl api.

However, I found that if the /etc/resolv.conf was set to a invalid value, such as nameserver 169.254.1.1, the getaddrinfo() will return a error value(-3). Even I update the /etc/resolv.conf to a valid value, the getaddrinfo() still return the error.

It seems that getaddrinfo() function won't update the nameserver value after the first time call.

My solution is to call the getaddrinfo() in a independent process(not thread). I found it's a feasible way to resolve the above issue.

What is the root cause of the above issue?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
gtv
  • 21
  • 1
  • 3
  • The root cause is that the whole DNS lookup design and implementation in the Linux standard runtime still sucks. It's not only inflexible its also slow as hell (if you run lots of lookups) and wastes lots of resources. – Lothar May 10 '16 at 13:01

1 Answers1

0

It seems this is by design, glibc itself reads resolv.conf only once - if the internal resolver is used.

I believe you can run nscd for resolving/caching, and it will pick up resolv.conf changes. (If nscd is running, glibc automatically contacts that daemon instead of using its internal resolver)

You can however force resolv.conf to be re-read by calling the res_init() function.

nos
  • 223,662
  • 58
  • 417
  • 506
  • Thanks,nos. Actually, the device didn;t have a nscd service running. My solution is to call the getaddrinfo() in a indepentant process(not thread), it seems the above isssue could be resolved. – gtv Nov 14 '13 at 02:11
  • As mentioned you could call the res_init() function too. so if e.g. getaddrinfo() didn't succeed, call res_init() and try getaddrinfo() one more time so a possibly new resolv.conf is picket up. – nos Nov 14 '13 at 02:26