Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 6 Jul 2017 14:47:59 +0000 (UTC)
From:      Alan Somers <asomers@FreeBSD.org>
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
Message-ID:  <201707061447.v66Elxp6047757@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
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 <sys/param.h>
 
 #include <atf-c.h>
+#include <err.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <string.h>
 #include <unistd.h>
 
-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
 



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201707061447.v66Elxp6047757>