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