casaroli opened a new pull request, #20595:
URL: https://github.com/apache/nuttx/pull/20595

   ## Summary
   
   `fclose()` of stdin, stdout or stderr flushed the stream and returned, so 
the file descriptor stayed open. POSIX says that `fclose()` closes the file 
descriptor of any stream. A program that sends its output through a pipe and 
ends it with `fclose(stdout)` did not close the write end, so the reader got 
the end of file only when the program exited, or never if the program waits for 
the reader.
   
   This PR closes the file descriptor of the builtin streams as for any other 
stream. The `FILE` structures stay in the stream list and are not freed, and 
they keep -1 as the file descriptor, so a later use of the stream fails and 
does not reach a new file with the same descriptor. `freopen()` of the stream 
still works. The first commit fixes the old nxstyle error in `lib_fclose.c`, 
with no code change.
   
   ## Impact
   
   `fclose(stdin)`, `fclose(stdout)` and `fclose(stderr)` close the descriptor. 
A program that used one of these streams after `fclose()` now gets an error, as 
POSIX says, instead of writing to the descriptor.
   
   ## Testing
   
   The run-time tests ran on a local branch that has this change and other 
pending changes on top of master; the builds are of this branch alone.
   
   qemu-armv8a, kernel build, QEMU 11 with HVF: a program prints a line, calls 
`fclose(stdout)` and then sleeps 3 s, with its output piped into `cat`. `cat` 
gets the end of file after 19 ms, and the pipeline ends after 3017 ms. Before, 
the write end stayed open until the program exited.
   
   Builds with `-Wno-cpp -Werror`: `qemu-armv8a:nsh`, `qemu-armv8a:knsh`, 
`lm3s6965-ek:qemu-flat`, `mps2-an500:nsh`. `tools/checkpatch.sh -c -u -m -g` 
passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to