I have a TCP server application which occasionally needs to reconfigure the bound ports by closing them and then opening them at a later time.
The application also needs to execute an external binary with communication to it. This is currently done using a popen() call. The external binary run time can span the period when the network ports need to be reconfigured.
The problem is, when the main application closes a port it is taken on by the 'forked' process which popen has created to run the binary.
That makes sense (it's discussed at What happens when a tcp server binds and forks before doing an accept ? Which process would handle the client requests? ), but is undesirable, as the main application is then unable to reopen the port.
Is this where FD_CLOEXEC O_CLOEXEC as available in popen(3) can be used? The application needs the pipe that popen(3) offers as stdin to the binary which is executed, is that filehandle left open when CLOEXEC closes the others.
Is there a better way to run the binary, which won't result in a forked process which will hold on to a closed port?
There is another, possibly related question at How to fork process without inheriting handles?