diff --git a/posix/Makefile b/posix/Makefile
index 693082ed28..0ef4e7f4be 100644
--- a/posix/Makefile
+++ b/posix/Makefile
@@ -361,6 +361,7 @@ $(objpfx)bug-ga2-mem.out: $(objpfx)bug-ga2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)bug-ga2.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)bug-ga2: $(libdl)
 bug-ga2-ENV = MALLOC_TRACE=$(objpfx)bug-ga2.mtrace
 
 bug-glob2-ENV = MALLOC_TRACE=$(objpfx)bug-glob2.mtrace
diff --git a/posix/bug-ga2.c b/posix/bug-ga2.c
index 5ea759b8ce..889f100358 100644
--- a/posix/bug-ga2.c
+++ b/posix/bug-ga2.c
@@ -1,16 +1,46 @@
 /* Test case by Sam Varshavchik <mrsam@courier-mta.com>.  */
 #include <mcheck.h>
 #include <netdb.h>
-#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-int
-main (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __getservbyname_r.  nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf =
+	"services: files\n"
+	"hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+       .services = "http 80/tcp\n"
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   struct addrinfo hints, *res;
   int i, ret;
 
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (i = 0; i < 100; i++)
     {
       memset (&hints, 0, sizeof (hints));
@@ -20,11 +50,35 @@ main (void)
       ret = getaddrinfo ("www.gnu.org", "http", &hints, &res);
 
       if (ret)
-	{
-	  printf ("%s\n", gai_strerror (ret));
-	  return 1;
-	}
+	FAIL_EXIT1 ("%s\n", gai_strerror (ret));
+
       freeaddrinfo (res);
     }
+
+  /* Ensure that the resources malloc'ed by getaddrinfo are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
+
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/resolv/Makefile b/resolv/Makefile
index dbd8f8bf4f..533d36eebf 100644
--- a/resolv/Makefile
+++ b/resolv/Makefile
@@ -157,6 +157,7 @@ $(objpfx)mtrace-tst-leaks.out: $(objpfx)tst-leaks.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks.mtrace > $@; \
 	$(evaluate-test)
 
+$(objpfx)tst-leaks2: $(libdl)
 tst-leaks2-ENV = MALLOC_TRACE=$(objpfx)tst-leaks2.mtrace
 $(objpfx)mtrace-tst-leaks2.out: $(objpfx)tst-leaks2.out
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-leaks2.mtrace > $@; \
diff --git a/resolv/tst-leaks2.c b/resolv/tst-leaks2.c
index a2f5db3adc..8f7a807572 100644
--- a/resolv/tst-leaks2.c
+++ b/resolv/tst-leaks2.c
@@ -21,20 +21,71 @@
 #include <mcheck.h>
 #include <netdb.h>
 #include <resolv.h>
+#include <stdlib.h>
+#include <dlfcn.h>
+#include <gnu/lib-names.h>
+#include <support/check.h>
+#include <support/namespace.h>
+#include <support/test-driver.h>
+#include <support/xunistd.h>
 
-static int
-do_test (void)
+struct support_chroot *chroot_env;
+
+static void
+prepare (int argc, char **argv)
+{
+  /* Generate a /etc/nsswitch.conf file instead of using __nss_configure_lookup
+     as it would lead to not setting up nsswitch.c:service_table before calling
+     __gethostbyname_r. nss_load_library would then add resources to
+     default_table which are not freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem)  */
+  chroot_env = support_chroot_create
+    ((struct support_chroot_configuration)
+     {
+       .nsswitch_conf = "hosts: files\n",
+       .host_conf = "multi on\n",
+       .hosts = "192.0.2.1 www.gnu.org\n",
+     });
+}
+
+static void
+subprocess_test (void *closure)
 {
   mtrace ();
+  xchroot (chroot_env->path_chroot);
+
   for (int i = 0; i < 20; ++i)
     {
       res_init ();
-      gethostbyname ("www.gnu.org");
+      if (gethostbyname ("www.gnu.org") == NULL)
+	FAIL_EXIT1 ("%s\n", hstrerror (h_errno));
     }
+
+  /* Ensure that the resources malloc'ed by gethostbyname are freed by
+     nss/nsswitch.c:libc_freeres_fn (free_mem).  */
+  exit (EXIT_SUCCESS);
+}
+
+
+static int
+do_test (void)
+{
+  support_become_root ();
+  if (!support_can_chroot ())
+    return EXIT_UNSUPPORTED;
+
+  /* Load the libraries now as those are not available in chroot.  */
+  if (dlopen (LIBNSS_FILES_SO, RTLD_LAZY) == NULL)
+    FAIL_EXIT1 ("could not load " LIBNSS_FILES_SO ": %s", dlerror ());
+
+  /* Run the chroot'ing test in a subprocess in order to be able to delete the
+     temporary files.  */
+  support_isolate_in_subprocess (subprocess_test, NULL);
+
+  support_chroot_free (chroot_env);
   return 0;
 }
 
-#define TEST_FUNCTION do_test ()
 #define TIMEOUT 30
-/* This defines the `main' function and some more.  */
-#include <test-skeleton.c>
+#define PREPARE prepare
+#include <support/test-driver.c>
diff --git a/support/namespace.h b/support/namespace.h
index 5210343877..1c74dcaa4b 100644
--- a/support/namespace.h
+++ b/support/namespace.h
@@ -75,6 +75,8 @@ struct support_chroot_configuration
   const char *hosts;            /* /etc/hosts.  */
   const char *host_conf;        /* /etc/host.conf.  */
   const char *aliases;          /* /etc/aliases.  */
+  const char *services;         /* /etc/services.  */
+  const char *nsswitch_conf;    /* /etc/nsswitch.conf.  */
 };
 
 /* The result of the creation of a chroot.  */
@@ -92,6 +94,8 @@ struct support_chroot
   char *path_hosts;             /* /etc/hosts.  */
   char *path_host_conf;         /* /etc/host.conf.  */
   char *path_aliases;           /* /etc/aliases.  */
+  char *path_services;          /* /etc/services.  */
+  char *path_nsswitch_conf;     /* /etc/nsswitch.conf.  */
 };
 
 /* Create a chroot environment.  The returned data should be freed
diff --git a/support/support_chroot.c b/support/support_chroot.c
index 4f435c1ac1..e6cabd30c5 100644
--- a/support/support_chroot.c
+++ b/support/support_chroot.c
@@ -24,6 +24,8 @@
 #include <support/test-driver.h>
 #include <support/xunistd.h>
 
+extern const char *__progname;
+
 /* If CONTENTS is not NULL, write it to the file at DIRECTORY/RELPATH,
    and store the name in *ABSPATH.  If CONTENTS is NULL, store NULL in
    *ABSPATH.  */
@@ -45,7 +47,7 @@ struct support_chroot *
 support_chroot_create (struct support_chroot_configuration conf)
 {
   struct support_chroot *chroot = xmalloc (sizeof (*chroot));
-  chroot->path_chroot = support_create_temp_directory ("tst-resolv-res_init-");
+  chroot->path_chroot = support_create_temp_directory (__progname);
 
   /* Create the /etc directory in the chroot environment.  */
   char *path_etc = xasprintf ("%s/etc", chroot->path_chroot);
@@ -57,6 +59,9 @@ support_chroot_create (struct support_chroot_configuration conf)
   write_file (path_etc, "hosts", conf.hosts, &chroot->path_hosts);
   write_file (path_etc, "host.conf", conf.host_conf, &chroot->path_host_conf);
   write_file (path_etc, "aliases", conf.aliases, &chroot->path_aliases);
+  write_file (path_etc, "services", conf.services, &chroot->path_services);
+  write_file (path_etc, "nsswitch.conf", conf.nsswitch_conf,
+	      &chroot->path_nsswitch_conf);
 
   free (path_etc);
 
@@ -79,5 +84,7 @@ support_chroot_free (struct support_chroot *chroot)
   free (chroot->path_hosts);
   free (chroot->path_host_conf);
   free (chroot->path_aliases);
+  free (chroot->path_services);
+  free (chroot->path_nsswitch_conf);
   free (chroot);
 }
