Donny9 opened a new issue, #6710:
URL: https://github.com/apache/incubator-nuttx/issues/6710

   # Issue
   The dup operation of nuttx is different from that of freebsd and opengroup.
   * Nuttx
     The old file descriptor and the new file descriptor generated by dup and 
dup2 don't share the file entity and are independent of each other. They 
correspond to different struct file entities in the filelist.  
   
     A file descriptor is an index into an fl_files array of sruct file. The 
struct file associates  the file descriptor to the file state and to a set of 
inode operations.
   ```
     struct file
     {
       int               f_oflags;   /* Open mode flags */
       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 */
     };
     
     /* This defines a two layer array of files indexed by the file descriptor.
      * Each row of this array is fixed size: 
CONFIG_NFILE_DESCRIPTORS_PER_BLOCK.
      * You can get file instance in filelist by the follow methods:
      * (file descriptor / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) as row index and
      * (file descriptor % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) as column index.
      */
     
     struct filelist
     {
       sem_t             fl_sem;     /* Manage access to the file list */
       uint8_t           fl_rows;    /* The number of rows of fl_files array */
       FAR struct file **fl_files;   /* The pointer of two layer file 
descriptors array */
     };                                                                         
          
   ```
   
   * FreeBSD/OpenGroup:
   
![image](https://user-images.githubusercontent.com/70748590/180934289-06d4cdab-eafc-4917-92e9-9fd99029b231.png)
   
   * Do you have good idea to fix this issue?
   In view of the above problems, it will cause some problems in the 
application of using dup, dup2 and fcntl on nuttx. 
   Here is one of the methods:
   I will use filenode to correspond to file descriptor one-to-one, and 
multiple filenodes can correspond to a unified file entity, When dup occurs, 
the file pointer corresponding to the old filenode can be copied to the new 
filenode to ensure sharing.
   ```
   struct file
   {
     int16_t           f_crefs;    /* References to file */
     int               f_oflags;   /* Open mode flags */
     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 */
   };
   
   /* A file descriptor is an index into an array of such types. The type is 
used to
    * multiple file descriptor to share the same open file, include file offset 
and
    * file status flags, buf don't share file descriptor flags "close-on-exec 
flag".
    */
   
   struct filenode
   {
     int               fn_oflags;  /* Open or duplicate mode flags */
     FAR struct file  *fn_file;    /* The pointer representation of an open 
file */
   };
   
   /* This defines a two layer array of files indexed by the file descriptor.
    * Each row of this array is fixed size: CONFIG_NFILE_DESCRIPTORS_PER_BLOCK.
    * You can get file instance in filelist by the follow methods:
    * (file descriptor / CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) as row index and
    * (file descriptor % CONFIG_NFILE_DESCRIPTORS_PER_BLOCK) as column index.
    */
   
   struct filelist
   {
     sem_t                 fl_sem;   /* Manage access to the file list */
     uint8_t               fl_rows;  /* The number of rows of fl_files array */
     FAR struct filenode **fl_files; /* The pointer of two layer file 
descriptors array */
   };
   ```
   And it also needs to modify file_openation:dup, directly delete or remove 
unnecessary features.
   
   
   # Refer to
   https://www.freebsd.org/cgi/man.cgi?query=dup&sektion=2
   https://pubs.opengroup.org/onlinepubs/009604499/functions/dup.html


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to