LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* reading a binary sysfs attribute continues forever
@ 2007-01-23 18:37 Jonathan M. McCune
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan M. McCune @ 2007-01-23 18:37 UTC (permalink / raw)
  To: linux-kernel

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);
}

static ssize_t binaryattrib_write(struct kobject *kobj, char *buf,
loff_t pos,
                              size_t count)
{
        memset(input_params, 0, PAGE_SIZE);

        if ((pos + count) > PAGE_SIZE)
                return -EINVAL;

        memcpy(input_params+pos, buf, count);
        input_params_size = count;

        return count;
}

If binaryattrib_read() returns 0, the userspace program reads no data.
If it returns any positive number, the userspace program reads forever.
 If it returns a negative number, that is interpreted as an error.

I am at a loss.  Is it the case that the userspace program should be
responsible enough to read the size out of the first 4 bytes of data (as
I have included above) and simply stop reading once it gets everything?

The documentation I have been able to find (mostly
http://lwn.net/Articles/54651/ and slight variants of it) suggests that
writes work in this way, in that the _write function will be called
arbitrarily many times with PAGE_SIZE or smaller chunks, and it is up to
the kernel module to determine when all the data has arrived.

Thank you to anyone who can offer me some insight!
-Jon

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

* Re: reading a binary sysfs attribute continues forever
       [not found] <fa.uWrqVGz9Bu85mnRCc+6sYuYueiQ@ifi.uio.no>
@ 2007-01-25  0:12 ` Robert Hancock
  0 siblings, 0 replies; 2+ messages in thread
From: Robert Hancock @ 2007-01-25  0:12 UTC (permalink / raw)
  To: linux-kernel; +Cc: Jonathan M. McCune

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/


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

end of thread, other threads:[~2007-01-25  0:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-23 18:37 reading a binary sysfs attribute continues forever Jonathan M. McCune
     [not found] <fa.uWrqVGz9Bu85mnRCc+6sYuYueiQ@ifi.uio.no>
2007-01-25  0:12 ` Robert Hancock

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).