CLOEXEC for windows

87 views
Skip to first unread message

Bill Kelly

unread,
Nov 4, 2009, 4:53:13 PM11/4/09
to eventm...@googlegroups.com
Hi,

I'm embedding EventMachine in a C++ app, and noticed my
EM sockets were being inherited by child processes on
windows.

On Unix systems it appears EM is already setting CLOEXEC
on at least some of the sockets it creates.

For my purposes on Windows it seemed easiest just to add
a catch-all line to SetSocketNonblocking that also
disables HANDLE_FLAG_INHERIT on each socket.

My crude-but-simple patch is below. I suppose a less
crude approach might be to add a DisableSocketInheritance()
method which works for both windows and unix, and then
replace the scattered CLOEXEC logic with a call to the new
method.

Regards,

Bill


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

diff --git a/ext/ed.cpp b/ext/ed.cpp
index 09ff528..c6b8ef6 100644
--- a/ext/ed.cpp
+++ b/ext/ed.cpp
@@ -33,6 +33,8 @@ bool SetSocketNonblocking (SOCKET sd)
#endif

#ifdef OS_WIN32
+ // disable socket inheritance
+ SetHandleInformation(reinterpret_cast<HANDLE>(sd), HANDLE_FLAG_INHERIT, 0);
#ifdef BUILD_FOR_RUBY
// 14Jun09 Ruby provides its own wrappers for ioctlsocket. On 1.8 this is a simple wrapper,
// however, 1.9 keeps its own state about the socket.


Reply all
Reply to author
Forward
0 new messages