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

[incubator-nuttx] branch releases/10.0 updated (7d4400c -> 1f3ee41)

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

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


    from 7d4400c  sched/task: do not migrate the task state to INVALID
     new 44e4495  libc: Skip close stdin/stdout/stderr in fclose
     new 1f3ee41  Remove all fclose with stdin, stdout and stderr

The 2 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.


Summary of changes:
 libs/libc/stdio/lib_fclose.c  | 27 ++++++++++++++++++---------
 libs/libc/unistd/lib_daemon.c | 21 +++------------------
 2 files changed, 21 insertions(+), 27 deletions(-)


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

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

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

commit 1f3ee41c1298d270eb39b4af94bddddd3e19b270
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 | 21 +++------------------
 1 file changed, 3 insertions(+), 18 deletions(-)

diff --git a/libs/libc/unistd/lib_daemon.c b/libs/libc/unistd/lib_daemon.c
index 43fb6dce..62461cd 100644
--- a/libs/libc/unistd/lib_daemon.c
+++ b/libs/libc/unistd/lib_daemon.c
@@ -133,11 +133,10 @@ int daemon(int nochdir, int noclose)
         }
 
 #ifdef CONFIG_FILE_STREAM
-      /* Make sure the stdin, stdout, and stderr are closed */
+      /* Make sure the stdout, and stderr are flushed */
 
-      fclose(stdin);
-      fclose(stdout);
-      fclose(stderr);
+      fflush(stdout);
+      fflush(stderr);
 #endif
       /* Dup the fd to create standard fd 0-2 */
 
@@ -145,20 +144,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)
        */


[incubator-nuttx] 01/02: libc: Skip close stdin/stdout/stderr in fclose

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

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

commit 44e44953eb2b80a25832a9ecec96e2868889fd7e
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Thu Oct 15 21:08:01 2020 +0800

    libc: Skip close stdin/stdout/stderr in fclose
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 libs/libc/stdio/lib_fclose.c | 27 ++++++++++++++++++---------
 1 file changed, 18 insertions(+), 9 deletions(-)

diff --git a/libs/libc/stdio/lib_fclose.c b/libs/libc/stdio/lib_fclose.c
index 1842d5f..a2302a2 100644
--- a/libs/libc/stdio/lib_fclose.c
+++ b/libs/libc/stdio/lib_fclose.c
@@ -81,6 +81,23 @@ int fclose(FAR FILE *stream)
 
   if (stream)
     {
+      ret = OK;
+
+      /* If the stream was opened for writing, then flush the stream */
+
+      if ((stream->fs_oflags & O_WROK) != 0)
+        {
+          ret = lib_fflush(stream, true);
+          errcode = get_errno();
+        }
+
+      /* Skip close the builtin streams(stdin, stdout and stderr) */
+
+      if (stream == stdin || stream == stdout || stream == stderr)
+        {
+          goto done;
+        }
+
       /* Remove FILE structure from the stream list */
 
       slist = nxsched_get_streams();
@@ -114,17 +131,8 @@ int fclose(FAR FILE *stream)
        * file.
        */
 
-      ret = OK;
       if (stream->fs_fd >= 0)
         {
-          /* If the stream was opened for writing, then flush the stream */
-
-          if ((stream->fs_oflags & O_WROK) != 0)
-            {
-              ret = lib_fflush(stream, true);
-              errcode = get_errno();
-            }
-
           /* Close the file descriptor and save the return status */
 
           status = close(stream->fs_fd);
@@ -157,6 +165,7 @@ int fclose(FAR FILE *stream)
       lib_free(stream);
     }
 
+done:
   /* On an error, reset the errno to the first error encountered and return
    * EOF.
    */