xiaoxiang781216 commented on code in PR #16326:
URL: https://github.com/apache/nuttx/pull/16326#discussion_r2075896334


##########
include/nuttx/fs/fs.h:
##########
@@ -462,6 +462,7 @@ struct file
   int               f_oflags;   /* Open mode flags */
 #ifdef CONFIG_FS_REFCOUNT
   atomic_t          f_refs;     /* Reference count */
+  sem_t             f_closem;   /* Free: file is fully closed */

Review Comment:
   from the spec(https://man7.org/linux/man-pages/man2/dup.2.html):
   ```
   After a successful return, the old and new file descriptors may be
          used interchangeably.  Since the two file descriptors refer to the
          same open file description, they share file offset and file status
          flags; for example, if the file offset is modified by using
          [lseek(2)](https://man7.org/linux/man-pages/man2/lseek.2.html) on one 
of the file descriptors, the offset is also
          changed for the other file descriptor.
   
          The two file descriptors do not share file descriptor flags (the
          close-on-exec flag).  The close-on-exec flag (FD_CLOEXEC; see
          [fcntl(2)](https://man7.org/linux/man-pages/man2/fcntl.2.html)) for 
the duplicate descriptor is off.
   ```
   we should split file into two struct:
   ```
   struct file
   {
   #ifdef CONFIG_FS_REFCOUNT
     atomic_t          f_refs;     /* Reference count */
   #endif
     off_t             f_pos;      /* File position */
     FAR struct inode *f_inode;    /* Driver or file system interface */
     FAR void         *f_priv;     /* Per file driver private data */
   #if CONFIG_FS_LOCK_BUCKET_SIZE > 0
     bool              locked; /* Filelock state: false - unlocked, true - 
locked */
   #endif
   };
   
   struct fd
   {
     struct file      *file;
     int               f_oflags;   /* Open mode flags */
   #ifdef CONFIG_FDSAN
     uint64_t          f_tag_fdsan; /* File owner fdsan tag, init to 0 */
   #endif
   #ifdef CONFIG_FDCHECK
     uint8_t           f_tag_fdcheck; /* File owner fdcheck tag, init to 0 */
   #endif
   #if CONFIG_FS_BACKTRACE > 0
     FAR void         *f_backtrace[CONFIG_FS_BACKTRACE]; /* Backtrace to while 
file opens */
   #endif
   };
   ```
   with this separation, we can avoid the crash natively since `struct fd` can 
be overwrite directly without any problem.



-- 
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

Reply via email to