From nobody Thu Jul 17 17:02:34 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4bjfP64Y3wz621Wc; Thu, 17 Jul 2025 17:02:34 +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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4bjfP63xb1z3F3d; Thu, 17 Jul 2025 17:02:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752771754; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=F/uBGa9XSnd5qbIzf8Y43RVQDSiydxw/LwiuOrHV0JY=; b=KyNj6rDKu9u/9gLJ0i+BAOi7zEpvdOVEHeKmsk0VzcvMXdYhO7mCYPQcfNSj/kg/Cs1ow5 AIqTvistIBwKKZyJaphygQiVzRBCh95Nrk0a9dAY6hvdMpCYDslbYea9spzLrD3jsutLKB Y3jgMiM5Cu9yF0TX3ii7ACfSpHTFGjc8TiiOiSlGX0SyNYUf+awOYEJ2ZmkkHk/umdreoS slH6jMsqNgstOHG/bsYRfRFwgnPuDyB2szULxN/Stu16SGiXIF5Gz/ppkjVoCCJXgXvcEr LosKEWVLVtunrJl1+P3QYQtfNANlaVHm6jR7x2sS/smfpvjL4+Y9DVi4TRtAsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1752771754; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=F/uBGa9XSnd5qbIzf8Y43RVQDSiydxw/LwiuOrHV0JY=; b=btB55y+s1Sf8Yj/6W/zC1sGZWvi0pQAyZiGRvCivxHJs5SdyiEZi0X5Jbh2N+81oJ3WSnv 0NNZksAm4n/4oKPwkuZRQOQd8o9XrZrQQ5eHDXUZ+mlt5rAPDOu1wlbgNoUoP8Imy1OMyJ yglIFtfCPcrcCE1ceVfsx9ghpZe2E3BRAea3y1F/saGeJ1dqMEdcs1wV+Ufq7uS7z+In9n mkJAON0u0b5nu57fPgA7W1E6IfyvAM0XRyPp4zAF9f6q1QCi+ggVC/4cBE3NzspDymDQNC OMxu3EmNBv0XhNMcfosPe9+dGSy30Ucgrigdx2RNvWjNF53TR59+dgQY4mARXw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1752771754; a=rsa-sha256; cv=none; b=HBkpcbDSTxWh1InKdJ15S9RRuGW69nfQ7y5IXM/10OztMW2yRfE87pPI0kq7xZPiJz/r0N 64Ay1y979Zd1ZtDrNEhGRvreQbyb9b/TBq2Z9W/pSFpkKvN9oritzAsa1oTvhOGE4hl3IB LLLGO3FaHn2OJMqiEXxfckOTY7zdHfnbN0bghCJPOoNuamTtY49afcIuYrHbwSKnr1of4F DWChrXc1Yfk0kKGsZYNZFCW+Xy5iv1Ae/cR24ekboJUcF0Xbay2UvyRGXVntPfABPTG51i mZ4hypY1EbpGWnafhbGKCkb5n74I7fDD0O0qgVTp9kowl9fv9VRhsFapDyHbAQ== 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 4bjfP634JCz13dw; Thu, 17 Jul 2025 17:02:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 56HH2YrA049527; Thu, 17 Jul 2025 17:02:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 56HH2YTp049523; Thu, 17 Jul 2025 17:02:34 GMT (envelope-from git) Date: Thu, 17 Jul 2025 17:02:34 GMT Message-Id: <202507171702.56HH2YTp049523@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: 8768b60de16a - main - lib: Fix calls that naively set F_SETFD. List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 8768b60de16a3d72a8783ec1241a711a782a36a9 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=8768b60de16a3d72a8783ec1241a711a782a36a9 commit 8768b60de16a3d72a8783ec1241a711a782a36a9 Author: Ricardo Branco AuthorDate: 2025-07-14 20:10:38 +0000 Commit: Mark Johnston CommitDate: 2025-07-17 17:00:32 +0000 lib: Fix calls that naively set F_SETFD. With the recent inclusion of the FD_CLOFORK and FD_RESOLVE_BENEATH flags, we must avoid clearing them when setting only FD_CLOEXEC. Signed-off-by: Ricardo Branco Reviewed by: kib, markj MFC after: 1 month Pull Request: https://github.com/freebsd/freebsd-src/pull/1766 --- lib/libc/gen/fdopendir.c | 10 +++++++++- lib/libc/stdio/fdopen.c | 18 ++++++++++++++---- lib/libc/stdio/freopen.c | 10 +++++++--- lib/libfetch/common.c | 7 +++++-- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/lib/libc/gen/fdopendir.c b/lib/libc/gen/fdopendir.c index df6709fbcb85..9393cbe28f85 100644 --- a/lib/libc/gen/fdopendir.c +++ b/lib/libc/gen/fdopendir.c @@ -48,8 +48,16 @@ DIR * fdopendir(int fd) { + int flags, rc; - if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + flags = _fcntl(fd, F_GETFD, 0); + if (flags == -1) return (NULL); + + if ((flags & FD_CLOEXEC) == 0) { + rc = _fcntl(fd, F_SETFD, flags | FD_CLOEXEC); + if (rc == -1) + return (NULL); + } return (__opendir_common(fd, DTF_HIDEW | DTF_NODUP, true)); } diff --git a/lib/libc/stdio/fdopen.c b/lib/libc/stdio/fdopen.c index a0d7b71df782..49ec97eda39d 100644 --- a/lib/libc/stdio/fdopen.c +++ b/lib/libc/stdio/fdopen.c @@ -46,7 +46,7 @@ FILE * fdopen(int fd, const char *mode) { FILE *fp; - int flags, oflags, fdflags, tmp; + int flags, oflags, fdflags, rc, tmp; /* * File descriptors are a full int, but _file is only a short. @@ -76,9 +76,19 @@ fdopen(int fd, const char *mode) if ((fp = __sfp()) == NULL) return (NULL); - if ((oflags & O_CLOEXEC) && _fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) { - fp->_flags = 0; - return (NULL); + if ((oflags & O_CLOEXEC) != 0) { + tmp = _fcntl(fd, F_GETFD, 0); + if (tmp == -1) { + fp->_flags = 0; + return (NULL); + } + if ((tmp & FD_CLOEXEC) == 0) { + rc = _fcntl(fd, F_SETFD, tmp | FD_CLOEXEC); + if (rc == -1) { + fp->_flags = 0; + return (NULL); + } + } } fp->_flags = flags; diff --git a/lib/libc/stdio/freopen.c b/lib/libc/stdio/freopen.c index f0732b6d6741..048fd30b3193 100644 --- a/lib/libc/stdio/freopen.c +++ b/lib/libc/stdio/freopen.c @@ -55,7 +55,7 @@ freopen(const char * __restrict file, const char * __restrict mode, FILE * __restrict fp) { int f; - int dflags, flags, isopen, oflags, sverrno, wantfd; + int dflags, fdflags, flags, isopen, oflags, sverrno, wantfd; if ((flags = __sflags(mode, &oflags)) == 0) { sverrno = errno; @@ -113,8 +113,12 @@ freopen(const char * __restrict file, const char * __restrict mode, (void) ftruncate(fp->_file, (off_t)0); if (!(oflags & O_APPEND)) (void) _sseek(fp, (fpos_t)0, SEEK_SET); - if (oflags & O_CLOEXEC) - (void) _fcntl(fp->_file, F_SETFD, FD_CLOEXEC); + if ((oflags & O_CLOEXEC) != 0) { + fdflags = _fcntl(fp->_file, F_GETFD, 0); + if (fdflags != -1 && (fdflags & FD_CLOEXEC) == 0) + (void) _fcntl(fp->_file, F_SETFD, + fdflags | FD_CLOEXEC); + } f = fp->_file; isopen = 0; wantfd = -1; diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index 0d85ed468284..786d5647d993 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -277,13 +277,16 @@ conn_t * fetch_reopen(int sd) { conn_t *conn; + int flags; int opt = 1; /* allocate and fill connection structure */ if ((conn = calloc(1, sizeof(*conn))) == NULL) return (NULL); - fcntl(sd, F_SETFD, FD_CLOEXEC); - setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt); + flags = fcntl(sd, F_GETFD); + if (flags != -1 && (flags & FD_CLOEXEC) == 0) + (void)fcntl(sd, F_SETFD, flags | FD_CLOEXEC); + (void)setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof(opt)); conn->sd = sd; ++conn->ref; return (conn);