From owner-dev-commits-src-branches@freebsd.org Wed Mar 17 14:05:23 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 7EDC05694A6; Wed, 17 Mar 2021 14:05:23 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4F0sPG1fyFz3srS; Wed, 17 Mar 2021 14:05:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id F1E837EF9; Wed, 17 Mar 2021 14:05:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 12HE5KTE086058; Wed, 17 Mar 2021 14:05:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 12HE5Kcb086057; Wed, 17 Mar 2021 14:05:20 GMT (envelope-from git) Date: Wed, 17 Mar 2021 14:05:20 GMT Message-Id: <202103171405.12HE5Kcb086057@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Alex Richardson Subject: git: 1d8b72afe89c - stable/13 - libc: Fix t_spawn_fileactions test after ATF update MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: arichardson X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 1d8b72afe89c2b9d76fe94e00230c29b1e8ddea9 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 17 Mar 2021 14:05:24 -0000 The branch stable/13 has been updated by arichardson: URL: https://cgit.FreeBSD.org/src/commit/?id=1d8b72afe89c2b9d76fe94e00230c29b1e8ddea9 commit 1d8b72afe89c2b9d76fe94e00230c29b1e8ddea9 Author: Alex Richardson AuthorDate: 2021-02-18 10:07:51 +0000 Commit: Alex Richardson CommitDate: 2021-03-17 12:22:24 +0000 libc: Fix t_spawn_fileactions test after ATF update Since https://github.com/freebsd/atf/commit/4581cefc1e3811dd3c926b5dd4b15fd63d2e19da ATF opens the results file on startup. This fixes problems like capsicumized tests not being able to open the file on exit. However, this test closes all file descriptors above 3 to get a deterministic fd table allocation for the child. Instead of using closefrom (which will close the ATF output file FD) I've changed this test use the lowest available fd and pass that to the helper program as a string. We could also try to re-open the results file in ATF if we get a EBADF error, but that will fail when running under Capsicum. Reviewed By: cem Differential Revision: https://reviews.freebsd.org/D28684 (cherry picked from commit 2aa3ef285a23d802f0bd6c7281612e16834e9b68) --- .../lib/libc/gen/posix_spawn/h_fileactions.c | 47 ++++++++++++++-------- .../lib/libc/gen/posix_spawn/t_fileactions.c | 22 ++++++---- 2 files changed, 45 insertions(+), 24 deletions(-) diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c index d92337074481..fd7fb21ad6f5 100644 --- a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/h_fileactions.c @@ -49,48 +49,61 @@ int main(int argc, char **argv) { int res = EXIT_SUCCESS; + long lowfd; char buf[BUFSIZE]; struct stat sb0, sb1; + if (argc < 2) { + fprintf(stderr, "%s: Not enough arguments: %d\n", getprogname(), + argc); + return EXIT_FAILURE; + } + lowfd = strtol(argv[1], NULL, 10); + if (lowfd < 3) { + fprintf(stderr, "%s: Invalid lowfd %d (as str: %s) \n", + getprogname(), argc, argv[1]); + return EXIT_FAILURE; + } + strcpy(buf, "test..."); - /* file desc 3 should be closed via addclose */ - if (read(3, buf, BUFSIZE) != -1 || errno != EBADF) { - fprintf(stderr, "%s: filedesc 3 is not closed\n", + /* First fd should be closed via addclose */ + if (read(lowfd, buf, BUFSIZE) != -1 || errno != EBADF) { + fprintf(stderr, "%s: first filedesc is not closed\n", getprogname()); res = EXIT_FAILURE; } - /* file desc 4 should be closed via closeonexec */ - if (read(4, buf, BUFSIZE) != -1 || errno != EBADF) { - fprintf(stderr, "%s: filedesc 4 is not closed\n", + /* Next file desc should be closed via closeonexec */ + if (read(lowfd + 1, buf, BUFSIZE) != -1 || errno != EBADF) { + fprintf(stderr, "%s: filedesc +1 is not closed\n", getprogname()); res = EXIT_FAILURE; } - /* file desc 5 remains open */ - if (write(5, buf, BUFSIZE) <= 0) { - fprintf(stderr, "%s: could not write to filedesc 5\n", + /* file desc + 2 remains open */ + if (write(lowfd + 2, buf, BUFSIZE) <= 0) { + fprintf(stderr, "%s: could not write to filedesc +2\n", getprogname()); res = EXIT_FAILURE; } - /* file desc 6 should be open (via addopen) */ - if (write(6, buf, BUFSIZE) <= 0) { - fprintf(stderr, "%s: could not write to filedesc 6\n", + /* file desc + 3 should be open (via addopen) */ + if (write(lowfd + 3, buf, BUFSIZE) <= 0) { + fprintf(stderr, "%s: could not write to filedesc +3\n", getprogname()); res = EXIT_FAILURE; } - /* file desc 7 should refer to stdout */ + /* file desc + 4 should refer to stdout */ fflush(stdout); if (fstat(fileno(stdout), &sb0) != 0) { fprintf(stderr, "%s: could not fstat stdout\n", getprogname()); res = EXIT_FAILURE; } - if (fstat(7, &sb1) != 0) { - fprintf(stderr, "%s: could not fstat filedesc 7\n", + if (fstat(lowfd + 4, &sb1) != 0) { + fprintf(stderr, "%s: could not fstat filedesc +4\n", getprogname()); res = EXIT_FAILURE; } - if (write(7, buf, strlen(buf)) <= 0) { - fprintf(stderr, "%s: could not write to filedesc 7\n", + if (write(lowfd + 4, buf, strlen(buf)) <= 0) { + fprintf(stderr, "%s: could not write to filedesc +4\n", getprogname()); res = EXIT_FAILURE; } diff --git a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c index 74009a805758..b3d364207fbb 100644 --- a/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c +++ b/contrib/netbsd-tests/lib/libc/gen/posix_spawn/t_fileactions.c @@ -301,26 +301,34 @@ ATF_TC_BODY(t_spawn_fileactions, tc) { int fd1, fd2, fd3, status, err; pid_t pid; - char * const args[2] = { __UNCONST("h_fileactions"), NULL }; + char *args[3] = { __UNCONST("h_fileactions"), NULL, NULL }; + int lowfd; + char lowfdstr[32]; char helper[FILENAME_MAX]; posix_spawn_file_actions_t fa; posix_spawn_file_actions_init(&fa); - closefrom(fileno(stderr)+1); + /* Note: this assumes no gaps in the fd table */ + lowfd = open("/", O_RDONLY); + ATF_REQUIRE(lowfd > 0); + ATF_REQUIRE_EQ(0, close(lowfd)); + snprintf(lowfdstr, sizeof(lowfdstr), "%d", lowfd); + args[1] = lowfdstr; fd1 = open("/dev/null", O_RDONLY); - ATF_REQUIRE(fd1 == 3); + ATF_REQUIRE_EQ(fd1, lowfd); fd2 = open("/dev/null", O_WRONLY, O_CLOEXEC); - ATF_REQUIRE(fd2 == 4); + ATF_REQUIRE_EQ(fd2, lowfd + 1); fd3 = open("/dev/null", O_WRONLY); - ATF_REQUIRE(fd3 == 5); + ATF_REQUIRE_EQ(fd3, lowfd + 2); posix_spawn_file_actions_addclose(&fa, fd1); - posix_spawn_file_actions_addopen(&fa, 6, "/dev/null", O_RDWR, 0); - posix_spawn_file_actions_adddup2(&fa, 1, 7); + posix_spawn_file_actions_addopen(&fa, lowfd + 3, "/dev/null", O_RDWR, + 0); + posix_spawn_file_actions_adddup2(&fa, 1, lowfd + 4); snprintf(helper, sizeof helper, "%s/h_fileactions", atf_tc_get_config_var(tc, "srcdir"));