Linux如何知道调用哪个ioctl函数?
发布时间:2020-12-13 19:55:43 所属栏目:Linux 来源:网络整理
导读:这是用户空间中的ioctl调用: int ioctl(int fd,int cmd,...); 据我所知,当我们想要执行IO操作时,我们使用一组请求(命令)定义我们自己的ioctl函数,将我们的ioctl分配给这样的file_operations结构: struct file_operations fops = { .read = device_read,.wr
这是用户空间中的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函数. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |