/ proc / filesystem read被称为三次

时间:2012-10-26 20:05:49

标签: linux filesystems linux-kernel proc

这是我的第一个内核,在下面的示例程序中,我创建了一个简单的proc / filesystem。每当我抓住内核时,我都会看到READ被调用3次。想知道为什么会这样做。

Out,代码在下面。

cat /proc/myKernel
dmesg | grep -i myKernel
myKernel: Read (/proc/myKernel) called
myKernel: Read (/proc/myKernel) called
myKernel: Read (/proc/myKernel) called

int myKernel_read( char *buffer, char **bufferLocation, off_t offset, int bufferLength, int *eof, void *data )
{
    int ret = 0;
    u64 msrvalue;

    printk(KERN_INFO "myKernel: Read (/proc/%s) called\n", procFile_name);
    ret = sprintf(buffer, "Hello World\n");

    return ret;
}

static int __init myKernel_init(void)
{
    entry = create_proc_entry(procFile_name, 0644, NULL);
    if(!entry)
        printk(KERN_INFO "myKernel: error registering proc control file\n");
    else 
    {
        entry->read_proc = myKernel_read;
    }

    return 0;
}

1 个答案:

答案 0 :(得分:1)

问题在于ret值。我仍然不知道为什么这被称为三次但我必须在偏移量小于零时进行实际读数。

int myKernel_read( char *buffer, char **bufferLocation, off_t offset, int bufferLength, int *eof, void *data )
{
    int ret;
    u64 msrvalue;

    if (offset > 0) 
    {
        /* we have finished to read, return 0 */
        ret  = 0;
    } else 
    {
        /* fill the buffer, return the buffer size */
                // DO THE READ HERE. NOT OUTSIDE
                //
                printk(KERN_INFO "myKernel: Read (/proc/%s) called\n", procFile_name);
        ret = sprintf(buffer, "MyKernel = %x\n", 0);
    }

    return ret;
}