views:

134

answers:

2

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.

+1  A: 

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.

Adam Liss
I'll try waitpid and report back here if it works
MattSmith
+1  A: 

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
MattSmith