From nobody Tue Dec 03 00:51:23 2024 X-Original-To: dev-commits-src-all@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 4Y2MXq6pQhz5fGp3; Tue, 03 Dec 2024 00:51: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 "R10" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Y2MXq1Jl9z4TT8; Tue, 3 Dec 2024 00:51:23 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733187083; 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=xUM+jTkRUh+i70MAnLwBoauTdiI8yMFciE/w8BhbdIU=; b=gvH+7Lea4bXIhV7MmD1sI6pf9/cRbU4M1r8JKHGlxxSfJC1ZILgzdP7L2Ds684EBhwkgUE ZqaCzC5aedBiuPO0R19278f//wEr72TEs3KqeIcFAV2WIQcAKFkaWOfeOYNKSsO4HsuxTe /iUoXnp7qvKqN0/5ykj2E6cnr0Icow3iIsyGRZtb45qRCvWcY3r3grCRouXM7WxNQW8fVn IMZqJf/4ezFPd24qTcdusNqUm00HRT4WujMwiaSEg+EQzJno0grBQIXQp/0Yd2Iz++DMB2 YEITYOK+R7jySXSpxXXKiC/xjH7W1jCz/u336y1DkByyZuAI9a4FTdCdrw1FjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1733187083; 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=xUM+jTkRUh+i70MAnLwBoauTdiI8yMFciE/w8BhbdIU=; b=M5C/vJEYvA0JRnoFosH443dHL2KmEiOZyeiBtYc40zhx3nvivWLJgtArgo4fZi5IAPLohb JgbvtgY0UL0AoRY4JCPI4p4CjT2mwQTxrTnL5GIvagda7mFJFMCIXNNIccuwqtfvSzd0sC 5x6unXunMk4AJPfReNY98sK5OYbZ7ftiVaqw8dy7ZJ3wSEre06RkHmYzk/6aDydlE6+FtC /0AljyOZppNxMDE0kppwKGL8rl3w9r0B74V96qWxOy0y+218BkVZ0Fdvedl7LW9XHAJoPh XS3nlPN/fIAg9skIJRWzOQw0YnxOZVeavsw5XcXIWE/S8Uq7nri0UmlmsBYVpQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1733187083; a=rsa-sha256; cv=none; b=FeDbfreUiVbz3lcqp5tE9NtcaO6yVZH7MqoBBVNTBKojJkSa5smpqXUDFZqz9w+bsndjA+ M4vnDwp5XTBNsRO+hiooZD8LF2k4edK75ofNSg0lMinAeHQp1C4y1IY/a7Sl9WeKOHnou5 g8SMdFOUc/L7WWxhswIzuYCkdMGWLzEe6/No3hmr4rchXmbmmkdF7+y2yjnk5GRXQqnHkw EI9cvhFc0nfm7q4WdHydezjOHCBkdPE7oz0eHL9MAn7FjDw263HwlhjKOsTbfMyl9gom8M 4kok8IM2ehQwZGhE9f1lCsGNvwgSHmpY8Lu50/kovRnMmbBPidEZfJpxazFI3A== 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 4Y2MXq0s9Cz117D; Tue, 3 Dec 2024 00:51:23 +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 4B30pNOb016151; Tue, 3 Dec 2024 00:51:23 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4B30pNJa016148; Tue, 3 Dec 2024 00:51:23 GMT (envelope-from git) Date: Tue, 3 Dec 2024 00:51:23 GMT Message-Id: <202412030051.4B30pNJa016148@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Konstantin Belousov Subject: git: 9fb5c02356b0 - stable/14 - fileno(3): set errno when returning -1 List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kib X-Git-Repository: src X-Git-Refname: refs/heads/stable/14 X-Git-Reftype: branch X-Git-Commit: 9fb5c02356b0717aa3cdde3c19adc6d3f0521225 Auto-Submitted: auto-generated The branch stable/14 has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=9fb5c02356b0717aa3cdde3c19adc6d3f0521225 commit 9fb5c02356b0717aa3cdde3c19adc6d3f0521225 Author: Konstantin Belousov AuthorDate: 2024-11-28 22:12:29 +0000 Commit: Konstantin Belousov CommitDate: 2024-12-03 00:39:22 +0000 fileno(3): set errno when returning -1 PR: 283014 (cherry picked from commit 7cd756ff4fe7e65a9a3f588904998bf6f4b37623) --- include/stdio.h | 4 ---- lib/libc/stdio/ferror.3 | 20 ++++++++++++++++++-- lib/libc/stdio/fileno.c | 25 ++++++++++++++++++++----- 3 files changed, 38 insertions(+), 11 deletions(-) diff --git a/include/stdio.h b/include/stdio.h index b7c412c81da7..0b6d9f22709d 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -497,10 +497,6 @@ extern int __isthreaded; #define ferror(p) (!__isthreaded ? __sferror(p) : (ferror)(p)) #define clearerr(p) (!__isthreaded ? __sclearerr(p) : (clearerr)(p)) -#if __POSIX_VISIBLE -#define fileno(p) (!__isthreaded ? __sfileno(p) : (fileno)(p)) -#endif - #define getc(fp) (!__isthreaded ? __sgetc(fp) : (getc)(fp)) #define putc(x, fp) (!__isthreaded ? __sputc(x, fp) : (putc)(x, fp)) diff --git a/lib/libc/stdio/ferror.3 b/lib/libc/stdio/ferror.3 index 1529c002fb7f..7c4a3ee58b23 100644 --- a/lib/libc/stdio/ferror.3 +++ b/lib/libc/stdio/ferror.3 @@ -112,9 +112,25 @@ before calling them. These functions may be used to avoid the overhead of locking the stream and to prevent races when multiple threads are operating on the same stream. .Sh ERRORS -These functions should not fail and do not set the external -variable +These functions, except +.Fn fileno , +should not fail and do not set the external variable .Va errno . +.Pp +On error, +.Fn fileno +returns \-1 and sets +.Va errno +to one of the following values: +.Bl -tag -width Er +.It Bq Er EBADF +The stream is not associated with a file. +.It Bq Er EBADF +The file descriptor underlying stream is not a valid file descriptor. +.Pp +Note that detection of this condition is not reliable, the error might +be not reported. +.El .Sh SEE ALSO .Xr open 2 , .Xr fdopen 3 , diff --git a/lib/libc/stdio/fileno.c b/lib/libc/stdio/fileno.c index 3adf8dcb959e..cb5bfef8409b 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -36,6 +36,7 @@ static char sccsid[] = "@(#)fileno.c 8.1 (Berkeley) 6/4/93"; #endif /* LIBC_SCCS and not lint */ #include "namespace.h" +#include #include #include "un-namespace.h" #include "libc_private.h" @@ -43,14 +44,29 @@ static char sccsid[] = "@(#)fileno.c 8.1 (Berkeley) 6/4/93"; #undef fileno #undef fileno_unlocked +static int +__fileno_impl(FILE *fp) +{ + int fd; + + fd = fp->_file; + if (fd == -1) + errno = EBADF; + return (fd); +} + int fileno(FILE *fp) { int fd; - FLOCKFILE(fp); - fd = __sfileno(fp); - FUNLOCKFILE(fp); + if (__isthreaded) { + FLOCKFILE(fp); + fd = __fileno_impl(fp); + FUNLOCKFILE(fp); + } else { + fd = __fileno_impl(fp); + } return (fd); } @@ -58,6 +74,5 @@ fileno(FILE *fp) int fileno_unlocked(FILE *fp) { - - return (__sfileno(fp)); + return (__fileno_impl(fp)); }