From 5c5f1dec08390cbb92484a2efbd2cbe5b75b3188 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Mon, 31 Jan 2022 23:19:44 +0800 Subject: [PATCH] sched: Don't duplicate caller file handler when creating kernel thread kernel thread should have only the starndard file i/o just like idle thread Signed-off-by: Xiang Xiao --- fs/inode/fs_files.c | 26 +++++++++++++++----------- include/nuttx/fs/fs.h | 3 ++- sched/group/group_setuptaskfiles.c | 16 ++++++++++------ 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 50ec037b78..96116546d0 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -250,7 +250,8 @@ int files_allocate(FAR struct inode *inode, int oflags, off_t pos, * ****************************************************************************/ -int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist) +int files_duplist(FAR struct filelist *plist, + FAR struct filelist *clist, bool stdio_only) { int ret; int i; @@ -264,25 +265,28 @@ int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist) return ret; } +#ifdef CONFIG_FDCLONE_STDIO + + /* Determine how many file descriptors to clone. If + * CONFIG_FDCLONE_DISABLE is set, no file descriptors will be + * cloned. If CONFIG_FDCLONE_STDIO is set, only the first + * three descriptors (stdin, stdout, and stderr) will be + * cloned. Otherwise all file descriptors will be cloned. + */ + + stdio_only = true; +#endif + for (i = 0; i < plist->fl_rows; i++) { for (j = 0; j < CONFIG_NFILE_DESCRIPTORS_PER_BLOCK; j++) { FAR struct file *filep; -#ifdef CONFIG_FDCLONE_STDIO - /* Determine how many file descriptors to clone. If - * CONFIG_FDCLONE_DISABLE is set, no file descriptors will be - * cloned. If CONFIG_FDCLONE_STDIO is set, only the first - * three descriptors (stdin, stdout, and stderr) will be - * cloned. Otherwise all file descriptors will be cloned. - */ - - if (i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j >= 3) + if (stdio_only && i * CONFIG_NFILE_DESCRIPTORS_PER_BLOCK + j >= 3) { goto out; } -#endif filep = &plist->fl_files[i][j]; if (filep->f_inode == NULL || (filep->f_oflags & O_CLOEXEC) != 0) diff --git a/include/nuttx/fs/fs.h b/include/nuttx/fs/fs.h index 2408077641..cde396ff53 100644 --- a/include/nuttx/fs/fs.h +++ b/include/nuttx/fs/fs.h @@ -759,7 +759,8 @@ void files_releaselist(FAR struct filelist *list); * ****************************************************************************/ -int files_duplist(FAR struct filelist *plist, FAR struct filelist *clist); +int files_duplist(FAR struct filelist *plist, + FAR struct filelist *clist, bool stdio_only); /**************************************************************************** * Name: file_dup diff --git a/sched/group/group_setuptaskfiles.c b/sched/group/group_setuptaskfiles.c index ca9a8e886b..4f55fba6ca 100644 --- a/sched/group/group_setuptaskfiles.c +++ b/sched/group/group_setuptaskfiles.c @@ -57,16 +57,14 @@ int group_setuptaskfiles(FAR struct task_tcb_s *tcb) { FAR struct task_group_s *group = tcb->cmn.group; + uint8_t ttype = tcb->cmn.flags & TCB_FLAG_TTYPE_MASK; #ifndef CONFIG_FDCLONE_DISABLE FAR struct tcb_s *rtcb = this_task(); int ret; #endif DEBUGASSERT(group); -#ifndef CONFIG_DISABLE_PTHREAD - DEBUGASSERT((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != - TCB_FLAG_TTYPE_PTHREAD); -#endif + DEBUGASSERT(ttype != TCB_FLAG_TTYPE_PTHREAD); /* Initialize file descriptors for the TCB */ @@ -75,9 +73,15 @@ int group_setuptaskfiles(FAR struct task_tcb_s *tcb) #ifndef CONFIG_FDCLONE_DISABLE DEBUGASSERT(rtcb->group); - /* Duplicate the parent task's file descriptors */ + /* Duplicate the parent task's file descriptors. + * If it's a kernel thread, only the first three + * descriptors (stdin, stdout, and stderr) will + * be cloned. + */ - ret = files_duplist(&rtcb->group->tg_filelist, &group->tg_filelist); + ret = files_duplist(&rtcb->group->tg_filelist, + &group->tg_filelist, + ttype == TCB_FLAG_TTYPE_KERNEL); if (ret < 0) { return ret;