Re: simple character device driver

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Sep 10, 2010 at 08:38:34PM +0200, fabio de francesco wrote:
> On Friday 10 September 2010 20:23:13 Josh Cartwright wrote:
> > On Fri, Sep 10, 2010 at 05:51:41PM +0200, fabio de francesco wrote:
> > > static ssize_t
> > > mycdrv_read (struct file *file, char __user * buf, size_t lbuf, loff_t *
> > > ppos) {
> > > 
> > >     int nbytes, maxbytes, bytes_to_do;
> > >     maxbytes = KBUF_SIZE - *ppos;
> > >     bytes_to_do = lbuf <= maxbytes ? lbuf : maxbytes;
> > >     nbytes = lbuf - copy_to_user (buf, kbuf + *ppos, bytes_to_do);
> > >     *ppos += nbytes;
> > >     printk (KERN_INFO "\n READING function, nbytes=%d, pos=%d\n", nbytes,
> > >     
> > >             (int)*ppos);
> > >     
> > >     return nbytes;
> > > 
> > > }
> > 
> > I'm not going to flat out answer your query, but help you work towards
> > the solution yourself.
> > 
> > Think about the termination case.  In order to terminate, you'll need to
> > return 0 to signal an end of file to the user-mode read.
> > 
> > In what case could your code return 0?
> 
> Ok. Good strategy!
> 
> Let me think...
> 
> When *ppos reaches the end of file it should be equal to KBUF_SIZE. Therefore 
> KBUF_SIZE - *ppos returns 0 to maxbytes. Immediatelly afterwards bytes_to_do 
> is set to 0 too. Right?
> 
> Ah! I think I see the bug at this point in walking the code... nbytes is set 
> to lbuf because copy_to_user() returns 0. So I never have nbytes set to 0 in 
> order to signal an end-of-file.
> 
> Is my reasoning correct?

Yep.  Thats how I reasoned about it.  Did you give it a try?

-- 
                                            - joshc

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux