This is the mail archive of the mailing list for the glibc project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Reloading of /etc/resolv.conf


There's a patch in the wild against the resolver which makes it reload
/etc/resolv.conf on change, see

However, this patch actually doesn't work properly for multi-threaded
programs, as only one thread will notice the change and refresh its
resolver state. I've attached a proper patch. It's for 2.5 but it should
work with current versions, too.

Even though the patch may not be interesting for upstream, I decided to
let you know about this problem, since the mentioned patch seems to be
used by at least Debian and Ubuntu.


diff -ur resolv.orig/res_libc.c resolv/res_libc.c
--- resolv.orig/res_libc.c	2005-11-01 01:06:40.000000000 +0100
+++ resolv/res_libc.c	2010-03-15 14:13:18.000000000 +0100
@@ -22,7 +22,7 @@
 #include <arpa/nameser.h>
 #include <resolv.h>
 #include <bits/libc-lock.h>
+#include <sys/stat.h>
 /* The following bit is copied from res_data.c (where it is #ifdef'ed
    out) since res_init() should go into but the rest of that
@@ -89,12 +89,34 @@
 	return (__res_vinit(&_res, 1));
+static time_t resconf_mtime;
+__libc_lock_define_initialized (static, resconf_mtime_lock);
+/* Check if the modification time of resolv.conf has changed.
+   If so, have all threads re-initialize their resolver states */
+static void
+__res_check_resconf (void)
+	struct stat statbuf;
+	if (stat (_PATH_RESCONF, &statbuf) == 0) {
+		__libc_lock_lock (resconf_mtime_lock);
+		if (statbuf.st_mtime != resconf_mtime) {
+			resconf_mtime = statbuf.st_mtime;
+			atomicinclock (lock);
+			atomicinc (__res_initstamp);
+			atomicincunlock (lock);
+		}
+		__libc_lock_unlock (resconf_mtime_lock);
+	}
 /* Initialize resp if RES_INIT is not yet set or if res_init in some other
    thread requested re-initializing.  */
 __res_maybe_init (res_state resp, int preinit)
 	if (resp->options & RES_INIT) {
+		__res_check_resconf ();
 		if (__res_initstamp != resp->_u._ext.initstamp) {
 			if (resp->nscount > 0) {
 				__res_iclose (resp, true);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]