Discussion:
[asio-users] [ASIO] closing all open sockets in child process
Avi Bahra
2011-06-27 11:54:13 UTC
Permalink
I have a ASIO based server. This server can spawn child process.
Is there any asio way of making sure that socket file
descriptors are not opened in any child process ?

Any help appreciated.

Best regards,
Ta,
Avi
Igor R
2011-06-27 12:15:25 UTC
Permalink
Post by Avi Bahra
I have a ASIO based server. This server can spawn child process.
Is there any asio way of making sure that socket file
descriptors are not opened in any child process ?
Do you spawn it with fork() or just create another process?
You didn't tell what platform you use, but on Windows you can either
disable handle inheritance, when calling CreateProcess, or explicitly
call SetHandleInformation() on a specific socket descriptor.
Avi Bahra
2011-06-27 12:41:57 UTC
Permalink
oops, its unix specific, I use fork/execl.
In my child process I could do:

int fd_limit = sysconf(_SC_OPEN_MAX);
for (int i=3; i<fd_limit; i++) close(i);

to remove all open file descriptions, apart for stdin.stdout,stdout.
However If I only wanted to remove the socket based file
descriptors opened by the server, then its not clear how I would
do this.
Either I should record in some singleton, asio handle_accept
where the connection is created, but from what I have seen
in all the examples, closing in done explicitly and not in
connector destructor.
OR use fctrl() so that socket file descriptor are not copied on exce's.

Also I cant see any api to get hold of the file descriptor from a
ip::tcp::socket

It would be very useful to know how other people have
approached this issue.

Ta,
Avi
Post by Igor R
Post by Avi Bahra
I have a ASIO based server. This server can spawn child process.
Is there any asio way of making sure that socket file
descriptors are not opened in any child process ?
Do you spawn it with fork() or just create another process?
You didn't tell what platform you use, but on Windows you can either
disable handle inheritance, when calling CreateProcess, or explicitly
call SetHandleInformation() on a specific socket descriptor.
------------------------------------------------------------------------------
All of the data generated in your IT infrastructure is seriously valuable.
Why? It contains a definitive record of application performance, security
threats, fraudulent activity, and more. Splunk takes this data and makes
sense of it. IT sense. And common sense.
http://p.sf.net/sfu/splunk-d2d-c2
_______________________________________________
asio-users mailing list
https://lists.sourceforge.net/lists/listinfo/asio-users
_______________________________________________
Using Asio? List your project at
http://think-async.com/Asio/WhoIsUsingAsio
--
Best regards,
Ta,
Avi
Igor R
2011-06-27 19:58:52 UTC
Permalink
Post by Avi Bahra
oops, its unix specific, I use fork/execl.
FWIW, if your child process is a result of fork, you can use the new
asio functionality to manipulate the inherited sockets:
http://think-async.com/Asio/boost_asio_1_5_3/doc/html/boost_asio/examples.html
(see Fork section).


<...>
Post by Avi Bahra
 Also I cant see any api to get hold of the file descriptor from a
ip::tcp::socket
socket::native_handle()
http://think-async.com/Asio/boost_asio_1_5_3/doc/html/boost_asio/reference/basic_stream_socket/native_handle.html
Loading...