Re: How to access a file from kernel module

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

 



On 9/16/05, yenganti pradeep <pradeepls143@xxxxxxxxxxx> wrote:
> 
> Hi all,
>        Is there any way to access a file from kernel
> module??
> 

Yes, here's the way/code for that !!!!


  mm_segment_t old_fs;
  int err;

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


  old_fs = get_fs();
  set_fs(get_ds());
  tmp = getname("<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;
  }

  err = fs_loader.file->f_op->read(fs_loader.file,
<buffer_to_read_in>, <bytes_to_read>,  &fs_loader.file->f_pos);

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

  return;


out_close_file:

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




-- 
Fawad Lateef
-
: send the line "unsubscribe linux-net" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Netdev]     [Ethernet Bridging]     [Linux 802.1Q VLAN]     [Linux Wireless]     [Kernel Newbies]     [Security]     [Linux for Hams]     [Netfilter]     [Git]     [Bugtraq]     [Yosemite News and Information]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux PCI]     [Linux Admin]     [Samba]

  Powered by Linux