Search code examples
node.jschild-process

Can a child process know how many additional file descriptors are expected?


I have a situation where a parent is spawning a process with some dynamic number of additional stdio pipes, as so:

const child = spawn("something", [], { stdio:["pipe", "pipe", "pipe", "pipe", "pipe"] });

I'd like to know whether in node (or any process really, but I happen to be using node in this case), the child can know that it is expected to interact with fd 3 and fd 4 beyond just 0, 1, and 3 (stdin, stdout, and stderr).

For some context, I am creating an in-between proxy-process, hence I want to be able to forward all the pipes, not just stdin, stdout, and stderr. Hence I'd like the in-between process to "realize" it was opened with fd 3 and fd 4, and thus also do the same for it's child so it can properly forward everything.


Solution

  • Use lsof -aU -d 0-999 -p process PID and take the first 0 through consecutive file descriptors, ignoring 0 through 2 (stdin, stdout, and stderr). The remaining ones are pipes.