jaco has asked for the wisdom of the Perl Monks concerning the following question:

Evenin' folks. I'm currently facing a problem which i don't quite understand. I'm trying to fork and exec and app, and redirect it's stdin and stdout to a PTY. I can do it just fine in C, but would like to make the socket IO, which is the other half of this, easier on me. anyway here's what i have.
use IO::Pty; use POSIX; use IO::Tty qw(TIOCSCTTY); $master = new IO::Pty || die "cant open PTY $!\n"; $slave = $master->slave(); $slave->set_raw(); $name = $slave->ttyname(); print STDERR "### PTY $name\n"; $slave_fd = $slave->fileno(); $master_fd = $master->fileno(); my $pid = fork(); die "Cannot fork" if not defined $pid; unless ($pid) { POSIX::close(1); POSIX::close(0); POSIX::close($master_fd); close($device->{handle}); POSIX::dup2($slave_fd,0); POSIX::dup2($slave_fd,1); POSIX::Setsid; ioctl($slave,TIOCSCTTY,0); $yes = POSIX::isatty($slave); print STDERR "## TTY $yes\n"; exec("$execline") || die "Shouldnt be here. Exec failed ? : $! +"; exit 0; } POSIX::close($slave_fd); print STDERR "forked\n"; POSIX::close($master_fd);

.... etc
that's just the part i'm worried about. Anyway, the application being exec'd requires a valid tty for STDIN, and i'm not getting that. Eventually i'll have to read the PTY and send it to a socket, but i can handle that on my own i think. Any ideas? any better way to allocate a pty? Thanks

Replies are listed 'Best First'.
Re: pty redir
by jaco (Pilgrim) on Feb 12, 2003 at 19:12 UTC
    i actually managed to figure this out, if you close the master_fd in the child, the slave wont ack the TIOCSCTTY and never be the controlling terminal. so above is true just #POSIX::close($master_fd);