Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 1 Aug 2017 16:00:21 +0000 (UTC)
From:      Alan Somers <asomers@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r321878 - in stable/10: contrib/netbsd-tests/lib/libc/gen lib/libc/tests/gen
Message-ID:  <201708011600.v71G0L92005317@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: asomers
Date: Tue Aug  1 16:00:21 2017
New Revision: 321878
URL: https://svnweb.freebsd.org/changeset/base/321878

Log:
  MFC r320737, r320914
  
  r320737:
  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
  Sponsored by:	Spectra Logic Corp
  Differential Revision:	https://reviews.freebsd.org/D11188
  
  r320914:
  Remove an extraneous strlen from t_setdomainname.c
  
  Reported by:	Coverity
  CID:		1377568
  X-MFC-With:	320737
  Sponsored by:	Spectra Logic Corp

Modified:
  stable/10/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c
  stable/10/lib/libc/tests/gen/Makefile
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c
==============================================================================
--- stable/10/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c	Tue Aug  1 15:51:16 2017	(r321877)
+++ stable/10/contrib/netbsd-tests/lib/libc/gen/t_setdomainname.c	Tue Aug  1 16:00:21 2017	(r321878)
@@ -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, n) != 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: stable/10/lib/libc/tests/gen/Makefile
==============================================================================
--- stable/10/lib/libc/tests/gen/Makefile	Tue Aug  1 15:51:16 2017	(r321877)
+++ stable/10/lib/libc/tests/gen/Makefile	Tue Aug  1 16:00:21 2017	(r321878)
@@ -76,6 +76,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?201708011600.v71G0L92005317>