Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux-kernel

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

like image 970
Varda Elentári Avatar asked Jun 20 '12 06:06

Varda Elentári


1 Answers

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 */
like image 96
ugoren Avatar answered Oct 05 '22 11:10

ugoren