You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2020/10/13 08:15:55 UTC

[incubator-nuttx] branch fclose created (now 7ecd501)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch fclose
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git.


      at 7ecd501  Remove all fclose with stdin, stdout and stderr

This branch includes the following new commits:

     new 7ecd501  Remove all fclose with stdin, stdout and stderr

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



[incubator-nuttx] 01/01: Remove all fclose with stdin, stdout and stderr

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch fclose
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git

commit 7ecd501a0d1a6ae7316c3c97b272f6faa40091e0
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Tue Oct 13 16:08:01 2020 +0800

    Remove all fclose with stdin, stdout and stderr
    
    since it is wrong to close the builtin stream and specially note
    https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html:
    
    Since after the call to fclose() any use of stream results in
    undefined behavior, fclose() should not be used on stdin, stdout,
    or stderr except immediately before process termination (see XBD
    Process Termination), so as to avoid triggering undefined behavior
    in other standard interfaces that rely on these streams. If there
    are any atexit() handlers registered by the application, such a
    call to fclose() should not occur until the last handler is
    finishing. Once fclose() has been used to close stdin, stdout, or
    stderr, there is no standard way to reopen any of these streams.
    
    and it is also unnecessary because the stream always get flushed.
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/unistd/lib_daemon.c | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/libs/libc/unistd/lib_daemon.c b/libs/libc/unistd/lib_daemon.c
index 43fb6dce..84450bc 100644
--- a/libs/libc/unistd/lib_daemon.c
+++ b/libs/libc/unistd/lib_daemon.c
@@ -133,11 +133,11 @@ int daemon(int nochdir, int noclose)
         }
 
 #ifdef CONFIG_FILE_STREAM
-      /* Make sure the stdin, stdout, and stderr are closed */
+      /* Make sure the stdin, stdout, and stderr are flushed */
 
-      fclose(stdin);
-      fclose(stdout);
-      fclose(stderr);
+      fflush(stdin);
+      fflush(stdout);
+      fflush(stderr);
 #endif
       /* Dup the fd to create standard fd 0-2 */
 
@@ -145,20 +145,6 @@ int daemon(int nochdir, int noclose)
       dup2(fd, 1);
       dup2(fd, 2);
 
-      /* fdopen to get the stdin, stdout and stderr streams. The
-       * following logic depends on the fact that the library layer
-       * will allocate FILEs in order.  And since we closed stdin,
-       * stdout, and stderr above, that is what we should get.
-       *
-       * fd = 0 is stdin  (read-only)
-       * fd = 1 is stdout (write-only, append)
-       * fd = 2 is stderr (write-only, append)
-       */
-
-      fdopen(0, "r");
-      fdopen(1, "a");
-      fdopen(2, "a");
-
       /* We can close the original file descriptor now (unless it was
        * one of* 0-2)
        */