5

After the slave end is used (open(), read(), write(), close()), the master's read() finishes and returns with an EIO error. But I was expecting to be able to open()/close() multiple times the slave end without disrupting the master fd.

Is it possible to keep the two ends valid after the slave's close()? How can I keep my "master" program running when the slave end is closed? Am I supposed to re-run to entire grantpt(); unlockpt(); ptsname(); sequence?

1

Short: no

Long: a pseudo-terminal slave connection is one end of a connection, like a pipe. Both are ways that data is passed back and forth between user processes through the kernel.

Once one end is closed, you lose the connection. The ptsname description in POSIX says:

Upon failure, ptsname() shall return a null pointer. This could occur if fildes is an invalid file descriptor or if the slave device name does not exist in the file system.

Once you close a file descriptor, it is invalid.

If you want to reuse a connection, you could do some workaround such as passing the open slave file descriptor to a newly-created process, e.g., from a server application which you write to manage the slave file descriptors.

Further reading:

  • According to pty_close() lxr.free-electrons.com/source/drivers/tty/pty.c#L41 closing the slave fd wakes reads and writes with flag TTY_IO_ERROR set, but only writes on the master fd are woken up with the flag TTY_OTHER_CLOSED set. The devpts entry is removed only when closing the master fd. It doesn't look like if the devpts entry should no longer be used, does it? – Julio Guerra Jun 20 '16 at 10:23
  • That's one of several implementations, and each requires analysis to see what holes (or opportunities) are provided to complicate portability. – Thomas Dickey Jun 20 '16 at 20:11
  • I finally used openpty() from the glibc which is able to keep de pty opened even after the slave end is closed. There solution is simple: openpty() opens the slave node so that the process which created the pty can keep it opened as long as necessary, while other processes can use the pty, including closing and reopening it. I didn't check if this is a standard behaviour. – Julio Guerra Jul 5 '17 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.