Simple Misc Driver - Problem with string copy to user
Hi! Goal, when the user do : # head -1 /dev/miscdrv The driver prints: Hello World! Steps: # Make # insmod misc.ko # head -1 /dev/miscdrv Why my driver doesn't work ? What is worg with my read operation? static ssize_t misc_drv_read(struct file *filp, char __user * buf, size_t count, loff_t * offp){ int nbytes; char * string = "hello World"; nbytes = copy_to_user(buf, string, 12); return nbytes; } -- Lucas Tanure Brazil
Lucas Tanure <tanure@linux.com> writes:
What is worg with my read operation?
static ssize_t misc_drv_read(struct file *filp, char __user * buf, size_t count, loff_t * offp){ int nbytes; char * string = "hello World"; nbytes = copy_to_user(buf, string, 12); return nbytes; }
copy_to_user returns the number of bytes which could *not* be copied. So it will return 0 on success, making your read return 0. Bjørn
So, My misc_drv_read returns 0, and it's ok. So why the command head didn't get the string ? -- Lucas Tanure +55 (19) 988176559 On Tue, May 27, 2014 at 9:59 AM, Bjørn Mork <bjorn@mork.no> wrote:
Lucas Tanure <tanure@linux.com> writes:
offp){ int nbytes; char * string = "hello World"; nbytes = copy_to_user(buf, string, 12); return nbytes; }
What is worg with my read operation?
static ssize_t misc_drv_read(struct file *filp, char __user * buf, size_t count, loff_t
copy_to_user returns the number of bytes which could *not* be copied. So it will return 0 on success, making your read return 0.
Bjørn
Wow, many thanks. So the read operation should return the total number of bytes, not a true/false int. I need to read more about this operations. Thanks -- Lucas Tanure +55 (19) 988176559 On Tue, May 27, 2014 at 10:05 AM, Bjørn Mork <bjorn@mork.no> wrote:
Lucas Tanure <tanure@linux.com> writes:
So, My misc_drv_read returns 0, and it's ok. So why the command head didn't get the string ?
You told it that it got a string with length 0. And that's what it printed.
Bjørn
Hi! On Die, 2014-05-27 at 10:09 -0300, Lucas Tanure wrote:
Wow, many thanks. So the read operation should return the total number of bytes, not a true/false int.
The syscall here (done by `head`) is read() ...
I need to read more about this operations.
.. and the drivers .read function is called if the user-space calls read() on the (opened) device. This .read function returns *) > 0 with the number of successfully read bytes *) == 0 on end-of-file. *) < 0 on errors. And these error codes are found on `man errno` and one just returns them as "-Exxxx". [ Fullquote deleted ] Kind regards, Bernd
I think it is good for you to read LDD3 or google some corresponding materials at first. 2014-05-27 20:48 GMT+08:00 Lucas Tanure <tanure@linux.com>:
Hi!
Goal, when the user do : # head -1 /dev/miscdrv
The driver prints: Hello World!
Steps: # Make # insmod misc.ko # head -1 /dev/miscdrv
Why my driver doesn't work ?
What is worg with my read operation?
static ssize_t misc_drv_read(struct file *filp, char __user * buf, size_t count, loff_t * offp){ int nbytes; char * string = "hello World"; nbytes = copy_to_user(buf, string, 12); return nbytes; }
-- Lucas Tanure Brazil
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
participants (4)
-
Bernd Petrovitsch -
Bjørn Mork -
Le Tan -
Lucas Tanure