From nobody Fri Nov 29 15:26:13 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 4Y0H855YZHz5fJwf; Fri, 29 Nov 2024 15:26:13 +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 4Y0H853YH5z4Ry5; Fri, 29 Nov 2024 15:26:13 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732893973; 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=MFgX5gt3eudmgP21ZhBwRZU0RmAy9+kctTAYoQhcWjM=; b=mybmYws5pNOIsgQcs1dKmvjpwbNxrir7bx597JCHCwzBqkdkkSbvsLDA6O4BfXLjYDckBY spSYud+BYwbfEEbqG9Pfhi5+p3tIC92j2pT//wLNUi9s9080w3hDO9xGLznz7ZEI4Mcpfe mDLnWkka0UWdNG9wCCJF9OIyCMbzswlzDYyMToEMf9rgwECS4siQ3gSMJoZdG5xbaeAk7Q IFmCgfyv3FElRVvyP0/5DgaK68kCufh/BXhompimkfbPzAeratsI9eVAygZyS/cSt5vvfq l7R+tx3569nPMt2teUY11pLRI4AyfHXl8PFBfxw7rAUohpJYOf18ZZBTK/fo7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1732893973; 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=MFgX5gt3eudmgP21ZhBwRZU0RmAy9+kctTAYoQhcWjM=; b=Jj0E/U36OJlq3Gh3apEmt4XWNFfyuk5NljyYLZ/KGQ3v4+YZM5XUp9ilwEWboXWIeTFbIk /GeXmZYOKhHB4zQAZh5mUsDChIjbTS9SB/pIPM+SO0lVc9xUgu8Nk0zWhb7/cQ+Rjjvgja r14/ff611L6R0LcvZtysZZypVBAaTCbk/OneWce4eGsrmjjIDjC1VVZXRsJl6YKUXAFnPs mk2MJKlJAiLSMLuUltwmMZtITg2arhXicB4h4cfvoCFMgAd1aMwc/0dh3fPJG3qjLCNmib 28AfUuCFu874qOyET19TSOcQ/fWOHly3V7dTez530Pd0FFbd0lCFLpW/Vy/z2w== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1732893973; a=rsa-sha256; cv=none; b=JThslM7t/K+UgrJFx5Zle+hKpCUw9EStzaAgflOcvemXre1PV65YUIzEUpQoFFk3WuroH2 c3IUpuYN7kHwSRX+5ZQO0AESv5Ut7x9bBixR/JgRSgMTqmq4ao5tEe7h32sZg7pOcNeTb7 Vc1U8mTbElLqiVQJ0cRMCRgAmJxNFY0fKWU9ztFW7L+Vl4+kvU0KkRK3gNFgdnQu/gQwRb YNzxLISMetE3fuGFXk4w5UGkRiFLkV1y+YqQ9biWZnNey36nmdpiE15NzFQ0gxRDdLQbHy EjOLcWN/Hma399pkdYULJn0qoQUEWmzeFOAa9DUwHGKm2CGUDIXuuz31TVcirQ== 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 4Y0H852B3TzSpv; Fri, 29 Nov 2024 15:26:13 +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 4ATFQDEU004973; Fri, 29 Nov 2024 15:26:13 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 4ATFQDEf004970; Fri, 29 Nov 2024 15:26:13 GMT (envelope-from git) Date: Fri, 29 Nov 2024 15:26:13 GMT Message-Id: <202411291526.4ATFQDEf004970@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Konstantin Belousov Subject: git: 7cd756ff4fe7 - main - 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/main X-Git-Reftype: branch X-Git-Commit: 7cd756ff4fe7e65a9a3f588904998bf6f4b37623 Auto-Submitted: auto-generated The branch main has been updated by kib: URL: https://cgit.FreeBSD.org/src/commit/?id=7cd756ff4fe7e65a9a3f588904998bf6f4b37623 commit 7cd756ff4fe7e65a9a3f588904998bf6f4b37623 Author: Konstantin Belousov AuthorDate: 2024-11-28 22:12:29 +0000 Commit: Konstantin Belousov CommitDate: 2024-11-29 15:25:32 +0000 fileno(3): set errno when returning -1 as required by IEEE Std 1003.1™-2024. PR: 283014 Reported by: Graham Percival Reviewed by: emaste, imp Sponsored by: The FreeBSD Foundation MFC after: 1 week Differential revision: https://reviews.freebsd.org/D47834 --- 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 ee3ab3bfb986..3dedb8fd5a54 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -506,10 +506,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 71671f69f247..b921c4280607 100644 --- a/lib/libc/stdio/ferror.3 +++ b/lib/libc/stdio/ferror.3 @@ -110,9 +110,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 66502430dc3d..0ba242c1aed3 100644 --- a/lib/libc/stdio/fileno.c +++ b/lib/libc/stdio/fileno.c @@ -33,6 +33,7 @@ */ #include "namespace.h" +#include #include #include "un-namespace.h" #include "libc_private.h" @@ -40,14 +41,29 @@ #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); } @@ -55,6 +71,5 @@ fileno(FILE *fp) int fileno_unlocked(FILE *fp) { - - return (__sfileno(fp)); + return (__fileno_impl(fp)); }