I've starting using socketpairs on linux and windows in order to capture the output of subprocesses on both platforms. I do this by copying STD* onto one of the sockets in the socketpair (I'm using Win32::SocketPair in perl for socketpair's on windows). The main reason I am doing this is so that read do NOT block on the output file handles.
The problem I have is that kill(0,...) doesnt work on windows, so I need another way to detect the process as down. I looked into SO_KEEPALIVE, but that doesn't seem to work on socketpairs... I used setsockopt(...) then getsockopt(...) and the SO_KEEPALIVE option was off before and after the call to setsockopt().
Then I started looking into polling for events on the socket. The POLLHUP event looked promising, but I'm not sure it works when they're used like this.
I'm looking to automate interactive programs (no, I can't use Except as it doesn't work on windows platforms... unless you found one that does?). I've been testing with "cat" on windows (I have cat.exe installed). It has looked good so far, but there are 2 issues:
- can't detect if process just dies (kill(0,...) doesnt work)
- can't detect EOF from subprocess (sysread seems to always return undef or bytes read, never 0, even after I shutdown($sock, 1))
I may be doing something extremely dumb, but any advice/help would be appreciated.
UPDATE: I started using waitpid($pid, WNOHANG) to detect the "still running" condition which helps as it seems the process always dies after everything has been read. waitpid returns 0 if the pid is still running. However, it's not an EOF, its better than nothing, but I'm still looking for other input. This obviously isn't ideal.
UPDATE2: This Q/A helped with the EOF part of my question, not perfect, but better: http://stackoverflow.com/questions/793646/how-epoll-detect-clientside-close-in-python