简体   繁体   English

无法理解 Linux 内核模块中 read_proc 的工作

[英]Unable to understand working of read_proc in Linux kernel module

I am reviewing the kernel module example at this page我正在查看此页面上的内核模块示例

The read_proc used in the program is as follows:程序中使用的read_proc如下:

int fortune_read( char *page, char **start, off_t off,

               int count, int *eof, void *data )

{

    int len;

     if (off > 0) {
         *eof = 1;
          return 0;
     }

     /* Wrap-around */
    if (next_fortune >= cookie_index) next_fortune = 0;

    len = sprintf(page, "%s\n", &cookie_pot[next_fortune]);

    next_fortune += len;

    return len;
}

Can someone explain why off is checked to be greater than 0. Moreover can someone explain what is the importance of the off and count arguments.有人可以解释为什么检查关闭大于 0。此外,有人可以解释关闭和计数参数的重要性。

My understanding so far is that we have to write data in page and have to set eof when data has ended.到目前为止,我的理解是我们必须在页面中写入数据,并且必须在数据结束时设置 eof。

Thanks.谢谢。

off is the position in the file from where data has to be read from. off 是文件中必须从中读取数据的位置。 This is like off set of normal file.这就像关闭正常文件的设置。 But, in the case of proc_read it is some what different.但是,在 proc_read 的情况下,它有些不同。 For example if you invoke a read call on the proc file to read 100 bytes of data, off and count in the proc_read will be like this:例如,如果您对 proc 文件调用 read 调用以读取 100 字节的数据,则 proc_read 中的 off 和 count 将如下所示:

in the first time, off = 0, count 100. Say for example in your proc_read you have returned only 10 bytes.在第一次,off = 0,计数 100。例如在您的 proc_read 中,您只返回了 10 个字节。 Then the control cannot come back to the user application, your proc_read will be called by the kernel once again with off as 10 and count as 90. Again if you return 20 in the proc_read, you will again called with off 30, count 70. Like this you will be called till count reaches 0. Then the data is written into the given user buffer and your application read() call returns.然后控制无法返回到用户应用程序,您的 proc_read 将被内核再次调用,关闭为 10 并计数为 90。再次如果您在 proc_read 中返回 20,您将再次调用关闭 30,计数为 70。像这样,您将被调用直到计数达到 0。然后将数据写入给定的用户缓冲区,您的应用程序 read() 调用返回。

But if you don't have hundred bytes of data and want to return only a few bytes, you must set the eof to 1. Then the read() function returns immediately.但是如果你没有一百字节的数据并且只想返回几个字节,你必须将 eof 设置为 1。然后 read() 函数立即返回。

For the start, the following comment explains better than me.首先,以下评论比我解释得更好。

      /*
       * How to be a proc read function
       * ------------------------------
                     * Prototype:
                     *    int f(char *buffer, char **start, off_t offset,
                     *          int count, int *peof, void *dat)
                     *
                     * Assume that the buffer is "count" bytes in size.
                     *
                     * If you know you have supplied all the data you
                     * have, set *peof.
                     *
                     * You have three ways to return data:
                     * 0) Leave *start = NULL.  (This is the default.)
                     *    Put the data of the requested offset at that
                     *    offset within the buffer.  Return the number (n)
                     *    of bytes there are from the beginning of the
                     *    buffer up to the last byte of data.  If the
                     *    number of supplied bytes (= n - offset) is 
                     *    greater than zero and you didn't signal eof
                     *    and the reader is prepared to take more data
                     *    you will be called again with the requested
                     *    offset advanced by the number of bytes 
                     *    absorbed.  This interface is useful for files
                     *    no larger than the buffer.
                     * 1) Set *start = an unsigned long value less than
                     *    the buffer address but greater than zero.
                     *    Put the data of the requested offset at the
                     *    beginning of the buffer.  Return the number of
                     *    bytes of data placed there.  If this number is
                     *    greater than zero and you didn't signal eof
                     *    and the reader is prepared to take more data
                     *    you will be called again with the requested
                     *    offset advanced by *start.  This interface is
                     *    useful when you have a large file consisting
                     *    of a series of blocks which you want to count
                     *    and return as wholes.
                     *    (Hack by Paul.Russell@rustcorp.com.au)
                     * 2) Set *start = an address within the buffer.
                     *    Put the data of the requested offset at *start.
                     *    Return the number of bytes of data placed there.
                     *    If this number is greater than zero and you
                     *    didn't signal eof and the reader is prepared to
                     *    take more data you will be called again with the
                     *    requested offset advanced by the number of bytes
                     *    absorbed.
                     */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM