The following issue has been SUBMITTED. ====================================================================== https://austingroupbugs.net/view.php?id=1902 ====================================================================== Reported By: geoffclare Assigned To: ====================================================================== Project: 1003.1(2024)/Issue8 Issue ID: 1902 Category: Shell and Utilities Type: Enhancement Request Severity: Objection Priority: normal Status: New Name: Geoff Clare Organization: The Open Group User Reference: Section: exec Page Number: 2538 Line Number: 82764 Interp Status: --- Final Accepted Text: ====================================================================== Date Submitted: 2025-01-23 11:53 UTC Last Modified: 2025-01-23 11:53 UTC ====================================================================== Summary: exec redirection and test -t Description: Currently it is not portable (by the standard; it works in practice) for an application to do something like:<blockquote><pre>exec 3> "$outfile" test -t 3 </pre></blockquote>This is because of the following text in the exec DESCRIPTION:<blockquote>If any file descriptors with numbers greater than 2 are opened by those redirections, it is unspecified whether those file descriptors remain open when the shell invokes another utility.</blockquote>which allows fd 3 to be closed when the shell invokes "test".
This text was included in POSIX.2-1992 because ksh88 would set the FD_CLOEXEC bit on fd's greater than 2 when opened by exec. However, since "test" was built into ksh88, this did not cause any problem with the above usage. Making this usage portable by the standard requires two changes: 1. The text on the exec page should change so that fd's greater than 2 can only be closed when the shell invokes utilities that are not built into the shell. (Note the careful phrasing here: this is not the same as "invokes a non-built-in utility" because regular built-ins are not required to be actually built into the shell - see the XBD definition of "built-in utility".) 2. The "test" utility needs to be required to be built into the shell, as a minimum in shells where exec sets FD_CLOEXEC, but it would be simpler just to require it in all shells. As far as I'm aware, the shells where it was not actually built in are now all ancient relics. Desired Action: On page 2538 line 82764 section exec (DESCRIPTION), change:<blockquote>when the shell invokes another utility</blockquote>to:<blockquote>when the shell invokes a utility that is not built into the shell</blockquote> After page 2539 line 82829 section exec (RATIONALE), add a paragraph:<blockquote>Versions of this standard earlier than Issue 8 TC1 allowed file descriptors greater than 2 that were opened by <i>exec</i> to be closed when the shell invokes any other utility. In practice this has always been implemented by setting the FD_CLOEXEC flag on these descriptors, meaning they are not closed when invoking a utility that is built into the shell. The wording has been updated to match this implementation practice. In particular, this makes the use of <i>test</i> <b>-t</b> <i>file_descriptor</i> portable for such file descriptors when the built-in version of <i>test</i> is used.</blockquote> On page 3431 line 116983 section test (DESCRIPTION), change:<blockquote>The <i>test</i> and <i>[</i> utilities may be implemented as a single linked utility which examines ...</blockquote>to:<blockquote>The <i>test</i> and <i>[</i> utilities shall be implemented both as regular built-in utilities that are built into the shell and as utilities that can be executed via the <i>exec</i> family of functions defined in the System Interfaces volume of POSIX.1-2024. The latter may be implemented as a single linked utility which examines ...</blockquote> After page 3436 line 117214 section test (APPLICATION USAGE), add a paragraph:<blockquote>The use of the <b>-t</b> <i>file_descriptor</i> primary with <i>file_descriptor</i> values greater than 2 that were opened by <i>exec</i> is only portable when using the shell built-in version of these utilities. When the version that is not built into the shell is invoked, file descriptors greater than 2 may be closed (see [xref to exec]).</blockquote> After page 3438 line 117285 section test (RATIONALE), add a paragraph:<blockquote>Unlike most regular built-in utilities, <i>test</i> and <i>[</i> are required to be implemented by being built into the shell. This is because if the shell sets the FD_CLOEXEC flag on file descriptors greater than 2 that are opened by <i>exec</i>, they need to remain open when the shell invokes <i>test</i> or <i>[</i> in order for the <b>-t</b> <i>file_descriptor</i> primary to be able to test them.</blockquote> ====================================================================== Issue History Date Modified Username Field Change ====================================================================== 2025-01-23 11:53 geoffclare New Issue 2025-01-23 11:53 geoffclare Name => Geoff Clare 2025-01-23 11:53 geoffclare Organization => The Open Group 2025-01-23 11:53 geoffclare Section => exec 2025-01-23 11:53 geoffclare Page Number => 2538 2025-01-23 11:53 geoffclare Line Number => 82764 2025-01-23 11:53 geoffclare Interp Status => --- ======================================================================
