xiaoxiang781216 commented on code in PR #16499: URL: https://github.com/apache/nuttx/pull/16499#discussion_r2136966092
########## fs/inode/fs_files.c: ########## @@ -587,37 +621,138 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode, #endif } +/**************************************************************************** + * Name: fdlist_allocate + * + * Description: + * Allocate a struct fd instance and associate it with an empty file + * instance. The difference between this function and + * file_allocate_from_inode is that this function is only used for + * placeholder purposes. Later, the caller will initialize the file entity + * through the returned filep. + * + * The fd allocated by this function can be released using fdlist_close. + * + * After the function call is completed, it will hold a reference count + * for the filep. Therefore, when the filep is no longer in use, it is + * necessary to call file_put to release the reference count, in order + * to avoid a race condition where the file might be closed during + * this process. + * + * Returned Value: + * Returns the file descriptor == index into the files array on success; + * a negated errno value is returned on any failure. + * + ****************************************************************************/ + +int fdlist_allocate(FAR struct fdlist *list, int oflags, + int minfd, FAR struct file **filep) +{ + int fd; + + *filep = fs_heap_zalloc(sizeof(struct file)); + if (*filep == NULL) + { + return -ENOMEM; + } + + fd = fdlist_dupfile(list, oflags, minfd, *filep); + if (fd < 0) + { + fs_heap_free(*filep); Review Comment: file_put ########## fs/inode/fs_files.c: ########## @@ -587,37 +621,138 @@ int file_allocate_from_tcb(FAR struct tcb_s *tcb, FAR struct inode *inode, #endif } +/**************************************************************************** + * Name: fdlist_allocate + * + * Description: + * Allocate a struct fd instance and associate it with an empty file + * instance. The difference between this function and + * file_allocate_from_inode is that this function is only used for + * placeholder purposes. Later, the caller will initialize the file entity + * through the returned filep. + * + * The fd allocated by this function can be released using fdlist_close. + * + * After the function call is completed, it will hold a reference count + * for the filep. Therefore, when the filep is no longer in use, it is + * necessary to call file_put to release the reference count, in order + * to avoid a race condition where the file might be closed during + * this process. + * + * Returned Value: + * Returns the file descriptor == index into the files array on success; + * a negated errno value is returned on any failure. + * + ****************************************************************************/ + +int fdlist_allocate(FAR struct fdlist *list, int oflags, + int minfd, FAR struct file **filep) +{ + int fd; + + *filep = fs_heap_zalloc(sizeof(struct file)); + if (*filep == NULL) + { + return -ENOMEM; + } + + fd = fdlist_dupfile(list, oflags, minfd, *filep); + if (fd < 0) + { + fs_heap_free(*filep); + } + else + { + file_ref(*filep); Review Comment: need add ref before dup to avoid the race condition -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org