From 7a758347d4f16abc6bc6a443d6e3eb4f34602cc3 Mon Sep 17 00:00:00 2001 From: vangyzen Date: Mon, 14 Dec 2015 17:21:06 +0000 Subject: resolver: preserve binary compatibility; reduce header pollution In r289315, I added new fields to res_state. This broke binary backward compatibility. It also broke some ports (and possibly other code) by requiring the definition of time_t and struct timespec. Fix these problems by moving the new fields into __res_state_ext. Suggested by: ume Reviewed by: ume MFC after: 3 days Sponsored by: Dell Inc. Differential Revision: https://reviews.freebsd.org/D4472 --- lib/libc/resolv/res_init.c | 26 +++++++++++--------------- lib/libc/resolv/res_private.h | 5 +++++ lib/libc/resolv/res_state.c | 18 +++++++++++++----- 3 files changed, 29 insertions(+), 20 deletions(-) (limited to 'lib/libc/resolv') diff --git a/lib/libc/resolv/res_init.c b/lib/libc/resolv/res_init.c index 087e6a8..8d59eba 100644 --- a/lib/libc/resolv/res_init.c +++ b/lib/libc/resolv/res_init.c @@ -228,7 +228,6 @@ __res_vinit(res_state statp, int preinit) { statp->pfcode = 0; statp->_vcsock = -1; statp->_flags = 0; - statp->reload_period = 2; statp->qhook = NULL; statp->rhook = NULL; statp->_u._ext.nscount = 0; @@ -238,6 +237,7 @@ __res_vinit(res_state statp, int preinit) { statp->_u._ext.ext->nsaddrs[0].sin = statp->nsaddr; strcpy(statp->_u._ext.ext->nsuffix, "ip6.arpa"); strcpy(statp->_u._ext.ext->nsuffix2, "ip6.int"); + statp->_u._ext.ext->reload_period = 2; } else { /* * Historically res_init() rarely, if at all, failed. @@ -326,17 +326,13 @@ __res_vinit(res_state statp, int preinit) { struct stat sb; struct timespec now; - if (_fstat(fileno(fp), &sb) == 0) { - statp->conf_mtim = sb.st_mtim; - if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) == 0) { - statp->conf_stat = now.tv_sec; - } else { - statp->conf_stat = 0; + if (statp->_u._ext.ext != NULL) { + if (_fstat(fileno(fp), &sb) == 0) { + statp->_u._ext.ext->conf_mtim = sb.st_mtim; + if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) == 0) { + statp->_u._ext.ext->conf_stat = now.tv_sec; + } } - } else { - statp->conf_mtim.tv_sec = 0; - statp->conf_mtim.tv_nsec = 0; - statp->conf_stat = 0; } /* read the config file */ @@ -599,9 +595,7 @@ res_setoptions(res_state statp, const char *options, const char *source) { const char *cp = options; int i; -#ifndef _LIBC struct __res_state_ext *ext = statp->_u._ext.ext; -#endif #ifdef DEBUG if (statp->options & RES_DEBUG) @@ -686,8 +680,10 @@ res_setoptions(res_state statp, const char *options, const char *source) statp->options |= RES_NOCHECKNAME; } else if (!strncmp(cp, "reload-period:", sizeof("reload-period:") - 1)) { - statp->reload_period = (u_short) - atoi(cp + sizeof("reload-period:") - 1); + if (ext != NULL) { + ext->reload_period = (u_short) + atoi(cp + sizeof("reload-period:") - 1); + } } #ifdef RES_USE_EDNS0 else if (!strncmp(cp, "edns0", sizeof("edns0") - 1)) { diff --git a/lib/libc/resolv/res_private.h b/lib/libc/resolv/res_private.h index 4e98157..a986e95 100644 --- a/lib/libc/resolv/res_private.h +++ b/lib/libc/resolv/res_private.h @@ -1,3 +1,5 @@ +/* $FreeBSD$ */ + #ifndef res_private_h #define res_private_h @@ -12,6 +14,9 @@ struct __res_state_ext { } sort_list[MAXRESOLVSORT]; char nsuffix[64]; char nsuffix2[64]; + struct timespec conf_mtim; /* mod time of loaded resolv.conf */ + time_t conf_stat; /* time of last stat(resolv.conf) */ + u_short reload_period; /* seconds between stat(resolv.conf) */ }; extern int diff --git a/lib/libc/resolv/res_state.c b/lib/libc/resolv/res_state.c index 87643422..6d31d92 100644 --- a/lib/libc/resolv/res_state.c +++ b/lib/libc/resolv/res_state.c @@ -37,6 +37,8 @@ #include "reentrant.h" #include "un-namespace.h" +#include "res_private.h" + #undef _res struct __res_state _res; @@ -66,20 +68,26 @@ res_check_reload(res_state statp) { struct timespec now; struct stat sb; + struct __res_state_ext *ext; + + if ((statp->options & RES_INIT) == 0) { + return (statp); + } - if ((statp->options & RES_INIT) == 0 || statp->reload_period == 0) { + ext = statp->_u._ext.ext; + if (ext == NULL || ext->reload_period == 0) { return (statp); } if (clock_gettime(CLOCK_MONOTONIC_FAST, &now) != 0 || - (now.tv_sec - statp->conf_stat) < statp->reload_period) { + (now.tv_sec - ext->conf_stat) < ext->reload_period) { return (statp); } - statp->conf_stat = now.tv_sec; + ext->conf_stat = now.tv_sec; if (stat(_PATH_RESCONF, &sb) == 0 && - (sb.st_mtim.tv_sec != statp->conf_mtim.tv_sec || - sb.st_mtim.tv_nsec != statp->conf_mtim.tv_nsec)) { + (sb.st_mtim.tv_sec != ext->conf_mtim.tv_sec || + sb.st_mtim.tv_nsec != ext->conf_mtim.tv_nsec)) { statp->options &= ~RES_INIT; } -- cgit v1.1 From a91f621c6e2380a9a23b0e3c8fcf784c09f5971c Mon Sep 17 00:00:00 2001 From: ngie Date: Tue, 15 Dec 2015 05:37:09 +0000 Subject: Allow users override `DEBUG` on the command line via DEBUG_FLAGS="-DDEBUG" with lib/libc/resolv by conditionalizing its definition MFC after: 3 days Reviewed by: ume, vangyzen Differential Revision: https://reviews.freebsd.org/D4519 --- lib/libc/resolv/res_init.c | 4 +++- lib/libc/resolv/res_mkquery.c | 4 +++- lib/libc/resolv/res_mkupdate.c | 4 +++- lib/libc/resolv/res_query.c | 4 +++- lib/libc/resolv/res_send.c | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) (limited to 'lib/libc/resolv') diff --git a/lib/libc/resolv/res_init.c b/lib/libc/resolv/res_init.c index 8d59eba..8832342 100644 --- a/lib/libc/resolv/res_init.c +++ b/lib/libc/resolv/res_init.c @@ -116,7 +116,9 @@ __FBSDID("$FreeBSD$"); /*% Options. Should all be left alone. */ #define RESOLVSORT -#define DEBUG +#ifndef DEBUG +#define DEBUG +#endif #ifdef SOLARIS2 #include diff --git a/lib/libc/resolv/res_mkquery.c b/lib/libc/resolv/res_mkquery.c index b60d8f5..0d45e34 100644 --- a/lib/libc/resolv/res_mkquery.c +++ b/lib/libc/resolv/res_mkquery.c @@ -83,7 +83,9 @@ __FBSDID("$FreeBSD$"); #include "port_after.h" /* Options. Leave them on. */ -#define DEBUG +#ifndef DEBUG +#define DEBUG +#endif extern const char *_res_opcodes[]; diff --git a/lib/libc/resolv/res_mkupdate.c b/lib/libc/resolv/res_mkupdate.c index d4981a5..c076c1e5 100644 --- a/lib/libc/resolv/res_mkupdate.c +++ b/lib/libc/resolv/res_mkupdate.c @@ -54,7 +54,9 @@ __FBSDID("$FreeBSD$"); #include "port_after.h" /* Options. Leave them on. */ -#define DEBUG +#ifndef DEBUG +#define DEBUG +#endif #define MAXPORT 1024 static int getnum_str(u_char **, u_char *); diff --git a/lib/libc/resolv/res_query.c b/lib/libc/resolv/res_query.c index 4ae97f6..5992edd 100644 --- a/lib/libc/resolv/res_query.c +++ b/lib/libc/resolv/res_query.c @@ -88,7 +88,9 @@ __FBSDID("$FreeBSD$"); #include "port_after.h" /* Options. Leave them on. */ -#define DEBUG +#ifndef DEBUG +#define DEBUG +#endif #if PACKETSZ > 1024 #define MAXPACKET PACKETSZ diff --git a/lib/libc/resolv/res_send.c b/lib/libc/resolv/res_send.c index 5690491..c127c3b 100644 --- a/lib/libc/resolv/res_send.c +++ b/lib/libc/resolv/res_send.c @@ -119,7 +119,9 @@ __FBSDID("$FreeBSD$"); #include "un-namespace.h" /* Options. Leave them on. */ -#define DEBUG +#ifndef DEBUG +#define DEBUG +#endif #include "res_debug.h" #include "res_private.h" -- cgit v1.1