How to use ioctl() from kernel space in Linux?

后端 未结 1 941
星月不相逢
星月不相逢 2021-02-09 19:07

Is it possible to call ioctl from a Linux kernel module? Can anyone provide an example of how it\'s used?

相关标签:
1条回答
  • 2021-02-09 19:28

    You can try to call sys_ioctl.
    It's exported if the kernel is compiled with CONFIG_COMPAT.

    Or, if you have the device driver's struct file_operations, you can call its ioctl handler directly.

    However, the ioctl handle would expect pointer parameters to be in the address space of the process currently running, not in the kernel address space. copy_from_user would be used to read them. If you give pointers to the kernel address space, copy_from_user will fail. I don't see how you would get around this.

    Edit:

    If you will call ioctl handler between below code than copy_from_user will not ever fail.

     mm_segment_t fs;
    
      fs = get_fs();     /* save previous value */
      set_fs (get_ds()); /* use kernel limit */
    
      /* system calls can be invoked */
    
      set_fs(fs); /* restore before returning to user space */
    
    0 讨论(0)
提交回复
热议问题