077c9d
commit 307d04334d516bb180f484a2b283f97310bfee66
077c9d
Author: Florian Weimer <fweimer@redhat.com>
077c9d
Date:   Thu Sep 20 12:03:01 2018 +0200
077c9d
077c9d
    misc: New test misc/tst-gethostid
077c9d
    
077c9d
    The empty /etc/hosts file used to trigger bug 23679.
077c9d
    
077c9d
    (cherry picked from commit db9a8ad4ff3fc58e3773a9a4d0cabe3c1bc9c94c)
077c9d
077c9d
diff --git a/misc/Makefile b/misc/Makefile
077c9d
index b7be2bc19a6f7ed5..c9f81515ac9aef2c 100644
077c9d
--- a/misc/Makefile
077c9d
+++ b/misc/Makefile
077c9d
@@ -86,6 +86,11 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \
077c9d
 	 tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \
077c9d
 	 tst-preadvwritev2 tst-preadvwritev64v2
077c9d
 
077c9d
+# Tests which need libdl.
077c9d
+ifeq (yes,$(build-shared))
077c9d
+tests += tst-gethostid
077c9d
+endif
077c9d
+
077c9d
 tests-internal := tst-atomic tst-atomic-long tst-allocate_once
077c9d
 tests-static := tst-empty
077c9d
 
077c9d
@@ -145,3 +150,5 @@ tst-allocate_once-ENV = MALLOC_TRACE=$(objpfx)tst-allocate_once.mtrace
077c9d
 $(objpfx)tst-allocate_once-mem.out: $(objpfx)tst-allocate_once.out
077c9d
 	$(common-objpfx)malloc/mtrace $(objpfx)tst-allocate_once.mtrace > $@; \
077c9d
 	$(evaluate-test)
077c9d
+
077c9d
+$(objpfx)tst-gethostid: $(libdl)
077c9d
diff --git a/misc/tst-gethostid.c b/misc/tst-gethostid.c
077c9d
new file mode 100644
077c9d
index 0000000000000000..1490aaf3f517ff1d
077c9d
--- /dev/null
077c9d
+++ b/misc/tst-gethostid.c
077c9d
@@ -0,0 +1,108 @@
077c9d
+/* Basic test for gethostid.
077c9d
+   Copyright (C) 2018 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <gnu/lib-names.h>
077c9d
+#include <nss.h>
077c9d
+#include <stdio.h>
077c9d
+#include <stdlib.h>
077c9d
+#include <string.h>
077c9d
+#include <support/namespace.h>
077c9d
+#include <support/support.h>
077c9d
+#include <support/temp_file.h>
077c9d
+#include <support/xdlfcn.h>
077c9d
+#include <support/xstdio.h>
077c9d
+#include <support/xunistd.h>
077c9d
+#include <unistd.h>
077c9d
+
077c9d
+/* Initial test is run outside a chroot, to increase the likelihood of
077c9d
+   success.  */
077c9d
+static void
077c9d
+outside_chroot (void *closure)
077c9d
+{
077c9d
+  long id = gethostid ();
077c9d
+  printf ("info: host ID outside chroot: 0x%lx\n", id);
077c9d
+}
077c9d
+
077c9d
+/* The same, but this time perform a chroot operation.  */
077c9d
+static void
077c9d
+in_chroot (void *closure)
077c9d
+{
077c9d
+  const char *chroot_path = closure;
077c9d
+  xchroot (chroot_path);
077c9d
+  long id = gethostid ();
077c9d
+  printf ("info: host ID in chroot: 0x%lx\n", id);
077c9d
+}
077c9d
+
077c9d
+static int
077c9d
+do_test (void)
077c9d
+{
077c9d
+  support_isolate_in_subprocess (outside_chroot, NULL);
077c9d
+
077c9d
+  /* Now run the test inside a chroot.  */
077c9d
+  support_become_root ();
077c9d
+  if (!support_can_chroot ())
077c9d
+    /* Cannot perform further tests.  */
077c9d
+    return 0;
077c9d
+
077c9d
+  /* Only use nss_files.  */
077c9d
+  __nss_configure_lookup ("hosts", "files");
077c9d
+
077c9d
+  /* Load the DSO outside of the chroot.  */
077c9d
+  xdlopen (LIBNSS_FILES_SO, RTLD_LAZY);
077c9d
+
077c9d
+  char *chroot_dir = support_create_temp_directory ("tst-gethostid-");
077c9d
+  support_isolate_in_subprocess (in_chroot, chroot_dir);
077c9d
+
077c9d
+  /* Tests with /etc/hosts in the chroot.  */
077c9d
+  {
077c9d
+    char *path = xasprintf ("%s/etc", chroot_dir);
077c9d
+    add_temp_file (path);
077c9d
+    xmkdir (path, 0777);
077c9d
+    free (path);
077c9d
+    path = xasprintf ("%s/etc/hosts", chroot_dir);
077c9d
+    add_temp_file (path);
077c9d
+
077c9d
+    FILE *fp = xfopen (path, "w");
077c9d
+    xfclose (fp);
077c9d
+    printf ("info: chroot test with an empty /etc/hosts file\n");
077c9d
+    support_isolate_in_subprocess (in_chroot, chroot_dir);
077c9d
+
077c9d
+    char hostname[1024];
077c9d
+    int ret = gethostname (hostname, sizeof (hostname));
077c9d
+    if (ret < 0)
077c9d
+      printf ("warning: invalid result from gethostname: %d\n", ret);
077c9d
+    else if (strlen (hostname) == 0)
077c9d
+      puts ("warning: gethostname returned empty string");
077c9d
+    else
077c9d
+      {
077c9d
+        printf ("info: chroot test with IPv6 address in /etc/hosts for: %s\n",
077c9d
+                hostname);
077c9d
+        fp = xfopen (path, "w");
077c9d
+        /* Use an IPv6 address to induce another lookup failure.  */
077c9d
+        fprintf (fp, "2001:db8::1 %s\n", hostname);
077c9d
+        xfclose (fp);
077c9d
+        support_isolate_in_subprocess (in_chroot, chroot_dir);
077c9d
+      }
077c9d
+    free (path);
077c9d
+  }
077c9d
+  free (chroot_dir);
077c9d
+
077c9d
+  return 0;
077c9d
+}
077c9d
+
077c9d
+#include <support/test-driver.c>