From dd8bacfd4eb32dad30dc95ae31f3a802b7cd151b Mon Sep 17 00:00:00 2001 From: Mark Johnston Date: Tue, 16 Jul 2019 16:33:44 +0000 Subject: [PATCH] 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 --- lib/libnv/tests/nvlist_send_recv_test.c | 99 ++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/lib/libnv/tests/nvlist_send_recv_test.c b/lib/libnv/tests/nvlist_send_recv_test.c index 5020014ec0a9..a8eed7adcd8d 100644 --- a/lib/libnv/tests/nvlist_send_recv_test.c +++ b/lib/libnv/tests/nvlist_send_recv_test.c @@ -30,8 +30,9 @@ #include __FBSDID("$FreeBSD$"); -#include +#include #include +#include #include #include @@ -39,6 +40,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include #include #include @@ -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()); }