Re: using ioctl in linux modules

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 11/12/05, Hossein Aminaiee <aminaiee@xxxxxxxxx> wrote:
> Dear all,
> I just wanted to know how it is possible to open a device file in a
> linux module and issue ioctl commands. I have included asm/ioctl.h in my
> code. But whenever I want to insert my module, I get "unknown symbol
> ioctl" message from the kernel.
> Is there any tutorial available, or Any tips and tricks?
>

You can't directly call ioctl function rather you have to get the the
file_operations or block_device_operation pointers of the device
(registered with kernel in registration function of device) on which
you want to call ioctl. Or you have to work-out on doing system-calls
from with-in the kernel module .... I am giving a code example which I
used to open a file in kernel module and perform read operation on it
and you can try/use the same method to call ioctl rather than read in
below code example !

function {

  mm_segment_t old_fs;
  int err;

  char *tmp;

  struct _file_struct {
	  struct file *file;
	  int pos;
	  int size;
  } fs_loader;


  old_fs = get_fs();
  set_fs(get_ds());
  tmp = getname("<file_name_or_might_be_device_file_name>");

  fs_loader.size = 0;
  fs_loader.file = filp_open(tmp, O_RDWR | O_NONBLOCK | O_CREAT, 0600);

  putname(tmp);

  if ((fs_loader.file->f_op == NULL) || (fs_loader.file->f_op->read == NULL) ||
      (fs_loader.file->f_op->write == NULL)) {
          printk("%s", "Wrong f_op or FS doesn't have required capabilities");
          res = -EINVAL;
          goto out_close_file;
  }

  // here f_op->read can be replaced by f_op->ioctl  ...
  err = fs_loader.file->f_op->read(fs_loader.file, lic_hash , 16, 
&fs_loader.file->f_pos);

  filp_close(fs_loader.file, current);
  fs_loader.file = NULL;
  set_fs(old_fs);

  return 0;

out_close_file:

  filp_close(fs_loader.file, current);
  fs_loader.file = NULL;
  set_fs(old_fs);

  return -1;

}


--
Fawad Lateef

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux