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