Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion components/dfs/dfs_v1/src/dfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,15 @@ int dfs_file_close(struct dfs_file *fd)
/**
* this function will perform an io control on a file descriptor.
*
* F_GETFL(=3) collides numerically with RT_DEVICE_CTRL_CONFIG, and
* F_SETFL(=4) collides with RT_DEVICE_CTRL_CLOSE. Disambiguate by the
* POSIX-vs-RT-Thread convention on `args`:
* - F_GETFL (POSIX) ignores args -> caller passes NULL/0. CONFIG needs a
* struct pointer, so args != NULL means RT_DEVICE_CTRL_CONFIG.
* - F_SETFL (POSIX) carries the new flags, so args != NULL means F_SETFL.
* CLOSE needs no args, so args == NULL means RT_DEVICE_CTRL_CLOSE.
* The fall-through path (FT_SOCKET / FT_USER / ...) is unchanged.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果是这样,是否把它们定义成不同的值,这样才不会冲突?从而从根本上解决这个问题。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对于目前rtt通用的device_ctrl。

于dfs_v1中

#define RT_DEVICE_CTRL_CONFIG           0x03            /**< configure device */
#define RT_DEVICE_CTRL_CLOSE            0x04            /**< close device */

F_GETFL	3
F_SETFL	4

于dfs_v2

#define RT_DEVICE_CTRL_RESUME           0x01            /**< resume device */
#define RT_DEVICE_CTRL_SUSPEND          0x02            /**< suspend device */
#define RT_DEVICE_CTRL_CONFIG           0x03            /**< configure device */
#define RT_DEVICE_CTRL_CLOSE            0x04            /**< close device */

F_DUPFD	0
F_GETFD	1
F_SETFD	2
F_GETFL	3
F_SETFL	4

是会相撞的

*
* @param fd the file descriptor.
* @param cmd the command to send to file descriptor.
* @param args the argument to send to file descriptor.
Expand All @@ -391,8 +400,25 @@ int dfs_file_ioctl(struct dfs_file *fd, int cmd, void *args)
switch (cmd)
{
case F_GETFL:
return fd->flags; /* return flags */
if (args != RT_NULL && fd->vnode->fops != RT_NULL && fd->vnode->fops->ioctl != RT_NULL)
{
/* args != NULL: treat cmd=3 as RT_DEVICE_CTRL_CONFIG and forward
* to the driver with the supplied config struct. */
return fd->vnode->fops->ioctl(fd, cmd, args);
}
else
{
/* args == NULL: POSIX F_GETFL, return current fd flags. */
return fd->flags; /* return flags */
}
case F_SETFL:
if (args == RT_NULL && fd->vnode->fops != RT_NULL && fd->vnode->fops->ioctl != RT_NULL)
{
/* args == NULL: treat cmd=4 as RT_DEVICE_CTRL_CLOSE and forward
* to the driver. */
return fd->vnode->fops->ioctl(fd, cmd, args);
}
else
{
int flags = (int)(rt_base_t)args;
int mask = O_NONBLOCK | O_APPEND;
Expand Down
38 changes: 38 additions & 0 deletions components/dfs/dfs_v2/filesystems/devfs/devfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,44 @@ static int dfs_devfs_ioctl(struct dfs_file *file, int cmd, void *args)
if ((file->dentry->pathname[0] == '/') && (file->dentry->pathname[1] == '\0'))
return -RT_ENOSYS;

/* Disambiguate F_GETFL(=3)/F_SETFL(=4) vs RT_DEVICE_CTRL_CONFIG(=3)/
* RT_DEVICE_CTRL_CLOSE(=4) by args nullness. POSIX ignores args on
* F_GETFL (caller passes NULL/0) and carries the new flags on
* F_SETFL (args != NULL); the RTT cmds take a non-NULL struct
* pointer on CONFIG and no args on CLOSE. */
if (cmd == F_GETFL)
{
if (args != RT_NULL)
{
/* args != NULL: treat cmd=3 as RT_DEVICE_CTRL_CONFIG and
* fall through to the driver/device. */
}
else
{
/* args == NULL: POSIX F_GETFL, return current fd flags. */
return file->flags;
}
}
else if (cmd == F_SETFL)
{
if (args == RT_NULL)
{
/* args == NULL: treat cmd=4 as RT_DEVICE_CTRL_CLOSE and
* fall through to the driver/device. */
}
else
{
/* args != NULL: POSIX F_SETFL, update fd flags in place. */
int flags = (int)(rt_base_t)args;
int mask = O_NONBLOCK | O_APPEND;

flags &= mask;
file->flags &= ~mask;
file->flags |= flags;
return RT_EOK;
}
}

#ifdef RT_USING_POSIX_DEVIO
if (device->fops && device->fops->ioctl)
{
Expand Down
17 changes: 16 additions & 1 deletion components/dfs/dfs_v2/src/dfs_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -1722,7 +1722,16 @@ int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
file->mode = arg;
break;
case F_GETFL:
ret = file->flags;
if (arg != RT_NULL && file->fops != RT_NULL && file->fops->ioctl != RT_NULL)
{
/* cmd=3 with args != NULL: treat as RT_DEVICE_CTRL_CONFIG and forward to the driver with the config struct. */
ret = file->fops->ioctl(file, cmd, (void *)(rt_base_t)arg);
}
else
{
/* args == NULL: POSIX F_GETFL, return current fd flags. */
ret = file->flags;
}
break;
case F_SETFL:
{
Expand All @@ -1739,6 +1748,12 @@ int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
#endif
O_APPEND | O_NONBLOCK;

if (arg == RT_NULL && file->fops != RT_NULL && file->fops->ioctl != RT_NULL)
{
/* cmd=4 with args == NULL: treat as RT_DEVICE_CTRL_CLOSE and forward to the driver. */
ret = file->fops->ioctl(file, cmd, (void *)(rt_base_t)arg);
break;
}
flags &= mask;
if (file->vnode->type == FT_SOCKET && file->fops != RT_NULL &&
file->fops->ioctl != RT_NULL)
Expand Down
Loading