疑似端末を使って、パスワードで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;
}
}
アプリは最初に「Enterキーを押してバイトを送信します」と出力します。Enterキーを押した後、子プロセスのread()が戻ることを期待しています。しかし、マスターのwrite()が成功したとしても、そこにあるread()は無期限にブロックされるように見えるため、マスターはwaitpid()を永久に待機します。どうしたの?