How to read characters printed on a process's own control terminal ? Or is this even possible under Linux ?
My program needs to launch an SSH client and start a password authenticated session (C/C++ programming language), so injecting the password into the SSH client's control terminal is required. Since the SSH client and my program share the same control terminal, password injection can be achieved through system call (man tty_ioctl):
ioctl(STDIN_FILENO, TIOCSTI, password)
However the injecting must be at the right time, if password injected too early, the injected data will be ignored, so my program has to wait for the password prompt "foobar's password:" before injecting password. Currently my solution is to wait a few seconds before the injection, which is very unreliable.
So what is right time to inject the password into the control terminal ?
I don't want to create a new pseudo-terminal, too many terminal related things have to be handled.