You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ar...@apache.org on 2023/10/17 05:34:08 UTC

[nuttx] 02/03: stdio: Initialize stdin, stdout and stderr directly

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

archer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 62c2b1abba7f62787220dc897c650aa10fdb41d7
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Sun Oct 15 14:28:39 2023 +0800

    stdio: Initialize stdin, stdout and stderr directly
    
    and then remove group_setupstreams
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 fs/vfs/fs_fdopen.c                 | 38 +++++---------------
 sched/group/CMakeLists.txt         |  1 -
 sched/group/Make.defs              |  2 +-
 sched/group/group.h                |  3 --
 sched/group/group_setupidlefiles.c |  5 ---
 sched/group/group_setupstreams.c   | 74 --------------------------------------
 sched/group/group_setuptaskfiles.c |  6 ----
 sched/tls/task_initinfo.c          | 36 +++++++++++++++----
 8 files changed, 39 insertions(+), 126 deletions(-)

diff --git a/fs/vfs/fs_fdopen.c b/fs/vfs/fs_fdopen.c
index 5f7bf7005a..6fd88212d8 100644
--- a/fs/vfs/fs_fdopen.c
+++ b/fs/vfs/fs_fdopen.c
@@ -118,14 +118,6 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
   FAR FILE              *stream;
   int                    ret = OK;
 
-  /* Check input parameters */
-
-  if (fd < 0)
-    {
-      ret = -EBADF;
-      goto errout;
-    }
-
   /* A NULL TCB pointer means to use this threads TCB.  This is a little
    * hack the let's this function be called from user-space (via a syscall)
    * without having access to the TCB.
@@ -138,13 +130,9 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
 
   DEBUGASSERT(tcb && tcb->group);
 
-  if (fd >= 3)
-    {
-      ret = fs_checkfd(tcb, fd, oflags);
-    }
-
   /* Do we have a good descriptor of some sort? */
 
+  ret = fs_checkfd(tcb, fd, oflags);
   if (ret < 0)
     {
       /* No... return the reported error */
@@ -198,24 +186,22 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
       stream = &slist->sl_std[fd];
     }
 
-#ifndef CONFIG_STDIO_DISABLE_BUFFERING
-#if CONFIG_STDIO_BUFFER_SIZE > 0
+#if !defined(CONFIG_STDIO_DISABLE_BUFFERING) && CONFIG_STDIO_BUFFER_SIZE > 0
   /* Set up pointers */
 
   stream->fs_bufstart = stream->fs_buffer;
-  stream->fs_bufend   = &stream->fs_bufstart[CONFIG_STDIO_BUFFER_SIZE];
+  stream->fs_bufend   = stream->fs_bufstart + CONFIG_STDIO_BUFFER_SIZE;
   stream->fs_bufpos   = stream->fs_bufstart;
   stream->fs_bufread  = stream->fs_bufstart;
   stream->fs_flags    = __FS_FLAG_UBF; /* Fake setvbuf and fclose */
 
-#ifdef CONFIG_STDIO_LINEBUFFER
+#  ifdef CONFIG_STDIO_LINEBUFFER
   /* Setup buffer flags */
 
   stream->fs_flags   |= __FS_FLAG_LBF; /* Line buffering */
 
-#endif /* CONFIG_STDIO_LINEBUFFER */
-#endif /* CONFIG_STDIO_BUFFER_SIZE > 0 */
-#endif /* CONFIG_STDIO_DISABLE_BUFFERING */
+#  endif /* CONFIG_STDIO_LINEBUFFER */
+#endif /* !CONFIG_STDIO_DISABLE_BUFFERING && CONFIG_STDIO_BUFFER_SIZE > 0 */
 
   /* Save the file description and open flags.  Setting the
    * file descriptor locks this stream.
@@ -224,18 +210,10 @@ int fs_fdopen(int fd, int oflags, FAR struct tcb_s *tcb,
   stream->fs_fd       = fd;
   stream->fs_oflags   = oflags;
 
-  if (filep != NULL)
-    {
-      *filep = stream;
-    }
-
+  *filep = stream;
   return OK;
 
 errout:
-  if (filep != NULL)
-    {
-      *filep = NULL;
-    }
-
+  *filep = NULL;
   return ret;
 }
diff --git a/sched/group/CMakeLists.txt b/sched/group/CMakeLists.txt
index dd3fced7f8..0f6bc8b22a 100644
--- a/sched/group/CMakeLists.txt
+++ b/sched/group/CMakeLists.txt
@@ -23,7 +23,6 @@ set(SRCS
     group_join.c
     group_leave.c
     group_find.c
-    group_setupstreams.c
     group_setupidlefiles.c
     group_setuptaskfiles.c
     group_foreachchild.c
diff --git a/sched/group/Make.defs b/sched/group/Make.defs
index 0328e042f4..7115c6fa29 100644
--- a/sched/group/Make.defs
+++ b/sched/group/Make.defs
@@ -19,7 +19,7 @@
 ############################################################################
 
 CSRCS += group_create.c group_join.c group_leave.c group_find.c
-CSRCS += group_setupstreams.c group_setupidlefiles.c group_setuptaskfiles.c
+CSRCS += group_setupidlefiles.c group_setuptaskfiles.c
 CSRCS += group_foreachchild.c group_killchildren.c group_signal.c
 CSRCS += group_argvstr.c
 
diff --git a/sched/group/group.h b/sched/group/group.h
index ee865dad9e..7988e6c1ce 100644
--- a/sched/group/group.h
+++ b/sched/group/group.h
@@ -121,8 +121,5 @@ void group_remove_children(FAR struct task_group_s *group);
 
 int  group_setupidlefiles(FAR struct task_tcb_s *tcb);
 int  group_setuptaskfiles(FAR struct task_tcb_s *tcb);
-#ifdef CONFIG_FILE_STREAM
-int  group_setupstreams(FAR struct task_tcb_s *tcb);
-#endif
 
 #endif /* __SCHED_GROUP_GROUP_H */
diff --git a/sched/group/group_setupidlefiles.c b/sched/group/group_setupidlefiles.c
index d54fcb1701..91f83e83e5 100644
--- a/sched/group/group_setupidlefiles.c
+++ b/sched/group/group_setupidlefiles.c
@@ -115,11 +115,6 @@ int group_setupidlefiles(FAR struct task_tcb_s *tcb)
 #warning file descriptors 0-2 are not opened
 #endif /* defined(CONFIG_DEV_CONSOLE) || defined(CONFIG_DEV_NULL) */
 
-  /* Allocate file/socket streams for the TCB */
-
-#ifdef CONFIG_FILE_STREAM
-  ret = group_setupstreams(tcb);
-#endif
   sched_trace_end();
   return ret;
 }
diff --git a/sched/group/group_setupstreams.c b/sched/group/group_setupstreams.c
deleted file mode 100644
index 1ad4eadbe2..0000000000
--- a/sched/group/group_setupstreams.c
+++ /dev/null
@@ -1,74 +0,0 @@
-/****************************************************************************
- * sched/group/group_setupstreams.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <sched.h>
-#include <fcntl.h>
-#include <assert.h>
-
-#include <nuttx/fs/fs.h>
-#include <nuttx/net/net.h>
-
-#include "group/group.h"
-
-/* Make sure that there are file or socket descriptors in the system and
- * that some number of streams have been configured.
- */
-
-#ifdef CONFIG_FILE_STREAM
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: group_setupstreams
- *
- * Description:
- *   Setup streams data structures that may be used for standard C buffered
- *   I/O with underlying socket or file descriptors
- *
- ****************************************************************************/
-
-int group_setupstreams(FAR struct task_tcb_s *tcb)
-{
-  DEBUGASSERT(tcb && tcb->cmn.group);
-
-  /* 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.
-   *
-   * fd = 0 is stdin  (read-only)
-   * fd = 1 is stdout (write-only, append)
-   * fd = 2 is stderr (write-only, append)
-   */
-
-  fs_fdopen(0, O_RDONLY,         (FAR struct tcb_s *)tcb, NULL);
-  fs_fdopen(1, O_WROK | O_CREAT, (FAR struct tcb_s *)tcb, NULL);
-  fs_fdopen(2, O_WROK | O_CREAT, (FAR struct tcb_s *)tcb, NULL);
-
-  return OK;
-}
-
-#endif /* CONFIG_FILE_STREAM */
diff --git a/sched/group/group_setuptaskfiles.c b/sched/group/group_setuptaskfiles.c
index 9a9caa49e9..95c7a7f8ad 100644
--- a/sched/group/group_setuptaskfiles.c
+++ b/sched/group/group_setuptaskfiles.c
@@ -83,12 +83,6 @@ int group_setuptaskfiles(FAR struct task_tcb_s *tcb)
     }
 #endif
 
-  /* Allocate file/socket streams for the new TCB */
-
-#ifdef CONFIG_FILE_STREAM
-  ret = group_setupstreams(tcb);
-#endif
-
   sched_trace_end();
   return ret;
 }
diff --git a/sched/tls/task_initinfo.c b/sched/tls/task_initinfo.c
index 14c366a146..b1fbafcdd5 100644
--- a/sched/tls/task_initinfo.c
+++ b/sched/tls/task_initinfo.c
@@ -23,6 +23,7 @@
  ****************************************************************************/
 
 #include <errno.h>
+#include <fcntl.h>
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/mutex.h>
@@ -46,6 +47,7 @@
 static void task_init_stream(FAR struct streamlist *list)
 {
   FAR struct file_struct *stream = list->sl_std;
+  int i;
 
   /* Initialize the list access mutex */
 
@@ -55,12 +57,34 @@ static void task_init_stream(FAR struct streamlist *list)
 
   /* Initialize stdin, stdout and stderr stream */
 
-  stream[0].fs_fd = -1;
-  nxrmutex_init(&stream[0].fs_lock);
-  stream[1].fs_fd = -1;
-  nxrmutex_init(&stream[1].fs_lock);
-  stream[2].fs_fd = -1;
-  nxrmutex_init(&stream[2].fs_lock);
+  for (i = 0; i < 3; i++)
+    {
+      nxrmutex_init(&stream[i].fs_lock);
+
+#if !defined(CONFIG_STDIO_DISABLE_BUFFERING) && CONFIG_STDIO_BUFFER_SIZE > 0
+      /* Set up pointers */
+
+      stream[i].fs_bufstart = stream[i].fs_buffer;
+      stream[i].fs_bufend   = stream[i].fs_bufstart +
+                              CONFIG_STDIO_BUFFER_SIZE;
+      stream[i].fs_bufpos   = stream[i].fs_bufstart;
+      stream[i].fs_bufread  = stream[i].fs_bufstart;
+      stream[i].fs_flags    = __FS_FLAG_UBF; /* Fake setvbuf and fclose */
+#  ifdef CONFIG_STDIO_LINEBUFFER
+      /* Setup buffer flags */
+
+      stream[i].fs_flags   |= __FS_FLAG_LBF; /* Line buffering */
+
+#  endif /* CONFIG_STDIO_LINEBUFFER */
+
+      /* Save the file description and open flags.  Setting the
+       * file descriptor locks this stream.
+       */
+
+      stream[i].fs_fd       = i;
+      stream[i].fs_oflags   = i ? O_WROK : O_RDONLY;
+#endif /* !CONFIG_STDIO_DISABLE_BUFFERING && CONFIG_STDIO_BUFFER_SIZE > 0 */
+    }
 }
 #endif