cat function calling read() infinite times

*爱你&永不变心* 提交于 2019-11-26 23:19:33

.read function should also correctly process its len and off arguments. The simplest way to implement reading from memory-buffered file is to use simple_read_from_buffer helper:

static ssize_t dev_read(struct file *filp, char *buff, size_t len, loff_t *off)
{
    return simple_read_from_buffer(buff, len, off, msg, 100);
}

You can inspect code of that helper (defined in fs/libfs.c) for educational purposes.

BTW, for your .write method you could use simple_write_to_buffer helper.

You are not respecting the buffer size passed into the dev_read function, so you may be invoking undefined behaviour in cat. Try this:

static ssize_t dev_read( struct file *filp, char *buff, size_t len, loff_t  *off )
{
    size_t count = 0;
    printk( KERN_ALERT"inside read %d\n", *off );
    while( msg[count] != 0 && count < len )
    {
        put_user( msg[count], buff++ );
        count++;
    }
    return count;
}
Nadim Almas Siddiqui

This problem can be solved by correctly setting *off (fourth parameter of my_read()).

You need to return count for the first time and zero from second time onwards.

if(*off == 0) {
    while (msg[count] != 0) {
        put_user(msg[count], buff++);
        count++;
        (*off)++;
    }
    return count;
}
else
return 0;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!