English 中文(简体)
读取 pty 文件描述失败的超时
原标题:Read timeout on pty file descriptor failing

我正在试图为代表 PTY 的文件描述符设定一个读过超时值。 我将 VMIN = 0 和 VTIME = 10 的名词设置为名词 。 我期望当字符可用时返回, 或者在一秒钟后, 如果没有字符可用时返回 。 但是, 我的程序会永远在读取调用中 。

PTY有什么特别之处使这个无效吗?有没有其他的 TERTIOS 设置使这个功能起作用?我在标准文件描述符上尝试过同样的配置,它和预期的一样有效。

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <fcntl.h>

#define debug(...) fprintf (stderr, __VA_ARGS__)

static void set_term (int fd)
{
    struct termios termios;
    int res;

    res = tcgetattr (fd, &termios);
    if (res) {
        debug ("TERM get error
");
        return;
    }

    cfmakeraw (&termios);
    termios.c_lflag &= ~(ICANON);
    termios.c_cc[VMIN] = 0;
    termios.c_cc[VTIME] = 10;        /* One second */

    res = tcsetattr (fd, TCSANOW, &termios);
    if (res) {
        debug ("TERM set error
");
        return;
    }

}

int get_term (void)
{
    int fd;
    int res;
    char *name;

    fd = posix_openpt (O_RDWR);
    if (fd < 0) {
        debug ("Error opening PTY
");
        exit (1);
    }

    res = grantpt (fd);
    if (res) {
        debug ("Error granting PTY
");
        exit (1);
    }

    res = unlockpt (fd);
    if (res) {
        debug ("Error unlocking PTY
");
        exit (1);
    }

    name = ptsname (fd);
    debug ("Attach terminal on %s
", name);

    return fd;
}

int main (int argc, char **argv)
{
    int read_fd;
    int bytes;
    char c;

    read_fd = get_term ();

    set_term (read_fd);
    bytes = read (read_fd, &c, 1);
    debug ("Read returned
");

    return 0;
}
问题回答

从linux < code>pty (7) manpage(维生素是我的):

A pseudoterminal (sometimes abbreviated "pty") is a pair of virtual character devices that provide a bidirectional communication channel. One end of the channel is called the master; the other end is called the slave. The slave end of the pseudoterminal provides an interface that behaves exactly like a classical terminal

然而,您的程序正在从主机主 读取您的程序, 无法期望它的行为完全像终端设备 。

如果您更改/扩展了 get_term 的最后几行,...

int slave_fd =  open (name, O_RDWR); /* now open the slave end..*/
if (slave_fd < 0) {
   debug ("Error opening slave PTY
");
   exit (1);
}

return slave_fd; /* ... and use it instead of the master..*/

...你的样板程序会像预期的一样有效





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签