简体   繁体   中英

Using one of the parameters in 'write' function for Linux Character Device Driver

I am a student who is trying to learn about Linux character Device Driver. I've been following the tutorial which is written here with some modifications. The read and write system calls for this driver only read and write on a char buffer. The problem is that it seems to me the parameter loff_t *off changes its value when it is supposed no to do that. What I mean is, if you see the next piece of code.

static ssize_t my_write(struct file *f, const char __user *buf,
    size_t len, loff_t *off){
int i;
char byte;
printk(KERN_INFO "Driver: write()\n");
printk(KERN_INFO "1. The value of *off is:%i\n",*off);
if (*off >= (BUFFER_SIZE - 1)){
        return 0;
    }
printk(KERN_INFO "2. The value of *off is:%i\n",*off);
    if ((*off + len) > (BUFFER_SIZE - 1)){
        len = BUFFER_SIZE - 1 - *off;
    }
printk(KERN_INFO "3. The value of *off is:%i\n",*off);
for (i = 0; i < len; i++){
        if (copy_from_user(&byte, buf + i, 1)){
                return -EFAULT;
        }
        c[i]=byte;
    }
printk(KERN_INFO "4. The value of *off is:%i\n",*off);
c[len]='\n';
*off += len;
printk(KERN_INFO "5. The value of *off is:%i\n",*off);
    return len;
}

The outcomes on the console for the printk calls are:

  1. The value of *off is:0
  2. The value of *off is:-1
  3. The value of *off is:0
  4. The value of *off is:693260
  5. The value of *off is:-2132049164

Could you tell me why? Thank you.

From the Chapter 3: Char Drivers in LDD...

off is a pointer to a “long offset type” object that indicates the file position the user is accessing.

Whatever the amount of data the methods transfer, they should generally update the file position at *offp to represent the current file position after successful completion of the system call.

For more information refer Figure 3-2 in https://lwn.net/images/pdf/LDD3/ch03.pdf

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM