views:

191

answers:

2

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

+1  A: 

If it's supported in the WIN32 environment, and your process is spawning the child processes, you could try setting up a signal handler to catch a SIGCHLD signal from the spawned processes in the parent process... something like:

$SIG{CHLD} = sub { print "caught sigchld from child process"; };

That should tell you if one of the subprocesses exited.

Aquatoad
... I'm pretty sure signals in general are a "unix-ism". Unless I'm missing something... I don't think this would work on the windows platform. But I spose I can try it...
xyld
Yes, that's true, signals are a UNIX thing. The question would be whether Win32 Perl uses Windows' ITC implementation and emulates them via the API (i.e. the signaling interface in Perl) or whether they provide a completely separate API to hook into messaging.
Aquatoad
And the answer would be no (unless you're using Cygwin).
mobrule
A: 
mobrule
I'm using select() to check for pending input, but the meat of my question is about how to detect if the connection is still alive. I also use non-blocking I/O even though it doesn't work well in windows... if at all. I'll look into the Win32::Process::Open method.
xyld
I'm pretty sure our brains are linked... I JUST implemented this... without looking at your edit first... and it just popped into my brain. hah!
xyld
Yeah, I've been thinking about fork/socket stuff a lot lately, too.
mobrule