diff --git a/posix/Makefile b/posix/Makefile
index 5b0e298..7211393 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -90,7 +90,7 @@ tests := tstgetopt testfnm runtests runptests \
bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \
tst-pathconf tst-getaddrinfo4 tst-rxspencer-no-utf8 \
tst-fnmatch3 bug-regex36 tst-getaddrinfo5 \
- tst-posix_spawn-fd
+ tst-posix_spawn-fd tst-getaddrinfo6
xtests := bug-ga2
ifeq (yes,$(build-shared))
test-srcs := globtest
diff --git a/posix/tst-getaddrinfo6.c b/posix/tst-getaddrinfo6.c
new file mode 100644
index 0000000..b515544
--- /dev/null
+++ b/posix/tst-getaddrinfo6.c
@@ -0,0 +1,115 @@
+/* Check that getaddrinfo still works after recovery from failure.
+ Copyright (C) 2016 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Lesser General Public
+ License as published by the Free Software Foundation; either
+ version 2.1 of the License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Lesser General Public License for more details.
+
+ You should have received a copy of the GNU Lesser General Public
+ License along with the GNU C Library; if not, see
+ . */
+
+/* Thanks to Dave Clausen for the test concept. */
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#define NUMFDS 10
+int fds[NUMFDS];
+
+static int
+do_test (void)
+{
+ char hostname[1024];
+ struct rlimit rlim;
+ int gaierr;
+ struct addrinfo hints, *ai;
+
+ gethostname (hostname, sizeof (hostname));
+
+ /* Bring the limit of open files down to a small predictable value. */
+
+ if (getrlimit (RLIMIT_NOFILE, &rlim) < 0)
+ {
+ printf ("error: getrlimit: %m\n");
+ return 1;
+ }
+ rlim.rlim_cur = NUMFDS;
+ if (setrlimit (RLIMIT_NOFILE, &rlim) < 0)
+ {
+ printf ("error: setrlimit: %m\n");
+ return 1;
+ }
+
+ memset (&hints, '\0', sizeof (hints));
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags = AI_CANONNAME;
+
+ gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+ if (gaierr != 0)
+ {
+ printf ("error: getaddrinfo: %s\n", gai_strerror (gaierr));
+ return 1;
+ }
+
+ // Create file descriptors until we completely run out.
+
+ memset (fds, '\0', sizeof (fds));
+ for (int i = 0; i < NUMFDS; ++i)
+ {
+ int fd = dup (STDOUT_FILENO);
+
+ if (fd == -1 && errno == EMFILE)
+ break;
+
+ fds[i] = fd;
+ }
+
+ gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+ if (gaierr != 0)
+ {
+ printf ("error (expected): getaddrinfo: %s\n", gai_strerror (gaierr));
+ /* Don't give up, we expected this error. */
+ }
+
+ // Now close the fds, freeing them up for use.
+
+ for (int i = 0; i < NUMFDS; ++i)
+ {
+ if (fds[i] == 0)
+ break;
+
+ close (fds[i]);
+ }
+
+ /* This one should succeed again. */
+
+ gaierr = getaddrinfo (hostname, NULL, &hints, &ai);
+ if (gaierr != 0)
+ {
+ printf ("error: getaddrinfo: %s\n", gai_strerror (gaierr));
+ return 1;
+ }
+
+ return 0;
+}
+
+#define TEST_FUNCTION do_test ()
+
+#include "../test-skeleton.c"
diff --git a/sysdeps/posix/getaddrinfo.c b/sysdeps/posix/getaddrinfo.c
index 574ce08..126c185 100644
--- a/sysdeps/posix/getaddrinfo.c
+++ b/sysdeps/posix/getaddrinfo.c
@@ -2295,6 +2295,7 @@ getaddrinfo (const char *name, const char *service,
{
int i = 0, last_i = 0;
int nresults = 0;
+ int saved_h_errno;
struct addrinfo *p = NULL;
struct gaih_service gaih_service, *pservice;
struct addrinfo local_hints;
@@ -2329,6 +2330,12 @@ getaddrinfo (const char *name, const char *service,
bool seen_ipv6 = false;
bool check_pf_called = false;
+ /* getaddrinfo() is not documented as setting h_errno, but lower-level
+ functions do set it even if the function as a whole is successful,
+ so we need to save and restore. */
+ saved_h_errno = h_errno;
+ __set_h_errno (0);
+
if (hints->ai_flags & AI_ADDRCONFIG)
{
/* We might need information about what interfaces are available.
@@ -2626,6 +2633,7 @@ getaddrinfo (const char *name, const char *service,
if (p)
{
*pai = p;
+ __set_h_errno (saved_h_errno);
return 0;
}