How can I create or read/write a file in linux device driver?

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

 



Hello everyone, I want to create and read/write a file in Linux kernel or device driver, I have write some code, the file could be created successfully, but it can not be wrote in anything, the function "write" always return a negative number, the content of the file is still empty. The following code is what I wrote in my device driver, the code reference the function sys_open() in open.c and sys_write() in read_write.c, please give me a help, thanks!

struct file *filp;
char testbuf[100] = {0};
char *ptestbuf = NULL;
ssize_t count = 0;
ssize_t ret = 0;
int ii = 0;
   
filp = filp_open("/logfile", O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (IS_ERR(filp))
   printk("<0>Create record file error!\n\r");
for(ii = 0; ii < 100; ii++)
   testbuf[ii] = 1;
ptestbuf = testbuf;

if (filp->f_mode & FMODE_WRITE) 
{
   struct inode *inode = filp->f_dentry->d_inode;
   ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, filp, filp->f_pos, count);
   if (!ret) 
   {
      ssize_t (*write)(struct file *, const char *, size_t, loff_t *);
      if (filp->f_op && (filp->f_op->write) != NULL) 
      {
         write = filp->f_op->write;
         count = 99;
         ret = write(filp, (const char *)ptestbuf, count, &filp->f_pos);
         printk("<0>After write, ret = %d   \n\r");
      }
   }
}

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[Index of Archives]     [Kernel Newbies]     [Netfilter]     [Bugtraq]     [Photo]     [Stuff]     [Gimp]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Video 4 Linux]     [Linux for the blind]     [Linux Resources]
  Powered by Linux