Typically when the ssh does not return with a non-zero exit.
PS; make the exit after the execl() non-zero! or at least report execl failure before exiting.
To know when you have finally connected you need to watch the output from the ssh for errors, login prompts, or shell prompt (success).
The better way is to specify the command you want to run on the ssh line and then just wait from that command to exit.
ASIDE: running background ssh (or telnet) from a shell is just a complex as from a C program. You may like to look at and understand shell co-processing techniques such as
http://www.ict.griffith.edu.au/antho...rocesses.hints
and
http://steve-parker.org/sh/hints.shtml#telnet
The C version would be very similar in style, though without needing named pipes.
Also "ssh" should not require a PTY, though without it you will may not get shell prompts. You may however like to merge STDERR and STDOUT into a single communications pipe (for ssh errors, remote command errors will be on standard output). If you don't then you will need to monitor both stderr and stdout simultaniously (using select() calls).
Did I say this was easy? I look forward to other peoples response to the query.