about summary refs log tree commit diff
path: root/posix
diff options
context:
space:
mode:
authorStan Shebs <stanshebs@google.com>2016-08-05 14:12:59 -0700
committerStan Shebs <stanshebs@google.com>2016-08-05 14:12:59 -0700
commitf01f990def48b88d819221907fee42308ad44520 (patch)
tree221137bcad41deb659bdaa665a3f0226dde248ea /posix
parentc0872d83ef8b103f7f6c702267ac280d4aa5f277 (diff)
downloadglibc-f01f990def48b88d819221907fee42308ad44520.tar.gz
glibc-f01f990def48b88d819221907fee42308ad44520.tar.xz
glibc-f01f990def48b88d819221907fee42308ad44520.zip
Fix error handling in getaddrinfo.
Diffstat (limited to 'posix')
-rw-r--r--posix/Makefile2
-rw-r--r--posix/tst-getaddrinfo6.c115
2 files changed, 116 insertions, 1 deletions
diff --git a/posix/Makefile b/posix/Makefile
index 05dc92c2b6..e344d85e29 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -86,7 +86,7 @@ tests		:= tstgetopt testfnm runtests runptests	     \
 		   tst-getaddrinfo3 tst-fnmatch2 tst-cpucount tst-cpuset \
 		   bug-getopt1 bug-getopt2 bug-getopt3 bug-getopt4 \
 		   bug-getopt5 tst-getopt_long1 bug-regex34 bug-regex35 \
-		   tst-pathconf tst-getaddrinfo4 tst-fnmatch3
+		   tst-pathconf tst-getaddrinfo4 tst-fnmatch3 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 0000000000..b515544440
--- /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
+   <http://www.gnu.org/licenses/>.  */
+
+/* Thanks to Dave Clausen for the test concept.  */
+
+#include <stdio.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#include <errno.h>
+#include <sys/resource.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#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"