System.Posix, forked processes and psuedo terminals on Linux

Hi all, I've got a but it code below thats not quite working as I expect it to. The basic idea is that it opens a master/slave pair of pseudo terminals, forks a child process and then perform bi-directional communication between the parent and the child via the master/slave psuedo terminal. The thing does really need to forkProcess because once the comms is working I want to exec another process in the child. I also intend to dup the file descriptors so that stdin, stdout and stderr all point to the slave's end of the pty. The code below *almost* works. Its currently printing out: parent : Forked child was here! parent : Message from parent. Read 21 bytes while I think it should print: parent : Forked child was here! parent : Read 21 bytes Any clues on why 'Message from parent.' is also ending up on stdout? This is ghc-6.12.1 on Debian Linux. Cheers, Erik import System.Posix.IO import System.Posix.Process import System.Posix.Terminal import System.Posix.Types main :: IO () main = do (master, slave) <- openPseudoTerminal _childId <- forkProcess $ forkedChild (master, slave) closeFd slave runParent master runParent :: Fd -> IO () runParent fd = do (str, _) <- fdRead fd 1024 putStr $ "parent : " ++ str _ <- fdWrite fd "Message from parent.\n" (str2, _) <- fdRead fd 1024 putStr $ "parent : " ++ str2 forkedChild :: (Fd, Fd) -> IO () forkedChild (master, fd) = do closeFd master _ <- fdWrite fd "Forked child was here!\n" (_, count) <- fdRead fd 1024 _ <- fdWrite fd $ "Read " ++ show count ++ " bytes\n" closeFd fd -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

Quoth Erik de Castro Lopo
The code below *almost* works. Its currently printing out:
parent : Forked child was here! parent : Message from parent. Read 21 bytes
while I think it should print:
parent : Forked child was here! parent : Read 21 bytes
Any clues on why 'Message from parent.' is also ending up on stdout? This is ghc-6.12.1 on Debian Linux.
My guess is that the default tty attributes include ECHO. So the data you write to the master fd is echoed back, as though by the fork process but actually by the terminal driver. You can turn ECHO off. Donn Cave, donn@avvanta.com

Donn Cave wrote:
My guess is that the default tty attributes include ECHO. So the data you write to the master fd is echoed back, as though by the fork process but actually by the terminal driver. You can turn ECHO off.
Thanks very much Donn, that was an excellent guess. Adding: attr <- getTerminalAttributes fd setTerminalAttributes fd (withoutMode attr EnableEcho) Immediately at the top of the runParent process gave me the results I expected. Cheers, Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/
participants (2)
-
Donn Cave
-
Erik de Castro Lopo