how is the correct way to use copy_from_user?

后端 未结 2 1584
谎友^
谎友^ 2021-01-05 11:02

I am trying to copy a value from user space to kernel space with the function:

static ssize_t device_write(struct file *filp, const char *buff, size_t len, l         


        
2条回答
  •  春和景丽
    2021-01-05 11:37

    The write function prototype in the manual is:

    ssize_t write(int fd, const void *buf, size_t count);

    So you only need to pass 3 values to write, namely: the file descriptor fd, buffer where your data lies, and count of bytes you want to write.

    This regarding the user space. Now let's move to the kernel space write function, i.e. your device_write.

    The argument buf to this function is the one which contains data which you want to write from user space, count is the length of data sent to be written by the kernel. So you are supposed to copy data from buf pointer and not len.

    So, the correct way would be:

    char *desp;  //allocate memory for this in kernel using malloc
    copy_from_user (desp, buff, len);
    

    This should do.

提交回复
热议问题