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
2008-11-05 06:07:42
I'll try waitpid and report back here if it works
MattSmith
2008-11-05 06:26:16
+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
2008-11-06 01:45:07