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

10

当我使用ioctl号码调用设备的ioctl时,它是如何知道调用哪个函数的?

3个回答

16

ioctl(2) 通过 fs/ioctl.c 函数进入:

SYSCALL_DEFINE3(ioctl, unsigned int, fd, unsigned int, cmd, unsigned long, arg)
{
    struct file *filp;
    int error = -EBADF;
    int fput_needed;

    filp = fget_light(fd, &fput_needed);
    if (!filp)
            goto out;

    error = security_file_ioctl(filp, cmd, arg);
    if (error)
            goto out_fput;

    error = do_vfs_ioctl(filp, fd, cmd, arg);
 out_fput:
    fput_light(filp, fput_needed);
 out:
    return error;
}

注意已经有一个文件描述符fd相关联。然后内核调用fget_light()查找一个filp(大致上是文件指针,但不要将其与标准IO FILE *文件指针混淆)。调用security_file_ioctl()检查加载的安全模块是否允许ioctl操作(无论是按名称,如AppArmor和TOMOYO,还是按标签,如SMACK和SELinux),以及用户是否具有正确的能力(capabilities(7))进行调用。如果允许调用,则调用do_vfs_ioctl()来处理常见的ioctls操作:

    switch (cmd) {
    case FIOCLEX:
            set_close_on_exec(fd, 1);
            break;
    /* ... */

如果这些常见情况都不符合要求,那么内核会调用一个辅助函数:

static long vfs_ioctl(struct file *filp, unsigned int cmd,
                  unsigned long arg)
{
    int error = -ENOTTY;

    if (!filp->f_op || !filp->f_op->unlocked_ioctl)
            goto out;

    error = filp->f_op->unlocked_ioctl(filp, cmd, arg);
    if (error == -ENOIOCTLCMD)
            error = -EINVAL;
 out:
    return error;
}

驱动程序提供自己的.unlocked_ioctl函数指针,就像在fs/pipe.c中的这个管道实现一样:

const struct file_operations rdwr_pipefifo_fops = {
    .llseek         = no_llseek,
    .read           = do_sync_read,
    .aio_read       = pipe_read,
    .write          = do_sync_write,
    .aio_write      = pipe_write,
    .poll           = pipe_poll,
    .unlocked_ioctl = pipe_ioctl,
    .open           = pipe_rdwr_open,
    .release        = pipe_rdwr_release,
    .fasync         = pipe_rdwr_fasync,
};

1

内核中有一个映射表。如果你编写驱动程序,可以注册自己的ioctl代码。

编辑:我曾经编写过一个ATA over Ethernet驱动程序,并实现了一个自定义的ioctl来在运行时调整驱动程序。


1

一个简化的解释:

你传递给 ioctl 的文件描述符指向代表你要 ioctl 的设备的 inode 结构。

inode 结构包含设备号 dev_t i_rdev,它被用作索引来查找设备驱动程序的 file_operations 结构。在这个结构中,有一个指向设备驱动程序定义的 ioctl 函数的指针。

你可以阅读 Linux 设备驱动程序第三版 获取更详细的解释。它可能有点过时,但仍然是一篇好文章。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接