简体   繁体   English

写入Linux设备驱动程序会导致无限循环

[英]Writing to Linux device driver causes infinite loop

I have been writing a kernel space device driver that can be read from and written to from user space. 我一直在编写一个可以从用户空间读取和写入的内核空间设备驱动程序。 The open, read, release operations all work perfectly. 开放,阅读,发布操作都完美无缺。 The problem I am having is with the user-space code that should access the device driver and and write something to it. 我遇到的问题是应该访问设备驱动程序并向其写入内容的用户空间代码。

The user-space program writes to two files: 1) to a .txt file (and prints a to the console to let the user know it was completed), and 2) to the device driver (and also prints a text to let the user know it was also completed). 用户空间程序写入两个文件:1)到.txt文件(并打印到控制台以让用户知道它已完成),以及2)到设备驱动程序(并且还打印文本以让用户知道它也已完成)。

Below is the user-space code in full: 以下是完整的用户空间代码:

int main() {
    FILE *fp;

    fp = fopen("./test.txt","w");
    fputs("Test\n", fp);
    fclose(fp);
    printf("Printed to txt\n"); //Prints normally.

    fp = fopen("/dev/testchar", "w");        
    fputs("Test\n", fp);
    fclose(fp);
    printf("Printed to dev\n"); //Never gets to this point

    return 0;
}

When I compile and run the code the program spits out 当我编译并运行代码程序吐出时

Printed to txt

and just hangs until ctrl+c is called. 然后挂起,直到调用ctrl + c。 It never gets beyond the second fputs() . 它永远不会超越第二个fputs()

While monitoring kern.log I see endless calls to write to the device driver. 在监视kern.log我看到无限次调用写入设备驱动程序。

Here I have extracted relevant code from the device driver: 这里我从设备驱动程序中提取了相关代码:

static char msg[256] = {0};

static struct file_operations fops =
{
    .write = dev_write
};


static ssize_t dev_write(struct file *file, const char *buf, size_t len, loff_t *ppos)
{
    sprintf(msg, "Input:%s, Chars:%lu\n", buf, len);
    printk(KERN_NOTICE "%s\n", msg);

    return 0;
}

uname -r: 4.10.0-38-generic

gcc -v: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

My question is: why is the program getting stuck in an infinite loop when writing to the device, and how do I fix it? 我的问题是:为什么程序在写入设备时会陷入无限循环,我该如何解决?

Thanks in advance. 提前致谢。 Any help will be greatly appreciated. 任何帮助将不胜感激。

I think the kernel write operation is supposed to return the number of bytes written. 我认为内核写操作应该返回写入的字节数。 You return 0. So the write system call returns to userspace with 0. However, since your userspace code is using stdio, then your userspace code tries the write again, assuming the system call simply didn't write out all the data. 您返回0.因此,写入系统调用返回到用户空间为0.但是,由于您的用户空间代码使用stdio,因此您的用户空间代码会再次尝试写入,假设系统调用只是没有写出所有数据。 If you return the length of the input, then stdio will know all the data was written. 如果返回输入的长度,则stdio将知道所有数据都已写入。 Alternatively you can use the write system call directly rather than fputs . 或者,您可以直接使用write系统调用而不是fputs Your kernel code will still be incorrect, but your program will terminate. 您的内核代码仍然不正确,但您的程序将终止。 You can test this using strace and see all the system calls. 您可以使用strace测试它并查看所有系统调用。

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

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