c - Child process unable to read from created pseudo terminal -
I am trying to write an app that can enter SSH with password, using proxy terminals. But if I write on the master device then the data is not displayed in the slave device somehow. Here is a simple test case:
#include & lt; Sys / wait.h & gt; # Include & lt; Sys / types.h & gt; # Include & lt; Stdio.h & gt; # Include & lt; Unistd.h & gt; #ifdef __linux__ #include & lt; Pty.h> #else #include & lt; Use.h & gt; #endif int main () {int master; Pid_t pid = forkpty (& master, NULL, NULL, NULL); If (pd == 0) {int ch; Read (0, and, 1, 1); _exit (1); } Else {printf (press Enter to send a byte. \ N "); Getchar (); Write (Master, "1", 1); Printf ("Waiting for process to complete ... \ n"); Waiting pill (PID, faucet, 0); Return 0; }}
After entering the first output "Press Enter to send a byte", I hope to read back to the child's process () but read () there is uncertain Even if the master's written () is successful, so the Guru is always waiting on waitpid (). What is happening? The problem is that you have not modified PTY line discipline, the default line is line-oriented line-oriented, Therefore, any input will not be sent to the slave process until a new letter is read. (You can send "\ n" to slave by sending it instead of "1.") You can call PTI in call mode, in the hair process, call tcgetattr
, Code> cfmakeraw < / Code> and tcsetattr
, as follows:
if (pid == 0) {int ch; Straight Termos T; TCZET (0, and T); Cfmakeraw (& amp; t); TCset Atter (0, TCSAU, and T); Read (0, and, 1, 1); _exit (1); } Else {
This works for me
Comments
Post a Comment