Linux字符设备驱动程序如何检测使用它的程序何时异常退出?

Dav*_*dia 6 c linux file-io driver

我有一个Linux字符设备驱动程序,它创建一个/dev/mything条目,然后是一个C++/Qt程序打开设备并使用它.如果该程序正确退出,exit()则关闭设备并且驱动程序正确地重置.但是,如果程序异常退出,通过段错误SIGINT或其他东西,设备未正确关闭.

我目前的解决方法是在驱动程序陷入"打开"状态时重新加载驱动程序.

驱动程序中的这一行试图阻止多个程序同时使用该设备:

int mything_open( struct inode* inode, struct file* filp ) {
    ...
    if ( port->rings[bufcount].virt_addr ) return -EBUSY;
    ...
}
Run Code Online (Sandbox Code Playgroud)

然后这清理它:

int mything_release( struct inode* inode, struct file* filp ) {
    ...
    port->rings[bufcount].virt_addr = NULL;
    ...
}
Run Code Online (Sandbox Code Playgroud)

我认为exit()导致mything_release被召唤但SIGINT不是.如何在这种情况下使驱动程序更加强大?

编辑:

以下是我实施的操作.也许我错过了什么?

static struct file_operations fatpipe_fops = {
    .owner =    THIS_MODULE,
    .open =     mything_open,
    .release =  mything_release,
    .read =     mything_read,
    .write =    mything_write,
    .ioctl =    mything_ioctl
};
Run Code Online (Sandbox Code Playgroud)

Dav*_*dia 1

问题归结为 中的这一行mything_release,放入其中等待一些内存写入完成:

if (wait_event_interruptible_timeout(port->inq, false, 10)) return -ERESTARTSYS;
Run Code Online (Sandbox Code Playgroud)

如果程序正常退出,这将旋转 10 jiffies 并继续运行。但是,由于 SIGINT 或其他异常退出,我认为可中断超时被中断并返回-ERESTARTSYS,导致我的 if 返回相同的值。

对我有用的就是摆脱if并等待:

wait_event_interruptible_timeout(port->inq, false, 10);
Run Code Online (Sandbox Code Playgroud)

几年前的这个补丁让我相信ERESTARTSYS从 close/_release 函数返回不是一个好主意:http://us. Generation-nt.com/answer/patch-fix-wrong-error-code-interrupted-close-系统调用-帮助-181191441.html