3

Is res_query (int res_query(const char *dname, int class, int type,unsigned char *answer, int anslen);) thread-safe?

I think so, because it writes its answer to an user-allocated buffer (in contrast to gethostbyname that uses a statically allocated buffer).

Does somebody know for sure?

IanH
  • 3,968
  • 2
  • 23
  • 26
  • [Doesn't seem to be MT safe](http://www.google.com/search?hl=en&safe=off&q=linux+res_query+multi+threaded&aq=f&aqi=&aql=&oq=&gs_rfai=) – Dummy00001 Aug 23 '10 at 15:39
  • @Dummy00001: There's only one hit, where somebody claims that res_query is not threadsafe, because it overwrites the internal configuration variable _res. I found some older discussion (from 2002) on the BIND mailing list (libresolv and thus res_query is part of BIND), that _res should be thread-local, so I think that they already implemented this years ago. – IanH Aug 23 '10 at 19:57
  • btw, [libevent](http://monkey.org/~provos/libevent/) advertises support for (async) name resolution. I bet they have better docs regarding MT-safeness. Good Luck. – Dummy00001 Aug 23 '10 at 20:49
  • @Dummy00001: Unfortunately I can't use another library, because res_query is called from a 3rd-party library. – IanH Aug 24 '10 at 08:01

1 Answers1

2

You were right that res_query is not threadsafe. You have to use res_nquery, which takes a 'res_state' argument first. From everything I've read, that is how the query should be done in a thread safe manner.

Philip J
  • 363
  • 1
  • 3
  • 12