简体   繁体   English

Linux内核模块奇怪的行为

[英]Linux kernel module strange behaviour

I am studying a linux kernel, so I try to write a simple modules. 我正在研究linux内核,因此我尝试编写一个简单的模块。

The following code is supposed to control how many times read() for the /proc/proc_test is called: 以下代码应该控制/proc/proc_test read()调用次数:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/proc_fs.h>
#include <linux/kernel.h>
#include <asm/current.h>

static int __init init(void);
static void __exit stop(void);
static int proc_read(char *buffer, char **start, off_t offset, int size, int *eof, void *data);

static int counter = 0;

static int proc_read(char *buffer, char **start, off_t offset, int size, int *eof, void *data) {
    if (size < 256) return -ENOMEM;
    return sprintf(buffer, "counter = %d\n", counter++); <-- supposed to be incremented once
}

static int __init init() {
    if (create_proc_read_entry("proc_test", 0, NULL, proc_read, NULL) == 0) {
        printk(KERN_ERR "Can not creat entry\n");
        return -ENOMEM;
    }
    printk("Entry created!\n");
    return 0;
}

static void __exit stop() {
    remove_proc_entry("proc_test", NULL);
    printk("Entry removed!\n");
    return;
}

module_init(init);
module_exit(stop);

MODULE_LICENSE("GPL");

I am facing the issue that when I read from the /proc/proc_test/ using cat or tail , the counter is incremented by 3 instead of by 1. 我遇到的问题是,当我使用cattail/proc/proc_test/读取时,计数器将增加3而不是增加1。

Output: 输出:

cat /proc/proc_test 
counter = 0

cat /proc/proc_test 
counter = 3

cat /proc/proc_test 
counter = 6

What am I doing wrong? 我究竟做错了什么?

try this: 尝试这个:

static int proc_read(char *buffer, char **start, off_t offset, int size, int *eof, void *data) {
    if (size < 256) return -ENOMEM;
    int count= sprintf(buffer, "counter = %d\n", counter++);
    *eof = 1;
    return count;
}

setting *eof=1, your driver informs the kernel (and the application which want to read your proc file) that your driver reached EOF. 设置* eof = 1,您的驱动程序会通知内核(以及要读取proc文件的应用程序)您的驱动程序已达到EOF。

The only thing wrong is that you have an unreasonable expectation. 唯一的错误是您的期望不合理。 What makes you think cat /proc/proc_test will only call read once? 是什么让您认为cat /proc/proc_test将仅调用一次read

$ strace cat /proc/self/stat | grep read
read(3, "17423 (cat) R 17420 17420 13224 "..., 32768) = 238
read(3, "", 32768)                      = 0
$

Even though this answer has a good hint, I have been having this issue too. 即使这个答案有一个很好的提示,我也一直遇到这个问题。 Setting *eof = 1 theoretically should have solved the problem but somehow it doesn't. 理论上设置*eof = 1应该可以解决该问题,但不能解决。

My fix was to also add this on the top of the function: 我的解决方法是将其添加到函数顶部:

if (offset > 0)    // even though I set *eof = 1, the kernel is still calling me, so return 0 for it to stop
    return 0;

The comment above is in fact what I had written in my own module. 上面的评论实际上是我在自己的模块中编写的。

What this does is to make sure only the first call to your function (in which offset is 0) does anything. 这是为了确保仅对函数的第一个调用( offset为0)会执行任何操作。

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

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