From 85bf03551e2a539cfe1cdf43b3510487fb857d25 Mon Sep 17 00:00:00 2001 From: Guangyao Ma Date: Thu, 3 Jun 2021 19:34:49 +0800 Subject: [PATCH] =?UTF-8?q?feat(vfs):=20vfs=E6=94=AF=E6=8C=81FD=5FCLOEXEC?= =?UTF-8?q?=E6=A0=87=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 首先,POSIX规范规定文件描述符需要支持close-on-exec属性,修改前的vfs不支持close-on-exec,当exec系列函数执行时,进程所有的 文件将会被关闭(0,1,2也重新被打开)。但是,系统有些时候是不能在exec时关闭全部文件的,例如在执行exec之前,就需要重定向进 程的某些文件描述符时(使用dup2),就希望该文件不被关闭,继续保持重定向属性,shell执行进程并重定向其标准输出到文件,这是我 们经常做的事情。 BREAKING CHANGE: 执行exec类函数后,进程拥有的文件描述符情况发生变化:修改前,默认关闭所有的进程文件描述符,0,1,2重新打开;修改后,除非文 件描述符拥有FD_CLOEXEC标记,否则该描述符不会被关闭。 re #I3U81W Change-Id: I2bdf8d81f629b43810a642148b6e31fb815fe288 Signed-off-by: Guangyao Ma --- fs/inode/fs_files.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/fs/inode/fs_files.c b/fs/inode/fs_files.c index 8c6dd92..2f34c49 100644 --- a/fs/inode/fs_files.c +++ b/fs/inode/fs_files.c @@ -363,17 +363,11 @@ int file_dup2(struct file *filep1, struct file *filep2) /* Call the open method on the file, driver, mountpoint so that it * can maintain the correct open counts. */ - if (vnode_ptr->vop) { - if (vnode_ptr->originMount) + if (vnode_ptr->flag & VNODE_FLAG_MOUNT_NEW) { - /* Dup the open file on the in the new file structure */ - - if (vnode_ptr == NULL) - { - ret = -ENOSYS; - } + ret = -ENOSYS; } else { @@ -728,6 +722,7 @@ static void copy_fds(const struct fd_table_s *new_fdt, const struct fd_table_s * (void)memcpy_s(new_fdt->ft_fds, sz, old_fdt->ft_fds, sz); } (void)memcpy_s(new_fdt->proc_fds, sizeof(fd_set), old_fdt->proc_fds, sizeof(fd_set)); + (void)memcpy_s(new_fdt->cloexec_fds, sizeof(fd_set), old_fdt->cloexec_fds, sizeof(fd_set)); } static void copy_fd_table(struct fd_table_s *new_fdt, struct fd_table_s *old_fdt) @@ -774,6 +769,7 @@ static struct fd_table_s * alloc_fd_table(unsigned int numbers) { fdt->ft_fds = NULL; fdt->proc_fds = NULL; + fdt->cloexec_fds = NULL; return fdt; } data = LOS_MemAlloc(m_aucSysMem0, numbers * sizeof(struct file_table_s)); @@ -788,13 +784,14 @@ static struct fd_table_s * alloc_fd_table(unsigned int numbers) fdt->ft_fds[i].sysFd = -1; } - data = LOS_MemAlloc(m_aucSysMem0, sizeof(fd_set)); + data = LOS_MemAlloc(m_aucSysMem0, 2 * sizeof(fd_set)); /* 2: proc_fds, cloexec_fds */ if (!data) { goto out_arr; } - (VOID)memset_s(data, sizeof(fd_set), 0, sizeof(fd_set)); - fdt->proc_fds = data; + (VOID)memset_s(data, 2 * sizeof(fd_set), 0, 2 * sizeof(fd_set)); + fdt->proc_fds = (fd_set *)data; + fdt->cloexec_fds = (fd_set *)((uintptr_t)data + sizeof(fd_set)); alloc_std_fd(fdt); @@ -878,15 +875,13 @@ struct files_struct *dup_fd(struct files_struct *old_files) * Name: delete_files * * Description: - * Close a processCB's files specified by processCB and files - * - * Assumuptions: - * processCB->files may != files and processCB may != current processCB. + * Close a current process's fd specified by struct files. + * And delete files struct. * ****************************************************************************/ -void delete_files(LosProcessCB *processCB, struct files_struct *files) +void delete_files(struct files_struct *files) { - if (files == NULL || processCB == NULL) + if (files == NULL) { return; } -- Gitee