ioctl 如何知道在 Linux 中调用哪个函数?
那么,当我使用 ioctl 编号在设备上调用 ioctl 时,它如何知道要调用哪个函数?
So when I call an ioctl on a device, with an ioctl number, how does it know which function to call?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ioctl(2)
通过fs/ioctl.c
函数进入:请注意,已经有一个关联的文件描述符
fd
。然后内核调用fget_light()
来查找filp
(粗略地说,文件指针,但不要将其与标准 IOFILE *
混淆) > 文件指针)。对security_file_ioctl()
的调用检查加载的安全模块是否允许ioctl
(无论是通过名称,如 AppArmor 和 TOMOYO,还是通过标签,如 SMACK 和 SELinux) ),以及用户是否具有进行呼叫的正确能力(能力(7))。如果允许调用,则调用do_vfs_ioctl()
来处理常见的 ioctl 本身:如果这些常见情况都不正确,则内核调用辅助例程:
驱动程序提供自己的
.unlocked_ioctl
函数指针,如fs/pipe.c
中的管道实现:The
ioctl(2)
enters via thefs/ioctl.c
function:Note that there is already a filedescriptor
fd
associated. The kernel then callsfget_light()
to look up afilp
(roughly, file pointer, but don't confuse this with the standard IOFILE *
file pointer). The call intosecurity_file_ioctl()
checks whether the loaded security module will allow theioctl
(whether by name, as in AppArmor and TOMOYO, or by labels, as in SMACK and SELinux), as well as whether or not the user has the correct capability (capabilities(7)) to make the call. If the call is allowed, thendo_vfs_ioctl()
is called to either handle common ioctls itself:If none of those common cases are correct, then the kernel calls a helper routine:
Drivers supply their own
.unlocked_ioctl
function pointer, like this pipe implementation infs/pipe.c
:简单的解释:
传递给 ioctl 的文件描述符指向表示要 ioctl 的设备的 inode 结构。
inode
结构包含设备号dev_t i_rdev
,它用作查找设备驱动程序的file_operations
结构的索引。在此结构中,有一个指向设备驱动程序定义的ioctl函数的指针。您可以阅读 Linux 设备驱动程序,第三版 以获取更详细的说明。它可能有点过时,但仍然是一本好书。
A simplified explanation:
The file descriptor you pass to
ioctl
points to theinode
structure that represents the device you are going to ioctl.The
inode
structure contains the device numberdev_t i_rdev
, which is used as an index to find the device driver'sfile_operations
structure. In this structure, there is a pointer to theioctl
function defined by the device driver.You can read Linux Device Drivers, 3rd Edition for a more detailed explanation. It may be a bit outdated, but a good read nevertheless.
内核中有一个映射。如果您编写驱动程序,则可以注册自己的 ioctl 代码。
编辑:我曾经编写过一个 ATA over Ethernet 驱动程序,并实现了一个自定义 ioctl,用于在运行时调整驱动程序。
There's a map in the kernel. You can register your own ioctl codes if you write a driver.
Edit: I wrote an ATA over Ethernet driver once and implemented a custom ioctl for tuning the driver at runtime.