我有一个伪终端从设备,它给我返回了一个读/写错误,错误信息为“资源暂时不可用(11)”。我一直无法解决这个问题,但在一周前我还不知道什么是pty。所以,我可能会错过一些显而易见的东西。
根据我的阅读,这可能是由于在非阻塞的pty上调用read()引起的。然而,当我在打开从设备pty后检查F_GETFL时,值显示它是一个阻塞文件描述符。
F_GETFL的输出显示O_NONBLOCK标志被禁用,O_RDWR标志被启用:
根据我的阅读,这可能是由于在非阻塞的pty上调用read()引起的。然而,当我在打开从设备pty后检查F_GETFL时,值显示它是一个阻塞文件描述符。
F_GETFL的输出显示O_NONBLOCK标志被禁用,O_RDWR标志被启用:
printf("F_GETFL: %x\n", fcntl( slavefd, F_GETFL)); // outputs F_GETFL: 2
我甚至尝试使用select()
将slavefd
视为非阻塞文件,以确定何时准备就绪。但是,它每次都超时。
那么,如果将slavefd
设置为阻塞状态,为什么read()
会将errno设置为“资源暂时不可用”?F_GETFL
的标志是否正确?还有什么其他方法可以尝试缩小此问题的原因?
更新:(更多信息)
我还不确定,但我认为pty从设备节点正在某种程度上被pppd锁定。我被告知可以向pty从设备中echo,这似乎是正确的,除了当pppd在使用它时。
更新2:(添加代码)
if (argc!=2)
return;
printf("opening %s\n", argv[1]);
slavefd = open(argv[1], O_RDWR );
if (slavefd < 0)
return;
这个更新展示了我如何打开从设备。因为我正在使用这个应用来调试,所以我直接使用argv[1]
。
问题解决:
我尝试读写的从节点被pppd修改了。当pppd控制一个tty/pty设备时,它会将line discipline从N_TTY
更改为N_PPP
。这意味着当你open()
然后read()
或write()
从从节点读取或写入时,PPP中间驱动程序被使用而不是TTY驱动程序。所以,read()
和write()
归结为完全不同的函数。查看N_PPP
驱动程序,我找到了以下内容。这回答了我的问题,为什么会返回EAGAIN。
/*
* Read does nothing - no data is ever available this way.
* Pppd reads and writes packets via /dev/ppp instead.
*/
static ssize_t
ppp_asynctty_read(struct tty_struct *tty, struct file *file,
unsigned char __user *buf, size_t count)
{
return -EAGAIN;
}
/*
* Write on the tty does nothing, the packets all come in
* from the ppp generic stuff.
*/
static ssize_t
ppp_asynctty_write(struct tty_struct *tty, struct file *file,
const unsigned char *buf, size_t count)
{
return -EAGAIN;
}
Socket operation on non-socket(88)
。 - rkyser