All of lore.kernel.org
 help / color / mirror / Atom feed
* Simple Misc Driver - Problem with string copy to user
@ 2014-05-27 12:48 Lucas Tanure
  2014-05-27 12:59 ` Bjørn Mork
  2014-05-28  0:27 ` Le Tan
  0 siblings, 2 replies; 7+ messages in thread
From: Lucas Tanure @ 2014-05-27 12:48 UTC (permalink / raw)
  To: kernelnewbies

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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140527/87c40ed5/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Makefile
Type: application/octet-stream
Size: 599 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140527/87c40ed5/attachment-0001.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: misc.c
Type: text/x-csrc
Size: 1288 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140527/87c40ed5/attachment-0001.bin 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 12:48 Simple Misc Driver - Problem with string copy to user Lucas Tanure
@ 2014-05-27 12:59 ` Bjørn Mork
  2014-05-27 13:03   ` Lucas Tanure
  2014-05-28  0:27 ` Le Tan
  1 sibling, 1 reply; 7+ messages in thread
From: Bjørn Mork @ 2014-05-27 12:59 UTC (permalink / raw)
  To: kernelnewbies

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 12:59 ` Bjørn Mork
@ 2014-05-27 13:03   ` Lucas Tanure
  2014-05-27 13:05     ` Bjørn Mork
  0 siblings, 1 reply; 7+ messages in thread
From: Lucas Tanure @ 2014-05-27 13:03 UTC (permalink / raw)
  To: kernelnewbies

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:
>
> >> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140527/58001a15/attachment.html 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 13:03   ` Lucas Tanure
@ 2014-05-27 13:05     ` Bjørn Mork
  2014-05-27 13:09       ` Lucas Tanure
  0 siblings, 1 reply; 7+ messages in thread
From: Bjørn Mork @ 2014-05-27 13:05 UTC (permalink / raw)
  To: kernelnewbies

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 13:05     ` Bjørn Mork
@ 2014-05-27 13:09       ` Lucas Tanure
  2014-05-27 13:33         ` Bernd Petrovitsch
  0 siblings, 1 reply; 7+ messages in thread
From: Lucas Tanure @ 2014-05-27 13:09 UTC (permalink / raw)
  To: kernelnewbies

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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20140527/2ebf48ed/attachment.html 

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 13:09       ` Lucas Tanure
@ 2014-05-27 13:33         ` Bernd Petrovitsch
  0 siblings, 0 replies; 7+ messages in thread
From: Bernd Petrovitsch @ 2014-05-27 13:33 UTC (permalink / raw)
  To: kernelnewbies

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Simple Misc Driver - Problem with string copy to user
  2014-05-27 12:48 Simple Misc Driver - Problem with string copy to user Lucas Tanure
  2014-05-27 12:59 ` Bjørn Mork
@ 2014-05-28  0:27 ` Le Tan
  1 sibling, 0 replies; 7+ messages in thread
From: Le Tan @ 2014-05-28  0:27 UTC (permalink / raw)
  To: kernelnewbies

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 at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-05-28  0:27 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-05-27 12:48 Simple Misc Driver - Problem with string copy to user Lucas Tanure
2014-05-27 12:59 ` Bjørn Mork
2014-05-27 13:03   ` Lucas Tanure
2014-05-27 13:05     ` Bjørn Mork
2014-05-27 13:09       ` Lucas Tanure
2014-05-27 13:33         ` Bernd Petrovitsch
2014-05-28  0:27 ` Le Tan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.