stdio: Initialize stdin, stdout and stderr directly
and then remove group_setupstreams Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
parent
b0d197534f
commit
62c2b1abba
8 changed files with 39 additions and 126 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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 */
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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 */
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue