Incompatible behaviour of res_query(3) w.r.t. NXDOMAIN

Submitted by Rich Felker on Aug. 25, 2020, 3:26 a.m.

Details

Message ID 20200825032558.GP3265@brightrain.aerifal.cx
State New
Series "Incompatible behaviour of res_query(3) w.r.t. NXDOMAIN"
Headers show

Commit Message

Rich Felker Aug. 25, 2020, 3:26 a.m.
On Mon, Aug 24, 2020 at 06:09:23PM -0400, Rich Felker wrote:
> On Mon, Aug 24, 2020 at 11:51:52PM +0200, Daniel Neri wrote:
> > On 24 Aug 2020, at 23:32, Rich Felker <dalias@libc.org> wrote:
> > > 
> > > Does such a distinction exist? I thought res_query was just equivalent
> > > to res_mkquery+res_send and that calling res_send directly would get
> > > you the same errors.
> > 
> > I thought so too, but I’ve been reading the musl implementation. ;-)
> > 
> > After looking more at the other implementations, I think Florian is
> > correct though: it’s more like res_mkquery+res_send+setting h_errno
> > and the return value based on the RCODE of the response.
> 
> If this is indeed the case and the right behavior can be obtained
> reliably by ignoring res_query and using res_mkquery+res_send, I have
> no fundamental objection to changing this. However we should have a
> plan for moving h_errno to be thread-local and figuring out what
> breakage if any there could be for apps built with it global.
> 
> Thankfully, it looks like we're already using (*__h_errno_location())
> even though it was not thread-local, so existing apps built against
> current musl headers should be immediately compatible with changing it
> to be thread-local.

I have the attached patches queued now.

Rich
From 9d0b8b92a508c328e7eac774847f001f80dfb5ff Mon Sep 17 00:00:00 2001
From: Rich Felker <dalias@aerifal.cx>
Date: Mon, 24 Aug 2020 21:38:49 -0400
Subject: [PATCH 1/2] make h_errno thread-local

the framework to do this always existed but it was deemed unnecessary
because the only [ex-]standard functions using h_errno were not
thread-safe anyway. however, some of the nonstandard res_* functions
are also supposed to set h_errno to indicate the cause of error, and
were unable to do so because it was not thread-safe. this change is a
prerequisite for fixing them.
---
 src/internal/pthread_impl.h | 1 +
 src/network/h_errno.c       | 6 ++----
 2 files changed, 3 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/src/internal/pthread_impl.h b/src/internal/pthread_impl.h
index 5742dfc5..5749a336 100644
--- a/src/internal/pthread_impl.h
+++ b/src/internal/pthread_impl.h
@@ -43,6 +43,7 @@  struct pthread {
 		long off;
 		volatile void *volatile pending;
 	} robust_list;
+	int h_errno_val;
 	volatile int timer_id;
 	locale_t locale;
 	volatile int killlock[1];
diff --git a/src/network/h_errno.c b/src/network/h_errno.c
index 4f700cea..8677a92b 100644
--- a/src/network/h_errno.c
+++ b/src/network/h_errno.c
@@ -1,9 +1,7 @@ 
 #include <netdb.h>
-
-#undef h_errno
-int h_errno;
+#include "pthread_impl.h"
 
 int *__h_errno_location(void)
 {
-	return &h_errno;
+	return &__pthread_self()->h_errno_val;
 }

Comments

Daniel Neri Aug. 25, 2020, 1:56 p.m.
On 25 Aug 2020, at 05:26, Rich Felker <dalias@libc.org> wrote:
> 
> I have the attached patches queued now.

Great! I've patched my Alpine machine and it seems to work as expected.

Thanks,
Daniel