1

我正在尝试使用伪终端编写一个可以使用密码登录 SSH 的应用程序。但是如果我 write() 到主设备,那么数据就不会出现在从设备中。这是一个简单的测试用例:

#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#ifdef __linux__
    #include <pty.h>
#else
    #include <util.h>
#endif

int
main() {
    int master;
    pid_t pid = forkpty(&master, NULL, NULL, NULL);
    if (pid == 0) {
        int ch;
        read(0, &ch, 1);
        _exit(1);
    } else {
        printf("Press Enter to send a byte.\n");
        getchar();
        write(master, "1", 1);
        printf("Done. Waiting for process to exit...\n");
        waitpid(pid, NULL, 0);
        return 0;
    }
}

该应用程序将首先输出“Press Enter to send a byte”。按 Enter 后,我希望子进程的 read() 返回。但是即使 master 的 write() 成功,那里的 read() 似乎也会无限期地阻塞,所以 master 在 waitpid() 上永远等待。这是怎么回事?

4

2 回答 2

2

问题是您没有修改 PTY 的线路规程。默认的行规则是面向行的,因此在读取换行符之前不会将任何输入发送到从属进程。(您可以通过向从属设备发送“\n”,而不仅仅是“1”来查看这一点。)您可以在 RAW 模式下运行 PTY,方法是在子进程中调用和tcgetattr,如下所示:cfmakerawtcsetattr

    if (pid == 0) {
        int ch;
        struct termios t;
        tcgetattr(0, &t);
        cfmakeraw(&t);
        tcsetattr(0, TCSANOW, &t);
        read(0, &ch, 1);
        _exit(1);
    } else {

这似乎对我有用。

于 2010-02-02T19:32:01.497 回答
1

此博客文章中的示例代码可能会有所帮助。作者对他的原始问题(与您的问题非常相似)进行了更新,并spawn (char *argv[]);给出了可用的功能。

于 2010-02-02T19:22:10.860 回答