应用程序以前由`root`运行时,QSerialPort无法打开tty [英] QSerialPort cannot open tty after application has previously been run by `root`

查看:838
本文介绍了应用程序以前由`root`运行时,QSerialPort无法打开tty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从串行端口读取和写入的应用程序(使用 QSerialPort )。当我以 root 用户身份运行此应用程序时,再次以非root用户身份运行,我不再能够写入串口,收到以下错误:

I have an application (using QSerialPort) that reads and writes from a serial port. When I run this application as the root user, then run it again as a non-root user, I am no longer able to write to the serial port, receiving the following error:

QIODevice::write (QSerialPort): device not open

非root用户位于 dialout 组中,而 / dev / tty ** 文件似乎没有变化:

The non-root user is in the dialout group, and the permissions on the /dev/tty** file in question appear to be unchanged:

crw-rw---T 1 root dialout ......

最奇怪的是,我不是

$> echo "foo bar baz" >> /dev/ttyS0
$> echo $?
0

我发现似乎解决问题的唯一的事情是重新启动机器。

The only thing I've found that appears to fix the problem is rebooting the machine.

可能在这里发生什么?

我在Debian 7上。 >

I'm on Debian 7.

推荐答案

更新:这是Qt中的一个错误,将在5.6.2版中修复本月晚些时候发布。

Update: this is a bug in Qt, and will be fixed in version 5.6.2, which is due for release later this month.

在Linux和Mac上, QSerialPort / var / lock / 打开串口。 lockfile有权限 0644 ,即只有文件的创建者可以写入它。

On Linux and Mac, QSerialPort creates a lock file in /var/lock/ when opening a serial port. The lockfile has permissions 0644, i.e. only the creator of the file can write to it.

如果进程打开串行端口,或者如果串行端口以任何其他方式不正确地关闭,则锁定文件将不会被删除。 lockfile包含打开串口的进程的PID;如果进程不再运行,Qt将尝试简单地占用该锁,更改文件中的PID。

If the process that opened the serial port dies or if the serial port is somehow improperly closed by any other means, the lock file will not be deleted. The lockfile contains the PID of the process that opened the serial port; if the process is no longer running, Qt will attempt to simply take possession of the lock, changing the PID in the file.

但是,由于lockfile具有 0644 权限,如果不正确关闭的过程由 root 运行,新进程将无法删除或覆盖锁定文件,导致权限错误。

However, since the lockfile has 0644 permissions, if the improperly-closing process was run by root, the new process will be unable to delete or overwrite the lockfile, resulting in a permissions error.

这是 固定为5.6.2版

请注意,QSerialPort 在自己之后清理:调用它的析构函数,端口关闭,锁定文件被删除。但是,默认情况下,当 SIGTERM SIGINT 时,Qt不会调用对象析构函数程序退出。 (个人而言,我认为这也是一个错误,但我认识到这更多是一个意见问题。)

Note that QSerialPort does clean up after itself: when its destructor is called, the port is closed and the lockfile is deleted. However, by default, Qt does not call object destructors when SIGTERM or SIGINT causes the program to exit. (Personally, I think this is also a bug, but I recognize that this is more a matter of opinion.)

另请参阅<一个href =http://stackoverflow.com/q/22885116/1858225>建议的dupe问题。从这个问题可以看出,目前的行为实际上是一个改进 - 以前,应用程序将简单地挂起

Also see the suggested dupe question. As can be seen from this question, the current behavior is actually an improvement--previously, the application would simply hang!

这篇关于应用程序以前由`root`运行时,QSerialPort无法打开tty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆