Linux如何知道调用哪个ioctl函数?

这是用户空间中的ioctl调用:
int ioctl(int fd,int cmd,...);

据我所知,当我们想要执行IO操作时,我们使用一组请求(命令)定义我们自己的ioctl函数,将我们的ioctl分配给这样的file_operations结构:

struct file_operations fops = {
 .read = device_read,.write = device_write,.ioctl = device_ioctl,// device_ioctl is our function
 .open = device_open,.release = device_release,};

与用户空间接口相比,device_ioctl函数的定义不同:

static long device_ioctl(struct file *f,unsigned int cmd,unsigned long arg)

我认为基于文件描述符,内核可以获得适当的文件结构并调用设备的ioctl.

这只是猜测,因为我找不到通用函数定义,其中内核根据传递到通用ioctl接口的文件描述符fd选择适当的ioctl函数?我只能找到3个ioctl定义,但显然那些只是设备的定义,而不是内核:ioctl

解决方法

查看Linux源代码,fs / ioctl.c( http://lxr.free-electrons.com/source/fs/ioctl.c)

在那里你会看到ioctl的系统调用:

SYSCALL_DEFINE3(ioctl,unsigned int,fd,cmd,unsigned long,arg)

这反过来调用do_vfs_ioctl(),它调用vfs_ioctl(),然后调用file_operations结构中为该文件系统定义的unlocked_ioctl函数.这将是您注册的device_ioctl函数.

相关文章

文章浏览阅读1.8k次,点赞63次,收藏54次。Linux下的目录权限...
文章浏览阅读1.6k次,点赞44次,收藏38次。关于Qt的安装、Wi...
本文介绍了使用shell脚本编写一个 Hello
文章浏览阅读1.5k次,点赞37次,收藏43次。【Linux】初识Lin...
文章浏览阅读3k次,点赞34次,收藏156次。Linux超详细笔记,...
文章浏览阅读6.8k次,点赞109次,收藏114次。【Linux】 Open...