From owner-svn-src-head@freebsd.org Thu Jul 6 14:48:00 2017 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id A1179DAFB4E; Thu, 6 Jul 2017 14:48:00 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 724E972BB1; Thu, 6 Jul 2017 14:48:00 +0000 (UTC) (envelope-from asomers@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v66ElxGa047759; Thu, 6 Jul 2017 14:47:59 GMT (envelope-from asomers@FreeBSD.org) Received: (from asomers@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v66Elxp6047757; Thu, 6 Jul 2017 14:47:59 GMT (envelope-from asomers@FreeBSD.org) Message-Id: <201707061447.v66Elxp6047757@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: asomers set sender to asomers@FreeBSD.org using -f From: Alan Somers Date: Thu, 6 Jul 2017 14:47:59 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r320737 - in head: contrib/netbsd-tests/lib/libc/gen lib/libc/tests/gen X-SVN-Group: head X-SVN-Commit-Author: asomers X-SVN-Commit-Paths: in head: contrib/netbsd-tests/lib/libc/gen lib/libc/tests/gen X-SVN-Commit-Revision: 320737 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 06 Jul 2017 14:48:00 -0000 Author: asomers Date: Thu Jul 6 14:47:59 2017 New Revision: 320737 URL: https://svnweb.freebsd.org/changeset/base/320737 Log: Fix cleanup in lib/libc/gen/setdomainname_test ATF cleanup routines run in separate processes from the tests themselves, so they can't share global variables. Also, setdomainname_test needs to be is_exclusive because the test cases access a global resource. PR: 219967 Reviewed by: ngie MFC after: 3 weeks Sponsored by: Spectra Logic Corp Differential Revision: https://reviews.freebsd.org/D11188 Modified: head/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c head/lib/libc/tests/gen/Makefile Modified: head/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c ============================================================================== --- head/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c Thu Jul 6 14:35:47 2017 (r320736) +++ head/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c Thu Jul 6 14:47:59 2017 (r320737) @@ -34,11 +34,13 @@ __RCSID("$NetBSD: t_setdomainname.c,v 1.2 2012/03/25 0 #include #include +#include #include +#include #include #include -static char domain[MAXHOSTNAMELEN]; +#define DOMAIN_BACKUP_FILE "domain.bak" static const char domains[][MAXHOSTNAMELEN] = { "1234567890", @@ -47,6 +49,45 @@ static const char domains[][MAXHOSTNAMELEN] = { "--------------------------------------------------------------------" }; +static void +backup_domain(void) +{ + char domain[MAXHOSTNAMELEN]; + int fd; + size_t l; + ssize_t r,n = 0; + + memset(domain, 0, sizeof(domain)); + + ATF_REQUIRE_EQ(0, getdomainname(domain, sizeof(domain))); + l = strnlen(domain, MAXHOSTNAMELEN); + fd = open(DOMAIN_BACKUP_FILE, O_WRONLY | O_CREAT | O_EXCL, 0644); + ATF_REQUIRE(fd >= 0); + while ((r = write(fd, domain + n, l - n)) > 0) + n += r; + ATF_REQUIRE_EQ(0, r); + close(fd); +} + +static void +restore_domain(void) +{ + char domain[MAXHOSTNAMELEN]; + int fd; + ssize_t r, n = 0; + + memset(domain, 0, sizeof(domain)); + if ((fd = open(DOMAIN_BACKUP_FILE, O_RDONLY)) < 0) + err(1, "open"); + while ((r = read(fd, domain + n, sizeof(domain) - n)) > 0) + n += r; + if (r < 0) + err(1, "read"); + if (setdomainname(domain, strlen(domain)) != 0) + err(1, "setdomainname"); + close(fd); +} + ATF_TC_WITH_CLEANUP(setdomainname_basic); ATF_TC_HEAD(setdomainname_basic, tc) { @@ -59,6 +100,7 @@ ATF_TC_BODY(setdomainname_basic, tc) char name[MAXHOSTNAMELEN]; size_t i; + backup_domain(); for (i = 0; i < __arraycount(domains); i++) { (void)memset(name, 0, sizeof(name)); @@ -80,12 +122,11 @@ ATF_TC_BODY(setdomainname_basic, tc) ATF_REQUIRE(strcmp(domains[i], name) == 0); } - (void)setdomainname(domain, sizeof(domain)); } ATF_TC_CLEANUP(setdomainname_basic, tc) { - (void)setdomainname(domain, sizeof(domain)); + restore_domain(); } ATF_TC_WITH_CLEANUP(setdomainname_limit); @@ -100,6 +141,7 @@ ATF_TC_BODY(setdomainname_limit, tc) char name[MAXHOSTNAMELEN + 1]; (void)memset(name, 0, sizeof(name)); + backup_domain(); #ifdef __FreeBSD__ ATF_REQUIRE(setdomainname(name, MAXHOSTNAMELEN - 1 ) == 0); @@ -110,10 +152,10 @@ ATF_TC_BODY(setdomainname_limit, tc) ATF_TC_CLEANUP(setdomainname_limit, tc) { - (void)setdomainname(domain, sizeof(domain)); + restore_domain(); } -ATF_TC_WITH_CLEANUP(setdomainname_perm); +ATF_TC(setdomainname_perm); ATF_TC_HEAD(setdomainname_perm, tc) { atf_tc_set_md_var(tc, "descr", "Can normal user set the domain name?"); @@ -122,24 +164,16 @@ ATF_TC_HEAD(setdomainname_perm, tc) ATF_TC_BODY(setdomainname_perm, tc) { + char domain[MAXHOSTNAMELEN]; - errno = 0; + memset(domain, 0, sizeof(domain)); + errno = 0; ATF_REQUIRE_ERRNO(EPERM, setdomainname(domain, sizeof(domain)) == -1); } -ATF_TC_CLEANUP(setdomainname_perm, tc) -{ - (void)setdomainname(domain, sizeof(domain)); -} - ATF_TP_ADD_TCS(tp) { - - (void)memset(domain, 0, sizeof(domain)); - - ATF_REQUIRE(getdomainname(domain, sizeof(domain)) == 0); - ATF_TP_ADD_TC(tp, setdomainname_basic); ATF_TP_ADD_TC(tp, setdomainname_limit); ATF_TP_ADD_TC(tp, setdomainname_perm); Modified: head/lib/libc/tests/gen/Makefile ============================================================================== --- head/lib/libc/tests/gen/Makefile Thu Jul 6 14:35:47 2017 (r320736) +++ head/lib/libc/tests/gen/Makefile Thu Jul 6 14:47:59 2017 (r320737) @@ -72,6 +72,7 @@ CFLAGS+= -I${.CURDIR} SRCS.fmtcheck2_test= fmtcheck_test.c SRCS.fnmatch2_test= fnmatch_test.c +TEST_METADATA.setdomainname_test+= is_exclusive=true TESTS_SUBDIRS= execve TESTS_SUBDIRS+= posix_spawn