Matthew Smith
Matthew Smith

Reputation: 6255

How can the master pty detect if a slave tty has exited?

I am using BSD style pty/tty pairs to implement running a sub shell. When the user exits the sub shell, how do I detect in the master process that this has occurred? I am using select(nfds, &read_fds, NULL, NULL, &timeout); with the master pty file descriptor set in the read_fds on the master side.

Upvotes: 1

Views: 673

Answers (2)

Matthew Smith
Matthew Smith

Reputation: 6255

I've found the answer to this question by examining the telnetd source code found in the GNU inetutils package. In telnetd, they use a SIGCHLD handler like this:

int status;
pid_t pid = waitpid((pid_t)-1, &status, WNOHANG);
syslog (LOG_INFO, "child process %ld exited: %d",
    (long) pid, WEXITSTATUS(status));
// do cleanup code

Upvotes: 2

Adam Liss
Adam Liss

Reputation: 48290

The subshell is typically created by a fork() of some sort. The PID of the child is returned to the master, which can check (with waitpid(), perhaps) if it's still running.

Upvotes: 3

Related Questions