From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 84323 invoked by alias); 20 Sep 2018 15:46:58 -0000 Mailing-List: contact libc-stable-help@sourceware.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Subscribe: List-Archive: Sender: libc-stable-owner@sourceware.org Received: (qmail 84227 invoked by uid 89); 20 Sep 2018 15:46:58 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Checked: by ClamAV 0.100.1 on sourceware.org X-Virus-Found: No X-Spam-SWARE-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,KAM_SHORT,SPF_HELO_PASS autolearn=ham version=3.3.2 spammy= X-Spam-Status: No, score=-25.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,GIT_PATCH_1,GIT_PATCH_2,GIT_PATCH_3,KAM_LAZY_DOMAIN_SECURITY,KAM_SHORT,SPF_HELO_PASS autolearn=ham version=3.3.2 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on sourceware.org X-Spam-Level: X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 20 Sep 2018 15:46:56 +0000 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 19DE667EAB for ; Thu, 20 Sep 2018 15:46:55 +0000 (UTC) Received: from oldenburg.str.redhat.com (dhcp-192-212.str.redhat.com [10.33.192.212]) by smtp.corp.redhat.com (Postfix) with ESMTP id B4EFF16E3F for ; Thu, 20 Sep 2018 15:46:54 +0000 (UTC) Received: by oldenburg.str.redhat.com (Postfix, from userid 1000) id 257A8441981D5; Thu, 20 Sep 2018 17:46:54 +0200 (CEST) Date: Mon, 01 Jan 2018 00:00:00 -0000 To: libc-stable@sourceware.org Subject: [2.28 COMMITTED] misc: New test misc/tst-gethostid User-Agent: Heirloom mailx 12.5 7/5/10 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20180920154654.257A8441981D5@oldenburg.str.redhat.com> From: fweimer@redhat.com (Florian Weimer) X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 20 Sep 2018 15:46:55 +0000 (UTC) X-IsSubscribed: yes X-SW-Source: 2018-09/txt/msg00005.txt.bz2 The empty /etc/hosts file used to trigger bug 23679. (cherry picked from commit db9a8ad4ff3fc58e3773a9a4d0cabe3c1bc9c94c) 2018-09-20 Florian Weimer * misc/tst-gethostid.c: New file. * misc/Makefile [$(build-shared)] (tests): Add tst-gethostid. (tst-gethostid): Link with -ldl. diff --git a/misc/Makefile b/misc/Makefile index b7be2bc19a..c9f81515ac 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -86,6 +86,11 @@ tests := tst-dirname tst-tsearch tst-fdset tst-efgcvt tst-mntent tst-hsearch \ tst-preadvwritev tst-preadvwritev64 tst-makedev tst-empty \ tst-preadvwritev2 tst-preadvwritev64v2 +# Tests which need libdl. +ifeq (yes,$(build-shared)) +tests += tst-gethostid +endif + tests-internal := tst-atomic tst-atomic-long tst-allocate_once tests-static := tst-empty @@ -145,3 +150,5 @@ tst-allocate_once-ENV = MALLOC_TRACE=$(objpfx)tst-allocate_once.mtrace $(objpfx)tst-allocate_once-mem.out: $(objpfx)tst-allocate_once.out $(common-objpfx)malloc/mtrace $(objpfx)tst-allocate_once.mtrace > $@; \ $(evaluate-test) + +$(objpfx)tst-gethostid: $(libdl) diff --git a/misc/tst-gethostid.c b/misc/tst-gethostid.c new file mode 100644 index 0000000000..1490aaf3f5 --- /dev/null +++ b/misc/tst-gethostid.c @@ -0,0 +1,108 @@ +/* Basic test for gethostid. + Copyright (C) 2018 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 + . */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* Initial test is run outside a chroot, to increase the likelihood of + success. */ +static void +outside_chroot (void *closure) +{ + long id = gethostid (); + printf ("info: host ID outside chroot: 0x%lx\n", id); +} + +/* The same, but this time perform a chroot operation. */ +static void +in_chroot (void *closure) +{ + const char *chroot_path = closure; + xchroot (chroot_path); + long id = gethostid (); + printf ("info: host ID in chroot: 0x%lx\n", id); +} + +static int +do_test (void) +{ + support_isolate_in_subprocess (outside_chroot, NULL); + + /* Now run the test inside a chroot. */ + support_become_root (); + if (!support_can_chroot ()) + /* Cannot perform further tests. */ + return 0; + + /* Only use nss_files. */ + __nss_configure_lookup ("hosts", "files"); + + /* Load the DSO outside of the chroot. */ + xdlopen (LIBNSS_FILES_SO, RTLD_LAZY); + + char *chroot_dir = support_create_temp_directory ("tst-gethostid-"); + support_isolate_in_subprocess (in_chroot, chroot_dir); + + /* Tests with /etc/hosts in the chroot. */ + { + char *path = xasprintf ("%s/etc", chroot_dir); + add_temp_file (path); + xmkdir (path, 0777); + free (path); + path = xasprintf ("%s/etc/hosts", chroot_dir); + add_temp_file (path); + + FILE *fp = xfopen (path, "w"); + xfclose (fp); + printf ("info: chroot test with an empty /etc/hosts file\n"); + support_isolate_in_subprocess (in_chroot, chroot_dir); + + char hostname[1024]; + int ret = gethostname (hostname, sizeof (hostname)); + if (ret < 0) + printf ("warning: invalid result from gethostname: %d\n", ret); + else if (strlen (hostname) == 0) + puts ("warning: gethostname returned empty string"); + else + { + printf ("info: chroot test with IPv6 address in /etc/hosts for: %s\n", + hostname); + fp = xfopen (path, "w"); + /* Use an IPv6 address to induce another lookup failure. */ + fprintf (fp, "2001:db8::1 %s\n", hostname); + xfclose (fp); + support_isolate_in_subprocess (in_chroot, chroot_dir); + } + free (path); + } + free (chroot_dir); + + return 0; +} + +#include