Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Jul 2019 16:33:44 +0000 (UTC)
From:      Mark Johnston <markj@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r350055 - head/lib/libnv/tests
Message-ID:  <201907161633.x6GGXiXI039798@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: markj
Date: Tue Jul 16 16:33:44 2019
New Revision: 350055
URL: https://svnweb.freebsd.org/changeset/base/350055

Log:
  Add a regression test which transfers varying number of rights.
  
  This exercises the PKG_MAX_SIZE limit mentioned in r350054.
  
  MFC after:	1 week
  Sponsored by:	The FreeBSD Foundation

Modified:
  head/lib/libnv/tests/nvlist_send_recv_test.c

Modified: head/lib/libnv/tests/nvlist_send_recv_test.c
==============================================================================
--- head/lib/libnv/tests/nvlist_send_recv_test.c	Tue Jul 16 16:28:50 2019	(r350054)
+++ head/lib/libnv/tests/nvlist_send_recv_test.c	Tue Jul 16 16:33:44 2019	(r350055)
@@ -30,8 +30,9 @@
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
 
-#include <sys/types.h>
+#include <sys/param.h>
 #include <sys/socket.h>
+#include <sys/sysctl.h>
 #include <sys/wait.h>
 #include <sys/nv.h>
 
@@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$");
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <paths.h>
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
@@ -375,11 +377,106 @@ ATF_TC_BODY(nvlist_send_recv__send_closed_fd, tc)
 	ATF_REQUIRE_ERRNO(EBADF, nvlist_send(socks[1], nvl) != 0);
 }
 
+static int
+nopenfds(void)
+{
+	size_t len;
+	int error, mib[4], n;
+
+	mib[0] = CTL_KERN;
+	mib[1] = KERN_PROC;
+	mib[2] = KERN_PROC_NFDS;
+	mib[3] = 0;
+
+	len = sizeof(n);
+	error = sysctl(mib, nitems(mib), &n, &len, NULL, 0);
+	if (error != 0)
+		return (-1);
+	return (n);
+}
+
+#define	NFDS	512
+
+static void
+send_many_fds_child(int sock)
+{
+	char name[16];
+	nvlist_t *nvl;
+	int anfds, bnfds, fd, i, j;
+
+	fd = open(_PATH_DEVNULL, O_RDONLY);
+	ATF_REQUIRE(fd >= 0);
+
+	for (i = 1; i < NFDS; i++) {
+		nvl = nvlist_create(0);
+		bnfds = nopenfds();
+		if (bnfds == -1)
+			err(EXIT_FAILURE, "sysctl");
+
+		for (j = 0; j < i; j++) {
+			snprintf(name, sizeof(name), "fd%d", j);
+			nvlist_add_descriptor(nvl, name, fd);
+		}
+		nvlist_send(sock, nvl);
+		nvlist_destroy(nvl);
+
+		anfds = nopenfds();
+		if (anfds == -1)
+			err(EXIT_FAILURE, "sysctl");
+		if (anfds != bnfds)
+			errx(EXIT_FAILURE, "fd count mismatch");
+	}
+}
+
+ATF_TC_WITHOUT_HEAD(nvlist_send_recv__send_many_fds);
+ATF_TC_BODY(nvlist_send_recv__send_many_fds, tc)
+{
+	char name[16];
+	nvlist_t *nvl;
+	int anfds, bnfds, fd, i, j, socks[2], status;
+	pid_t pid;
+
+	ATF_REQUIRE(socketpair(PF_UNIX, SOCK_STREAM, 0, socks) == 0);
+
+	pid = fork();
+	ATF_REQUIRE(pid >= 0);
+	if (pid == 0) {
+		/* Child. */
+		(void)close(socks[0]);
+		send_many_fds_child(socks[1]);
+		_exit(0);
+	}
+
+	(void)close(socks[1]);
+
+	for (i = 1; i < NFDS; i++) {
+		bnfds = nopenfds();
+		ATF_REQUIRE(bnfds != -1);
+
+		nvl = nvlist_recv(socks[0], 0);
+		ATF_REQUIRE(nvl != NULL);
+		for (j = 0; j < i; j++) {
+			snprintf(name, sizeof(name), "fd%d", j);
+			fd = nvlist_take_descriptor(nvl, name);
+			ATF_REQUIRE(close(fd) == 0);
+		}
+		nvlist_destroy(nvl);
+
+		anfds = nopenfds();
+		ATF_REQUIRE(anfds != -1);
+		ATF_REQUIRE(anfds == bnfds);
+	}
+
+	ATF_REQUIRE(waitpid(pid, &status, 0) == pid);
+	ATF_REQUIRE(status == 0);
+}
+
 ATF_TP_ADD_TCS(tp)
 {
 
 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_nvlist);
 	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_closed_fd);
+	ATF_TP_ADD_TC(tp, nvlist_send_recv__send_many_fds);
 
 	return (atf_no_error());
 }



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