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

发布于 2025-01-06 05:10:43 字数 685 浏览 1 评论 0原文

上的内核模块示例

我正在查看此 页面 程序中使用的 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;
}

有人可以解释一下为什么 off 被检查为大于 0。此外有人可以解释一下 off 和 count 参数的重要性吗?

到目前为止我的理解是我们必须在页面中写入数据并且必须在数据结束时设置eof。

谢谢。

I am reviewing the kernel module example at this page

The read_proc used in the program is as follows:

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.

My understanding so far is that we have to write data in page and have to set eof when data has ended.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2025-01-13 05:10:43

off 是文件中必须读取数据的位置。这就像普通文件的偏移量。但是,对于 proc_read 来说,情况有些不同。例如,如果您对 proc 文件调用 read 调用来读取 100 个字节的数据,proc_read 中的 off 和 count 将如下所示:

第一次,off = 0,count 100。例如,在 proc_read 中您有仅返回 10 个字节。然后控制权将无法返回到用户应用程序,您的 proc_read 将再次被内核调用,off 为 10,计数为 90。同样,如果您在 proc_read 中返回 20,您将再次以 off 30,计数 70 调用。像这样,您将被调用,直到计数达到 0。然后数据被写入给定的用户缓冲区,并且您的应用程序 read() 调用返回。

但如果你没有一百个字节的数据并且只想返回几个字节,则必须将eof设置为1。然后read()函数立即返回。

首先,以下评论比我解释得更好。

      /*
       * 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 [email protected])
                     * 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.
                     */

off is the position in the file from where data has to be read from. This is like off set of normal file. But, in the case of proc_read it is some what different. 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:

in the first time, off = 0, count 100. Say for example in your proc_read you have returned only 10 bytes. 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.

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.

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 [email protected])
                     * 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.
                     */
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文