LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Robert Hancock <hancockr@shaw.ca>
To: linux-kernel@vger.kernel.org
Cc: "Jonathan M. McCune" <jonmccune@cmu.edu>
Subject: Re: reading a binary sysfs attribute continues forever
Date: Wed, 24 Jan 2007 18:12:48 -0600	[thread overview]
Message-ID: <45B7F600.1040906@shaw.ca> (raw)
In-Reply-To: <fa.uWrqVGz9Bu85mnRCc+6sYuYueiQ@ifi.uio.no>

Jonathan M. McCune wrote:
> Hello,
> 
> I have written a kernel module which introduces a new subsystem in
> sysfs, and it contains several attributes, one of which is binary.  So
> far, I've been testing it using text.  My problem is, attempting to read
> data continues forever.  For example:
> 
> # echo "test data" > /sys/mystuff/binaryattrib
> # cat /sys/mystuff/binaryattrib
> 
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> test data
> .. and so on forever.
> 
> Here are the read and write functions for the binary attribute:
> 'void *input_params' is a pointer to PAGE_SIZE (4096 for me) bytes, and
> 'size_t input_params_size' contains the actual number of bytes which
> were written  to input_params by the _write function.  input_params_size
> is initialized to zero when the module is first loaded.
> 
> static ssize_t binaryattrib_read(struct kobject *kobj, char *buf, loff_t
> pos,
>                            size_t count) {
> 
>     if(input_params_size + sizeof(size_t) > count)
>         return -EINVAL;
> 
>     memcpy(buf, (void *)&input_params_size, sizeof(size_t));
>     memcpy(buf+sizeof(size_t), input_params, input_params_size);
> 
>     return input_params_size + sizeof(size_t);

I don't know sysfs functions well but this looks wrong, you're ignoring 
the pos argument completely and always returning the same data. You 
should be copying the data starting at pos and returning only the number 
of bytes available from that point (possibly zero). Otherwise the user 
app will never get an end of file since more data is always available.

Also, you shouldn't be returning EINVAL if they try to read less than 
the size of your data as that is acceptable behavior and will work fine 
if you handle the pos argument properly.

-- 
Robert Hancock      Saskatoon, SK, Canada
To email, remove "nospam" from hancockr@nospamshaw.ca
Home Page: http://www.roberthancock.com/


       reply	other threads:[~2007-01-25  0:13 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <fa.uWrqVGz9Bu85mnRCc+6sYuYueiQ@ifi.uio.no>
2007-01-25  0:12 ` Robert Hancock [this message]
2007-01-23 18:37 Jonathan M. McCune

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=45B7F600.1040906@shaw.ca \
    --to=hancockr@shaw.ca \
    --cc=jonmccune@cmu.edu \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: reading a binary sysfs attribute continues forever' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).