I have a C++ application listening on a particular TCP port. The application also launches a child process using the Boost child class from Boost process library. As soon as the child is launched the output of netstat command shows that the TCP port also gets associated with the created child process. Is there a way by which I can prevent the child from inheriting the parent's ports when using boost process library ? The child is created as :
bp::child* proc = new bp::child("a.out", bp::std_out > stdout, bp::std_err > stderr);
I am using Linux platform. Thanks.
There's currently no way to do it.
I've toyed with a patch to add it, but I've had some problems and ran out of time testing things. I can share the patch, but you'll be on your own testing it.
Note everything below assumes POSIX systems.
Simple Take #1
Simplest thing to do would seem to be closing all non-standard fds, without exceptions:
Now you'd simply pass that keyword into your process creation API:
For one thing, there's the mandatory parent-child pipe used for internal communication within the Boost Process internals (e.g. error handling/reporting). We didn't account for it, and it wouldn't be predictable what
fd
value is involved, so let's move to more sophisticated ideas:More Complicated Setups
More intelligent setups would take any fds into account that are involved by Boost Process. This includes the pipe (mentioned above) and any other fds that might be the result of redirections specified by the other process launch arguments.
Here's the patch as I had it prepared
Usage would look similar to above:
Note that it allows coordination with other (custom) extensions to collect FDs that are supposed to be inherited.
I'll let the comments in the source code be your guide to understand the idea.