Discussion:
[asio-users] Close socket on exec()
Nicolas Alvarez
2008-10-05 18:48:00 UTC
Permalink
I have an application that currently uses plain BSD sockets for a custom
protocol, and libcurl for HTTP.

The application spawns also new processes via fork/exec. For its
self-created sockets, it calls fcntl(sock, F_SETFD, FD_CLOEXEC); to avoid
the spawned processes inheriting the socket. For the curl sockets, it
doesn't do anything special, and over time I see netstat output filling
with sockets in CLOSE_WAIT status, "owned" by the child processes (who have
no idea what to do with them).

I was considering moving it to asio (long term task). tcp::socket has
set_option (which calls setsockopt) and io_control (which calls ioctl), but
I don't see anything to call fcntl. Is there any way to call fcntl on an
asio socket? Or, is there a way to set a socket to "close on exec" using
set_option or io_control? Or, does asio already set this by default?

My other option, of course, would be to call fcntl directly, passing
socket.native(). Just wondering if asio already has a way to do this.
Robert Hegner
2012-10-22 13:29:52 UTC
Permalink
I have the same question as in the (very old) original post. Is there a
way to open an asio socket with the FD_CLOEXEC flag set?
Post by Nicolas Alvarez
I have an application that currently uses plain BSD sockets for a custom
protocol, and libcurl for HTTP.
The application spawns also new processes via fork/exec. For its
self-created sockets, it calls fcntl(sock, F_SETFD, FD_CLOEXEC); to avoid
the spawned processes inheriting the socket. For the curl sockets, it
doesn't do anything special, and over time I see netstat output filling
with sockets in CLOSE_WAIT status, "owned" by the child processes (who have
no idea what to do with them).
I was considering moving it to asio (long term task). tcp::socket has
set_option (which calls setsockopt) and io_control (which calls ioctl), but
I don't see anything to call fcntl. Is there any way to call fcntl on an
asio socket? Or, is there a way to set a socket to "close on exec" using
set_option or io_control? Or, does asio already set this by default?
My other option, of course, would be to call fcntl directly, passing
socket.native(). Just wondering if asio already has a way to do this.
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
Loading...